Fix bone offset matrix being unnecessarily inverted
authorLuke Lau <luke_lau@icloud.com>
Mon, 10 Feb 2020 02:48:03 +0000 (02:48 +0000)
committerLuke Lau <luke_lau@icloud.com>
Mon, 10 Feb 2020 02:48:03 +0000 (02:48 +0000)
Makefile
model.cpp
model.hpp

index 42ebf39a4decb5882ac181c406f8193a24f2628b..2225d1914b3cccb7596068546a2b95d28225c104 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 all: bin/main
 
-CXX_FLAGS := -g --std=c++17
+CXX_FLAGS := -g --std=c++17 -Wall
 
 bin/main: model.o material.o image.o skybox.o program.o main.cpp
        clang++ $(CXX_FLAGS) $^ \
index 107fda0b321317ab54a145d7e1249c08f340abde..f68db4b6407f2096c4313794e1bb3d1c00e569f1 100644 (file)
--- a/model.cpp
+++ b/model.cpp
@@ -232,11 +232,11 @@ void Model::Node::draw(   const std::vector<Mesh> &meshes,
        }
        
 
-       glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation) * model;
+       glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation);
 
-       for (auto child: children) {
-               boneTransforms[std::string(ai.mName.C_Str())] = m;
-       }
+       /* for (auto child: children) { */
+       /*      boneTransforms[std::string(ai.mName.C_Str())] = m; */
+       /* } */
        
        for (unsigned int i: meshIndices) {
                const Mesh &mesh = meshes[i];
@@ -250,13 +250,17 @@ void Model::Node::draw(   const std::vector<Mesh> &meshes,
 
                        std::string nodeName = pair.first;
                        unsigned int boneId = pair.second.first;
+                       // This is actually an inverse-bind matrix
+                       // i.e. position of the mesh in bone 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];
-                       }
+                       /* 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) {
@@ -266,7 +270,7 @@ void Model::Node::draw(     const std::vector<Mesh> &meshes,
                                }
                        }
 
-                       boneTrans = boneTrans * glm::inverse(boneOffset);
+                       boneTrans = boneTrans * boneOffset;
 
 
                        std::string boneLocStr = "bones[" + std::to_string(boneId) + "]";
index 5c74cb386ef50a1bca316dc3bd286132710c939c..2803a7ee3cc7147550af2928ce5978419727f6f8 100644 (file)
--- a/model.hpp
+++ b/model.hpp
@@ -45,7 +45,6 @@ class Model {
                                void draw(const std::vector<Mesh> &meshes, const std::vector<Material> &materials, const Skybox s, const float tick, glm::mat4 parentModel, BoneTransforms boneTransforms) const;
                                const std::vector<Node*> &getChildren() const { return children; }
                                Node* findNode(const aiNode &aiNode);
-                               glm::mat4 model = glm::mat4(1);
                                const aiNode &ai;
 
                        private: