From cf964bf2351a3c9c7b03cb4f6baeb7a90cbf1c9e Mon Sep 17 00:00:00 2001 From: Warwick Date: Tue, 14 Jun 2022 14:34:30 +0100 Subject: [PATCH] Unload textures when model is deleted, and remove warnings. --- src/Mesh.cpp | 12 ++++++++++++ src/Mesh.h | 4 ++++ src/Model.cpp | 11 ++++++++++- src/Model.h | 6 +++++- src/main.cpp | 5 +++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 6b9b227..d6eabeb 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -63,3 +63,15 @@ void Mesh::draw(ShaderLoader &shader) { glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); glBindVertexArray(0); } + +void Mesh::unloadTextures() { + for (int i = 0; i < textures.size(); i++) { + glDeleteTextures(1, &textures[i].id); + } +} + +// Until I have a firmer grasp on memory management, textures should only be +// managed by the Model class +Mesh::~Mesh() { // this->unloadTextures(); + // error.log("Mesh destructor called"); +} diff --git a/src/Mesh.h b/src/Mesh.h index ddd6f4f..52ce0f1 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -24,6 +24,8 @@ private: unsigned int VAO, VBO, EBO; void setupMesh(); + void unloadTextures(); + public: std::vector vertices; std::vector indices; @@ -33,4 +35,6 @@ public: Mesh(std::vector vertices, std::vector indices, std::vector textures); + + ~Mesh(); }; diff --git a/src/Model.cpp b/src/Model.cpp index 87145b4..6cd3111 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -1,6 +1,6 @@ #include "Model.h" -Model::Model(char *path) { loadModel(path); } +Model::Model(std::string path) { loadModel(path); } Model::Model(Mesh mesh) { this->meshes.push_back(mesh); } Model::Model(std::vector meshes) { this->meshes = meshes; } @@ -126,6 +126,7 @@ std::vector Model::loadMaterialTextures(aiMaterial *material, } return textures; } + unsigned int Model::loadTextureFromFile(std::string file, std::string directory) { // Use sdl2_image to load the texture. @@ -165,3 +166,11 @@ unsigned int Model::loadTextureFromFile(std::string file, return texture; } + +void Model::unloadTextures() { + for (int i = 0; i < textures_loaded.size(); i++) { + glDeleteTextures(1, &textures_loaded[i].id); + } +} + +Model::~Model() { this->unloadTextures(); } diff --git a/src/Model.h b/src/Model.h index 79e5e23..31c6fad 100644 --- a/src/Model.h +++ b/src/Model.h @@ -33,13 +33,15 @@ private: aiTextureType type, std::string typeName); + void unloadTextures(); + std::string directory; unsigned int loadTextureFromFile(std::string texture, std::string directory); std::vector textures_loaded; public: // Create a model from file - Model(char *path); + Model(std::string path); // Used to create a mesh out of a single mesh. Model(Mesh mesh); // Used to create a mesh out of multiple meshes. @@ -50,4 +52,6 @@ public: // Translate the model void translate(glm::vec3 translation); + + ~Model(); }; diff --git a/src/main.cpp b/src/main.cpp index 9b1198a..73e7221 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,7 +75,8 @@ int main(int argc, char **argv) { ROOT_DIR "data/shaders/fragment.glsl"); Model backpack(ROOT_DIR "data/models/backpack/backpack.obj"); - // Model cube(ROOT_DIR "data/models/cube/cube.obj"); + Model cube(ROOT_DIR "data/models/cube/cube.obj"); + cube.translate(glm::vec3(3.0f, 0.0f, -1.0f)); // Mess with perspective // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 @@ -124,7 +125,7 @@ int main(int argc, char **argv) { shader.setMat4("MVP", camera.getMVP()); // Draw Meshes - // cube.draw(shader); + cube.draw(shader); backpack.draw(shader); // Finally render everything