-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSkeletonLoader.cpp
More file actions
157 lines (132 loc) · 4.31 KB
/
Copy pathSkeletonLoader.cpp
File metadata and controls
157 lines (132 loc) · 4.31 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
#include "SkeletonLoader.h"
#include <mathematics/matrix4x4.hpp>
namespace
{
// Assimp 행렬을 읽은 뒤 전치하던 기존 숫자 배치를 필드 단위로 명시한다.
math::matrix4x4 SkeletonLoaderMatrixFromAssimp(const aiMatrix4x4& source)
{
return math::matrix4x4{
source.a1, source.b1, source.c1, source.d1,
source.a2, source.b2, source.c2, source.d2,
source.a3, source.b3, source.c3, source.d3,
source.a4, source.b4, source.c4, source.d4 };
}
}
SkeletonLoader::SkeletonLoader(const aiScene* scene) :
m_scene(scene)
{
}
SkeletonLoader::~SkeletonLoader()
{
m_boneMap.clear();
}
Skeleton* SkeletonLoader::GenerateSkeleton(aiNode* root)
{
Skeleton* skeleton = new Skeleton();
aiNode* boneRoot = FindBoneRoot(root);
if (boneRoot == nullptr)
{
LoadAnimations(skeleton);
return skeleton;
}
// Parent is not a bone recorded
Bone* parent = new Bone(std::string(boneRoot->mName.data), m_bones.size(),
SkeletonLoaderMatrixFromAssimp(root->mTransformation));
m_bones.push_back(parent);
skeleton->m_rootBone = parent;
ProcessBones(boneRoot, parent);
skeleton->m_bones = std::move(m_bones);
for (Bone* bone : skeleton->m_bones)
{
skeleton->m_boneMap.emplace(bone->m_name, bone);
}
skeleton->m_rootTransform =
SkeletonLoaderMatrixFromAssimp(boneRoot->mTransformation);
skeleton->m_globalInverseTransform =
math::inverse(skeleton->m_rootTransform);
LoadAnimations(skeleton);
return skeleton;
}
int SkeletonLoader::AddBone(aiBone* _bone)
{
std::string boneName(_bone->mName.data);
if (m_boneMap.find(boneName) == m_boneMap.end())
{
Bone* bone = new Bone(boneName, m_bones.size(),
SkeletonLoaderMatrixFromAssimp(_bone->mOffsetMatrix));
m_bones.push_back(bone);
m_boneMap.emplace(boneName, bone);
}
return m_boneMap[boneName]->m_index;
}
void SkeletonLoader::ProcessBones(aiNode* node, Bone* parentBone)
{
for (UINT i = 0; i < node->mNumChildren; ++i)
{
aiNode* childNode = node->mChildren[i];
Bone* nextParentBone = parentBone; // Keep the same parent by default
// Check if the child node is a registered bone
auto it = m_boneMap.find(childNode->mName.data);
if (it != m_boneMap.end())
{
// If it is a bone, add it as a child to the current parent
// and set it as the new parent for subsequent recursive calls.
Bone* childBone = it->second;
parentBone->m_children.push_back(childBone);
nextParentBone = childBone;
}
// Continue traversal down the hierarchy, regardless of whether the child
// was a bone or not.
ProcessBones(childNode, nextParentBone);
}
}
void SkeletonLoader::LoadAnimations(Skeleton* skeleton)
{
skeleton->m_animations.reserve(m_scene->mNumAnimations);
for (UINT i = 0; i < m_scene->mNumAnimations; ++i)
{
std::optional<Animation> anim = m_animationLoader.LoadAnimation(m_scene->mAnimations[i]);
if (anim.has_value())
{
skeleton->m_animations.push_back(anim.value());
}
}
}
aiNode* SkeletonLoader::FindBoneRoot(aiNode* root)
{
if (!root)
{
return nullptr;
}
std::queue<aiNode*> queue;
queue.push(root);
while (!queue.empty())
{
// Process one level at a time
int levelSize = queue.size();
std::vector<aiNode*> bonesOnThisLevel;
for (int i = 0; i < levelSize; ++i)
{
aiNode* node = queue.front();
queue.pop();
// Check if the current node is a bone
if (m_boneMap.count(node->mName.data))
{
bonesOnThisLevel.push_back(node);
}
// Enqueue children for the next level
for (UINT j = 0; j < node->mNumChildren; ++j)
{
queue.push(node->mChildren[j]);
}
}
// If we found any bones on this level, this is the highest level
// with bones. The parent of the first one is our root.
if (!bonesOnThisLevel.empty())
{
return bonesOnThisLevel[0]->mParent;
}
}
// No bones were found in the entire hierarchy
return nullptr;
}