-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimator.cpp
More file actions
53 lines (44 loc) · 1.55 KB
/
Animator.cpp
File metadata and controls
53 lines (44 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "precomp.h"
#include "Animator.h"
Framework::Animator::Animator(const Animation* startingAnimation)
{
PlayAnimation(startingAnimation);
}
void Framework::Animator::UpdateAnimation(const float deltaTime)
{
if (mCurrentAnimation != nullptr)
{
float previousTime = mCurrentTime;
mCurrentTime += mCurrentAnimation->GetTicksPerSecond() * deltaTime;
mCurrentTime = mLoopAnimation ? fmodf(mCurrentTime, mCurrentAnimation->GetDuration()) : std::min(mCurrentTime, mCurrentAnimation->GetDuration());
if (previousTime != mCurrentTime)
{
CalculateBoneTransform(mCurrentAnimation->GetRootNode(), glm::mat4(1.0f));
}
}
}
void Framework::Animator::PlayAnimation(const Animation* animation)
{
mCurrentAnimation = animation;
mCurrentTime = 0.0f;
CalculateBoneTransform(mCurrentAnimation->GetRootNode(), glm::mat4(1.0f));
}
void Framework::Animator::CalculateBoneTransform(const AssimpNodeData& node, const glm::mat4& parentTransform)
{
const std::optional<const Bone*> bone = node.mBone;
const glm::mat4 nodeTransform = bone.has_value() ? bone.value()->GetLocalMatrix(mCurrentTime) : node.mTransformMatrix;
const glm::mat4 globalTransformation = parentTransform * nodeTransform;
if (bone.has_value())
{
const size_t boneIndex = bone.value()->GetBoneIndex();
if (boneIndex >= mFinalBoneMatrices.size())
{
mFinalBoneMatrices.resize(boneIndex + 1);
}
mFinalBoneMatrices[boneIndex] = globalTransformation * bone.value()->GetOffset();
}
for (const AssimpNodeData& child : node.mChildren)
{
CalculateBoneTransform(child, globalTransformation);
}
}