/
MD5Surface.cpp
481 lines (381 loc) · 11.4 KB
/
MD5Surface.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "MD5Surface.h"
#include "ivolumetest.h"
#include "GLProgramAttributes.h"
#include "string/convert.h"
#include "MD5Model.h"
#include "math/Ray.h"
namespace md5
{
inline VertexPointer vertexpointer_arbitrarymeshvertex(const ArbitraryMeshVertex* array)
{
return VertexPointer(&array->vertex, sizeof(ArbitraryMeshVertex));
}
// Constructor
MD5Surface::MD5Surface() :
_originalShaderName(""),
_mesh(new MD5Mesh),
_normalList(0),
_lightingList(0)
{}
MD5Surface::MD5Surface(const MD5Surface& other) :
_aabb_local(other._aabb_local),
_originalShaderName(other._originalShaderName),
_mesh(other._mesh),
_normalList(0),
_lightingList(0)
{}
// Destructor
MD5Surface::~MD5Surface()
{
releaseDisplayLists();
}
// Update geometry
void MD5Surface::updateGeometry()
{
_aabb_local = AABB();
for (Vertices::const_iterator i = _vertices.begin(); i != _vertices.end(); ++i)
{
_aabb_local.includePoint(i->vertex);
}
for (Indices::iterator i = _indices.begin();
i != _indices.end();
i += 3)
{
ArbitraryMeshVertex& a = _vertices[*(i + 0)];
ArbitraryMeshVertex& b = _vertices[*(i + 1)];
ArbitraryMeshVertex& c = _vertices[*(i + 2)];
ArbitraryMeshTriangle_sumTangents(a, b, c);
}
for (Vertices::iterator i = _vertices.begin();
i != _vertices.end();
++i)
{
i->tangent.normalise();
i->bitangent.normalise();
}
// Build the display lists
createDisplayLists();
}
// Back-end render
void MD5Surface::render(const RenderInfo& info) const
{
if (info.checkFlag(RENDER_BUMP))
{
glCallList(_lightingList);
}
else
{
glCallList(_normalList);
}
}
// Construct the display lists
void MD5Surface::createDisplayLists()
{
// Release old display lists first
releaseDisplayLists();
// Create the list for lighting mode
_lightingList = glGenLists(1);
assert(_lightingList != 0);
glNewList(_lightingList, GL_COMPILE);
glBegin(GL_TRIANGLES);
for (Indices::const_iterator i = _indices.begin();
i != _indices.end();
++i)
{
// Get the vertex for this index
ArbitraryMeshVertex& v = _vertices[*i];
// Submit the vertex attributes and coordinate
if (GLEW_ARB_vertex_program) {
// Submit the vertex attributes and coordinate
glVertexAttrib2dvARB(ATTR_TEXCOORD, v.texcoord);
glVertexAttrib3dvARB(ATTR_TANGENT, v.tangent);
glVertexAttrib3dvARB(ATTR_BITANGENT, v.bitangent);
glVertexAttrib3dvARB(ATTR_NORMAL, v.normal);
}
glVertex3dv(v.vertex);
}
glEnd();
glEndList();
// Generate the list for flat-shaded (unlit) mode
_normalList = glGenLists(1);
assert(_normalList != 0);
glNewList(_normalList, GL_COMPILE);
glBegin(GL_TRIANGLES);
for (Indices::const_iterator i = _indices.begin();
i != _indices.end();
++i)
{
// Get the vertex for this index
ArbitraryMeshVertex& v = _vertices[*i];
// Submit attributes
glNormal3dv(v.normal);
glTexCoord2dv(v.texcoord);
glVertex3dv(v.vertex);
}
glEnd();
glEndList();
}
void MD5Surface::releaseDisplayLists()
{
// Release GL display lists if applicable
if (_normalList != 0)
{
glDeleteLists(_normalList, 1);
_normalList = 0;
}
if (_lightingList != 0)
{
glDeleteLists(_lightingList, 1);
_lightingList = 0;
}
}
// Selection test
void MD5Surface::testSelect(Selector& selector,
SelectionTest& test,
const Matrix4& localToWorld)
{
test.BeginMesh(localToWorld);
SelectionIntersection best;
test.TestTriangles(
vertexpointer_arbitrarymeshvertex(_vertices.data()),
IndexPointer(_indices.data(), IndexPointer::index_type(_indices.size())),
best
);
if(best.isValid()) {
selector.addIntersection(best);
}
}
bool MD5Surface::getIntersection(const Ray& ray, Vector3& intersection, const Matrix4& localToWorld)
{
Vector3 bestIntersection = ray.origin;
Vector3 triIntersection;
for (Indices::const_iterator i = _indices.begin();
i != _indices.end();
i += 3)
{
// Get the vertices for this triangle
const ArbitraryMeshVertex& p1 = _vertices[*(i)];
const ArbitraryMeshVertex& p2 = _vertices[*(i+1)];
const ArbitraryMeshVertex& p3 = _vertices[*(i+2)];
if (ray.intersectTriangle(localToWorld.transformPoint(p1.vertex),
localToWorld.transformPoint(p2.vertex), localToWorld.transformPoint(p3.vertex), triIntersection))
{
intersection = triIntersection;
// Test if this surface intersection is better than what we currently have
float oldDistSquared = (bestIntersection - ray.origin).getLengthSquared();
float newDistSquared = (triIntersection - ray.origin).getLengthSquared();
if ((oldDistSquared == 0 && newDistSquared > 0) || newDistSquared < oldDistSquared)
{
bestIntersection = triIntersection;
}
}
}
if ((bestIntersection - ray.origin).getLengthSquared() > 0)
{
intersection = bestIntersection;
return true;
}
else
{
return false;
}
}
void MD5Surface::setDefaultMaterial(const std::string& name)
{
_originalShaderName = name;
}
const AABB& MD5Surface::localAABB() const {
return _aabb_local;
}
void MD5Surface::render(RenderableCollector& collector, const Matrix4& localToWorld,
const ShaderPtr& shader, const IRenderEntity& entity) const
{
assert(shader); // shader must be captured at this point
collector.SetState(shader, RenderableCollector::eFullMaterials);
collector.addRenderable(*this, localToWorld, entity);
}
int MD5Surface::getNumVertices() const
{
return static_cast<int>(_vertices.size());
}
int MD5Surface::getNumTriangles() const
{
return static_cast<int>(_indices.size() / 3);
}
const ArbitraryMeshVertex& MD5Surface::getVertex(int vertexIndex) const
{
assert(vertexIndex >= 0 && vertexIndex < static_cast<int>(_vertices.size()));
return _vertices[vertexIndex];
}
model::ModelPolygon MD5Surface::getPolygon(int polygonIndex) const
{
assert(polygonIndex >= 0 && polygonIndex*3 < static_cast<int>(_indices.size()));
model::ModelPolygon poly;
poly.a = _vertices[_indices[polygonIndex*3]];
poly.b = _vertices[_indices[polygonIndex*3 + 1]];
poly.c = _vertices[_indices[polygonIndex*3 + 2]];
return poly;
}
const std::string& MD5Surface::getDefaultMaterial() const
{
return _originalShaderName;
}
void MD5Surface::updateToDefaultPose(const MD5Joints& joints)
{
if (_vertices.size() != _mesh->vertices.size())
{
_vertices.resize(_mesh->vertices.size());
}
for (std::size_t j = 0; j < _mesh->vertices.size(); ++j)
{
MD5Vert& vert = _mesh->vertices[j];
Vector3 skinned(0, 0, 0);
for (std::size_t k = 0; k != vert.weight_count; ++k)
{
MD5Weight& weight = _mesh->weights[vert.weight_index + k];
const MD5Joint& joint = joints[weight.joint];
Vector3 rotatedPoint = joint.rotation.transformPoint(weight.v);
skinned += (rotatedPoint + joint.position) * weight.t;
}
_vertices[j].vertex = skinned;
_vertices[j].texcoord = TexCoord2f(vert.u, vert.v);
_vertices[j].normal = Normal3f(0,0,0);
}
// Ensure the index array is ok
if (_indices.empty())
{
buildIndexArray();
}
buildVertexNormals();
updateGeometry();
}
void MD5Surface::updateToSkeleton(const MD5Skeleton& skeleton)
{
// Ensure we have all vertices allocated
if (_vertices.size() != _mesh->vertices.size())
{
_vertices.resize(_mesh->vertices.size());
}
// Deform vertices to fit the skeleton
for (std::size_t j = 0; j < _mesh->vertices.size(); ++j)
{
MD5Vert& vert = _mesh->vertices[j];
Vector3 skinned(0, 0, 0);
for (std::size_t k = 0; k != vert.weight_count; ++k)
{
MD5Weight& weight = _mesh->weights[vert.weight_index + k];
const IMD5Anim::Key& key = skeleton.getKey(weight.joint);
//const Joint& joint = skeleton.getJoint(weight.joint);
Vector3 rotatedPoint = key.orientation.transformPoint(weight.v);
skinned += (rotatedPoint + key.origin) * weight.t;
}
_vertices[j].vertex = skinned;
_vertices[j].texcoord = TexCoord2f(vert.u, vert.v);
_vertices[j].normal = Normal3f(0,0,0);
}
// Ensure the index array is ok
if (_indices.empty())
{
buildIndexArray();
}
buildVertexNormals();
updateGeometry();
}
void MD5Surface::buildVertexNormals()
{
for (Indices::iterator j = _indices.begin(); j != _indices.end(); j += 3)
{
ArbitraryMeshVertex& a = _vertices[*(j + 0)];
ArbitraryMeshVertex& b = _vertices[*(j + 1)];
ArbitraryMeshVertex& c = _vertices[*(j + 2)];
Vector3 weightedNormal((c.vertex - a.vertex).crossProduct(b.vertex - a.vertex));
a.normal += weightedNormal;
b.normal += weightedNormal;
c.normal += weightedNormal;
}
// Normalise all normal vectors
for (Vertices::iterator j = _vertices.begin(); j != _vertices.end(); ++j)
{
j->normal = Normal3f(j->normal.getNormalised());
}
}
void MD5Surface::buildIndexArray()
{
_indices.clear();
// Build the indices based on the triangle information
for (MD5Tris::const_iterator j = _mesh->triangles.begin(); j != _mesh->triangles.end(); ++j)
{
const MD5Tri& tri = (*j);
_indices.push_back(static_cast<RenderIndex>(tri.a));
_indices.push_back(static_cast<RenderIndex>(tri.b));
_indices.push_back(static_cast<RenderIndex>(tri.c));
}
}
void MD5Surface::parseFromTokens(parser::DefTokeniser& tok)
{
// Start of datablock
tok.assertNextToken("mesh");
tok.assertNextToken("{");
// Get the reference to the mesh definition
MD5Mesh& mesh = *_mesh;
// Get the shader name
tok.assertNextToken("shader");
setDefaultMaterial(tok.nextToken());
// ----- VERTICES ------
// Read the vertex count
tok.assertNextToken("numverts");
std::size_t numVerts = string::convert<std::size_t>(tok.nextToken());
// Initialise the vertex vector
MD5Verts& verts = mesh.vertices;
verts.resize(numVerts);
// Populate each vertex struct with parsed values
for (MD5Verts::iterator vt = verts.begin(); vt != verts.end(); ++vt) {
tok.assertNextToken("vert");
// Index of vert
vt->index = string::convert<std::size_t>(tok.nextToken());
// U and V texcoords
tok.assertNextToken("(");
vt->u = string::convert<float>(tok.nextToken());
vt->v = string::convert<float>(tok.nextToken());
tok.assertNextToken(")");
// Weight index and count
vt->weight_index = string::convert<std::size_t>(tok.nextToken());
vt->weight_count = string::convert<std::size_t>(tok.nextToken());
} // for each vertex
// ------ TRIANGLES ------
// Read the number of triangles
tok.assertNextToken("numtris");
std::size_t numTris = string::convert<std::size_t>(tok.nextToken());
// Initialise the triangle vector
MD5Tris& tris = mesh.triangles;
tris.resize(numTris);
// Read each triangle
for(MD5Tris::iterator tr = tris.begin(); tr != tris.end(); ++tr) {
tok.assertNextToken("tri");
// Triangle index, followed by the indexes of its 3 vertices
tr->index = string::convert<std::size_t>(tok.nextToken());
tr->a = string::convert<std::size_t>(tok.nextToken());
tr->b = string::convert<std::size_t>(tok.nextToken());
tr->c = string::convert<std::size_t>(tok.nextToken());
} // for each triangle
// ----- WEIGHTS ------
// Read the number of weights
tok.assertNextToken("numweights");
std::size_t numWeights = string::convert<std::size_t>(tok.nextToken());
// Initialise weights vector
MD5Weights& weights = mesh.weights;
weights.resize(numWeights);
// Populate with weight data
for(MD5Weights::iterator w = weights.begin(); w != weights.end(); ++w) {
tok.assertNextToken("weight");
// Index and joint
w->index = string::convert<std::size_t>(tok.nextToken());
w->joint = string::convert<std::size_t>(tok.nextToken());
// Strength and relative position
w->t = string::convert<float>(tok.nextToken());
w->v = MD5Model::parseVector3(tok);
} // for each weight
// ----- END OF MESH DECL -----
tok.assertNextToken("}");
}
} // namespace md5