Final project!
[opengl.git] / model.cpp
index 29e3d8ae8f2889a65e4ca569046715649cfe1b1d..efc7f222b9db70856e362cf50b15549b5d5c4d27 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -1,26 +1,46 @@
 #include "model.hpp"
 #include <iostream>
-#include <assimp/Importer.hpp>
-#include <assimp/scene.h>
 #include <assimp/postprocess.h>
+#include <assimp/quaternion.h>
+#include <glm/gtc/type_ptr.hpp>
+
+glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
+       glm::mat4 to;
+       for (int i = 0; i < 4; i++)
+               for (int j = 0; j < 4; j++)
+                       to[i][j] = from[j][i];
+       return to;
+}
 
 Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
+
+       std::vector<glm::vec3> vertices, normals, tangents, bitangents;
+       std::vector<glm::vec2> texCoords;
+
        for (int i = 0; i < aiMesh->mNumVertices; i++) {
                if (aiMesh->HasPositions()) {
                        aiVector3D v = aiMesh->mVertices[i];
                        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 {
+                       std::cerr << "Missing normals" << std::endl;
+                       exit(1);
                }
                // check for texture coord set 0
                if (aiMesh->HasTextureCoords(0)) {
                        const aiVector3D v = aiMesh->mTextureCoords[0][i];
                        texCoords.push_back(glm::vec2(v.x, v.y));
+               } else {
+                       texCoords.push_back(glm::vec2(0));
                }
+               materialIndex = aiMesh->mMaterialIndex;
        }
 
+       std::vector<GLuint> indices;
+
        for (int i = 0; i < aiMesh->mNumFaces; i++) {
                const aiFace &face = aiMesh->mFaces[i];
                if(face.mNumIndices == 3) {
@@ -30,35 +50,257 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) {
                }
        } 
        
+       numIndices = indices.size();
+       
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        
-       GLuint vbos[3];
-       glGenBuffers(3, vbos);
-       vertexVbo = vbos[0], normalVbo = vbos[1], indicesVbo = vbos[2];
-       
-       GLuint posLoc = glGetAttribLocation(progId, "vPosition");
-       GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
+       GLuint vbos[6];
+       glGenBuffers(6, vbos);
+       GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3];
+       GLuint boneVbo = vbos[4];
        
+       GLuint posLoc = glGetAttribLocation(progId, "pos");
        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, "unscaledNormal");
        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);
+
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
-};
 
-void Model::loadModel(const std::string &file) {
-       Assimp::Importer importer;
-       const aiScene *scene = importer.ReadFile(file, 
-                       aiProcess_Triangulate | aiProcess_PreTransformVertices |
-                       aiProcess_GenNormals);
+       // bones
+       std::vector<VertBones> vertBones(aiMesh->mNumVertices);
+
+       std::map<unsigned int, std::vector<std::pair<unsigned int, float>>> boneWeightMap;
+
+       for (unsigned int i = 0; i < aiMesh->mNumBones; i++) {
+               aiBone *aiBone = aiMesh->mBones[i];
+
+               boneMap[std::string(aiBone->mName.C_Str())] = std::pair(i + 1, aiMatrixToMat4(aiBone->mOffsetMatrix));
+
+               for (int j = 0; j < aiBone->mNumWeights; j++) {
+                       aiVertexWeight vw = aiBone->mWeights[j];
+
+                       if (!boneWeightMap.count(vw.mVertexId)) boneWeightMap[vw.mVertexId] = std::vector<std::pair<unsigned int, float>>();
+                       boneWeightMap[vw.mVertexId].push_back(std::pair(i + 1, vw.mWeight));
+               }
+       }
+
+       for (auto pair: boneWeightMap) {
+               unsigned int vertexId = pair.first;
+               for (int i = 0; i < pair.second.size() && i < 4; i++) {
+                       unsigned int boneId = pair.second[i].first;
+                       float weight = pair.second[i].second;
+                       vertBones[vertexId].ids[i] = boneId;
+                       vertBones[vertexId].weights[i] = weight;
+               }
+       }
+       
+       glBindBuffer(GL_ARRAY_BUFFER, boneVbo);
+       glBufferData(GL_ARRAY_BUFFER, sizeof(VertBones) * vertBones.size(), &vertBones[0], GL_STATIC_DRAW);
+
+       GLuint boneIdLoc = glGetAttribLocation(progId, "boneIds");
+       glEnableVertexAttribArray(boneIdLoc);
+       glVertexAttribIPointer(boneIdLoc, 4, GL_INT, sizeof(VertBones), 0);
+
+       GLuint boneWeightLoc = glGetAttribLocation(progId, "boneWeights");
+       glEnableVertexAttribArray(boneWeightLoc);
+       glVertexAttribPointer(boneWeightLoc, 4, GL_FLOAT, GL_FALSE, sizeof(VertBones), (const GLvoid *)sizeof(VertBones::ids));
+}
+
+Model::Node::Node(const aiNode &node, GLuint progId, AnimMap *am): ai(node), progId(progId), animMap(am) {
+       for (int i = 0; i < node.mNumMeshes; i++) {
+               meshIndices.push_back(node.mMeshes[i]);
+       }
+       for (int i = 0; i < node.mNumChildren; i++) {
+               const aiNode *child = node.mChildren[i];
+               children.push_back(new Node(*child, progId, am));
+       }
+}
+
+glm::mat4 lerpPosition(const aiNodeAnim *anim, const float tick) {
+       if (anim->mNumPositionKeys == 0) return glm::mat4(1.f);
+
+       int yIndex = -1;
+       for (int i = 0; i < anim->mNumPositionKeys; i++) {
+               aiVectorKey vk = anim->mPositionKeys[i];
+               if (vk.mTime > tick) {
+                       yIndex = i;
+                       break;
+               }
+       }
+       aiVector3D lerpPos;
+       if (yIndex == 0) {
+               lerpPos = anim->mPositionKeys[0].mValue;
+       } else if (yIndex == -1) {
+               lerpPos = anim->mPositionKeys[anim->mNumPositionKeys - 1].mValue;
+       } else {
+               auto X = anim->mPositionKeys[yIndex - 1];
+               auto Y = anim->mPositionKeys[yIndex];
+
+               lerpPos = (X.mValue * (float)(Y.mTime - tick) + Y.mValue * (float)(tick - X.mTime)) / (float)(Y.mTime - X.mTime);
+       }
+       aiMatrix4x4 result;
+       aiMatrix4x4::Translation(lerpPos, result);
+       return aiMatrixToMat4(result);
+}
+
+glm::mat4 lerpRotation(const aiNodeAnim *anim, const float tick) {
+       int yIndex = -1;
+       for (int i = 0; i < anim->mNumRotationKeys; i++) {
+               aiQuatKey vk = anim->mRotationKeys[i];
+               if (vk.mTime > tick) {
+                       yIndex = i;
+                       break;
+               }
+       }
+
+       aiQuaternion result;
+       if (yIndex < 1) {
+               result = anim->mRotationKeys[0].mValue;
+       } else if (yIndex == -1) {
+               result = anim->mRotationKeys[anim->mNumRotationKeys - 1].mValue;
+       } else {
+
+               auto X = anim->mRotationKeys[yIndex - 1];
+               auto Y = anim->mRotationKeys[yIndex];
+
+               float mix = (tick - X.mTime) / (Y.mTime - X.mTime);
+
+               aiQuaternion::Interpolate(result, X.mValue, Y.mValue, mix);
+
+       }
+       return aiMatrixToMat4(aiMatrix4x4(result.GetMatrix()));
+}
+
+glm::mat4 lerpScaling(const aiNodeAnim *anim, const float tick) {
+       int yIndex = -1;
+       for (int i = 0; i < anim->mNumScalingKeys; i++) {
+               aiVectorKey vk = anim->mScalingKeys[i];
+               if (vk.mTime > tick) {
+                       yIndex = i;
+                       break;
+               }
+       }
+
+       aiVector3D lerpPos;
+       if (yIndex < 1) {
+               lerpPos = anim->mScalingKeys[0].mValue;
+       } else {
+               auto X = anim->mScalingKeys[yIndex - 1];
+               auto Y = anim->mScalingKeys[yIndex];
+
+               lerpPos = (X.mValue * (float)(Y.mTime - tick) + Y.mValue * (float)(tick - X.mTime)) / (float)(Y.mTime - X.mTime);
+       }
+       aiMatrix4x4 result;
+       aiMatrix4x4::Scaling(lerpPos, result);
+       return aiMatrixToMat4(result);
+}
+
+void Model::Node::draw(        const std::vector<Mesh> &meshes,
+                                               const std::vector<Material> &materials,
+                                               const Skybox skybox,
+                                               const float tick,
+                                               glm::mat4 parentTrans = glm::mat4(1),
+                                               BoneTransforms boneTransforms = BoneTransforms()) const {
+
+       GLuint modelLoc = glGetUniformLocation(progId, "model");
+
+       glm::mat4 animTrans(1.f);
+       if (animMap->count(std::string(ai.mName.C_Str()))) {
+               for (const Animation anim: animMap->at(std::string(ai.mName.C_Str()))) {
+                       float t = fmod(tick, anim.duration);
+                       for (const aiNodeAnim *nodeAnim: anim.nodeAnims) {
+                               animTrans *= lerpPosition(nodeAnim, t);
+                               animTrans *= lerpRotation(nodeAnim, t);
+                               animTrans *= lerpScaling(nodeAnim, t);
+                       }
+               }
+       }
+       
+
+       glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation) * model;
+
+       for (auto child: children) {
+               boneTransforms[std::string(ai.mName.C_Str())] = m;
+       }
+       
+       for (unsigned int i: meshIndices) {
+               const Mesh &mesh = meshes[i];
+               glBindVertexArray(mesh.vao);
+
+               // bones
+               std::vector<glm::mat4> idBones(17, glm::mat4(1.f));
+               glUniformMatrix4fv(glGetUniformLocation(progId, "bones"), 17, GL_FALSE, glm::value_ptr(idBones[0]));
+
+               for (std::pair<std::string, std::pair<unsigned int, glm::mat4>> pair: mesh.boneMap) {
+
+                       std::string nodeName = pair.first;
+                       unsigned int boneId = pair.second.first;
+                       glm::mat4 boneOffset = pair.second.second;
+
+                       glm::mat4 boneTrans(1.f);
+                       if (boneTransforms.count(nodeName)) {
+                               std::cerr << "got bone transform from map" << std::endl;
+                               boneTrans = boneTransforms[nodeName];
+                       }
+                       for (const Animation anim: animMap->at(nodeName)) {
+                               float t = fmod(tick, anim.duration);
+                               for (const aiNodeAnim *nodeAnim: anim.nodeAnims) {
+                                       boneTrans = boneTrans * lerpPosition(nodeAnim, t);
+                                       boneTrans = boneTrans * lerpRotation(nodeAnim, t);
+                                       boneTrans = boneTrans * lerpScaling(nodeAnim, t);
+                               }
+                       }
+
+                       boneTrans = boneTrans * glm::inverse(boneOffset);
+
+
+                       std::string boneLocStr = "bones[" + std::to_string(boneId) + "]";
+                       GLuint boneLoc = glGetUniformLocation(progId, boneLocStr.c_str());
+                       glUniformMatrix4fv(boneLoc, 1, GL_FALSE, glm::value_ptr(boneTrans));
+               }
+
+               Material material = materials[mesh.materialIndex];
+               material.bind();
+
+               glUniform1i(glGetUniformLocation(progId, "irradianceMap"), 4);
+               glActiveTexture(GL_TEXTURE4);
+               glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getIrradianceMap());
+
+               glUniform1i(glGetUniformLocation(progId, "prefilterMap"), 5);
+               glActiveTexture(GL_TEXTURE5);
+               glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getPrefilterMap());
+
+               glUniform1i(glGetUniformLocation(progId, "brdfMap"), 6);
+               glActiveTexture(GL_TEXTURE6);
+               glBindTexture(GL_TEXTURE_2D, skybox.getBRDFMap());
+               
+               glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
+
+               glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
+       }
+       for (Node *child: children) child->draw(meshes, materials, skybox, tick, m, boneTransforms);
+}
+
+Model::Model(const std::string &path, Program p): program(p) {
+       glUseProgram(p.progId);
+       
+       const aiScene *scene = importer.ReadFile(path, 
+                       aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals | aiProcess_FlipUVs);
        if (!scene) {
                std::cerr << importer.GetErrorString() << std::endl;
                exit(1);
@@ -66,13 +308,81 @@ void Model::loadModel(const std::string &file) {
 
        for (int i = 0; i < scene->mNumMeshes; i++) {
                const aiMesh *mesh = scene->mMeshes[i];
-               meshes.push_back(Mesh(mesh, progId));
+               meshes.push_back(Mesh(mesh, p.progId));
        }
+
+       // TODO: handle default material inserted at the end by assimp
+       for (unsigned int i = 0; i < scene->mNumMaterials - 1; i++) {
+               const aiMaterial &material = *scene->mMaterials[i];
+               materials.push_back(Material(material, p.progId));
        }
 
-void Model::draw() {
-       for (Mesh &mesh: meshes) {
-               glBindVertexArray(mesh.vao);
-               glDrawElements(GL_TRIANGLES, mesh.indices.size(), GL_UNSIGNED_INT, 0);
+       AnimMap *animMap = new AnimMap();
+       for (int i = 0; i < scene->mNumAnimations; i++) {
+               const aiAnimation *aiAnim = scene->mAnimations[i];
+
+               std::map<std::string, std::vector<const aiNodeAnim*>> nodeAnims;
+
+               for (int j = 0; j < aiAnim->mNumChannels; j++) {
+                       const aiNodeAnim *nodeAnim = aiAnim->mChannels[j];
+                       std::string nodeName = std::string(nodeAnim->mNodeName.C_Str());
+                       
+                       if (!nodeAnims.count(nodeName)) nodeAnims[nodeName] = std::vector<const aiNodeAnim*>();
+
+                       nodeAnims[nodeName].push_back(nodeAnim);
+               }
+
+               for (std::pair<std::string, std::vector<const aiNodeAnim*>> pair: nodeAnims) {
+                       std::string nodeName = pair.first;
+
+                       if (!animMap->count(nodeName)) (*animMap)[nodeName] = std::vector<const Animation>();
+                       (*animMap)[nodeName].push_back({ aiAnim->mDuration, pair.second });
+               }
+       }
+
+       root = new Node(*(scene->mRootNode), p.progId, animMap);
+}
+
+void Model::calcBoneTransforms(aiNode &node, glm::mat4 parentTrans = glm::mat4(1), BoneTransforms boneTrans = BoneTransforms()) {
+       glm::mat4 animTrans(1.f);
+       if (animMap->count(std::string(ai.mName.C_Str()))) {
+               for (const Animation anim: animMap->at(std::string(ai.mName.C_Str()))) {
+                       float t = fmod(tick, anim.duration);
+                       for (const aiNodeAnim *nodeAnim: anim.nodeAnims) {
+                               animTrans *= lerpPosition(nodeAnim, t);
+                               animTrans *= lerpRotation(nodeAnim, t);
+                               animTrans *= lerpScaling(nodeAnim, t);
+                       }
+               }
+       }
+       
+
+       glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation) * model;
+}
+
+void Model::draw(Skybox skybox, const float tick) const {
+       glUseProgram(program.progId);
+
+       
+
+       root->draw(meshes, materials, skybox, tick);
+}
+
+Model::Node* Model::find(const std::string &name) {
+       return find(aiString(name));
+}
+
+Model::Node* Model::find(const aiString name) {
+       const aiNode *node = root->ai.FindNode(name);
+       Model::Node* res = root->findNode(*node);
+       return res;
+}
+
+Model::Node* Model::Node::findNode(const aiNode &aiNode) {
+       if (&ai == &aiNode) return this;
+       for (Model::Node *child: children) {
+               Model::Node *res = child->findNode(aiNode);
+               if (res) return res;
        }
+       return nullptr;
 }