-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkeleton.cpp
104 lines (81 loc) · 2.78 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
#include "Skeleton.h"
#include "Utils.h"
#include <QDomElement>
#include <QDomDocument>
#include <QtMath>
Skeleton::Skeleton()
{
}
int Skeleton::numBones() const
{
return bones.size();
}
Bone Skeleton::bone(QString name) const
{
return bones[name];
}
QMap<QString, Bone> Skeleton::getBoneInfo(QDomElement skeleton) {
QMap<QString, Bone> bones;
QDomNodeList boneList = skeleton.firstChildElement("bones").childNodes();
for(int i = 0; i < boneList.length(); i++) {
QDomElement bone = boneList.item(i).toElement();
int id = bone.attribute("id").toInt();
if(i != id) {
qDebug() << "Error: IDs are not in order :(";
}
QString name = bone.attribute("name");
// Make sure equipped items don't recognize these bones.
if(name != "root") {
name = "Dx_" + name;
}
QDomElement pos = bone.firstChildElement("position");
QVector3D position = {
pos.attribute("x").toFloat(),
pos.attribute("y").toFloat(),
pos.attribute("z").toFloat()
};
QDomElement rot = bone.firstChildElement("rotation");
float angle = rot.attribute("angle").toFloat();
QDomElement axisE = rot.firstChildElement("axis");
QVector3D axis = {
axisE.attribute("x").toFloat(),
axisE.attribute("y").toFloat(),
axisE.attribute("z").toFloat(),
};
Bone b;
b.name = name;
b.position = position;
b.rotation = QQuaternion::fromAxisAndAngle(axis, qRadiansToDegrees(angle));
b.id = bone.attribute("id").toInt();
bones.insert(name, b);
}
return bones;
}
Skeleton Skeleton::fromFile(QString filename, bool withPrefix)
{
QDomDocument doc = Utils::readXMLFile(filename);
return fromDocument(doc, withPrefix);
}
Skeleton Skeleton::fromDocument(QDomDocument doc, bool withPrefix)
{
QDomElement sk = doc.firstChildElement("skeleton");
QMap<QString, Bone> skeleton = getBoneInfo(sk);
QDomElement boneHierarchy = sk.firstChildElement("bonehierarchy");
QDomNodeList boneparents = boneHierarchy.childNodes();
for(int i = 0; i < boneparents.length(); i++) {
QDomElement boneparent = boneparents.item(i).toElement();
QString name = boneparent.attribute("bone");
if(withPrefix && name != "root") {
name = "Dx_" + name;
}
QString parent = boneparent.attribute("parent");
if(withPrefix && parent != "root") {
parent = "Dx_" + parent;
}
skeleton[name].parent = parent;
skeleton[parent].children.append(name);
}
Skeleton ret;
ret.bones = skeleton;
return ret;
}