debugged texture pack serialization

This commit is contained in:
Anselme 2016-07-28 17:01:57 +02:00
parent 9269f345b0
commit 4a792fbadf
5 changed files with 46 additions and 30 deletions

View File

@ -102,7 +102,7 @@ bool Image::save(const std::string &filename)
unsigned error = lodepng::encode(filename, pixels, width, height, colorType, 8); unsigned error = lodepng::encode(filename, pixels, width, height, colorType, 8);
if(error) if(error)
fprintf(stderr, lodepng_error_text(error)); fprintf(stderr, lodepng_error_text(error));
return error != 0; return error == 0;
} }
bool Image::save(std::vector<unsigned char> &out) bool Image::save(std::vector<unsigned char> &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); m_name = getNameFromFilename(folderPath);
tinydir_dir dir; tinydir_dir dir;
@ -220,12 +220,17 @@ TexturePack::TexturePack(const std::string &folderPath) : TexturePack()
if(err == -1) if(err == -1)
{ {
fprintf(stderr, "can't open directory : \"%s\"\n", folderPath.c_str()); fprintf(stderr, "can't open directory : \"%s\"\n", folderPath.c_str());
return; return false;
} }
int n = dir.n_files; while(dir.has_next)
for(int i=0; i<n; ++i)
{ {
const tinydir_file &file = dir._files[i]; tinydir_file file;
tinydir_readfile(&dir, &file);
if(std::string(file.name) == "." || std::string(file.name) == ".." || file.is_dir)
{
tinydir_next(&dir);
continue;
}
if(std::string(file.extension) == "png") if(std::string(file.extension) == "png")
{ {
Image* img = new Image(std::string(file.path)); Image* img = new Image(std::string(file.path));
@ -234,20 +239,26 @@ TexturePack::TexturePack(const std::string &folderPath) : TexturePack()
} }
else else
fprintf(stderr, "%s is not a png file, ignoring\n", file.path); fprintf(stderr, "%s is not a png file, ignoring\n", file.path);
tinydir_next(&dir);
} }
return true;
} }
bool TexturePack::save(const std::string &folderPath) bool TexturePack::save(const std::string &folderPath)
{ {
std::string path = folderPath + '/' + m_name; std::string path = folderPath + '/' + m_name;
if(!tinydir_create_directory(path.c_str()))
{
fprintf(stderr, "can't create directory \"%s\"\n", path.c_str());
return false;
}
tinydir_dir dir; tinydir_dir dir;
int err = tinydir_open(&dir, path.c_str()); int err = tinydir_open(&dir, path.c_str());
if(err == -1) if(err == -1)
{
if(!tinydir_create_directory(path.c_str()))
{
fprintf(stderr, "can't create directory \"%s\"\n", path.c_str());
return false;
}
}
err = tinydir_open(&dir, path.c_str());
if(err == -1)
{ {
fprintf(stderr, "can't open directory : \"%s\"\n", path.c_str()); fprintf(stderr, "can't open directory : \"%s\"\n", path.c_str());
return false; return false;
@ -258,7 +269,7 @@ bool TexturePack::save(const std::string &folderPath)
TexImage &texImg = pair.second; TexImage &texImg = pair.second;
if(texImg.img != NULL) if(texImg.img != NULL)
{ {
std::string filename = path + '/' + texImg.name + ".png"; std::string filename = path + '/' + texImg.name;
if(!texImg.img->save(filename)) if(!texImg.img->save(filename))
fprintf(stderr, "can't save image : \"%s\"\n", filename.c_str()); fprintf(stderr, "can't save image : \"%s\"\n", filename.c_str());
} }

View File

@ -110,7 +110,7 @@ public:
* every png file in the folder will be loaded in the pack * every png file in the folder will be loaded in the pack
* and named from the file name minus its extension * 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 * @brief saves the texture pack as png images in the specified existing folder

View File

@ -8,7 +8,7 @@ Model::Model() : m_texPack(NULL) {}
Model::~Model() Model::~Model()
{ {
destroy(); initDestroy();
destroyGL(); destroyGL();
for(Mesh *m : m_meshes) for(Mesh *m : m_meshes)
delete m; delete m;
@ -22,22 +22,29 @@ void Model::setTexturePack(TexturePack *texPack)
m_texPack = texPack; m_texPack = texPack;
} }
void Model::destroy() void Model::initDestroy()
{ {
initGL();
for(Mesh* m : m_meshes) for(Mesh* m : m_meshes)
m->clearData(); m->clearData();
if(m_texPack != NULL)
m_texPack->initDestroyImages();
} }
void Model::initGL() void Model::initGL()
{ {
for(Mesh* m : m_meshes) for(Mesh* m : m_meshes)
m->initGL(); m->initGL();
if(m_texPack != NULL)
m_texPack->initGL();
} }
void Model::destroyGL() void Model::destroyGL()
{ {
for(Mesh* m : m_meshes) for(Mesh* m : m_meshes)
m->destroyGL(); m->destroyGL();
if(m_texPack != NULL)
m_texPack->destroyGL();
} }
bool Model::save(const std::string &filename, const std::string &texturesPath) 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) if(file == NULL)
return false; return false;
// write meshes and materials, there is no material sharing (1 mesh = 1 material)
bool ok = false; bool ok = false;
int size = m_meshes.size(); int size = m_meshes.size();
if(std::fwrite(&size, sizeof(int), 1, file)) if(std::fwrite(&size, sizeof(int), 1, file))
{ {
// write meshes and materials, there is no material sharing (1 mesh = 1 material)
ok = true; ok = true;
for(Mesh* m : m_meshes) 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(); PhongMaterial* mat = (PhongMaterial*)m->getMaterial();
ok = ok && mat->serialize(file); 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); std::fclose(file);
} }
// saves the TexturePack
if(m_texPack != NULL)
ok = ok && m_texPack->save(texturesPath);
// finishing // finishing
return ok; return ok;
} }
@ -86,6 +87,7 @@ Model* Model::load(const std::string &filename, const std::string &texturesPath)
bool ok = false; bool ok = false;
// finishing // finishing
return NULL; return NULL;
} }

View File

@ -42,10 +42,11 @@ public:
//Texture* getTexture(const std::string &name); //Texture* getTexture(const std::string &name);
/** /**
* @brief destroy frees Mesh and Image memory in the RAM, but it stays allocated in GPU memory. * @brief uploads meshes and images to GPU memory,
* (not supported yet when rendering because of flags) * 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 * @brief initGL uploads image and mesh data to GPU memory

View File

@ -95,6 +95,8 @@ bool PhongMaterial::serialize(PhongMaterial* mat, FILE *file)
{ {
if(mat->textures[i] != NULL) if(mat->textures[i] != NULL)
header.textureLengths[i] = mat->textureNames[i].length()+1; header.textureLengths[i] = mat->textureNames[i].length()+1;
else
header.textureLengths[i] = 0;
} }
if(!std::fwrite(&header, sizeof(MaterialHeader), 1, file)) if(!std::fwrite(&header, sizeof(MaterialHeader), 1, file))