diff --git a/src/image.h b/src/image.h index e59da71..0343c19 100644 --- a/src/image.h +++ b/src/image.h @@ -3,6 +3,9 @@ #include #include +#include + +class Texture; struct Image { @@ -31,14 +34,81 @@ struct Image bool save(std::vector &out); }; -struct TexturePack +class TexturePack { +private: + struct TexImage + { + std::string name; + Texture* tex; + Image* img; + }; + std::string m_name; - std::vector m_images; + std::unordered_map m_images; + bool allocatedImg; + bool allocatedTex; - TexturePack(std::string name) : m_name(name) {} - TexturePack(const std::string &filename); - bool save(const std::string &filename); +public: + /** + * @brief creates a new empty texture pack + * the name must be a valid string for a filename : + * (no symbols except underscore, avoid spaces, avoid special characters) + */ + TexturePack(std::string name = "texture_pack") : + m_name(name), allocatedImg(false), allocatedTex(false) {} + + /** + * @brief rename renames the texture pack + * the name must be a valid string for a filename : + * (no symbols except underscore, avoid spaces, avoid special characters) + */ + void rename(const std::string &name) { m_name = name; } + + /** + * @brief addImage adds an image to the pack, if an image of this name already exists in the pack, it will be replaced. + */ + void addImage(Image* img); + + /** + * @brief addImage adds an image from an OpenGL texture and names it with the provided name. + * @param downloadFromGL if this is false, the Image will not be downloaded from the texture, only the texture will be added. + */ + void addImage(Texture* tex, const std::string &name, bool downloadFromGL = true); + + /** + * @brief initGL creates an OpenGL texture for every Image in the pack. + */ + void initGL(); + + /** + * @brief initDestroyImages creates an OpenGL texture for every Image in the pack, and then deletes all Images. + */ + void initDestroyImages(); + + /** + * @brief getImagesFromGL downloads the images from the opengl texture memory for all available textures + */ + void getImagesFromGL(); + + /** + * @brief destroyGL destroys all the OpenGL textures of the pack. + */ + void destroyGL(); + + /** + * @brief loads a texture pack from a folder + * the texture pack name will be the folder name + * 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); + + /** + * @brief saves the texture pack as png images in the specified existing folder + * @return true if serialisation succeeded + */ + bool save(const std::string &folderPath); }; #endif // IMAGE diff --git a/src/light.cpp b/src/light.cpp index 12314b5..5db5283 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -9,6 +9,7 @@ #include "camera.h" #include #include +#include "image.h" RESOURCE_PACK(shaders) @@ -335,10 +336,11 @@ void PointLight::updateShadowMap(Scene* scene) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); m_shadowMap->bindFBO(); glViewport(0, 0, m_shadowMapResolution, m_shadowMapResolution); - glClearDepth(1.0); - glClear(GL_DEPTH_BUFFER_BIT); + glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); + glClearDepth(0.2); glDepthFunc(GL_LESS); + glClear(GL_DEPTH_BUFFER_BIT); for(SceneIterator* geometryIt = scene->getGeometry(); geometryIt->isValid(); geometryIt->next()) { @@ -354,9 +356,20 @@ void PointLight::updateShadowMap(Scene* scene) node->mesh->draw(m_shaders[shaderId], false, shaderId, false); } } + tempGetShadowMap(); FrameBuffer::screen->bindFBO(); } - + +void PointLight::tempGetShadowMap() +{ + m_shadowMap->getTexture(0)->getData(0)->save("cubemapFace0.png"); + m_shadowMap->getTexture(0)->getData(1)->save("cubemapFace1.png"); + m_shadowMap->getTexture(0)->getData(2)->save("cubemapFace2.png"); + m_shadowMap->getTexture(0)->getData(3)->save("cubemapFace3.png"); + m_shadowMap->getTexture(0)->getData(4)->save("cubemapFace4.png"); + m_shadowMap->getTexture(0)->getData(5)->save("cubemapFace5.png"); +} + // OLD IMPLEMENTATION /* diff --git a/src/light.h b/src/light.h index 31ae946..0fd93b8 100644 --- a/src/light.h +++ b/src/light.h @@ -108,6 +108,8 @@ public: void destroyShadowMap(); void updateShadowMap(Scene* scene); + void tempGetShadowMap(); + private: void updateTransform(); diff --git a/src/model.cpp b/src/model.cpp index a1c4c25..02b19a0 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -4,6 +4,8 @@ #include "texture.h" #include "phongmaterial.h" +Model::Model() : m_texPack(NULL) {} + Model::~Model() { destroy(); @@ -11,7 +13,7 @@ Model::~Model() for(Mesh *m : m_meshes) delete m; } - +/* Image* Model::getImage(const std::string &name) { for(TextureImg &tex : m_textures) @@ -30,7 +32,7 @@ Texture* Model::getTexture(const std::string &name) return tex.tex; } return NULL; -} +}*/ void Model::destroy() { @@ -78,7 +80,8 @@ bool Model::save(const std::string &filename, const std::string &texturesPath) 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) { @@ -86,14 +89,16 @@ bool Model::save(const std::string &filename, const std::string &texturesPath) PhongMaterial* mat = (PhongMaterial*)m->getMaterial(); ok = ok && mat->serialize(file); } - for(Model::TextureImg &texImg : m_textures) + // 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); } diff --git a/src/model.h b/src/model.h index cd2dda0..60666d6 100644 --- a/src/model.h +++ b/src/model.h @@ -6,12 +6,12 @@ class Mesh; class Image; -class Texture; +class TexturePack; class Model { protected: - struct TextureImg + /*struct TextureImg { std::string name; Texture* tex; @@ -19,12 +19,16 @@ protected: TextureImg(const std::string n, Image* i) : name(n), tex(NULL), img(i) {} - }; + };*/ + TexturePack *m_texPack; std::vector m_meshes; - std::vector m_textures; + + //std::vector m_textures; public: + Model(); + ~Model(); /** @@ -35,10 +39,10 @@ public: /** * @brief addImage adds an image to the texture pack */ - void addImage(const std::string &name, Image* img) + /*void addImage(const std::string &name, Image* img) { m_textures.push_back(TextureImg(name, img)); - } + }*/ /** * @brief getMeshes returns the mesh array @@ -48,15 +52,16 @@ public: /** * @brief getImage returns the image which has the specified name, or NULL if there are no images of this name. */ - Image* getImage(const std::string &name); + //Image* getImage(const std::string &name); /** * @brief getTexture returns the texture which has the specified name, or NULL if there are no images of this name. */ - 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. + * (not supported yet when rendering because of flags) */ void destroy(); diff --git a/src/texture.cpp b/src/texture.cpp index 630c00e..b94a81a 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -162,7 +162,7 @@ void Texture::unbind() glBindTexture(m_target, 0); } -Image* Texture::getData() +Image* Texture::getData(int faceId) { bind(); int nbVal; @@ -172,11 +172,11 @@ Image* Texture::getData() case GL_RGBA: nbVal = 4; break; case GL_RGB: nbVal = 3; break; case GL_RED: nbVal = 1; break; + case GL_DEPTH_COMPONENT: nbVal = 1; break; default: return NULL; } switch(m_target) { - case GL_TEXTURE_CUBE_MAP: nbImages = 6; break; case GL_TEXTURE_2D_ARRAY: nbImages = 3; break; // TODO : add a member specifying how many layers has the texture default: nbImages = 1; break; } @@ -188,6 +188,9 @@ Image* Texture::getData() img->height = m_height; img->width = m_width; img->allocate(size); - glGetTexImage(m_target, 0, m_format, GL_UNSIGNED_BYTE, img->pixels.data()); + if(m_target == GL_TEXTURE_CUBE_MAP) + glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X+faceId, 0, m_format, GL_UNSIGNED_BYTE, img->pixels.data()); + else + glGetTexImage(m_target, 0, m_format, GL_UNSIGNED_BYTE, img->pixels.data()); return img; } diff --git a/src/texture.h b/src/texture.h index 6b85d97..08d6282 100644 --- a/src/texture.h +++ b/src/texture.h @@ -53,7 +53,12 @@ public: int getWidth() {return m_width;} int getHeight() {return m_height;} - Image* getData(); + /** + * @brief getData downloads the texture data as an Image from GPU memory, + * it is particulary useful to take a screenshot or debugging a framebuffer. + * @param faceId is the id of the face if the texture is a cubemap, must be between 0 and 5 + */ + Image* getData(int faceId = 0); }; #endif // TEXTURE_H