-
Notifications
You must be signed in to change notification settings - Fork 4
/
Skeleton.cpp
145 lines (116 loc) · 3.56 KB
/
Skeleton.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
// Ahmed S. Tolba 2015-2018
---------------------------------------------------------
#include "Skeleton.h"
#include <fstream>
#include <sstream>
#include <rapidjson/document.h>
#include <SDL/SDL_log.h>
#include "MatrixPalette.h"
bool Skeleton::Load(const std::string& fileName)
{
std::ifstream file(fileName);
if (!file.is_open())
{
SDL_Log("File not found: Skeleton %s", fileName.c_str());
return false;
}
std::stringstream fileStream;
fileStream << file.rdbuf();
std::string contents = fileStream.str();
rapidjson::StringStream jsonStr(contents.c_str());
rapidjson::Document doc;
doc.ParseStream(jsonStr);
if (!doc.IsObject())
{
SDL_Log("Skeleton %s is not valid json", fileName.c_str());
return false;
}
int ver = doc["version"].GetInt();
// Check the metadata
if (ver != 1)
{
SDL_Log("Skeleton %s unknown format", fileName.c_str());
return false;
}
const rapidjson::Value& bonecount = doc["bonecount"];
if (!bonecount.IsUint())
{
SDL_Log("Skeleton %s doesn't have a bone count.", fileName.c_str());
return false;
}
size_t count = bonecount.GetUint();
if (count > MAX_SKELETON_BONES)
{
SDL_Log("Skeleton %s exceeds maximum bone count.", fileName.c_str());
return false;
}
mBones.reserve(count);
const rapidjson::Value& bones = doc["bones"];
if (!bones.IsArray())
{
SDL_Log("Skeleton %s doesn't have a bone array?", fileName.c_str());
return false;
}
if (bones.Size() != count)
{
SDL_Log("Skeleton %s has a mismatch between the bone count and number of bones", fileName.c_str());
return false;
}
Bone temp;
for (rapidjson::SizeType i = 0; i < count; i++)
{
if (!bones[i].IsObject())
{
SDL_Log("Skeleton %s: Bone %d is invalid.", fileName.c_str(), i);
return false;
}
const rapidjson::Value& name = bones[i]["name"];
temp.mName = name.GetString();
const rapidjson::Value& parent = bones[i]["parent"];
temp.mParent = parent.GetInt();
const rapidjson::Value& bindpose = bones[i]["bindpose"];
if (!bindpose.IsObject())
{
SDL_Log("Skeleton %s: Bone %d is invalid.", fileName.c_str(), i);
return false;
}
const rapidjson::Value& rot = bindpose["rot"];
const rapidjson::Value& trans = bindpose["trans"];
if (!rot.IsArray() || !trans.IsArray())
{
SDL_Log("Skeleton %s: Bone %d is invalid.", fileName.c_str(), i);
return false;
}
temp.mLocalBindPose.mRotation.x = rot[0].GetDouble();
temp.mLocalBindPose.mRotation.y = rot[1].GetDouble();
temp.mLocalBindPose.mRotation.z = rot[2].GetDouble();
temp.mLocalBindPose.mRotation.w = rot[3].GetDouble();
temp.mLocalBindPose.mTranslation.x = trans[0].GetDouble();
temp.mLocalBindPose.mTranslation.y = trans[1].GetDouble();
temp.mLocalBindPose.mTranslation.z = trans[2].GetDouble();
mBones.emplace_back(temp);
}
// Now that we have the bones
ComputeGlobalInvBindPose();
return true;
}
void Skeleton::ComputeGlobalInvBindPose()
{
// Resize to number of bones, which automatically fills identity
mGlobalInvBindPoses.resize(GetNumBones());
// Step 1: Compute global bind pose for each bone
// The global bind pose for root is just the local bind pose
mGlobalInvBindPoses[0] = mBones[0].mLocalBindPose.ToMatrix();
// Each remaining bone's global bind pose is its local pose
// multiplied by the parent's global bind pose
for (size_t i = 1; i < mGlobalInvBindPoses.size(); i++)
{
Matrix4 localMat = mBones[i].mLocalBindPose.ToMatrix();
mGlobalInvBindPoses[i] = localMat * mGlobalInvBindPoses[mBones[i].mParent];
}
// Step 2: Invert
for (size_t i = 0; i < mGlobalInvBindPoses.size(); i++)
{
mGlobalInvBindPoses[i].Invert();
}
}