-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mesh.cpp
executable file
·553 lines (444 loc) · 17.5 KB
/
Mesh.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// ************************************************************************
//
// File: Mesh.h
// Programmer: T.J. Eason
// Project: Game Engine
// Description: Load and manage animated and static meshes
// Date: 3-16-10
// Revision 1: 3-17-10
// Revision 2: 3-24-10
// Revision 3: 3-25-10
// Revision 4: 4-6-10
//
// *************************************************************************
#include "Engine.h"
// Creates a new frame.
HRESULT AllocateHierarchy::CreateFrame( THIS_ LPCSTR Name, LPD3DXFRAME *ppNewFrame )
{
// Create the new frame and zero its memory.
Frame *frame = new Frame;
ZeroMemory( frame, sizeof( Frame ) );
// Copy the frame's name.
if( Name == NULL )
{
// There is no name, so create a unique name.
static unsigned long nameCount = 0;
char newName[32];
sprintf( newName, "unknown_frame_%d", nameCount );
nameCount++;
frame->Name = new char[strlen( newName ) + 1];
strcpy( frame->Name, newName );
}
else
{
frame->Name = new char[strlen( Name ) + 1];
strcpy( frame->Name, Name );
}
*ppNewFrame = frame;
return S_OK;
}
// Creates a new mesh container.
HRESULT AllocateHierarchy::CreateMeshContainer( THIS_ LPCSTR Name, CONST D3DXMESHDATA *pMeshData, CONST D3DXMATERIAL *pMaterials, CONST D3DXEFFECTINSTANCE *pEffectInstances, DWORD NumMaterials, CONST DWORD *pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER *ppNewMeshContainer )
{
// Create the new mesh container and zero its memory.
MeshContainer *meshContainer = new MeshContainer;
ZeroMemory( meshContainer, sizeof( MeshContainer ) );
// Copy the mesh's name.
if( Name == NULL )
{
// There is no name, so create a unique name.
static unsigned long nameCount = 0;
char newName[32];
sprintf( newName, "unknown_mesh_%d", nameCount );
nameCount++;
meshContainer->Name = new char[strlen( newName ) + 1];
strcpy( meshContainer->Name, newName );
}
else
{
meshContainer->Name = new char[strlen( Name ) + 1];
strcpy( meshContainer->Name, Name );
}
// Check if the mesh has any materials.
if( ( meshContainer->NumMaterials = NumMaterials ) > 0 )
{
// Allocate some memory for the mesh's materials, and their names (i.e. texture names).
meshContainer->materials = new Material*[meshContainer->NumMaterials];
meshContainer->materialNames = new char*[meshContainer->NumMaterials];
// Store all the material (texture) names.
for( unsigned long m = 0; m < NumMaterials; m++ )
{
if( pMaterials[m].pTextureFilename )
{
meshContainer->materialNames[m] = new char[strlen( pMaterials[m].pTextureFilename ) + 1];
memcpy( meshContainer->materialNames[m], pMaterials[m].pTextureFilename, ( strlen( pMaterials[m].pTextureFilename ) + 1 ) * sizeof( char ) );
}
else
meshContainer->materialNames[m] = NULL;
meshContainer->materials[m] = NULL;
}
}
// Store the mesh's adjacency information.
meshContainer->pAdjacency = new DWORD[pMeshData->pMesh->GetNumFaces() * 3];
memcpy( meshContainer->pAdjacency, pAdjacency, sizeof( DWORD ) * pMeshData->pMesh->GetNumFaces() * 3 );
// Store the mesh data.
meshContainer->MeshData.pMesh = meshContainer->originalMesh = pMeshData->pMesh;
meshContainer->MeshData.Type = D3DXMESHTYPE_MESH;
pMeshData->pMesh->AddRef();
pMeshData->pMesh->AddRef();
// Check if this mesh is a skinned mesh.
if( pSkinInfo != NULL )
{
// Store the skin information and the mesh data.
meshContainer->pSkinInfo = pSkinInfo;
pSkinInfo->AddRef();
// Clone the original mesh to create the skinned mesh.
meshContainer->originalMesh->CloneMeshFVF( D3DXMESH_MANAGED, meshContainer->originalMesh->GetFVF(), g_engine->GetDevice(), &meshContainer->MeshData.pMesh );
// Store the attribute table.
meshContainer->MeshData.pMesh->GetAttributeTable( NULL, &meshContainer->totalAttributeGroups );
meshContainer->attributeTable = new D3DXATTRIBUTERANGE[meshContainer->totalAttributeGroups];
meshContainer->MeshData.pMesh->GetAttributeTable( meshContainer->attributeTable, NULL );
}
*ppNewMeshContainer = meshContainer;
return S_OK;
}
// Destroys the given frame.
HRESULT AllocateHierarchy::DestroyFrame( THIS_ LPD3DXFRAME pFrameToFree )
{
SAFE_DELETE_ARRAY( pFrameToFree->Name );
SAFE_DELETE( pFrameToFree );
return S_OK;
}
// Destroys the given mesh conatiner.
HRESULT AllocateHierarchy::DestroyMeshContainer( THIS_ LPD3DXMESHCONTAINER pMeshContainerToFree )
{
MeshContainer *meshContainer = (MeshContainer*)pMeshContainerToFree;
// Go through all of the materials.
for( unsigned long m = 0; m < meshContainer->NumMaterials; m++ )
{
// Remove this material from the material manager.
if( meshContainer->materials )
g_engine->GetMaterialManager()->Remove( &meshContainer->materials[m] );
// Destroy the material's name.
SAFE_DELETE_ARRAY( meshContainer->materialNames[m] );
}
// Destroy the mesh container.
SAFE_DELETE_ARRAY( meshContainer->Name );
SAFE_DELETE_ARRAY( meshContainer->pAdjacency );
SAFE_DELETE_ARRAY( meshContainer->pMaterials );
SAFE_DELETE_ARRAY( meshContainer->materialNames );
SAFE_DELETE_ARRAY( meshContainer->materials );
SAFE_DELETE_ARRAY( meshContainer->boneMatrixPointers );
SAFE_DELETE_ARRAY( meshContainer->attributeTable );
if( meshContainer ->MeshData.pMesh )
{
meshContainer ->MeshData.pMesh ->Release();
meshContainer ->MeshData.pMesh = NULL;
}
if( meshContainer ->pSkinInfo )
{
meshContainer ->pSkinInfo ->Release();
meshContainer ->pSkinInfo = NULL;
}
if( meshContainer ->originalMesh )
{
meshContainer ->originalMesh ->Release();
meshContainer ->originalMesh = NULL;
}
SAFE_DELETE( meshContainer );
return S_OK;
}
// The mesh class constructor.
Mesh::Mesh( char *name, char *path ) : Resource< Mesh >( name, path )
{
// Create the list of reference points.
m_frames = new LinkedList< Frame >;
m_refPoints = new LinkedList< Frame >;
// Load the mesh's frame hierarchy.
AllocateHierarchy ah;
D3DXLoadMeshHierarchyFromX( GetFilename(), D3DXMESH_MANAGED, g_engine->GetDevice(), &ah, NULL, (D3DXFRAME**)&m_firstFrame, &m_animationController );
// Disable all the animation tracks initially.
if( m_animationController != NULL )
for( unsigned long t = 0; t < m_animationController->GetMaxNumTracks(); ++t )
m_animationController->SetTrackEnable( t, false );
// Invalidate the bone transformation matrices array.
m_boneMatrices = NULL;
m_totalBoneMatrices = 0;
// Prepare the frame hierarchy.
PrepareFrame( m_firstFrame );
// Allocate memory for the bone matrices.
m_boneMatrices = new D3DXMATRIX[m_totalBoneMatrices];
// Create a static (non-animated) version of the mesh.
m_staticMesh = new MeshContainer;
ZeroMemory( m_staticMesh, sizeof( MeshContainer ) );
// Load the mesh.
ID3DXBuffer *materialBuffer, *adjacencyBuffer;
D3DXLoadMeshFromX( GetFilename(), D3DXMESH_MANAGED, g_engine->GetDevice(), &adjacencyBuffer, &materialBuffer, NULL, &m_staticMesh->NumMaterials, &m_staticMesh->originalMesh );
// Optimise the mesh for better rendering performance.
m_staticMesh->originalMesh->OptimizeInplace( D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL );
// Finished with the adjacency buffer, so destroy it.
if( adjacencyBuffer )
{
adjacencyBuffer ->Release();
adjacencyBuffer = NULL;
}
// Check if the mesh has any materials.
if( m_staticMesh->NumMaterials > 0 )
{
// Create the array of materials.
m_staticMesh->materials = new Material*[m_staticMesh->NumMaterials];
// Get the list of materials from the material buffer.
D3DXMATERIAL *materials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
// Load each material into the array via the material manager.
for( unsigned long m = 0; m < m_staticMesh->NumMaterials; m++ )
{
// Ensure the material has a texture.
if( materials[m].pTextureFilename )
{
// Get the name of the material's script and load it.
char *name = new char[strlen( materials[m].pTextureFilename ) + 5];
sprintf( name, "%s.txt", materials[m].pTextureFilename );
m_staticMesh->materials[m] = g_engine->GetMaterialManager()->Add( name, GetPath() );
SAFE_DELETE_ARRAY( name );
}
else
m_staticMesh->materials[m] = NULL;
}
}
// Create the bounding volume around the mesh.
BoundVolumeFromMesh( m_staticMesh->originalMesh );
// Destroy the material buffer.
if( materialBuffer )
{
materialBuffer ->Release();
materialBuffer = NULL;
}
// Create a vertex array and an array of indices into the vertex array.
m_vertices = new Vertex[m_staticMesh->originalMesh->GetNumVertices()];
m_indices = new unsigned short[m_staticMesh->originalMesh->GetNumFaces() * 3];
// Use the arrays to store a local copy of the static mesh's vertices and
// indices so that they can be used by the scene manager on the fly.
Vertex* verticesPtr;
m_staticMesh->originalMesh->LockVertexBuffer( 0, (void**)&verticesPtr );
unsigned short *indicesPtr;
m_staticMesh->originalMesh->LockIndexBuffer( 0, (void**)&indicesPtr );
memcpy( m_vertices, verticesPtr, VERTEX_FVF_SIZE * m_staticMesh->originalMesh->GetNumVertices() );
memcpy( m_indices, indicesPtr, sizeof( unsigned short ) * m_staticMesh->originalMesh->GetNumFaces() * 3 );
m_staticMesh->originalMesh->UnlockVertexBuffer();
m_staticMesh->originalMesh->UnlockIndexBuffer();
}
// The mesh class destructor.
Mesh::~Mesh()
{
// Destroy the frame hierarchy.
AllocateHierarchy ah;
D3DXFrameDestroy( m_firstFrame, &ah );
// Destroy the frames list and reference points list.
m_frames->ClearPointers();
SAFE_DELETE( m_frames );
m_refPoints->ClearPointers();
SAFE_DELETE( m_refPoints );
// Release the animation controller.
if( m_animationController )
{
m_animationController ->Release();
m_animationController = NULL;
}
// Destroy the bone matrices.
SAFE_DELETE_ARRAY( m_boneMatrices );
// Destroy the static mesh.
if( m_staticMesh )
{
// Remove all the static mesh's textures.
for( unsigned long m = 0; m < m_staticMesh->NumMaterials; m++ )
if( m_staticMesh->materials )
g_engine->GetMaterialManager()->Remove( &m_staticMesh->materials[m] );
// Clean up the rest of it.
SAFE_DELETE_ARRAY( m_staticMesh->materials );
if( m_staticMesh ->originalMesh )
{
m_staticMesh ->originalMesh ->Release();
m_staticMesh ->originalMesh = NULL;
}
SAFE_DELETE( m_staticMesh );
}
// Destroy the vertex and index arrays.
SAFE_DELETE_ARRAY( m_vertices );
SAFE_DELETE_ARRAY( m_indices );
}
// Updates the mesh.
void Mesh::Update()
{
UpdateFrame( m_firstFrame );
}
// Renders the mesh.
void Mesh::Render()
{
RenderFrame( m_firstFrame );
}
// Create a clone of the mesh's animation controller.
void Mesh::CloneAnimationController( ID3DXAnimationController **animationController )
{
if( m_animationController )
m_animationController->CloneAnimationController( m_animationController->GetMaxNumAnimationOutputs(), m_animationController->GetMaxNumAnimationSets(), m_animationController->GetMaxNumTracks(), m_animationController->GetMaxNumEvents(), &*animationController );
else
*animationController = NULL;
}
// Returns the static (non-animated) version of the mesh.
MeshContainer *Mesh::GetStaticMesh()
{
return m_staticMesh;
}
// Returns the mesh's static (non-animated) vertices.
Vertex *Mesh::GetVertices()
{
return m_vertices;
}
// Returns the mesh's face indices for the vertices.
unsigned short *Mesh::GetIndices()
{
return m_indices;
}
// Returns the list of frames in the mesh.
LinkedList< Frame > *Mesh::GetFrameList()
{
return m_frames;
}
// Returns the frame with the given name.
Frame *Mesh::GetFrame( char *name )
{
m_frames->Iterate( true );
while( m_frames->Iterate() )
if( strcmp( m_frames->GetCurrent()->Name, name ) == 0 )
return m_frames->GetCurrent();
return NULL;
}
// Returns the reference point wth the given name.
Frame *Mesh::GetReferencePoint( char *name )
{
m_refPoints->Iterate( true );
while( m_refPoints->Iterate() )
if( strcmp( m_refPoints->GetCurrent()->Name, name ) == 0 )
return m_refPoints->GetCurrent();
return NULL;
}
// Prepares the given frame.
void Mesh::PrepareFrame( Frame *frame )
{
m_frames->Add( frame );
// Check if this frame is actually a reference point.
if( strncmp( "rp_", frame->Name, 3 ) == 0 )
m_refPoints->Add( frame );
// Set the initial final transformation.
frame->finalTransformationMatrix = frame->TransformationMatrix;
// Prepare the frame's mesh container, if it has one.
if( frame->pMeshContainer != NULL )
{
MeshContainer *meshContainer = (MeshContainer*)frame->pMeshContainer;
// Check if this mesh is a skinned mesh.
if( meshContainer->pSkinInfo != NULL )
{
// Create the array of bone matrix pointers.
meshContainer->boneMatrixPointers = new D3DXMATRIX*[meshContainer->pSkinInfo->GetNumBones()];
// Set up the pointers to the mesh's bone transformation matrices.
for( unsigned long b = 0; b < meshContainer->pSkinInfo->GetNumBones(); b++ )
{
Frame *bone = (Frame*)D3DXFrameFind( m_firstFrame, meshContainer->pSkinInfo->GetBoneName( b ) );
if( bone == NULL )
continue;
meshContainer->boneMatrixPointers[b] = &bone->finalTransformationMatrix;
}
// Keep track of the maximum bones out of all the mesh containers.
if( m_totalBoneMatrices < meshContainer->pSkinInfo->GetNumBones() )
m_totalBoneMatrices = meshContainer->pSkinInfo->GetNumBones();
}
// Check if the mesh has any materials.
if( meshContainer->NumMaterials > 0 )
{
// Load all the materials in via the material manager.
for( unsigned long m = 0; m < meshContainer->NumMaterials; m++ )
{
// Ensure the material has a texture.
if( meshContainer->materialNames[m] != NULL )
{
// Get the name of the material's script and load it.
char *name = new char[strlen( meshContainer->materialNames[m] ) + 5];
sprintf( name, "%s.txt", meshContainer->materialNames[m] );
meshContainer->materials[m] = g_engine->GetMaterialManager()->Add( name, GetPath() );
SAFE_DELETE_ARRAY( name );
}
}
}
}
// Prepare the frame's siblings.
if( frame->pFrameSibling != NULL )
PrepareFrame( (Frame*)frame->pFrameSibling );
// Prepare the frame's children.
if( frame->pFrameFirstChild != NULL )
PrepareFrame( (Frame*)frame->pFrameFirstChild );
}
// Updates the given frame's transformation matrices.
void Mesh::UpdateFrame( Frame *frame, D3DXMATRIX *parentTransformationMatrix )
{
if( parentTransformationMatrix != NULL )
D3DXMatrixMultiply( &frame->finalTransformationMatrix, &frame->TransformationMatrix, parentTransformationMatrix );
else
frame->finalTransformationMatrix = frame->TransformationMatrix;
// Update the frame's siblings.
if( frame->pFrameSibling != NULL )
UpdateFrame( (Frame*)frame->pFrameSibling, parentTransformationMatrix );
// Update the frame's children.
if( frame->pFrameFirstChild != NULL )
UpdateFrame( (Frame*)frame->pFrameFirstChild, &frame->finalTransformationMatrix );
}
// Renders the given frame's mesh containers, if it has any.
void Mesh::RenderFrame( Frame *frame )
{
MeshContainer *meshContainer = (MeshContainer*)frame->pMeshContainer;
// Render this frame's mesh, if it has one.
if( frame->pMeshContainer != NULL )
{
// Check if this mesh is a skinned mesh.
if( meshContainer->pSkinInfo != NULL )
{
// Create the bone transformations using the mesh's transformation matrices.
for( unsigned long b = 0; b < meshContainer->pSkinInfo->GetNumBones(); ++b )
D3DXMatrixMultiply( &m_boneMatrices[b], meshContainer->pSkinInfo->GetBoneOffsetMatrix( b ), meshContainer->boneMatrixPointers[b] );
// Update the meshes vertices with the new bone transformation matrices.
PBYTE sourceVertices, destinationVertices;
meshContainer->originalMesh->LockVertexBuffer( D3DLOCK_READONLY, (void**)&sourceVertices );
meshContainer->MeshData.pMesh->LockVertexBuffer( 0, (void**)&destinationVertices );
meshContainer->pSkinInfo->UpdateSkinnedMesh( m_boneMatrices, NULL, sourceVertices, destinationVertices );
meshContainer->originalMesh->UnlockVertexBuffer();
meshContainer->MeshData.pMesh->UnlockVertexBuffer();
// Render the mesh by atrtribute group.
for( unsigned long a = 0; a < meshContainer->totalAttributeGroups; a++ )
{
g_engine->GetDevice()->SetMaterial( meshContainer->materials[meshContainer->attributeTable[a].AttribId]->GetLighting() );
g_engine->GetDevice()->SetTexture( 0, meshContainer->materials[meshContainer->attributeTable[a].AttribId]->GetTexture() );
meshContainer->MeshData.pMesh->DrawSubset( meshContainer->attributeTable[a].AttribId );
}
}
else
{
// This is not a skinned mesh, so render it like a static mesh.
for( unsigned long m = 0; m < meshContainer->NumMaterials; m++)
{
if( meshContainer->materials[m] )
{
g_engine->GetDevice()->SetMaterial( meshContainer->materials[m]->GetLighting() );
g_engine->GetDevice()->SetTexture( 0, meshContainer->materials[m]->GetTexture() );
}
else
g_engine->GetDevice()->SetTexture( 0, NULL );
meshContainer->MeshData.pMesh->DrawSubset( m );
}
}
}
// Render the frame's siblings.
if( frame->pFrameSibling != NULL )
RenderFrame( (Frame*)frame->pFrameSibling );
// Render the frame's children.
if( frame->pFrameFirstChild != NULL )
RenderFrame( (Frame*)frame->pFrameFirstChild );
}