-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.h
More file actions
207 lines (199 loc) · 5.45 KB
/
Copy pathAnimation.h
File metadata and controls
207 lines (199 loc) · 5.45 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#pragma once
#include <string>
#include <vector>
#include <map>
#include "Maths.h"
struct Bone
{
std::string name;
Matrix offset;
int parentIndex;
};
struct Skeleton
{
std::vector<Bone> bones;
Matrix globalInverse;
int findBone(std::string name)
{
for (int i = 0; i < bones.size(); i++)
{
if (bones[i].name == name)
{
return i;
}
}
return -1;
}
};
struct AnimationFrame
{
std::vector<Vec3> positions;
std::vector<Quaternion> rotations;
std::vector<Vec3> scales;
};
struct AnimationSequence // This holds rescaled times
{
std::vector<AnimationFrame> frames;
float ticksPerSecond;
Vec3 interpolate(Vec3 p1, Vec3 p2, float t)
{
return ((p1 * (1.0f - t)) + (p2 * t));
}
Quaternion interpolate(Quaternion q1, Quaternion q2, float t)
{
return Quaternion::slerp(q1, q2, t);
}
float duration()
{
return ((float)frames.size() / ticksPerSecond);
}
void calcFrame(float t, int& frame, float& interpolationFact)
{
interpolationFact = t * ticksPerSecond;
frame = (int)floorf(interpolationFact);
interpolationFact = interpolationFact - (float)frame;
frame = std::min(frame, (int)(frames.size() - 1));
}
bool running(float t)
{
if ((int)floorf(t * ticksPerSecond) < frames.size())
{
return true;
}
return false;
}
int nextFrame(int frame)
{
return std::min(frame + 1, (int)(frames.size() - 1));
}
Matrix interpolateBoneToGlobal(Matrix* matrices, int baseFrame, float interpolationFact, Skeleton* skeleton, int boneIndex)
{
Matrix scale;
scale.scaling(interpolate(frames[baseFrame].scales[boneIndex], frames[nextFrame(baseFrame)].scales[boneIndex], interpolationFact).x
, interpolate(frames[baseFrame].scales[boneIndex], frames[nextFrame(baseFrame)].scales[boneIndex], interpolationFact).y
, interpolate(frames[baseFrame].scales[boneIndex], frames[nextFrame(baseFrame)].scales[boneIndex], interpolationFact).z);
Matrix rotation = interpolate(frames[baseFrame].rotations[boneIndex], frames[nextFrame(baseFrame)].rotations[boneIndex], interpolationFact).toMatrix();
Matrix translation;
translation.translation(interpolate(frames[baseFrame].positions[boneIndex], frames[nextFrame(baseFrame)].positions[boneIndex], interpolationFact).x
, interpolate(frames[baseFrame].positions[boneIndex], frames[nextFrame(baseFrame)].positions[boneIndex], interpolationFact).y
, interpolate(frames[baseFrame].positions[boneIndex], frames[nextFrame(baseFrame)].positions[boneIndex], interpolationFact).z);
Matrix local = translation * rotation * scale;
if (skeleton->bones[boneIndex].parentIndex > -1)
{
Matrix global = matrices[skeleton->bones[boneIndex].parentIndex] * local;
return global;
}
return local;
}
};
class Animation
{
public:
std::map<std::string, AnimationSequence> animations;
Skeleton skeleton;
int bonesSize()
{
return skeleton.bones.size();
}
void calcFrame(std::string name, float t, int& frame, float& interpolationFact)
{
animations[name].calcFrame(t, frame, interpolationFact);
}
Matrix interpolateBoneToGlobal(std::string name, Matrix* matrices, int baseFrame, float interpolationFact, int boneIndex)
{
return animations[name].interpolateBoneToGlobal(matrices, baseFrame, interpolationFact, &skeleton, boneIndex);
}
void calcTransforms(Matrix* matrices, Matrix coordTransform)
{
for (int i = 0; i < bonesSize(); i++)
{
matrices[i] = coordTransform * skeleton.globalInverse * matrices[i] * skeleton.bones[i].offset ;
}
}
bool hasAnimation(std::string name)
{
if (animations.find(name) == animations.end())
{
return false;
}
return true;
}
};
class AnimationInstance
{
public:
Animation* animation;
std::string usingAnimation;
float t;
Matrix matrices[256]; // This is defined as 256 to match the maximum number in the shader
Matrix matricesPose[256]; // This is to store transforms needed for finding bone positions
Matrix coordTransform;
void init(Animation* _animation, int fromYZX)
{
animation = _animation;
t = 0.0f;
if (fromYZX == 1)
{
memset(coordTransform.a, 0, 16 * sizeof(float));
coordTransform.a[0][0] = 1.0f;
coordTransform.a[2][1] = 1.0f;
coordTransform.a[1][2] = -1.0f;
coordTransform.a[3][3] = 1.0f;
}
}
void update(std::string name, float dt)
{
if (name == usingAnimation)
{
t += dt;
} else
{
usingAnimation = name;
t = 0;
}
if (animationFinished() == true)
{
return;
}
int frame = 0;
float interpolationFact = 0;
animation->calcFrame(name, t, frame, interpolationFact);
for (int i = 0; i < animation->bonesSize(); i++)
{
matrices[i] = animation->interpolateBoneToGlobal(name, matrices, frame, interpolationFact, i);
}
animation->calcTransforms(matrices, coordTransform);
}
void resetAnimationTime()
{
t = 0;
}
bool animationFinished()
{
if (usingAnimation.empty()) return false;
if (t > animation->animations[usingAnimation].duration())
{
return true;
}
return false;
}
Matrix findWorldMatrix(std::string boneName)
{
int boneID = animation->skeleton.findBone(boneName);
std::vector<int> boneChain;
int ID = boneID;
while (ID != -1)
{
boneChain.push_back(ID);
ID = animation->skeleton.bones[ID].parentIndex;
}
int frame = 0;
float interpolationFact = 0;
animation->calcFrame(usingAnimation, t, frame, interpolationFact);
for (int i = boneChain.size() - 1; i > -1; i = i - 1)
{
matricesPose[boneChain[i]] = animation->interpolateBoneToGlobal(usingAnimation, matricesPose, frame, interpolationFact, boneChain[i]);
}
return (coordTransform * matricesPose[boneID]);
}
};