diff --git a/src/image.cpp b/src/image.cpp index 244b9aa..3bdf2a3 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -102,7 +102,7 @@ bool Image::save(const std::string &filename) unsigned error = lodepng::encode(filename, pixels, width, height, colorType, 8); if(error) fprintf(stderr, lodepng_error_text(error)); - return error != 0; + return error == 0; } bool Image::save(std::vector &out) @@ -212,7 +212,7 @@ void TexturePack::destroyGL() } } -TexturePack::TexturePack(const std::string &folderPath) : TexturePack() +bool TexturePack::load(const std::string &folderPath) { m_name = getNameFromFilename(folderPath); tinydir_dir dir; @@ -220,12 +220,17 @@ TexturePack::TexturePack(const std::string &folderPath) : TexturePack() if(err == -1) { fprintf(stderr, "can't open directory : \"%s\"\n", folderPath.c_str()); - return; + return false; } - int n = dir.n_files; - for(int i=0; isave(filename)) fprintf(stderr, "can't save image : \"%s\"\n", filename.c_str()); } diff --git a/src/image.h b/src/image.h index 635bbfd..17ae9d5 100644 --- a/src/image.h +++ b/src/image.h @@ -110,7 +110,7 @@ public: * every png file in the folder will be loaded in the pack * and named from the file name minus its extension */ - TexturePack(const std::string &folderPath); + bool load(const std::string &folderPath); /** * @brief saves the texture pack as png images in the specified existing folder diff --git a/src/model.cpp b/src/model.cpp index 3f30fb0..f9429ad 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -8,7 +8,7 @@ Model::Model() : m_texPack(NULL) {} Model::~Model() { - destroy(); + initDestroy(); destroyGL(); for(Mesh *m : m_meshes) delete m; @@ -22,22 +22,29 @@ void Model::setTexturePack(TexturePack *texPack) m_texPack = texPack; } -void Model::destroy() +void Model::initDestroy() { + initGL(); for(Mesh* m : m_meshes) m->clearData(); + if(m_texPack != NULL) + m_texPack->initDestroyImages(); } void Model::initGL() { for(Mesh* m : m_meshes) m->initGL(); + if(m_texPack != NULL) + m_texPack->initGL(); } void Model::destroyGL() { for(Mesh* m : m_meshes) m->destroyGL(); + if(m_texPack != NULL) + m_texPack->destroyGL(); } bool Model::save(const std::string &filename, const std::string &texturesPath) @@ -47,11 +54,11 @@ bool Model::save(const std::string &filename, const std::string &texturesPath) if(file == NULL) return false; + // write meshes and materials, there is no material sharing (1 mesh = 1 material) bool ok = false; int size = m_meshes.size(); if(std::fwrite(&size, sizeof(int), 1, file)) - { - // write meshes and materials, there is no material sharing (1 mesh = 1 material) + { ok = true; for(Mesh* m : m_meshes) { @@ -59,19 +66,13 @@ bool Model::save(const std::string &filename, const std::string &texturesPath) PhongMaterial* mat = (PhongMaterial*)m->getMaterial(); ok = ok && mat->serialize(file); } - // write texture pack - - /*for(Model::TextureImg &texImg : m_textures) - { - std::string texFilename = texturesPath + texImg.name + ".png"; - if(texImg.img == NULL && texImg.tex != NULL) - texImg.img = texImg.tex->getData(); - if(texImg.img != NULL) - ok = ok && texImg.img->save(texFilename); - }*/ std::fclose(file); } + // saves the TexturePack + if(m_texPack != NULL) + ok = ok && m_texPack->save(texturesPath); + // finishing return ok; } @@ -86,6 +87,7 @@ Model* Model::load(const std::string &filename, const std::string &texturesPath) bool ok = false; + // finishing return NULL; } diff --git a/src/model.h b/src/model.h index 89e4455..c51c4a8 100644 --- a/src/model.h +++ b/src/model.h @@ -42,10 +42,11 @@ public: //Texture* getTexture(const std::string &name); /** - * @brief destroy frees Mesh and Image memory in the RAM, but it stays allocated in GPU memory. - * (not supported yet when rendering because of flags) + * @brief uploads meshes and images to GPU memory, + * then frees Mesh and Image memory in the RAM, + * so it stays allocated only in GPU memory. */ - void destroy(); + void initDestroy(); /** * @brief initGL uploads image and mesh data to GPU memory diff --git a/src/phongmaterial.cpp b/src/phongmaterial.cpp index 4ef2b7e..aa10f48 100644 --- a/src/phongmaterial.cpp +++ b/src/phongmaterial.cpp @@ -95,6 +95,8 @@ bool PhongMaterial::serialize(PhongMaterial* mat, FILE *file) { if(mat->textures[i] != NULL) header.textureLengths[i] = mat->textureNames[i].length()+1; + else + header.textureLengths[i] = 0; } if(!std::fwrite(&header, sizeof(MaterialHeader), 1, file))