-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.h
More file actions
371 lines (293 loc) · 7.72 KB
/
Animation.h
File metadata and controls
371 lines (293 loc) · 7.72 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#pragma once
#define GLM_ENABLE_EXPERIMENTAL
#include<array>
#include<vector>
#include<map>
#include<memory>
#include<glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include<iostream>
class Model;
struct AnimationKeyData
{
std::map<float, glm::vec3> animationScaleKey;
//std::map<float, aiQuaternion> animationRotKey;
std::map<float, glm::vec3> animationPositionKey;
};
class AnimNode
{
private:
bool animNode;
uint32_t childrenCount;
std::vector<AnimNode*> children;
std::string name;
AnimationKeyData animKeyData;
glm::mat4 matrix;
public:
AnimNode(std::string nodeName,AnimationKeyData data, unsigned int cCount)
{
animNode = true;
name = nodeName;
animKeyData = data;
children.resize(cCount);
childrenCount = cCount;
}
AnimNode(std::string nodeName, glm::mat4 mat,unsigned int cCount)
{
animNode = false;
name = nodeName;
matrix = mat;
childrenCount = cCount;
}
std::string getNodeName()
{
return name;
}
/*
glm::mat4 aiMatrix3x3ToGlm(const aiMatrix3x3& from)
{
return glm::transpose(glm::make_mat3(&from.a1));
}
void resizeChildren(int childNum)
{
children.resize(childNum);
}
void setChild(int i, AnimNode* child)
{
if (i < children.size())
{
children[i] = child;
}
}
int getChildrenCount()
{
return children.size();
}
std::string getName()
{
return name;
}
AnimNode* getChildren(int i)
{
if (i < children.size())
{
return children[i];
}
return nullptr;
}
void findScale(float animTime,float& t1,float& t2)
{
auto end = animKeyData.animationScaleKey.end();
end--;
for (auto itr = animKeyData.animationScaleKey.begin(); itr != end; itr++)
{
auto next = itr;
next++;
if (animTime < next->first)
{
t1 = itr->first;
t2 = next->first;
return;
}
}
t1 = -1.0f;
t2 = -1.0f;
}
void findRotate(float animTime, float& t1, float& t2)
{
auto end = animKeyData.animationRotKey.end();
end--;
for (auto itr = animKeyData.animationRotKey.begin(); itr != end; itr++)
{
auto next = itr;
next++;
if (animTime < next->first)
{
t1 = itr->first;
t2 = next->first;
return;
}
}
t1 = -1.0f;
t2 = -1.0f;
}
void findPosition(float animTime, float& t1, float& t2)
{
auto end = animKeyData.animationPositionKey.end();
end--;
for (auto itr = animKeyData.animationPositionKey.begin(); itr != end; itr++)
{
auto next = itr;
next++;
if (animTime < next->first)
{
t1 = itr->first;
t2 = next->first;
return;
}
}
t1 = -1.0f;
t2 = -1.0f;
}
glm::vec3 calcInterpolatedScaling(float animTime)
{
if (animKeyData.animationScaleKey.size() == 1)
{
return animKeyData.animationScaleKey.begin()->second;
}
float t1, t2;
findScale(animTime,t1,t2);//現在の時刻に最も近いノードを探す
if (t1 >= 0.0f)
{
float DeltaTime = t2 - t1;//時間の差分を取得
float Factor = (animTime - t1) / DeltaTime;//時間の差分をとる、現在の時間と直前のノードの時間を取得して、直前のノードと直後のノードの間のどれくらいの場所にいるのかを調べる
const glm::vec3 Start = animKeyData.animationScaleKey[t1];//スケーリングの値をとる
const glm::vec3 End = animKeyData.animationScaleKey[t2];//スケーリングの値をとる
glm::vec3 Delta = End - Start;//差分とる
return Start + Factor * Delta;//比率から計算する
}
else//最初のキーフレームよりも前の時間
{
t2 = animKeyData.animationScaleKey.begin()->first;
if (t2 <= 0.0f)
{
t2 = 1.0f;
}
float Factor = 1 - (t2 - animTime) / t2;
if (!(Factor >= 0.0f && Factor <= 1.0f)) {
Factor = 1.f;
}
const glm::vec3 Start = glm::vec3(1.0, 1.0, 1.0);
const glm::vec3 End = animKeyData.animationScaleKey.begin()->second;
glm::vec3 Delta = End - Start;
return Start + Factor * Delta;
}
}
aiQuaternion calcInterpolatedQuat(float animTime)
{
// 補間には最低でも2つの値が必要
if (animKeyData.animationRotKey.size() == 1) {
return animKeyData.animationRotKey.begin()->second;
}
float t1, t2;
findRotate(animTime,t1,t2);
if (t1 >= 0.0f)
{
float DeltaTime = t2 - t1;
float Factor = (animTime - t1) / DeltaTime;
const aiQuaternion StartRotationQ = animKeyData.animationRotKey[t1];
const aiQuaternion EndRotationQ = animKeyData.animationRotKey[t2];
aiQuaternion Out;
aiQuaternion::Interpolate(Out, StartRotationQ, EndRotationQ, Factor);
Out = Out.Normalize();
return Out;
}
else//最初のキーフレームよりも前の時間
{
t2 = animKeyData.animationRotKey.begin()->first;
if (t2 <= 0.0f)
{
t2 = 1.0f;
}
float Factor = 1 - (t2 - animTime) / t2;
if (!(Factor >= 0.0f && Factor <= 1.0f)) {
Factor = 1.f;
}
const aiQuaternion& StartRotationQ = aiQuaternion(1.0,0.0,0.0,0.0);
const aiQuaternion& EndRotationQ = animKeyData.animationRotKey.begin()->second;
aiQuaternion Out;
aiQuaternion::Interpolate(Out, StartRotationQ, EndRotationQ, Factor);
Out = Out.Normalize();
return Out;
}
}
glm::vec3 calcInterpolatedPos(float animTime)
{
if (animKeyData.animationPositionKey.size() == 1)
{
return animKeyData.animationPositionKey.begin()->second;
}
float t1, t2;
findPosition(animTime,t1,t2);//現在の時刻に最も近いノードを探す
if (t1 >= 0.0f)
{
float DeltaTime = t2 - t1;//時間の差分を取得
float Factor = (animTime - t1) / DeltaTime;//時間の差分をとる、現在の時間と直前のノードの時間を取得して、直前のノードと直後のノードの間のどれくらいの場所にいるのかを調べる
const glm::vec3 Start = animKeyData.animationPositionKey[t1];//スケーリングの値をとる
const glm::vec3 End = animKeyData.animationPositionKey[t2];//スケーリングの値をとる
glm::vec3 Delta = End - Start;//差分とる
return Start + Factor * Delta;//比率から計算する
}
else//最初のキーフレームよりも前の時間
{
t2 = animKeyData.animationPositionKey.begin()->first;
if (t2 <= 0.0f)
{
t2 = 1.0f;
}
float Factor = 1 - (t2 - animTime) / t2;
if (!(Factor >= 0.0f && Factor <= 1.0f)) {
Factor = 1.f;
}
const glm::vec3 Start = glm::vec3(0.0, 0.0, 0.0);
const glm::vec3 End = animKeyData.animationPositionKey.begin()->second;
glm::vec3 Delta = End - Start;
return Start + Factor * Delta;
}
}
glm::mat4 getAnimMatrix(float animTime,glm::mat4& transform)
{
//補間などを計算して、平行移動と回転、スケーリングの行列を合成した行列を返す
if (animNode)
{
glm::vec3 scale = calcInterpolatedScaling(animTime);
glm::mat4 scaleMat = glm::scale(glm::mat4(1.0f),scale);
aiQuaternion quat = calcInterpolatedQuat(animTime);
glm::mat4 quatMat = aiMatrix3x3ToGlm(quat.GetMatrix());
glm::vec3 position = calcInterpolatedPos(animTime);
glm::mat4 posMat = glm::translate(glm::mat4(1.0f), position);
return transform * posMat * quatMat * scaleMat;
}
else
{
return transform * matrix;
}
}
*/
};
struct BoneInfo
{
std::vector<glm::mat4> offsetMatrix;
};
class GltfModel;
class Pose
{
private:
std::array<glm::mat4, 250> boneMatrix;
public:
void setPoseMatrix(std::array<glm::mat4, 250>& matrix);
void setFinalTransform(std::array<glm::mat4, 250>& boneFinalTransforms);
};
class Animation
{
private:
float startTime;
float endTime;
float duration;
AnimNode* rootNode;
glm::mat4 inverseGlobalTransform;
public:
Animation();
Animation(float startTime,float endTime,float duration);
~Animation();
//void DeleteAnimTree(AnimNode* node);
void setRootNode(AnimNode* rootNode)
{
this->rootNode = rootNode;
}
void setGlobalInverseTransform(glm::mat4 mat) { inverseGlobalTransform = mat; }
//void setFinalTransform(float animationTime, std::array<glm::mat4, 250>& boneFinalTransforms, GltfModel* model);
//void setFinalTransform(float animationTime, std::array<glm::mat4, 250>& boneFinalTransforms,AnimNode* node,glm::mat4 parentMatrix, GltfModel* model);
};