X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=model.cpp;h=8046e29434fcf467b29942dc56aa5b60b76b84cf;hp=4c5e2d33278f0120a72f1c07d2c72813bf20b713;hb=ba9c738e8660304aa0341eb44118e63502a4a009;hpb=37cba564a96018a5500e942498d4e48c0ebe73ed diff --git a/model.cpp b/model.cpp index 4c5e2d3..8046e29 100644 --- a/model.cpp +++ b/model.cpp @@ -5,7 +5,7 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { - std::vector vertices, normals; + std::vector vertices, normals, tangents, bitangents; std::vector texCoords; for (int i = 0; i < aiMesh->mNumVertices; i++) { @@ -14,11 +14,20 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { vertices.push_back(glm::vec3(v.x, v.y, v.z)); } if (aiMesh->HasNormals()) { - const aiVector3D v = aiMesh->mNormals[i]; + aiVector3D v = aiMesh->mNormals[i]; normals.push_back(glm::vec3(v.x, v.y, v.z)); } else { normals.push_back(glm::vec3(0)); } + if (aiMesh->HasTangentsAndBitangents()) { + aiVector3D t = aiMesh->mTangents[i]; + tangents.push_back(glm::vec3(t.x, t.y, t.z)); + aiVector3D b = aiMesh->mBitangents[i]; + bitangents.push_back(glm::vec3(b.x, b.y, b.z)); + } else { + tangents.push_back(glm::vec3(0)); + bitangents.push_back(glm::vec3(0)); + } // check for texture coord set 0 if (aiMesh->HasTextureCoords(0)) { const aiVector3D v = aiMesh->mTextureCoords[0][i]; @@ -45,29 +54,41 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { glGenVertexArrays(1, &vao); glBindVertexArray(vao); - GLuint vbos[4]; - glGenBuffers(4, vbos); + GLuint vbos[6]; + glGenBuffers(6, vbos); GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3]; + GLuint tangentVbo = vbos[4], bitangentVbo = vbos[5]; GLuint posLoc = glGetAttribLocation(progId, "vPosition"); - GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); - GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord"); - glBindBuffer(GL_ARRAY_BUFFER, vertexVbo); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(posLoc); glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); glBindBuffer(GL_ARRAY_BUFFER, normalVbo); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW); glEnableVertexAttribArray(normalLoc); glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + GLuint texCoordLoc = glGetAttribLocation(progId, "vTexCoord"); glBindBuffer(GL_ARRAY_BUFFER, texCoordVbo); glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), &texCoords[0], GL_STATIC_DRAW); glEnableVertexAttribArray(texCoordLoc); glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0); + GLuint tangentLoc = glGetAttribLocation(progId, "tangent"); + glBindBuffer(GL_ARRAY_BUFFER, tangentVbo); + glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW); + glEnableVertexAttribArray(tangentLoc); + glVertexAttribPointer(tangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + + GLuint bitangentLoc = glGetAttribLocation(progId, "bitangent"); + glBindBuffer(GL_ARRAY_BUFFER, bitangentVbo); + glBufferData(GL_ARRAY_BUFFER, bitangents.size() * sizeof(glm::vec3), &bitangents[0], GL_STATIC_DRAW); + glEnableVertexAttribArray(bitangentLoc); + glVertexAttribPointer(bitangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW); }; @@ -113,8 +134,7 @@ void Model::Node::draw( const std::vector &meshes, Model::Model(const std::string &path, GLuint progId): progId(progId) { const aiScene *scene = importer.ReadFile(path, - aiProcess_Triangulate | - aiProcess_GenNormals); + aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals); if (!scene) { std::cerr << importer.GetErrorString() << std::endl; exit(1);