From 4fc7751a561866478143ef91e2c5040ab0e280c6 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:45:21 -0400 Subject: [PATCH 01/14] Fixed texture bug When you load a model that does not have a diffuse, specular, normal or height map, it will use one from the previously rendered model. --- includes/learnopengl/mesh.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/includes/learnopengl/mesh.h b/includes/learnopengl/mesh.h index 803f4896d..99f83d418 100644 --- a/includes/learnopengl/mesh.h +++ b/includes/learnopengl/mesh.h @@ -91,7 +91,11 @@ class Mesh { glBindVertexArray(0); // always good practice to set everything back to defaults once configured. - glActiveTexture(GL_TEXTURE0); + for (unsigned int i = 0; i < textures.size(); i++) + { + glActiveTexture(GL_TEXTURE0 + i); + glBindTexture(GL_TEXTURE_2D, 0); + } } private: From faaa346d319a79b8ecf8628eda60d93cd0661c24 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Wed, 3 Aug 2022 09:20:31 -0400 Subject: [PATCH 02/14] Update mesh.h --- includes/learnopengl/mesh.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/includes/learnopengl/mesh.h b/includes/learnopengl/mesh.h index 99f83d418..e104f87fa 100644 --- a/includes/learnopengl/mesh.h +++ b/includes/learnopengl/mesh.h @@ -91,11 +91,7 @@ class Mesh { glBindVertexArray(0); // always good practice to set everything back to defaults once configured. - for (unsigned int i = 0; i < textures.size(); i++) - { - glActiveTexture(GL_TEXTURE0 + i); - glBindTexture(GL_TEXTURE_2D, 0); - } + glActiveTexture(GL_TEXTURE0); } private: From 857731d2bdca3eec64c181876adf113afe9066b0 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Wed, 3 Aug 2022 09:23:16 -0400 Subject: [PATCH 03/14] Update model.h --- includes/learnopengl/model.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index 81f864f24..ba7b8fc23 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -173,6 +173,39 @@ class Model vector loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName) { vector textures; + + // first check if there isn't a texture, if so create an empty image + if (mat->GetTextureCount(type) == 0) + { + std::string path = "DefaultTexture"; + + bool skip = false; + for (unsigned int j = 0; j < textures_loaded.size(); j++) + { + if (std::strcmp(textures_loaded[j].path.data(), path.c_str()) == 0) + { + textures.push_back(textures_loaded[j]); + skip = true; // a texture with the same filepath has already been loaded, continue to next one. (optimization) + break; + } + } + if (!skip) + { // if the empty texture hasn't been loaded already, load it + Texture texture; + + // just create an empty texture + unsigned int textureID; + glGenTextures(1, &textureID); + texture.id = textureID; + //texture.id = textureFromFile(path.c_str(), this->directory); + + texture.type = typeName; + texture.path = path.c_str(); + textures.push_back(texture); + textures_loaded.push_back(texture); // store it as texture loaded for entire model, to ensure we won't unnecessary load duplicate textures. + } + } + for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) { aiString str; From b3eaa6aaf6e530b8f2d2488c825fdf9035fc240f Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Wed, 3 Aug 2022 09:23:29 -0400 Subject: [PATCH 04/14] Restore mesh.h --- includes/learnopengl/mesh.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/learnopengl/mesh.h b/includes/learnopengl/mesh.h index e104f87fa..803f4896d 100644 --- a/includes/learnopengl/mesh.h +++ b/includes/learnopengl/mesh.h @@ -91,7 +91,7 @@ class Mesh { glBindVertexArray(0); // always good practice to set everything back to defaults once configured. - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0); } private: From a541d1a75952e154fd7459355cb37162f4da56a3 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Wed, 3 Aug 2022 11:03:04 -0400 Subject: [PATCH 05/14] Fixed graphical bugs Looks like you can't create a texture ID with no data at all, otherwise you can get graphical artifacts like with the sponza plants. Now I am binding the texture then creating a texture with no (nullptr) data. --- includes/learnopengl/model.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index ba7b8fc23..e2fb80f60 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -196,6 +196,8 @@ class Model // just create an empty texture unsigned int textureID; glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_2D, textureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); texture.id = textureID; //texture.id = textureFromFile(path.c_str(), this->directory); From 934f81aadc51502601b9ff54a1493b1c4d04e27e Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Wed, 3 Aug 2022 21:03:29 -0400 Subject: [PATCH 06/14] Update model.h Added a default for no specular maps instead of using the diffuse default. Personally I don't what anything with no specular map to be automatically bright. --- includes/learnopengl/model.h | 88 ++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index e2fb80f60..e5d8e6f6f 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -26,6 +26,8 @@ unsigned int TextureFromFile(const char *path, const string &directory, bool gam class Model { public: + static unsigned int noDiffuse, noSpecular, noNormal; + // model data vector textures_loaded; // stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once. vector meshes; @@ -35,6 +37,43 @@ class Model // constructor, expects a filepath to a 3D model. Model(string const &path, bool gamma = false) : gammaCorrection(gamma) { + noDiffuse = 0; + noSpecular = 0; + noNormal = 0; + if (noDiffuse == 0) + { + uint8_t data[4] = { 255,255,255 }; + glGenTextures(1, &noDiffuse); + glBindTexture(GL_TEXTURE_2D, noDiffuse); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + } + if (noSpecular == 0) + { + uint8_t data[4] = { 0,0,0,0 }; + glGenTextures(1, &noSpecular); + glBindTexture(GL_TEXTURE_2D, noSpecular); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + } + if (noNormal == 0) + { + uint8_t data[3] = { 128,128,255 }; + glGenTextures(1, &noNormal); + glBindTexture(GL_TEXTURE_2D, noNormal); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + } + loadModel(path); } @@ -174,38 +213,38 @@ class Model { vector textures; - // first check if there isn't a texture, if so create an empty image + // First check if there isn't a texture, if so create an empty image if (mat->GetTextureCount(type) == 0) { - std::string path = "DefaultTexture"; - bool skip = false; for (unsigned int j = 0; j < textures_loaded.size(); j++) { - if (std::strcmp(textures_loaded[j].path.data(), path.c_str()) == 0) + if (std::strcmp(textures_loaded[j].path.data(), "NO_TEXTURE") == 0) { textures.push_back(textures_loaded[j]); - skip = true; // a texture with the same filepath has already been loaded, continue to next one. (optimization) + skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization) break; } } if (!skip) - { // if the empty texture hasn't been loaded already, load it + { // If texture hasn't been loaded already, load it Texture texture; - - // just create an empty texture - unsigned int textureID; - glGenTextures(1, &textureID); - glBindTexture(GL_TEXTURE_2D, textureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); - texture.id = textureID; - //texture.id = textureFromFile(path.c_str(), this->directory); - + + if (typeName == "texture_diffuse") + texture.id = noDiffuse; // Use the default diffuse map + else if (typeName == "texture_specular") + texture.id = noSpecular; // Use the default specular map + else if (typeName == "texture_normal") + texture.id = noNormal; // Use the next default normal map + else + texture.id = noSpecular; // Otherwise use the default specular map because it is just empty + texture.type = typeName; - texture.path = path.c_str(); + texture.path = "NO_TEXTURE"; textures.push_back(texture); - textures_loaded.push_back(texture); // store it as texture loaded for entire model, to ensure we won't unnecessary load duplicate textures. + textures_loaded.push_back(texture); // Store it as texture loaded for entire model, to ensure we won't unnecessary load duplicate textures. } + std::cout << "Lack " << typeName << "\n"; } for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) @@ -227,6 +266,13 @@ class Model { // if texture hasn't been loaded already, load it Texture texture; texture.id = TextureFromFile(str.C_Str(), this->directory); + if (texture.id == noDiffuse) + { + if (typeName == "texture_specular") + texture.id = noSpecular; + if (typeName == "texture_normal") + texture.id = noNormal; + } texture.type = typeName; texture.path = str.C_Str(); textures.push_back(texture); @@ -237,6 +283,8 @@ class Model } }; +unsigned int Model::noDiffuse = 0, Model::noSpecular = 0, Model::noNormal = 0; + unsigned int TextureFromFile(const char *path, const string &directory, bool gamma) { @@ -268,13 +316,17 @@ unsigned int TextureFromFile(const char *path, const string &directory, bool gam glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); stbi_image_free(data); + + return textureID; } else { std::cout << "Texture failed to load at path: " << path << std::endl; stbi_image_free(data); + + return Model::noDiffuse; } - return textureID; + return Model::noDiffuse; } #endif From 3b96b0d21b888d61492e3883668e60bf338aa470 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 08:51:00 -0400 Subject: [PATCH 07/14] Removed extra code --- includes/learnopengl/model.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index e5d8e6f6f..d7cdb2dfb 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -232,8 +232,6 @@ class Model if (typeName == "texture_diffuse") texture.id = noDiffuse; // Use the default diffuse map - else if (typeName == "texture_specular") - texture.id = noSpecular; // Use the default specular map else if (typeName == "texture_normal") texture.id = noNormal; // Use the next default normal map else From bdd256c5bd58ed96a826d21f1c80e88a30858767 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 08:52:26 -0400 Subject: [PATCH 08/14] Changed if to else if --- includes/learnopengl/model.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index d7cdb2dfb..c7c5613e4 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -268,7 +268,7 @@ class Model { if (typeName == "texture_specular") texture.id = noSpecular; - if (typeName == "texture_normal") + else if (typeName == "texture_normal") texture.id = noNormal; } texture.type = typeName; From 9e885e1c73ea82f75865cf7025ecf55c5ef2fa6c Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 08:56:14 -0400 Subject: [PATCH 09/14] Added specular https://github.com/JoeyDeVries/LearnOpenGL/pull/319#issuecomment-1204677128 --- includes/learnopengl/model.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index c7c5613e4..f0e5584cc 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -53,10 +53,10 @@ class Model } if (noSpecular == 0) { - uint8_t data[4] = { 0,0,0,0 }; + uint8_t data[3] = { 255,255,255 }; glGenTextures(1, &noSpecular); glBindTexture(GL_TEXTURE_2D, noSpecular); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); From c0000d6f7cf91668d0635c7a15a3aee1d4ea7da7 Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:30:21 -0400 Subject: [PATCH 10/14] Fixed bug (read commit description) After slapping some std::cout statements everywhere I found that once the program loads a default diffuse, specular or normal texture it would use that again for the next missing texture! So for example if a model doesn't have a specular or normal, it will check if the specular map is loaded, and because it is not it will use the default specular map for the missing specular map and the texture.path will be NO_TEXTURE. When we check if the normal map is already loaded it will see there is a texture already loaded with the name NO_TEXTURE so it will just use that texture thinking its the specular map it wanted, when in reality that is a specular map, not a normal map! Same goes if a model doesn't have any textures, it will use the default texture for diffuse, specular and normal. --- includes/learnopengl/model.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index f0e5584cc..451840701 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -219,7 +219,8 @@ class Model bool skip = false; for (unsigned int j = 0; j < textures_loaded.size(); j++) { - if (std::strcmp(textures_loaded[j].path.data(), "NO_TEXTURE") == 0) + std::string name = "DEFAULT_" + typeName; + if (std::strcmp(textures_loaded[j].path.data(), name.c_str()) == 0) { textures.push_back(textures_loaded[j]); skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization) @@ -238,7 +239,7 @@ class Model texture.id = noSpecular; // Otherwise use the default specular map because it is just empty texture.type = typeName; - texture.path = "NO_TEXTURE"; + texture.path = "DEFAULT_" + typeName; textures.push_back(texture); textures_loaded.push_back(texture); // Store it as texture loaded for entire model, to ensure we won't unnecessary load duplicate textures. } From a195123409a96f283d690027493c0f707b8895ad Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:41:16 -0400 Subject: [PATCH 11/14] Added default 'other' for height and other maps Before we used the specular map for things that aren't diffuse and specular maps. That would mean height maps would use the specular map and if emissive and other maps are added to the code, they too would use the specular map. --- includes/learnopengl/model.h | 62 +++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index 451840701..aebcc8a06 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -26,7 +26,7 @@ unsigned int TextureFromFile(const char *path, const string &directory, bool gam class Model { public: - static unsigned int noDiffuse, noSpecular, noNormal; + static unsigned int defaultDiffuse, defaultSpecular, defaultNormal, defaultOther; // model data vector textures_loaded; // stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once. @@ -37,42 +37,54 @@ class Model // constructor, expects a filepath to a 3D model. Model(string const &path, bool gamma = false) : gammaCorrection(gamma) { - noDiffuse = 0; - noSpecular = 0; - noNormal = 0; - if (noDiffuse == 0) + defaultDiffuse = 0; + defaultSpecular = 0; + defaultNormal = 0; + defaultOther = 0; + if (defaultDiffuse == 0) { uint8_t data[4] = { 255,255,255 }; - glGenTextures(1, &noDiffuse); - glBindTexture(GL_TEXTURE_2D, noDiffuse); + glGenTextures(1, &defaultDiffuse); + glBindTexture(GL_TEXTURE_2D, defaultDiffuse); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } - if (noSpecular == 0) + if (defaultSpecular == 0) { uint8_t data[3] = { 255,255,255 }; - glGenTextures(1, &noSpecular); - glBindTexture(GL_TEXTURE_2D, noSpecular); + glGenTextures(1, &defaultSpecular); + glBindTexture(GL_TEXTURE_2D, defaultSpecular); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } - if (noNormal == 0) + if (defaultNormal == 0) { uint8_t data[3] = { 128,128,255 }; - glGenTextures(1, &noNormal); - glBindTexture(GL_TEXTURE_2D, noNormal); + glGenTextures(1, &defaultNormal); + glBindTexture(GL_TEXTURE_2D, defaultNormal); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } + if (defaultOther == 0) + { + uint8_t data[4] = { 0,0,0,0 }; + glGenTextures(1, &defaultOther); + glBindTexture(GL_TEXTURE_2D, defaultOther); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + } loadModel(path); } @@ -232,11 +244,13 @@ class Model Texture texture; if (typeName == "texture_diffuse") - texture.id = noDiffuse; // Use the default diffuse map + texture.id = defaultDiffuse; // Use the default diffuse texture + else if (typeName == "texture_specular") + texture.id = defaultSpecular; // Use the default specular map else if (typeName == "texture_normal") - texture.id = noNormal; // Use the next default normal map + texture.id = defaultNormal; // Use the default normal map else - texture.id = noSpecular; // Otherwise use the default specular map because it is just empty + texture.id = defaultOther; // Otherwise use the default other because it is just empty texture.type = typeName; texture.path = "DEFAULT_" + typeName; @@ -265,12 +279,16 @@ class Model { // if texture hasn't been loaded already, load it Texture texture; texture.id = TextureFromFile(str.C_Str(), this->directory); - if (texture.id == noDiffuse) + if (texture.id == defaultDiffuse) { + if (typeName == "texture_diffuse") + texture.id = defaultDiffuse; if (typeName == "texture_specular") - texture.id = noSpecular; + texture.id = defaultSpecular; else if (typeName == "texture_normal") - texture.id = noNormal; + texture.id = defaultNormal; + else + texture.id = defaultOther; } texture.type = typeName; texture.path = str.C_Str(); @@ -282,7 +300,7 @@ class Model } }; -unsigned int Model::noDiffuse = 0, Model::noSpecular = 0, Model::noNormal = 0; +unsigned int Model::defaultDiffuse = 0, Model::defaultSpecular = 0, Model::defaultNormal = 0, Model::defaultOther = 0; unsigned int TextureFromFile(const char *path, const string &directory, bool gamma) @@ -323,9 +341,9 @@ unsigned int TextureFromFile(const char *path, const string &directory, bool gam std::cout << "Texture failed to load at path: " << path << std::endl; stbi_image_free(data); - return Model::noDiffuse; + return Model::defaultDiffuse; } - return Model::noDiffuse; + return Model::defaultDiffuse; } #endif From 1465d41632c7861210a7bdbc07991e3cb60ab6fe Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 14:50:21 -0400 Subject: [PATCH 12/14] Removed useless code --- includes/learnopengl/model.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index aebcc8a06..b547a2039 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -37,10 +37,6 @@ class Model // constructor, expects a filepath to a 3D model. Model(string const &path, bool gamma = false) : gammaCorrection(gamma) { - defaultDiffuse = 0; - defaultSpecular = 0; - defaultNormal = 0; - defaultOther = 0; if (defaultDiffuse == 0) { uint8_t data[4] = { 255,255,255 }; From d72f83eb52807a826303305129a321ae74e2d25c Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 14:57:41 -0400 Subject: [PATCH 13/14] Removed temp variable --- includes/learnopengl/model.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index b547a2039..cd01b9a54 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -227,8 +227,7 @@ class Model bool skip = false; for (unsigned int j = 0; j < textures_loaded.size(); j++) { - std::string name = "DEFAULT_" + typeName; - if (std::strcmp(textures_loaded[j].path.data(), name.c_str()) == 0) + if (std::strcmp(textures_loaded[j].path.data(), ("DEFAULT_" + typeName).c_str()) == 0) { textures.push_back(textures_loaded[j]); skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization) From e6a5bfa1c4485a34b10093cbadf4547a059e915f Mon Sep 17 00:00:00 2001 From: John Alexander Le Roux <94056103+CMDR-JohnAlex@users.noreply.github.com> Date: Thu, 4 Aug 2022 15:24:18 -0400 Subject: [PATCH 14/14] Removed 'defaultOther' --- includes/learnopengl/model.h | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/includes/learnopengl/model.h b/includes/learnopengl/model.h index cd01b9a54..039b5e7ba 100644 --- a/includes/learnopengl/model.h +++ b/includes/learnopengl/model.h @@ -26,7 +26,7 @@ unsigned int TextureFromFile(const char *path, const string &directory, bool gam class Model { public: - static unsigned int defaultDiffuse, defaultSpecular, defaultNormal, defaultOther; + static unsigned int defaultDiffuse, defaultSpecular, defaultNormal; // model data vector textures_loaded; // stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once. @@ -70,17 +70,6 @@ class Model glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } - if (defaultOther == 0) - { - uint8_t data[4] = { 0,0,0,0 }; - glGenTextures(1, &defaultOther); - glBindTexture(GL_TEXTURE_2D, defaultOther); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - } loadModel(path); } @@ -240,12 +229,10 @@ class Model if (typeName == "texture_diffuse") texture.id = defaultDiffuse; // Use the default diffuse texture - else if (typeName == "texture_specular") - texture.id = defaultSpecular; // Use the default specular map else if (typeName == "texture_normal") texture.id = defaultNormal; // Use the default normal map else - texture.id = defaultOther; // Otherwise use the default other because it is just empty + texture.id = defaultSpecular; // Use the default specular map texture.type = typeName; texture.path = "DEFAULT_" + typeName; @@ -278,12 +265,10 @@ class Model { if (typeName == "texture_diffuse") texture.id = defaultDiffuse; - if (typeName == "texture_specular") - texture.id = defaultSpecular; else if (typeName == "texture_normal") texture.id = defaultNormal; else - texture.id = defaultOther; + texture.id = defaultSpecular; } texture.type = typeName; texture.path = str.C_Str(); @@ -295,7 +280,7 @@ class Model } }; -unsigned int Model::defaultDiffuse = 0, Model::defaultSpecular = 0, Model::defaultNormal = 0, Model::defaultOther = 0; +unsigned int Model::defaultDiffuse = 0, Model::defaultSpecular = 0, Model::defaultNormal = 0; unsigned int TextureFromFile(const char *path, const string &directory, bool gamma)