From a051a65d8043b88542abcd1223e2bdd33f9ba75b Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Wed, 8 Sep 2021 20:43:58 +0200 Subject: [PATCH] Updated source code files of skeletal animation guest article. --- includes/learnopengl/animation.h | 9 +++-- includes/learnopengl/animator.h | 26 +++++++------ includes/learnopengl/animdata.h | 13 ++----- .../2020/skeletal_animation/anim_model.fs | 11 ++++++ .../2020/skeletal_animation/anim_model.vs | 39 +++++++++++++++++++ 5 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/8.guest/2020/skeletal_animation/anim_model.fs create mode 100644 src/8.guest/2020/skeletal_animation/anim_model.vs diff --git a/includes/learnopengl/animation.h b/includes/learnopengl/animation.h index 4305eb4..bf7b64c 100644 --- a/includes/learnopengl/animation.h +++ b/includes/learnopengl/animation.h @@ -33,7 +33,7 @@ public: aiMatrix4x4 globalTransformation = scene->mRootNode->mTransformation; globalTransformation = globalTransformation.Inverse(); ReadHeirarchyData(m_RootNode, scene->mRootNode); - SetupBones(animation, *model); + ReadMissingBones(animation, *model); } ~Animation() @@ -62,13 +62,14 @@ public: } private: - void SetupBones(const aiAnimation* animation, Model& model) + void ReadMissingBones(const aiAnimation* animation, Model& model) { int size = animation->mNumChannels; - auto& boneInfoMap = model.GetOffsetMatMap(); - int& boneCount = model.GetBoneCount(); + auto& boneInfoMap = model.GetBoneInfoMap();//getting m_BoneInfoMap from Model class + int& boneCount = model.GetBoneCount(); //getting the m_BoneCounter from Model class + //reading channels(bones engaged in an animation and their keyframes) for (int i = 0; i < size; i++) { auto channel = animation->mChannels[i]; diff --git a/includes/learnopengl/animator.h b/includes/learnopengl/animator.h index 341ccb2..6364f41 100644 --- a/includes/learnopengl/animator.h +++ b/includes/learnopengl/animator.h @@ -9,15 +9,17 @@ #include class Animator -{ +{ public: - Animator::Animator(Animation* current) + Animator::Animator(Animation* animation) { - m_CurrentAnimation = current; m_CurrentTime = 0.0; - m_Transforms.reserve(100); + m_CurrentAnimation = animation; + + m_FinalBoneMatrices.reserve(100); + for (int i = 0; i < 100; i++) - m_Transforms.push_back(glm::mat4(1.0f)); + m_FinalBoneMatrices.push_back(glm::mat4(1.0f)); } void Animator::UpdateAnimation(float dt) @@ -57,22 +59,22 @@ public: { int index = boneInfoMap[nodeName].id; glm::mat4 offset = boneInfoMap[nodeName].offset; - m_Transforms[index] = globalTransformation * offset; + m_FinalBoneMatrices[index] = globalTransformation * offset; } for (int i = 0; i < node->childrenCount; i++) CalculateBoneTransform(&node->children[i], globalTransformation); } - std::vector GetPoseTransforms() - { - return m_Transforms; + std::vector GetFinalBoneMatrices() + { + return m_FinalBoneMatrices; } - + private: - std::vector m_Transforms; + std::vector m_FinalBoneMatrices; Animation* m_CurrentAnimation; float m_CurrentTime; float m_DeltaTime; - + }; \ No newline at end of file diff --git a/includes/learnopengl/animdata.h b/includes/learnopengl/animdata.h index 7ba8e02..e7c25a2 100644 --- a/includes/learnopengl/animdata.h +++ b/includes/learnopengl/animdata.h @@ -1,18 +1,13 @@ #pragma once -#include +#include struct BoneInfo { - /* - For uniquely indentifying the bone and - for indexing bone transformation in shaders - */ + /*id is index in finalBoneMatrices*/ int id; - /* - map from bone name to offset matrix. - offset matrix transforms bone from bone space to local space - */ + + /*offset matrix transforms vertex from model space to bone space*/ glm::mat4 offset; }; diff --git a/src/8.guest/2020/skeletal_animation/anim_model.fs b/src/8.guest/2020/skeletal_animation/anim_model.fs new file mode 100644 index 0000000..1835b23 --- /dev/null +++ b/src/8.guest/2020/skeletal_animation/anim_model.fs @@ -0,0 +1,11 @@ +#version 430 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D texture_diffuse1; + +void main() +{ + FragColor = texture(texture_diffuse1, TexCoords); +} \ No newline at end of file diff --git a/src/8.guest/2020/skeletal_animation/anim_model.vs b/src/8.guest/2020/skeletal_animation/anim_model.vs new file mode 100644 index 0000000..654c57e --- /dev/null +++ b/src/8.guest/2020/skeletal_animation/anim_model.vs @@ -0,0 +1,39 @@ +#version 430 core + +layout(location = 0) in vec3 pos; +layout(location = 1) in vec3 norm; +layout(location = 2) in vec2 tex; +layout(location = 3) in ivec4 boneIds; +layout(location = 4) in vec4 weights; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +const int MAX_BONES = 100; +const int MAX_BONE_INFLUENCE = 4; +uniform mat4 finalBonesMatrices[MAX_BONES]; + +out vec2 TexCoords; + +void main() +{ + vec4 totalPosition = vec4(0.0f); + for(int i = 0 ; i < MAX_BONE_INFLUENCE ; i++) + { + if(boneIds[i] == -1) + continue; + if(boneIds[i] >=MAX_BONES) + { + totalPosition = vec4(pos,1.0f); + break; + } + vec4 localPosition = finalBonesMatrices[boneIds[i]] * vec4(pos,1.0f); + totalPosition += localPosition * weights[i]; + vec3 localNormal = mat3(finalBonesMatrices[boneIds[i]]) * norm; + } + + mat4 viewModel = view * model; + gl_Position = projection * viewModel * totalPosition; + TexCoords = tex; +} \ No newline at end of file