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);
if(error)
fprintf(stderr, lodepng_error_text(error));
return error != 0;
return error == 0;
}
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);
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; i<n; ++i)
while(dir.has_next)
{
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")
{
Image* img = new Image(std::string(file.path));
@ -234,20 +239,26 @@ TexturePack::TexturePack(const std::string &folderPath) : TexturePack()
}
else
fprintf(stderr, "%s is not a png file, ignoring\n", file.path);
tinydir_next(&dir);
}
return true;
}
bool TexturePack::save(const std::string &folderPath)
{
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;
int err = tinydir_open(&dir, path.c_str());
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());
return false;
@ -258,7 +269,7 @@ bool TexturePack::save(const std::string &folderPath)
TexImage &texImg = pair.second;
if(texImg.img != NULL)
{
std::string filename = path + '/' + texImg.name + ".png";
std::string filename = path + '/' + texImg.name;
if(!texImg.img->save(filename))
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
* 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

View File

@ -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;
}

View File

@ -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

View File

@ -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))