Updated source code files of skeletal animation guest article.

This commit is contained in:
Joey de Vries
2021-09-08 20:43:58 +02:00
parent 0ce49088e5
commit a051a65d80
5 changed files with 73 additions and 25 deletions

View File

@@ -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];

View File

@@ -9,15 +9,17 @@
#include <learnopengl/bone.h>
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<glm::mat4> GetPoseTransforms()
{
return m_Transforms;
std::vector<glm::mat4> GetFinalBoneMatrices()
{
return m_FinalBoneMatrices;
}
private:
std::vector<glm::mat4> m_Transforms;
std::vector<glm::mat4> m_FinalBoneMatrices;
Animation* m_CurrentAnimation;
float m_CurrentTime;
float m_DeltaTime;
};

View File

@@ -1,18 +1,13 @@
#pragma once
#include <glm/glm.hpp>
#include<glm/glm.hpp>
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;
};

View File

@@ -0,0 +1,11 @@
#version 430 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()
{
FragColor = texture(texture_diffuse1, TexCoords);
}

View File

@@ -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;
}