-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.cpp
271 lines (229 loc) · 7.1 KB
/
animation.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
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
#include "animation.h"
#include "time.h"
Animation::Animation(const std::string& animationPath, Model* model)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(animationPath, aiProcess_Triangulate);
assert(scene && scene->mRootNode);
auto animation = scene->mAnimations[0];
m_Duration = animation->mDuration;
m_TicksPerSecond = animation->mTicksPerSecond;
ReadHeirarchyData(m_RootNode, scene->mRootNode);
ReadBones(animation, *model);
//Sets the end effector "Bip01_L_Finger12", and 7 nodes/bones above that as joints
SetManipulator(m_RootNode, "Bip01_L_Finger12",7);
//Sets the default priority (lower for each parent)
SetDefaultPriority();
for (auto iter = m_BoneInfoMap.begin(); iter != m_BoneInfoMap.end(); iter++)
{
std::cout << iter->first << " " << iter->second.isIK << std::endl;
}
}
Bone* Animation::FindBone(const std::string& name)
{
auto iter = std::find_if(m_Bones.begin(), m_Bones.end(),
[&](const Bone& Bone)
{
return Bone.GetBoneName() == name;
}
);
if (iter == m_Bones.end()) return nullptr;
else return &(*iter);
}
void Animation::GetSkeletonBones()
{
int i = 0;
for (auto iter = m_BoneInfoMap.begin(); iter != m_BoneInfoMap.end(); iter++)
{
iter->second.indexInList = i;
m_SkeletonBones.push_back(iter->second.boneVertex);
m_SkeletonBonesIndices.push_back(i);
i++;
}
}
void Animation::GetSkeletonBoneHiearchy(const AssimpNodeData* node, int parentBoneIndex)
{
std::string nodeName = node->name;
auto boneInfoMap = GetBoneIDMap();
int index = parentBoneIndex;
if (boneInfoMap.find(nodeName) != boneInfoMap.end())
{
index = boneInfoMap[nodeName].indexInList;
}
if (index != 0 && parentBoneIndex != 0)
{
m_SkeletonBoneLineIndices.push_back(index);
m_SkeletonBoneLineIndices.push_back(parentBoneIndex);
}
for (int i = 0; i < node->childrenCount; i++)
GetSkeletonBoneHiearchy(&node->children[i], index);
}
void Animation::ReadBones(const aiAnimation* animation, Model& model)
{
int size = animation->mNumChannels;
auto& boneInfoMap = model.GetBoneInfoMap();//getting m_BoneInfoMap from Model class
int& boneCount = model.GetBoneCount(); //getting the m_BoneCounter from Model class
//reading channels(bones engaged in an animation and their keyframes)
for (int i = 0; i < size; i++)
{
auto channel = animation->mChannels[i];
std::string boneName = channel->mNodeName.data;
if (boneInfoMap.find(boneName) == boneInfoMap.end())
{
boneInfoMap[boneName].id = boneCount;
boneCount++;
}
m_Bones.push_back(Bone(channel->mNodeName.data,
boneInfoMap[channel->mNodeName.data].id, channel));
}
m_BoneInfoMap = boneInfoMap;
}
void Animation::ReadHeirarchyData(AssimpNodeData& dest, const aiNode* src)
{
assert(src);
dest.name = src->mName.data;
//dest.transformation = ConvertMatrixToGLMFormat(src->mTransformation);
dest.vqsTrans.Decompose(src->mTransformation);
dest.childrenCount = src->mNumChildren;
for (int i = 0; i < src->mNumChildren; i++)
{
AssimpNodeData newData;
ReadHeirarchyData(newData, src->mChildren[i]);
dest.children.push_back(newData);
}
}
//void Animation::SetIKRoot(AssimpNodeData& dest,std::string name)
//{
// for (int i = 0; i < dest.childrenCount; i++)
// {
// if (dest.name == name)
// {
// dest.isIK = true;
// Bone* findingBone = FindBone(dest.name);
// if (findingBone)
// {
// m_BoneInfoMap[dest.name].isIK = dest.isIK;
// }
// return;
// }
// SetIKRoot(dest.children[i], name);
// }
//}
//void Animation::SetIKLinks(AssimpNodeData& dest)
//{
// for (int i = 0; i < dest.childrenCount; i++)
// {
// if (dest.isIK)
// {
// dest.children[i].isIK = true;
// }
//
// Bone* findingBone = FindBone(dest.children[i].name);
// if (findingBone)
// {
// m_BoneInfoMap[dest.children[i].name].isIK = dest.isIK;
// }
//
// //std::cout << dest.name << " " << dest.isIK << std::endl;
// SetIKLinks(dest.children[i]);
// }
//
//}
void Animation::SetIsIK(AssimpNodeData& dest, std::string name)
{
//Finds the bone that matches the name, returns when found
if (dest.name == name)
{
dest.isIK = true;
Bone* findingBone = FindBone(name);
if (findingBone)
{
m_BoneInfoMap[name].isIK = dest.isIK;
}
return;
}
//Keep on searching through children if not found
for (int i = 0; i < dest.childrenCount; i++)
{
SetIsIK(dest.children[i], name);
}
}
std::string Animation::FindParentNodeName(AssimpNodeData& dest, std::string name)
{
for (int i = 0; i < dest.childrenCount; i++)
{
//if there is a children matches the name, return this node as the parent
if (dest.children[i].name == name)
{
return dest.name;
}
//Keep on searching through children if not found
std::string parentName = FindParentNodeName(dest.children[i], name);
//returns once it is found
if (parentName != name) { return parentName; }
}
//returns the name in search if we can't find one matching in childrens
return name;
}
void Animation::SetManipulator(AssimpNodeData& dest, std::string name, int count)
{
//Pushes the end effector into the list of OK nodes
SetIsIK(m_RootNode, name);
m_manip.nodeNames.push_back(name);
std::string parentName = name;
//for every added node, find its parent and add it to the list, till there is no parent
for (int i = 0; i < count; i++)
{
parentName = FindParentNodeName(m_RootNode, parentName);
SetIsIK(m_RootNode, parentName);
if (m_manip.nodeNames.back() != parentName) { m_manip.nodeNames.push_back(parentName); }
else { break; }
}
//Allocate a list of priority
for (int k = 0; k < m_manip.nodeNames.size() - 1; k++)
{
m_manip.priority.push_back(k);
}
//Set the priority to default
SetDefaultPriority();
}
void Animation::SetDefaultPriority()
{
for (int k = 0; k < m_manip.nodeNames.size() - 1; k++)
{
m_manip.priority[k] = k;
}
}
int Random(int size)
{
return rand() % size;
}
void Animation::SetNewPriority()
{
time_t t;
srand(int(time(&t)));
int size = m_manip.priority.size();
for (int k = 0; k < size; k++)
{
m_manip.priority[k] = size;
}
for (int k = 0; k < size; k++)
{
int random = Random(size);
while (m_manip.priority[random] < k)
{
random=(random+1)%size;
}
m_manip.priority[random] = k;
}
}
std::string Animation::SetPriorityString()
{
std::string PriorString;
int size = m_manip.priority.size();
for (int k = 0; k < size; k++)
{
PriorString += std::to_string(m_manip.priority[k]);
}
return PriorString;
}