mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-30 20:13:22 +08:00
Updated source code files of skeletal animation guest article.
This commit is contained in:
@@ -33,7 +33,7 @@ public:
|
|||||||
aiMatrix4x4 globalTransformation = scene->mRootNode->mTransformation;
|
aiMatrix4x4 globalTransformation = scene->mRootNode->mTransformation;
|
||||||
globalTransformation = globalTransformation.Inverse();
|
globalTransformation = globalTransformation.Inverse();
|
||||||
ReadHeirarchyData(m_RootNode, scene->mRootNode);
|
ReadHeirarchyData(m_RootNode, scene->mRootNode);
|
||||||
SetupBones(animation, *model);
|
ReadMissingBones(animation, *model);
|
||||||
}
|
}
|
||||||
|
|
||||||
~Animation()
|
~Animation()
|
||||||
@@ -62,13 +62,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SetupBones(const aiAnimation* animation, Model& model)
|
void ReadMissingBones(const aiAnimation* animation, Model& model)
|
||||||
{
|
{
|
||||||
int size = animation->mNumChannels;
|
int size = animation->mNumChannels;
|
||||||
|
|
||||||
auto& boneInfoMap = model.GetOffsetMatMap();
|
auto& boneInfoMap = model.GetBoneInfoMap();//getting m_BoneInfoMap from Model class
|
||||||
int& boneCount = model.GetBoneCount();
|
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++)
|
for (int i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
auto channel = animation->mChannels[i];
|
auto channel = animation->mChannels[i];
|
||||||
|
|||||||
@@ -11,13 +11,15 @@
|
|||||||
class Animator
|
class Animator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Animator::Animator(Animation* current)
|
Animator::Animator(Animation* animation)
|
||||||
{
|
{
|
||||||
m_CurrentAnimation = current;
|
|
||||||
m_CurrentTime = 0.0;
|
m_CurrentTime = 0.0;
|
||||||
m_Transforms.reserve(100);
|
m_CurrentAnimation = animation;
|
||||||
|
|
||||||
|
m_FinalBoneMatrices.reserve(100);
|
||||||
|
|
||||||
for (int i = 0; i < 100; i++)
|
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)
|
void Animator::UpdateAnimation(float dt)
|
||||||
@@ -57,20 +59,20 @@ public:
|
|||||||
{
|
{
|
||||||
int index = boneInfoMap[nodeName].id;
|
int index = boneInfoMap[nodeName].id;
|
||||||
glm::mat4 offset = boneInfoMap[nodeName].offset;
|
glm::mat4 offset = boneInfoMap[nodeName].offset;
|
||||||
m_Transforms[index] = globalTransformation * offset;
|
m_FinalBoneMatrices[index] = globalTransformation * offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < node->childrenCount; i++)
|
for (int i = 0; i < node->childrenCount; i++)
|
||||||
CalculateBoneTransform(&node->children[i], globalTransformation);
|
CalculateBoneTransform(&node->children[i], globalTransformation);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<glm::mat4> GetPoseTransforms()
|
std::vector<glm::mat4> GetFinalBoneMatrices()
|
||||||
{
|
{
|
||||||
return m_Transforms;
|
return m_FinalBoneMatrices;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<glm::mat4> m_Transforms;
|
std::vector<glm::mat4> m_FinalBoneMatrices;
|
||||||
Animation* m_CurrentAnimation;
|
Animation* m_CurrentAnimation;
|
||||||
float m_CurrentTime;
|
float m_CurrentTime;
|
||||||
float m_DeltaTime;
|
float m_DeltaTime;
|
||||||
|
|||||||
@@ -4,15 +4,10 @@
|
|||||||
|
|
||||||
struct BoneInfo
|
struct BoneInfo
|
||||||
{
|
{
|
||||||
/*
|
/*id is index in finalBoneMatrices*/
|
||||||
For uniquely indentifying the bone and
|
|
||||||
for indexing bone transformation in shaders
|
|
||||||
*/
|
|
||||||
int id;
|
int id;
|
||||||
/*
|
|
||||||
map from bone name to offset matrix.
|
/*offset matrix transforms vertex from model space to bone space*/
|
||||||
offset matrix transforms bone from bone space to local space
|
|
||||||
*/
|
|
||||||
glm::mat4 offset;
|
glm::mat4 offset;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
11
src/8.guest/2020/skeletal_animation/anim_model.fs
Normal file
11
src/8.guest/2020/skeletal_animation/anim_model.fs
Normal 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);
|
||||||
|
}
|
||||||
39
src/8.guest/2020/skeletal_animation/anim_model.vs
Normal file
39
src/8.guest/2020/skeletal_animation/anim_model.vs
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user