diff --git a/BaseGLProject/Models/Mesh.cpp b/BaseGLProject/Models/Mesh.cpp index 00505f8..6c82207 100644 --- a/BaseGLProject/Models/Mesh.cpp +++ b/BaseGLProject/Models/Mesh.cpp @@ -231,7 +231,7 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m) if (material->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { std::string FullPath = parent->directory + Path.data; //texture file - textures.push_back(Texture(FullPath)); + textures.push_back(parent->textures[FullPath]); } } } @@ -341,6 +341,22 @@ Mesh::Mesh(const char *filename, Shader *sh) return; } + // Load all textures + for (unsigned int i = 0; i < scene->mNumMaterials; i++) + { + aiMaterial* material = scene->mMaterials[i]; + if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) { //we only care diffuse texture + aiString Path; + if (material->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, + NULL, NULL) == AI_SUCCESS) { + std::string FullPath = directory + Path.data; //texture file + textures.emplace(FullPath, Texture(FullPath)); + } + } + } + + + for (unsigned int i = 0; i < scene->mNumMeshes; ++i) { meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this)); } @@ -393,19 +409,20 @@ void Mesh::textureUnbinding() void Mesh::textureBinding(Shader *shd) { int nmap_counter = 0; - - for (GLuint i = 0; i < textures.size(); i++) + int i = 0; + for (std::map::iterator it = textures.begin(); it != textures.end(); ++it) { glActiveTexture(GL_TEXTURE0 + i); - glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref); - shd->applyTextureMaterial(textures[i]); - if (textures[i].isNmap) + glBindTexture(GL_TEXTURE_2D, it->second.tex_ref); + shd->applyTextureMaterial(it->second); + if (it->second.isNmap) { shd->addUniform("nmap", (int)i); nmap_counter++; } else - shd->addUniform("tex[" + std::to_string(i) + "]", (int)i - nmap_counter); + shd->addUniform("tex[" + std::to_string(i - nmap_counter) + "]", (int)i); + i++; } if (textures.size() > 0) shd->addUniform("TexCount", (int)textures.size() - nmap_counter); @@ -432,23 +449,23 @@ void Mesh::draw(SceneContext &ctx) { glm::vec3 specular = glm::vec3(meshEntries.at(i)->scolor.r, meshEntries.at(i)->scolor.g, meshEntries.at(i)->scolor.b); glm::vec3 ambient = glm::vec3(meshEntries.at(i)->acolor.r, meshEntries.at(i)->acolor.g, meshEntries.at(i)->acolor.b); - if (glm::length(ambient) == 0 && textures.size() > 0 && textures[0].mat.enabled) - ambient = textures[0].mat.ka; + if (glm::length(ambient) == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled) + ambient = textures.begin()->second.mat.ka; else if (glm::length(ambient) == 0) ambient = glm::vec3(0.1, 0.1, 0.1); - if (glm::length(diffuse) == 0 && textures.size() > 0 && textures[0].mat.enabled) - diffuse = textures[0].mat.ks; + if (glm::length(diffuse) == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled) + diffuse = textures.begin()->second.mat.ks; else if (glm::length(diffuse) == 0) diffuse = glm::vec3(0.9, 0.9, 0.9); - if (glm::length(specular) == 0 && textures.size() > 0 && textures[0].mat.enabled) - specular = textures[0].mat.ks; + if (glm::length(specular) == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled) + specular = textures.begin()->second.mat.ks; else if (glm::length(specular) == 0) specular = glm::vec3(0.4, 0.4, 0.4); - if (shininess == 0 && textures.size() > 0 && textures[0].mat.enabled) - shininess = textures[0].mat.shininess; + if (shininess == 0 && textures.size() > 0 && textures.begin()->second.mat.enabled) + shininess = textures.begin()->second.mat.shininess; else if (shininess == 0) shininess = 150.0f; @@ -476,7 +493,7 @@ void Mesh::draw(SceneContext &ctx) { void Mesh::assignTexture(Texture &texture) { - textures.emplace_back(texture); + //textures.emplace_back(texture); } void Mesh::effectTransformations() diff --git a/BaseGLProject/Models/Mesh.h b/BaseGLProject/Models/Mesh.h index 98bf562..95afea0 100644 --- a/BaseGLProject/Models/Mesh.h +++ b/BaseGLProject/Models/Mesh.h @@ -72,7 +72,7 @@ public: ~Mesh(void); std::string directory; std::vector meshEntries; - std::vector textures; + std::map textures; private: std::vector> _transformations; diff --git a/BaseGLProject/MyGLWindow.cpp b/BaseGLProject/MyGLWindow.cpp index 6b7dc39..0d51478 100644 --- a/BaseGLProject/MyGLWindow.cpp +++ b/BaseGLProject/MyGLWindow.cpp @@ -133,15 +133,15 @@ void MyGlWindow::setup() shaderSetup(); lightSetup(); - meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"])); - meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0)); - meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]); - meshes["Ogre"]->assignTexture(_scnctx.textures["OgreNmap"]); + //meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"])); + //meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0)); + //meshes["Ogre"]->assignTexture(_scnctx.textures["OgreTex"]); + //meshes["Ogre"]->assignTexture(_scnctx.textures["OgreNmap"]); - meshes.emplace("Cube", new Mesh("cube/cube.obj", shaders["TexNmapLight"])); - meshes["Cube"]->addTranslation(glm::vec4(0.5, 1, 0, 0)); - meshes["Cube"]->assignTexture(_scnctx.textures["CubeTex"]); - meshes["Cube"]->assignTexture(_scnctx.textures["CubeNmap"]); + //meshes.emplace("Cube", new Mesh("cube/cube.obj", shaders["TexNmapLight"])); + //meshes["Cube"]->addTranslation(glm::vec4(0.5, 1, 0, 0)); + //meshes["Cube"]->assignTexture(_scnctx.textures["CubeTex"]); + //meshes["Cube"]->assignTexture(_scnctx.textures["CubeNmap"]); //meshes["Buddha"]->addScaling(glm::vec4(4, 4, 4, 0)); //meshes["Buddha"]->addRotation(glm::vec4(0, 1, 0, 180)); @@ -157,11 +157,11 @@ void MyGlWindow::setup() //meshes["Cube"]->assignTexture(_scnctx.textures["BrickTex"]); //meshes["Cube"]->assignTexture(_scnctx.textures["MossTex"]); //meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0)); - + //meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"])); - //meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"])); - //meshes["Sponza"]->addScaling(glm::vec4(0.2, 0.2, 0.2, 1)); - //meshes["Sponza"]->cullMode = CullFace::BACK; + + meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"])); + meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));