diff --git a/src/Model.cpp b/src/Model.cpp index 85b78cb..492d5bd 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -6,6 +6,13 @@ Model::Model(const char* pathToModel) { loadModel(pathToModel); } +Model::~Model() { + // Go through all loaded textures and delete them + for(auto it = loadedTextures.begin(); it != loadedTextures.end(); it++) { + delete (*it); + } +} + void Model::draw(ShaderProgram *shaderProgram) { // Iterate through every mesh and call the draw function @@ -106,20 +113,24 @@ std::vector Model::loadMaterialTextures(aiMaterial *mat, aiTextureType aiString filename; mat->GetTexture(type, i, &filename); + std::string currentPath = directory + '/' + filename.C_Str(); + bool skip = 0; for(uint j = 0; j < loadedTextures.size(); j++) { - if(std::strcmp(loadedTextures[j].getPath().data(), filename.C_Str()) == 0) { - textures.push_back(&loadedTextures[j]); + if(std::strcmp(loadedTextures[j]->getPath().c_str(), currentPath.c_str()) == 0) { + textures.push_back(loadedTextures[j]); skip = 1; break; } } if(!skip) { - std::string path = directory + '/' + filename.C_Str(); - Texture texture(path.c_str(), textureType); - textures.push_back(&texture); + Texture *texture = new Texture(currentPath.c_str(), textureType); loadedTextures.push_back(texture); + + // Add newest texture pointer to the mesh's texture vector + Texture *new_tex = loadedTextures.back(); + textures.push_back(new_tex); } } diff --git a/src/Model.h b/src/Model.h index e31d7d0..08b96ef 100644 --- a/src/Model.h +++ b/src/Model.h @@ -13,7 +13,7 @@ class Model { public: Model(const char* pathToModel); - ~Model() = default; + ~Model(); void draw(ShaderProgram *shaderProgram); @@ -30,7 +30,7 @@ private: std::vector meshes; - std::vector loadedTextures; + std::vector loadedTextures; void textureFromFile(aiMaterial *mat, aiTextureType type); diff --git a/src/Texture.cpp b/src/Texture.cpp index 1cf9125..21378ef 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -27,7 +27,6 @@ Texture::Texture(const char* texturePath, uint8_t textureType) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureBuffer); //glGenerateMipmap(GL_TEXTURE_2D); - } else { diff --git a/src/Window.cpp b/src/Window.cpp index 2472863..107f330 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -28,7 +28,7 @@ Window::Window() { glEnable(GL_DEPTH_TEST); // Disable mouse cursor - setGrabbedCursor(0); + setGrabbedCursor(1); #ifdef _DEBUG std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;