-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.h
141 lines (116 loc) · 3.85 KB
/
Animation.h
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
#pragma once
#include <vector>
#include <string>
#include <map>
#include "mathlib.h"
struct Bone {
std::string name;
Matrix offset;
int parentIndex;
};
struct Skeleton {
std::vector<Bone> bones;
Matrix globalInverse;
};
struct AnimationFrame {
std::vector<Vec3> positions;
std::vector<Quaternion> rotations;
std::vector<Vec3> scales;
};
class AnimationSequence {
public:
std::vector<AnimationFrame> frames;
float ticksPerSecond;
Vec3 interpolate(Vec3& p1, Vec3& p2, float t) {
return lerp<Vec3>(p1, p2, t);
}
Quaternion interpolate(Quaternion& q1, Quaternion& q2, float t) {
return 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 = mn(frame, frames.size() - 1);
}
int nextFrame(int frame) {
return mn(frame + 1, frames.size() - 1);
}
Matrix interpolateBoneToGlobal(Matrix* matrices, int baseFrame, float interpolationFact, Skeleton* skeleton, int boneIndex) {
int nextFrameIndex = nextFrame(baseFrame);
Matrix scale = Matrix::scaling(interpolate(frames[baseFrame].scales[boneIndex], frames[nextFrameIndex].scales[boneIndex], interpolationFact));
Matrix rotation = interpolate(frames[baseFrame].rotations[boneIndex], frames[nextFrameIndex].rotations[boneIndex], interpolationFact).toMatrix();
Matrix translation = Matrix::translation(interpolate(frames[baseFrame].positions[boneIndex], frames[nextFrameIndex].positions[boneIndex], interpolationFact));
//Matrix scale = Matrix::scaling(frames[baseFrame].scales[boneIndex]);
//Matrix rotation = frames[baseFrame].rotations[boneIndex].toMatrix();
//Matrix translation = Matrix::translation(frames[baseFrame].positions[boneIndex]);
Matrix local = scale * rotation * translation;
if (skeleton->bones[boneIndex].parentIndex > -1) {
Matrix global = local * matrices[skeleton->bones[boneIndex].parentIndex];
return global;
}
return local;
}
};
class Animation {
public:
std::map<std::string, AnimationSequence> animations;
Skeleton skeleton;
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 calcFinalTransforms(Matrix* matrices) {
for (int i = 0; i < skeleton.bones.size(); i++) {
matrices[i] = skeleton.bones[i].offset * matrices[i] * skeleton.globalInverse;
}
}
};
class AnimationInstance {
public:
Animation* animation;
std::string currentAnimation;
float t;
Matrix matrices[256];
void init(Animation* _animation) {
animation = _animation;
t = 0;
}
void resetAnimationTime() {
t = 0;
}
bool animationFinished() {
if (t > animation->animations[currentAnimation].duration()) {
return true;
}
return false;
}
void update(std::string name, float dt) {
if (name == currentAnimation) {
t += dt;
} else {
currentAnimation = name;
t = 0;
}
if (animationFinished() == true) {
if (currentAnimation == "roar") {
currentAnimation = "Run";
resetAnimationTime();
} else {
resetAnimationTime();
}
}
int frame = 0;
float interpolationFact = 0;
animation->calcFrame(name, t, frame, interpolationFact);
for (int i = 0; i < animation->skeleton.bones.size(); i++) {
matrices[i] = animation->interpolateBoneToGlobal(name, matrices, frame, interpolationFact, i);
}
animation->calcFinalTransforms(matrices);
}
};