/
MorphTargets.cpp
195 lines (150 loc) · 4.14 KB
/
MorphTargets.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
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
#include "MorphTargets.h"
MorphTargets::MorphTargets()
{
currentFrame = 0;
nextFrame = 1;
looping = true;
finalVerts = new float;
}
MorphTargets::~MorphTargets()
{
}
void MorphTargets::LoadAnimations(void)
{
double time = glfwGetTime();
static const char *folder = "Resources/Animations/";
char currentIndex[256] = "";
std::vector<OBJModel> idle;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "IdleAnimation/idle");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath,".obj");
idle.push_back(OBJModel(filePath, false));
}
animations.push_back(idle);
std::vector<OBJModel> move;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "RunAnimation/run");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath, ".obj");
move.push_back(OBJModel(filePath, false));
}
animations.push_back(move);
std::vector<OBJModel> punch;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "PunchAnimation/punch");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath, ".obj");
punch.push_back(OBJModel(filePath, false));
}
animations.push_back(punch);
std::vector<OBJModel> block;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "BlockAnimation/block");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath, ".obj");
block.push_back(OBJModel(filePath, false));
}
animations.push_back(block);
std::vector<OBJModel> stagger;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "StaggerAnimation/stagger");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath, ".obj");
stagger.push_back(OBJModel(filePath, false));
}
animations.push_back(stagger);
std::vector<OBJModel> groundKick;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "GroundKickAnimation/groundkick");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath, ".obj");
groundKick.push_back(OBJModel(filePath, false));
}
animations.push_back(groundKick);
std::vector<OBJModel> airKick;
for (unsigned int i = 1; i <= 10; ++i)
{
char filePath[256] = "";
//Load the file
strcat_s(filePath, folder);
strcat_s(filePath, "DiveKickAnimation/divekick");
_itoa_s(i, currentIndex, 10);
strcat_s(filePath, currentIndex);
strcat_s(filePath, ".obj");
airKick.push_back(OBJModel(filePath, false));
}
animations.push_back(airKick);
}
void MorphTargets::Update(float *dt)
{
if (*dt >= 1.0f)
{
*dt = 0.0f;
currentFrame = nextFrame;
if (nextFrame >= MAX_FRAMES_IN_ANIMATION-1)
{
if (looping)
{
nextFrame = 0;
}
}
else
{
nextFrame += 1;
}
}
//These are needed to store the vector before turning it into an array for faster looping through
std::vector<float> currentFrameVertsVector = animations[currentAnimation][currentFrame].GetVerts();
std::vector<float> nextFrameVertsVector = animations[currentAnimation][nextFrame].GetVerts();
//Load the current frame of the animation
float *currentFrameVerts = ¤tFrameVertsVector[0];
unsigned int numVerts = currentFrameVertsVector.size();
//Load the next frame of the animation
float *nextFrameVerts = &nextFrameVertsVector[0];
//create an empty array to store the new values of the verts
finalVerts = new float[numVerts];
//Update each of the verts by LERPing between the current and next frame
for (unsigned int i = 0; i < numVerts; ++i)
{
finalVerts[i] = LERP(currentFrameVerts[i], nextFrameVerts[i], *dt);
}
}
void MorphTargets::SetAnimation(playerAnimations anim)
{
currentAnimation = anim;
}
float* MorphTargets::GetFinalVerts(void)
{
return finalVerts;
}