X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=model.cpp;h=3c1ecf4830c2ce95989a09bfb4935cfb7d21d767;hp=efc7f222b9db70856e362cf50b15549b5d5c4d27;hb=83eb84c159c8ec75cda9fbeb05e3d5746b54ad53;hpb=610bb8ddab4ec871cadfed0a0b66695b8fea41a4 diff --git a/model.cpp b/model.cpp index efc7f22..3c1ecf4 100644 --- a/model.cpp +++ b/model.cpp @@ -1,16 +1,8 @@ #include "model.hpp" #include -#include #include #include -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) { @@ -232,11 +224,11 @@ void Model::Node::draw( const std::vector &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]; @@ -249,14 +241,21 @@ void Model::Node::draw( const std::vector &meshes, for (std::pair> pair: mesh.boneMap) { std::string nodeName = pair.first; + + if (animMap->count(nodeName) <= 0) break; + 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 +265,7 @@ void Model::Node::draw( const std::vector &meshes, } } - boneTrans = boneTrans * glm::inverse(boneOffset); + boneTrans = boneTrans * boneOffset; std::string boneLocStr = "bones[" + std::to_string(boneId) + "]"; @@ -296,25 +295,18 @@ void Model::Node::draw( const std::vector &meshes, for (Node *child: children) child->draw(meshes, materials, skybox, tick, m, boneTransforms); } -Model::Model(const std::string &path, Program p): program(p) { +Model::Model(const aiScene *scene, 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); - } - for (int i = 0; i < scene->mNumMeshes; i++) { const aiMesh *mesh = scene->mMeshes[i]; 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++) { + for (unsigned int i = 0; i < scene->mNumMaterials; i++) { const aiMaterial &material = *scene->mMaterials[i]; - materials.push_back(Material(material, p.progId)); + materials.push_back(Material(material, *scene, p.progId)); } AnimMap *animMap = new AnimMap(); @@ -343,22 +335,22 @@ Model::Model(const std::string &path, Program p): program(p) { 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); - } - } - } +/* 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; -} +/* glm::mat4 m = parentTrans * animTrans * aiMatrixToMat4(ai.mTransformation) * model; */ +/* } */ void Model::draw(Skybox skybox, const float tick) const { glUseProgram(program.progId);