-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationHelper.cpp
More file actions
executable file
·70 lines (54 loc) · 1.98 KB
/
AnimationHelper.cpp
File metadata and controls
executable file
·70 lines (54 loc) · 1.98 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "AnimationHelper.h"
Keyframe::Keyframe():TimePos(0),Translation(0.0f,0.0f,0.0f),Scale(1.0f,1.0f,1.0f),RotationQuat(0.0f,0.0f,0.0f,1.0f)
{
}
Keyframe::~Keyframe()
{
}
float BoneAnimation::GetStartTime()const
{
return Keyframes.front().TimePos;
}
float BoneAnimation::GetEndTime()const
{
return Keyframes.back().TimePos;
}
void BoneAnimation::Interpolate(float t , XMFLOAT4X4& M)const
{
if(t <= Keyframes.front().TimePos)
{
XMVECTOR S = XMLoadFloat3(&Keyframes.front().Scale);
XMVECTOR P = XMLoadFloat3(&Keyframes.front().Translation);
XMVECTOR Q = XMLoadFloat4(&Keyframes.front().RotationQuat);
XMVECTOR zero = XMVectorSet(0.0f,0.0f,0.0f,1.0f);
XMStoreFloat4x4(&M,XMMatrixAffineTransformation(S,zero,Q,P));
}else if(t >= Keyframes.back().TimePos)
{
XMVECTOR S = XMLoadFloat3(&Keyframes.back().Scale);
XMVECTOR P = XMLoadFloat3(&Keyframes.back().Translation);
XMVECTOR Q = XMLoadFloat4(&Keyframes.back().RotationQuat);
XMVECTOR zero = XMVectorSet(0.0f,0.0f,0.0f,1.0f);
XMStoreFloat4x4(&M,XMMatrixAffineTransformation(S,zero,Q,P));
}else
{
for (UINT i = 0; i < Keyframes.size()-1; i++)
{
if(t >= Keyframes[i].TimePos && t <= Keyframes[i+1].TimePos)
{
float lerpPercent = (t - Keyframes[i].TimePos)/(Keyframes[i+1].TimePos - Keyframes[i].TimePos);
XMVECTOR s0 = XMLoadFloat3(&Keyframes[i].Scale);
XMVECTOR s1 = XMLoadFloat3(&Keyframes[i+1].Scale);
XMVECTOR p0 = XMLoadFloat3(&Keyframes[i].Translation);
XMVECTOR p1 = XMLoadFloat3(&Keyframes[i+1].Translation);
XMVECTOR q0 = XMLoadFloat4(&Keyframes[i].RotationQuat);
XMVECTOR q1 = XMLoadFloat4(&Keyframes[i+1].RotationQuat);
XMVECTOR S = XMVectorLerp(s0,s1,lerpPercent);
XMVECTOR P = XMVectorLerp(p0,p1,lerpPercent);
XMVECTOR Q = XMQuaternionSlerp(q0,q1,lerpPercent);
XMVECTOR zero = XMVectorSet(0.0f,0.0f,0.0f,1.0f);
XMStoreFloat4x4(&M,XMMatrixAffineTransformation(S,zero,Q,P));
break;
}
}
}
}