diff --git a/BaseGLProject/Models/Mesh.cpp b/BaseGLProject/Models/Mesh.cpp index 9dd0c0e..b03468b 100644 --- a/BaseGLProject/Models/Mesh.cpp +++ b/BaseGLProject/Models/Mesh.cpp @@ -29,6 +29,8 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m) vbo[TEXCOORD_BUFFER] = NULL; vbo[COLOR_BUFFER] = NULL; vbo[INDEX_BUFFER] = NULL; + vbo[TANGENTS_BUFFER] = NULL; + vbo[BITTANGENTS_BUFFER] = NULL; glGenVertexArrays(1, &vao); glBindVertexArray(vao); @@ -93,7 +95,7 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m) } -Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::string dirname) +Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m) { renderType = OBJ; parent = m; @@ -220,6 +222,15 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::st material->Get(AI_MATKEY_COLOR_DIFFUSE, dcolor); material->Get(AI_MATKEY_COLOR_AMBIENT, acolor); material->Get(AI_MATKEY_COLOR_SPECULAR, scolor); + + 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 = parent->directory + Path.data; //texture file + textures.push_back(Texture(FullPath)); + } + } } glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -250,6 +261,14 @@ Mesh::MeshEntry::~MeshEntry() { glDeleteBuffers(1, &vbo[COLOR_BUFFER]); } + if (vbo[BITTANGENTS_BUFFER]) { + glDeleteBuffers(1, &vbo[BITTANGENTS_BUFFER]); + } + + if (vbo[TANGENTS_BUFFER]) { + glDeleteBuffers(1, &vbo[TANGENTS_BUFFER]); + } + glDeleteVertexArrays(1, &vao); } @@ -258,6 +277,7 @@ Mesh::MeshEntry::~MeshEntry() { **/ void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) { + textureBinding(shd); glBindVertexArray(vao); if (renderType == NO_INDEX) { @@ -269,9 +289,31 @@ void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) { glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); glDrawElements(GL_TRIANGLES, size / sizeof(unsigned int), GL_UNSIGNED_INT, NULL); } + textureUnbinding(); glBindVertexArray(0); } +void Mesh::MeshEntry::textureBinding(Shader * shd) +{ + for (GLuint i = 0; i < textures.size(); i++) + { + glActiveTexture(GL_TEXTURE0 + i); + glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref); + shd->addUniform("tex[" + std::to_string(i) + "]", (int)i); + } + if (textures.size() > 0) + shd->addUniform("TexCount", (int)textures.size()); +} + +void Mesh::MeshEntry::textureUnbinding() +{ + for (GLuint i = 0; i < textures.size(); i++) + { + glActiveTexture(GL_TEXTURE0 + i); + glBindTexture(GL_TEXTURE_2D, 0); + } +} + /** * Mesh constructor, loads the specified filename if supported by Assimp **/ @@ -281,8 +323,8 @@ Mesh::Mesh(const char *filename, Shader *sh) std::string fullname; fullname = std::string("./Models/")+ std::string(filename); - std::string dirname = fullname; - dirname.resize(fullname.find_last_of('/')); + directory = fullname; + directory.resize(fullname.find_last_of('/') + 1); Assimp::Importer importer; //aiProcessPreset_TargetRealtime_Fast const aiScene* scene = importer.ReadFile(fullname.c_str(), @@ -296,34 +338,8 @@ Mesh::Mesh(const char *filename, Shader *sh) return; } - for (unsigned int i = 0; i < scene->mNumMaterials; i++) { - const aiMaterial* pMaterial = scene->mMaterials[i]; - if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) { //we only care diffuse texture - aiString Path; - if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, - NULL, NULL) == AI_SUCCESS) { - - std::string FullPath = dirname + "/" + Path.data; //texture file - - aiColor3D diffuse; - aiColor3D specular; - aiColor3D ambient; - - pMaterial->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); - pMaterial->Get(AI_MATKEY_COLOR_SPECULAR, specular); - pMaterial->Get(AI_MATKEY_COLOR_AMBIENT, ambient); - textures.push_back(Texture(FullPath)); - textures.back().mat.enabled = true; - textures.back().mat.ka = glm::vec3(ambient.r, ambient.g, ambient.b); - textures.back().mat.kd = glm::vec3(diffuse.r, diffuse.g, diffuse.b); - textures.back().mat.ks = glm::vec3(specular.r, specular.g, specular.b); - } - } - } - - for (unsigned int i = 0; i < scene->mNumMeshes; ++i) { - meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this, dirname)); + meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this)); } } diff --git a/BaseGLProject/Models/Mesh.h b/BaseGLProject/Models/Mesh.h index d07a62c..b4ae36a 100644 --- a/BaseGLProject/Models/Mesh.h +++ b/BaseGLProject/Models/Mesh.h @@ -52,20 +52,25 @@ public: glm::vec3 ssbo_data[3]; aiColor3D dcolor; aiColor3D acolor; - aiColor3D scolor; + aiColor3D scolor; + std::vector textures; float shininessStrength; - MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::string dirname); + MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m); MeshEntry(Dataset &set, Mesh *m); ~MeshEntry(); Mesh * parent; - void render(SceneContext &ctx, Shader *shd); + void render(SceneContext &ctx, Shader *shd); + private: + void textureBinding(Shader *shd); + void textureUnbinding(); }; public: Mesh(const char *filename, Shader *sh); Mesh(Dataset &dataset, Shader *sh); ~Mesh(void); + std::string directory; std::vector meshEntries; std::vector textures; @@ -74,8 +79,8 @@ private: void enableCulling(); void textureBinding(Shader *shd); - void disableCulling(); void textureUnbinding(); + void disableCulling(); public: Model model; CullFace cullMode = NONE;