-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skin.cpp
277 lines (250 loc) · 7.48 KB
/
Skin.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
272
273
274
275
276
#include "Skin.h"
#include <QFile>
#include <QFileInfo>
#include "Skeleton.h"
#include "MorphTarget.h"
QHash<QString, Skin::loadFunction> Skin::_loadingFunctions = {
{"positions", &Skin::_loadPositions},
{"normals", &Skin::_loadNormals},
{"skinweights", &Skin::_loadSkinWeights},
{"triangles", &Skin::_loadTriangles},
{"bindings", &Skin::_loadBindings},
{"texcoords", &Skin::_loadTexCoords},
{"material", &Skin::_loadMaterial}
};
Skin::Skin(Node *parent) : Mesh(parent), _skeleton(NULL)
{
setName("Skin");
setType("Skin");
}
Skin::~Skin()
{
}
bool Skin::loadFromFile(const QString &filename )
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QFileInfo fileInfo(filename);
_currentFileDir = fileInfo.dir();
BasicTokenizer tokenizer(&file);
if (!_load(tokenizer)) {
deleteChildren();
file.close();
qDebug() << "Invalid file";
return false;
}
QFileInfo info(filename);
setName(info.baseName());
file.close();
return true;
}
void Skin::update(float elapsed)
{
if (_skeleton)
_skeleton->update(0);
QVector<float> positions = _skinVertices;
QVector<float> normals = _skinNormals;
QVector<QMatrix4x4> cacheValues;
if (_skeleton) {
cacheValues.resize(qMax(_skeleton->jointNumber(), _bindings.size()));
for (int i = 0; i < cacheValues.size(); ++i) {
if (i < _skeleton->jointNumber())
cacheValues[i] = _skeleton->jointWorld(i);
if (i < _bindings.size())
cacheValues[i] *= _bindings[i];
}
}
for (int i = 0; i + 2 < positions.size() && i + 2 < normals.size(); i += 3) {
QVector4D v(positions[i], positions[i + 1], positions[i + 2], 1),
n(normals[i], normals[i + 1], normals[i + 2], 0),
newV(0, 0, 0, 0), newN(0, 0, 0, 0);
int idx = i / 3;
if (_skeleton) {
QVector<QPair<int, float>>& weights = _skinWeights[idx];
for (int j = 0; j < weights.size(); ++j) {
int jointId = weights[j].first;
float weight = weights[j].second;
newV += weight * cacheValues[jointId] * v;
newN += weight * cacheValues[jointId] * n;
}
} else {
newV = v;
newN = n;
}
foreach (Node* child, children()) {
MorphTarget* morphTarget = dynamic_cast<MorphTarget*>(child);
if (morphTarget) {
QPair<QVector3D, QVector3D> delta = morphTarget->delta(idx);
float DOFValue = morphTarget->DOFValue(DOF::MorphTarget);
newV += DOFValue * QVector4D(delta.first, 0);
newN += DOFValue * QVector4D(delta.second, 0);
}
}
newN.normalize();
positions[i] = newV.x();
positions[i + 1] = newV.y();
positions[i + 2] = newV.z();
normals[i] = newN.x();
normals[i + 1] = newN.y();
normals[i + 2] = newN.z();
}
setVertices(positions);
setNormals(normals);
Mesh::update(elapsed);
}
void Skin::init()
{
Mesh::init();
}
void Skin::setSkeleton(Skeleton *newSkel)
{
_skeleton = newSkel;
}
Skeleton *Skin::skeleton() const
{
return _skeleton;
}
QVector3D Skin::baseVertex(int idx) const
{
idx *= 3;
return (idx + 2 < _skinVertices.size()) ?
QVector3D(_skinVertices[idx],
_skinVertices[idx + 1],
_skinVertices[idx + 2]) :
QVector3D();
}
QVector3D Skin::baseNormal(int idx) const
{
idx *= 3;
return (idx + 2 < _skinNormals.size()) ?
QVector3D(_skinNormals[idx],
_skinNormals[idx + 1],
_skinNormals[idx + 2]) :
QVector3D();
}
bool Skin::_load(BasicTokenizer &tokenizer)
{
QString token = tokenizer.nextToken();
while (!token.isEmpty()) {
if (_loadingFunctions.contains(token) && (this->*_loadingFunctions[token])(tokenizer)) {
token = tokenizer.nextToken();
continue;
}
return false;
}
return true;
}
bool Skin::_loadFloat3Vector(QVector<float> &toFill, BasicTokenizer &tokenizer)
{
int size = tokenizer.nextInt() * 3;
if (tokenizer.nextToken() != "{")
return false;
toFill.resize(size);
for (int i = 0; i < size; ++i) {
toFill[i] = tokenizer.nextFloat();
}
return tokenizer.nextToken() == "}";
}
bool Skin::_loadInt3Vector(QVector<int> &toFill, BasicTokenizer &tokenizer)
{
int size = tokenizer.nextInt() * 3;
if (tokenizer.nextToken() != "{")
return false;
toFill.resize(size);
for (int i = 0; i < size; ++i) {
toFill[i] = tokenizer.nextInt();
}
return tokenizer.nextToken() == "}";
}
bool Skin::_loadPositions(BasicTokenizer &tokenizer)
{
if (!_loadFloat3Vector(_skinVertices, tokenizer))
return false;
setVertices(_skinVertices);
return true;
}
bool Skin::_loadNormals(BasicTokenizer &tokenizer)
{
if (!_loadFloat3Vector(_skinNormals, tokenizer))
return false;
setNormals(_skinNormals);
return true;
}
bool Skin::_loadSkinWeights(BasicTokenizer &tokenizer)
{
int size = tokenizer.nextInt();
if (tokenizer.nextToken() != "{")
return false;
_skinWeights.resize(size);
for (int i = 0; i < size; ++i) {
int numBinds = tokenizer.nextInt();
_skinWeights[i].resize(numBinds);
for (int j = 0; j < numBinds; ++j) {
int id = tokenizer.nextInt();
float value = tokenizer.nextFloat();
_skinWeights[i][j] = QPair<int, float>(id, value);
}
}
return tokenizer.nextToken() == "}";
}
bool Skin::_loadTriangles(BasicTokenizer &tokenizer)
{
QVector<int> faces;
if (!_loadInt3Vector(faces, tokenizer))
return false;
setFaces(faces);
return true;
}
bool Skin::_loadBindings(BasicTokenizer &tokenizer)
{
int size = tokenizer.nextInt();
if (tokenizer.nextToken() != "{")
return false;
_bindings.resize(size);
for (int i = 0; i < size; ++i) {
if (tokenizer.nextToken() != "matrix")
return false;
if (tokenizer.nextToken() != "{")
return false;
for (int j = 0; j < 4; ++j) {
QVector3D current = tokenizer.nextVector();
QVector4D column(current, j == 3 ? 1 : 0);
_bindings[i].setColumn(j, column);
}
_bindings[i] = _bindings[i].inverted();
if (tokenizer.nextToken() != "}")
return false;
}
return tokenizer.nextToken() == "}";
}
bool Skin::_loadTexCoords(BasicTokenizer &tokenizer)
{
int size = tokenizer.nextInt() * 2;
if (tokenizer.nextToken() != "{")
return false;
QVector<float> coords;
coords.resize(size);
for (int i = 0; i < size; ++i) {
coords[i] = tokenizer.nextFloat();
}
setTexCoords(coords);
return tokenizer.nextToken() == "}";
}
bool Skin::_loadMaterial(BasicTokenizer &tokenizer)
{
QString name = tokenizer.nextToken();
if (name.isEmpty())
return false;
if (tokenizer.nextToken() != "{")
return false;
if (tokenizer.nextToken() != "texture")
return false;
QString textureName = tokenizer.nextToken();
if (textureName.isEmpty())
return false;
textureName = _currentFileDir.absoluteFilePath(textureName);
if (tokenizer.nextToken() != "}")
return false;
return material()->loadTexture(textureName);
}