X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=model.cpp;h=f4fc307ebca7db0515ce0af40bc74d916ccc2021;hp=a11a0745dd9d4b8d41da1c8a3809fa34384e1037;hb=45e43c5d0b6a1415185211128d82190fd19c3fe9;hpb=b75b26b3081207cd4169f2ac50875e35d19b8a14 diff --git a/model.cpp b/model.cpp index a11a074..f4fc307 100644 --- a/model.cpp +++ b/model.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "util.hpp" Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { @@ -19,7 +20,7 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { normals.push_back(glm::vec3(v.x, v.y, v.z)); } else { std::cerr << "Missing normals" << std::endl; - exit(1); + abort(); } // check for texture coord set 0 if (aiMesh->HasTextureCoords(0)) { @@ -81,12 +82,13 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { 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)); + boneMap[std::string(aiBone->mName.C_Str())] = std::pair(i + 1, aiBone); for (int j = 0; j < aiBone->mNumWeights; j++) { aiVertexWeight vw = aiBone->mWeights[j]; - if (!boneWeightMap.count(vw.mVertexId)) boneWeightMap[vw.mVertexId] = std::vector>(); + if (!boneWeightMap.count(vw.mVertexId)) + boneWeightMap[vw.mVertexId] = std::vector>(); boneWeightMap[vw.mVertexId].push_back(std::pair(i + 1, vw.mWeight)); } } @@ -113,13 +115,13 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { 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) { +Model::Node::Node(aiNode &node, GLuint progId, AnimMap *am, std::set allBones, Node *p): ai(node), parent(p), progId(progId), animMap(am), isBone(allBones.count(std::string(node.mName.C_Str())) > 0) { 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)); + aiNode *child = node.mChildren[i]; + children.push_back(new Node(*child, progId, am, allBones, this)); } } @@ -202,33 +204,50 @@ glm::mat4 lerpScaling(const aiNodeAnim *anim, const float tick) { return aiMatrixToMat4(result); } -void Model::Node::draw( const std::vector &meshes, - const std::vector &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); +glm::mat4 Model::Node::totalTrans(const glm::mat4 parentTrans, const float tick) const { + glm::mat4 aiTrans = aiMatrixToMat4(ai.mTransformation); if (animMap->count(std::string(ai.mName.C_Str()))) { for (const Animation anim: animMap->at(std::string(ai.mName.C_Str()))) { + // animations are *absolute* + // they replace aiNode.mTransformation!! + aiTrans = glm::mat4(1); float t = fmod(tick, anim.duration); for (const aiNodeAnim *nodeAnim: anim.nodeAnims) { - animTrans *= lerpPosition(nodeAnim, t); - animTrans *= lerpRotation(nodeAnim, t); - animTrans *= lerpScaling(nodeAnim, t); + aiTrans *= lerpPosition(nodeAnim, t); + aiTrans *= lerpRotation(nodeAnim, t); + aiTrans *= lerpScaling(nodeAnim, t); } } } + glm::mat4 m = parentTrans * aiTrans * transform; + return m; +} - glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation); +const Model::Node &Model::Node::getRoot() const { + const Model::Node *rootPtr = this; + while (rootPtr->parent != nullptr) + rootPtr = rootPtr->parent; + const Model::Node &root = *rootPtr; + return root; +} - /* for (auto child: children) { */ - /* boneTransforms[std::string(ai.mName.C_Str())] = m; */ - /* } */ +void Model::Node::draw( const std::vector &meshes, + const std::vector &materials, + const Skybox skybox, + const float tick, + const BoneTransforms &boneTransforms, + glm::mat4 parentTrans = glm::mat4(1)) const { + + GLuint modelLoc = glGetUniformLocation(progId, "model"); + glm::mat4 m = totalTrans(parentTrans, tick); + +#ifdef DEBUG_NODES + if (isBone) + drawDebugNode(m, {0, 0.5, 1, 1}); + else + drawDebugNode(m); +#endif for (unsigned int i: meshIndices) { const Mesh &mesh = meshes[i]; @@ -238,32 +257,23 @@ void Model::Node::draw( const std::vector &meshes, std::vector idBones(17, glm::mat4(1.f)); glUniformMatrix4fv(glGetUniformLocation(progId, "bones"), 17, GL_FALSE, glm::value_ptr(idBones[0])); - for (std::pair> pair: mesh.boneMap) { + // bonemap: map from bone nodes to bone ids and aiBones + for (auto pair: mesh.boneMap) { + + std::string boneName = pair.first; - std::string nodeName = pair.first; unsigned int boneId = pair.second.first; + aiBone *bone = pair.second.second; // This is actually an inverse-bind matrix - // i.e. position of the mesh in bone space + // i.e. transforms bone space -> mesh space // so no need to inverse again! // https://github.com/assimp/assimp/pull/1803/files - 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); - } - } + glm::mat4 boneOffset = aiMatrixToMat4(bone->mOffsetMatrix); - boneTrans = boneTrans * boneOffset; + if (!boneTransforms.count(boneName)) abort(); + glm::mat4 boneTrans = boneTransforms.at(boneName); + boneTrans = boneTrans * boneOffset; std::string boneLocStr = "bones[" + std::to_string(boneId) + "]"; GLuint boneLoc = glGetUniformLocation(progId, boneLocStr.c_str()); @@ -289,24 +299,34 @@ void Model::Node::draw( const std::vector &meshes, glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0); } - for (Node *child: children) child->draw(meshes, materials, skybox, tick, m, boneTransforms); + for (Node *child: children) child->draw(meshes, materials, skybox, tick, boneTransforms, m); +} + +void printHierarchy(aiNode *n, int indent = 0) { + for (int i = 0; i < indent; i++) + fprintf(stderr, "\t"); + fprintf(stderr,"%s\n", n->mName.C_Str()); + printMatrix4x4(n->mTransformation); + for (int i = 0; i < n->mNumChildren; i++) + printHierarchy(n->mChildren[i], indent + 1); } Model::Model(const aiScene *scene, Program p): program(p) { glUseProgram(p.progId); + std::set allBones; for (int i = 0; i < scene->mNumMeshes; i++) { const aiMesh *mesh = scene->mMeshes[i]; meshes.push_back(Mesh(mesh, p.progId)); + for (int j = 0; j < mesh->mNumBones; j++) + allBones.insert(std::string(mesh->mBones[j]->mName.C_Str())); } - // TODO: handle default material inserted at the end by assimp - for (unsigned int i = 0; i < scene->mNumMaterials - 1; i++) { + for (unsigned int i = 0; i < scene->mNumMaterials; i++) { const aiMaterial &material = *scene->mMaterials[i]; materials.push_back(Material(material, *scene, p.progId)); } - AnimMap *animMap = new AnimMap(); for (int i = 0; i < scene->mNumAnimations; i++) { const aiAnimation *aiAnim = scene->mAnimations[i]; @@ -324,44 +344,49 @@ Model::Model(const aiScene *scene, Program p): program(p) { for (std::pair> pair: nodeAnims) { std::string nodeName = pair.first; - if (!animMap->count(nodeName)) (*animMap)[nodeName] = std::vector(); - (*animMap)[nodeName].push_back({ aiAnim->mDuration, pair.second }); + if (!animMap.count(nodeName)) animMap[nodeName] = std::vector(); + animMap[nodeName].push_back({ aiAnim->mDuration, pair.second }); } } - root = new Node(*(scene->mRootNode), p.progId, animMap); + root = new Node(*(scene->mRootNode), p.progId, &animMap, allBones, nullptr); } -/* 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); */ -/* } */ -/* } */ -/* } */ +std::map Model::calcBoneTransforms(const Node &n, const float tick, const std::set bones, const glm::mat4 parentTrans = glm::mat4(1)) const { + std::string name = std::string(n.ai.mName.C_Str()); -/* glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation) * model; */ -/* } */ + glm::mat4 m = n.totalTrans(parentTrans, tick); + + BoneTransforms res; + if (bones.count(name) > 0) + res[std::string(n.ai.mName.C_Str())] = m; // take part in hierarchy + else + m = glm::mat4(1); // ignore this node transformation + for (const auto child: n.getChildren()) + res.merge(calcBoneTransforms(*child, tick, bones, m)); + return res; +} void Model::draw(Skybox skybox, const float tick) const { glUseProgram(program.progId); + std::set bones; + for (auto m: this->meshes) { + for (auto b: m.boneMap) { + bones.insert(b.first); + } + } + auto boneTransforms = calcBoneTransforms(*root, tick, bones); - - root->draw(meshes, materials, skybox, tick); + root->draw(meshes, materials, skybox, tick, boneTransforms); } -Model::Node* Model::find(const std::string &name) { +Model::Node* Model::find(const std::string &name) const { return find(aiString(name)); } -Model::Node* Model::find(const aiString name) { +Model::Node* Model::find(const aiString name) const { const aiNode *node = root->ai.FindNode(name); Model::Node* res = root->findNode(*node); return res; @@ -375,3 +400,7 @@ Model::Node* Model::Node::findNode(const aiNode &aiNode) { } return nullptr; } + +bool Model::Node::operator==(const Model::Node &rhs) const { + return &ai == &rhs.ai; +}