-
Notifications
You must be signed in to change notification settings - Fork 1
/
Animator.cpp
144 lines (111 loc) · 4.32 KB
/
Animator.cpp
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
#include "Application.h"
#include "Animator.h"
Animator::Animator(GameObject * gameobject, Component_Type type) : Component(type, gameobject){}
Animator::~Animator()
{
}
void Animator::Draw()
{
glDisable(GL_LIGHTING);
glColor3f(255.0f, 255.0f, 0.0f);
if (myAnimatedBody != nullptr && currAnimation != nullptr) {
Joint* root = myAnimatedBody->GetRootJoint();
DrawJoints(currAnimation->keyframes_list[AnimationTime], root);
}
glEnable(GL_LIGHTING);
}
void Animator::UpdateAnim()
{
if (currAnimation != nullptr) {
IncreaseAnimationTime();
std::map<string, mat4x4> currPose = CalculateCurrAnimationPose();
ApplyPoseToJoints(currPose, myAnimatedBody->GetRootJoint(), mat4x4());
}
if (App->input->GetKey(SDL_SCANCODE_1) == KEY_DOWN){
doAnimation(0);
}if (App->input->GetKey(SDL_SCANCODE_2) == KEY_DOWN) {
doAnimation(1);
}
}
void Animator::DrawJoints(Keyframe* frame, Joint* root) {
JointTransform* bone_transform = frame->pose[root->name];
for (int i = 0; i < root->children.size(); ++i) {
Joint* child = root->children[i];
JointTransform* bone_transform2 = frame->pose[child->name];
glBegin(GL_LINES);
glVertex3f(bone_transform->Position.x, bone_transform->Position.y, bone_transform->Position.z);
glVertex3f(bone_transform2->Position.x, bone_transform2->Position.y, bone_transform2->Position.z);
glEnd();
LOGC("%s - %s \n", root->name.c_str(), child->name.c_str());
DrawJoints(frame, child);
}
/* glVertex3f(bone_transform->Position.x, bone_transform->Position.y, bone_transform->Position.z);
if (root->children.size() - 1 == i) {
glVertex3f(bone_transform->Position.x, bone_transform->Position.y, bone_transform->Position.z);
}
else {
Joint* child2 = root->children[i + 1];
JointTransform* bone_transform2 = frame->pose[child->name];
glVertex3f(bone_transform2->Position.x, bone_transform2->Position.y, bone_transform2->Position.z);
}*/
}
void Animator::doAnimation(uint index) {
if (index < Animations.size()) {
AnimationTime = 0;
currAnimation = Animations.at(index);
id_anim = index;
}
}
void Animator::IncreaseAnimationTime() {
AnimationTime += 1; //need to create a separated stable 30fps system for animation, not good idea to make them depend of the engine framerate;
if (AnimationTime >= currAnimation->GetLength()) {
AnimationTime = 0;
}
}
map<string, mat4x4> Animator::CalculateCurrAnimationPose()
{
Keyframe frames[2];
GetPreviousAndNextFrame(frames);
float progression = CalculateProgression(frames[0], frames[1]);
return InterpolatePoses(&frames[0],&frames[1],progression);
}
void Animator::ApplyPoseToJoints(map<string, mat4x4> currPose, Joint *ParentJoint, mat4x4& Parentmat) {
mat4x4 currLocalTransform = currPose[ParentJoint->name];
mat4x4 currTransform = Parentmat * currLocalTransform;
for (int i = 0; i < ParentJoint->children.size(); ++i) {
ApplyPoseToJoints(currPose, ParentJoint->children[i], currTransform);
}
mat4x4 container = currTransform * ParentJoint->GetInverseBindTransform();
ParentJoint->SetAnimationTransform(container);
}
void Animator::GetPreviousAndNextFrame(Keyframe* frames) {
std::map<uint, Keyframe*> allFrames = *currAnimation->GetKeyframes();
Keyframe* prevFrame = allFrames[0];
Keyframe* nextFrame = allFrames[0];
for (int i = 0; i < currAnimation->keyframes_list.size(); i++) {
nextFrame = allFrames[i];
if (nextFrame->GetTimePosition() == AnimationTime) {
break;
}
prevFrame = allFrames[i];
}
frames[0] = *prevFrame;
frames[1] = *nextFrame;
//DrawJoints(Keyframe *prevFrame);
}
float Animator::CalculateProgression(Keyframe prev, Keyframe next) {
float Totaltime = next.GetTimePosition() - prev.GetTimePosition();
float currentTime = AnimationTime - prev.GetTimePosition();
return currentTime / Totaltime;
}
map<string, mat4x4> Animator::InterpolatePoses(Keyframe* prevFrame, Keyframe* nextFrame, float progression){
map<string, mat4x4> currPose;
for (std::map<std::string, JointTransform*>::iterator _it = prevFrame->pose.begin(); _it != prevFrame->pose.end(); ++_it) {
JointTransform* prevTransform = prevFrame->pose[_it->first];
JointTransform* nextTransform = nextFrame->pose[_it->first];
prevFrame->pose[_it->first] = &prevTransform->Interpolate(prevTransform, nextTransform, progression);
mat4x4 mat = prevTransform->GetLocalTransform();
currPose[_it->first] = mat;
}
return currPose;
}