-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshLoader.cpp
More file actions
725 lines (616 loc) · 21.6 KB
/
MeshLoader.cpp
File metadata and controls
725 lines (616 loc) · 21.6 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//--------------------------------------------------------------------------------------
// File: MeshLoader.cpp
//
// Wrapper class for ID3DXMesh interface. Handles loading mesh data from an .obj file
// and resource management for material textures.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "DXUT.h"
#include "SDKmisc.h"
#pragma warning(disable: 4995)
#include "meshloader.h"
#include <fstream>
using namespace std;
#pragma warning(default: 4995)
// Vertex declaration
D3DVERTEXELEMENT9 VERTEX_DECL[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{ 0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
{ 0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0},
{ 0, 48, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
//--------------------------------------------------------------------------------------
CMeshLoader::CMeshLoader()
{
m_pd3dDevice = NULL;
m_pMesh = NULL;
ZeroMemory( m_strMediaDir, sizeof( m_strMediaDir ) );
}
//--------------------------------------------------------------------------------------
CMeshLoader::~CMeshLoader()
{
Destroy();
}
//--------------------------------------------------------------------------------------
void CMeshLoader::Destroy()
{
for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
{
Material* pMaterial = m_Materials.GetAt( iMaterial );
// Avoid releasing the same texture twice
for( int x = iMaterial + 1; x < m_Materials.GetSize(); x++ )
{
Material* pCur = m_Materials.GetAt( x );
if( pCur->pDiffuseTex == pMaterial->pDiffuseTex )
pCur->pDiffuseTex = NULL;
}
SAFE_RELEASE( pMaterial->pDiffuseTex );
}
for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
{
Material* pMaterial = m_Materials.GetAt( iMaterial );
// Avoid releasing the same texture twice
for( int x = iMaterial + 1; x < m_Materials.GetSize(); x++ )
{
Material* pCur = m_Materials.GetAt( x );
if( pCur->pSpecularTex == pMaterial->pSpecularTex )
pCur->pSpecularTex = NULL;
}
SAFE_RELEASE( pMaterial->pSpecularTex );
}
for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
{
Material* pMaterial = m_Materials.GetAt( iMaterial );
// Avoid releasing the same texture twice
for( int x = iMaterial + 1; x < m_Materials.GetSize(); x++ )
{
Material* pCur = m_Materials.GetAt( x );
if( pCur->pNormalTex == pMaterial->pNormalTex )
pCur->pNormalTex = NULL;
}
SAFE_RELEASE( pMaterial->pNormalTex );
}
for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
{
Material* pMaterial = m_Materials.GetAt( iMaterial );
SAFE_DELETE( pMaterial );
}
m_Materials.RemoveAll();
m_Vertices.RemoveAll();
m_Indices.RemoveAll();
m_Attributes.RemoveAll();
SAFE_RELEASE( m_pMesh );
m_pd3dDevice = NULL;
}
//--------------------------------------------------------------------------------------
HRESULT CMeshLoader::Create( IDirect3DDevice9* pd3dDevice, const WCHAR* strFilename )
{
HRESULT hr;
WCHAR str[ MAX_PATH ] = {0};
// Start clean
Destroy();
// Store the device pointer
m_pd3dDevice = pd3dDevice;
// Load the vertex buffer, index buffer, and subset information from a file. In this case,
// an .obj file was chosen for simplicity, but it's meant to illustrate that ID3DXMesh objects
// can be filled from any mesh file format once the necessary data is extracted from file.
V_RETURN( LoadGeometryFromOBJ( strFilename ) );
// Set the current directory based on where the mesh was found
WCHAR wstrOldDir[MAX_PATH] = {0};
GetCurrentDirectory( MAX_PATH, wstrOldDir );
SetCurrentDirectory( m_strMediaDir );
// Load material textures
for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
{
Material* pMaterial = m_Materials.GetAt( iMaterial );
if( pMaterial->strDiffuseTex[0] )
{
// Avoid loading the same texture twice
bool bFound = false;
for( int x = 0; x < iMaterial; x++ )
{
Material* pCur = m_Materials.GetAt( x );
if( 0 == wcscmp( pCur->strDiffuseTex, pMaterial->strDiffuseTex ) )
{
bFound = true;
pMaterial->pDiffuseTex = pCur->pDiffuseTex;
break;
}
}
// Not found, load the texture
if( !bFound )
{
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, pMaterial->strDiffuseTex ) );
V_RETURN( D3DXCreateTextureFromFile( pd3dDevice, pMaterial->strDiffuseTex,
&( pMaterial->pDiffuseTex ) ) );
}
}
if( pMaterial->strSpecularTex[0] )
{
// Avoid loading the same texture twice
bool bFound = false;
for( int x = 0; x < iMaterial; x++ )
{
Material* pCur = m_Materials.GetAt( x );
if( 0 == wcscmp( pCur->strSpecularTex, pMaterial->strSpecularTex ) )
{
bFound = true;
pMaterial->pSpecularTex = pCur->pSpecularTex;
break;
}
}
// Not found, load the texture
if( !bFound )
{
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, pMaterial->strSpecularTex ) );
V_RETURN( D3DXCreateTextureFromFile( pd3dDevice, pMaterial->strSpecularTex,
&( pMaterial->pSpecularTex ) ) );
}
}
if( pMaterial->strNormalTex[0] )
{
// Avoid loading the same texture twice
bool bFound = false;
for( int x = 0; x < iMaterial; x++ )
{
Material* pCur = m_Materials.GetAt( x );
if( 0 == wcscmp( pCur->strNormalTex, pMaterial->strNormalTex ) )
{
bFound = true;
pMaterial->pNormalTex = pCur->pNormalTex;
break;
}
}
// Not found, load the texture
if( !bFound )
{
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, pMaterial->strNormalTex ) );
V_RETURN( D3DXCreateTextureFromFile( pd3dDevice, pMaterial->strNormalTex,
&( pMaterial->pNormalTex ) ) );
}
}
}
// Restore the original current directory
SetCurrentDirectory( wstrOldDir );
// compute TBN data
FillTBN();
// Create the encapsulated mesh
ID3DXMesh* pMesh = NULL;
V_RETURN( D3DXCreateMesh( m_Indices.GetSize() / 3, m_Vertices.GetSize(),
D3DXMESH_MANAGED | D3DXMESH_32BIT, VERTEX_DECL,
pd3dDevice, &pMesh ) );
// Copy the vertex data
VERTEX* pVertex;
V_RETURN( pMesh->LockVertexBuffer( 0, ( void** )&pVertex ) );
memcpy( pVertex, m_Vertices.GetData(), m_Vertices.GetSize() * sizeof( VERTEX ) );
pMesh->UnlockVertexBuffer();
m_Vertices.RemoveAll();
// Copy the index data
DWORD* pIndex;
V_RETURN( pMesh->LockIndexBuffer( 0, ( void** )&pIndex ) );
memcpy( pIndex, m_Indices.GetData(), m_Indices.GetSize() * sizeof( DWORD ) );
pMesh->UnlockIndexBuffer();
m_Indices.RemoveAll();
// Copy the attribute data
DWORD* pSubset;
V_RETURN( pMesh->LockAttributeBuffer( 0, &pSubset ) );
memcpy( pSubset, m_Attributes.GetData(), m_Attributes.GetSize() * sizeof( DWORD ) );
pMesh->UnlockAttributeBuffer();
m_Attributes.RemoveAll();
// Reorder the vertices according to subset and optimize the mesh for this graphics
// card's vertex cache. When rendering the mesh's triangle list the vertices will
// cache hit more often so it won't have to re-execute the vertex shader.
DWORD* aAdjacency = new DWORD[pMesh->GetNumFaces() * 3];
if( aAdjacency == NULL )
return E_OUTOFMEMORY;
V( pMesh->GenerateAdjacency( 1e-6f, aAdjacency ) );
V( pMesh->OptimizeInplace( D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, aAdjacency, NULL, NULL, NULL ) );
SAFE_DELETE_ARRAY( aAdjacency );
m_pMesh = pMesh;
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CMeshLoader::LoadGeometryFromOBJ( const WCHAR* strFileName )
{
WCHAR strMaterialFilename[MAX_PATH] = {0};
WCHAR wstr[MAX_PATH];
char str[MAX_PATH];
HRESULT hr;
// Find the file
V_RETURN( DXUTFindDXSDKMediaFileCch( wstr, MAX_PATH, strFileName ) );
WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, MAX_PATH, NULL, NULL );
// Store the directory where the mesh was found
wcscpy_s( m_strMediaDir, MAX_PATH - 1, wstr );
WCHAR* pch = wcsrchr( m_strMediaDir, L'\\' );
if( pch )
*pch = NULL;
// Create temporary storage for the input data. Once the data has been loaded into
// a reasonable format we can create a D3DXMesh object and load it with the mesh data.
CGrowableArray <D3DXVECTOR3> Positions;
CGrowableArray <D3DXVECTOR2> TexCoords;
CGrowableArray <D3DXVECTOR3> Normals;
// The first subset uses the default material
Material* pMaterial = new Material();
if( pMaterial == NULL )
return E_OUTOFMEMORY;
InitMaterial( pMaterial );
wcscpy_s( pMaterial->strName, MAX_PATH - 1, L"default" );
m_Materials.Add( pMaterial );
DWORD dwCurSubset = 0;
// File input
WCHAR strCommand[256] = {0};
wifstream InFile( str );
if( !InFile )
return DXTRACE_ERR( L"wifstream::open", E_FAIL );
for(; ; )
{
InFile >> strCommand;
if( !InFile )
break;
if( 0 == wcscmp( strCommand, L"#" ) )
{
// Comment
}
else if( 0 == wcscmp( strCommand, L"v" ) )
{
// Vertex Position
float x, y, z;
InFile >> x >> y >> z;
Positions.Add( D3DXVECTOR3( x, y, z ) );
}
else if( 0 == wcscmp( strCommand, L"vt" ) )
{
// Vertex TexCoord
float u, v;
InFile >> u >> v;
TexCoords.Add( D3DXVECTOR2( u, v ) );
}
else if( 0 == wcscmp( strCommand, L"vn" ) )
{
// Vertex Normal
float x, y, z;
InFile >> x >> y >> z;
Normals.Add( D3DXVECTOR3( x, y, z ) );
}
else if( 0 == wcscmp( strCommand, L"f" ) )
{
// Face
UINT iPosition, iTexCoord, iNormal;
VERTEX vertex;
for( UINT iFace = 0; iFace < 3; iFace++ )
{
ZeroMemory( &vertex, sizeof( VERTEX ) );
// OBJ format uses 1-based arrays
InFile >> iPosition;
vertex.position = Positions[ iPosition - 1 ];
if( '/' == InFile.peek() )
{
InFile.ignore();
if( '/' != InFile.peek() )
{
// Optional texture coordinate
InFile >> iTexCoord;
vertex.texcoord = TexCoords[ iTexCoord - 1 ];
}
if( '/' == InFile.peek() )
{
InFile.ignore();
// Optional vertex normal
InFile >> iNormal;
vertex.normal = Normals[ iNormal - 1 ];
}
}
// If a duplicate vertex doesn't exist, add this vertex to the Vertices
// list. Store the index in the Indices array. The Vertices and Indices
// lists will eventually become the Vertex Buffer and Index Buffer for
// the mesh.
DWORD index = AddVertex( iPosition, &vertex );
if ( index == (DWORD)-1 )
return E_OUTOFMEMORY;
m_Indices.Add( index );
}
m_Attributes.Add( dwCurSubset );
}
else if( 0 == wcscmp( strCommand, L"mtllib" ) )
{
// Material library
InFile >> strMaterialFilename;
}
else if( 0 == wcscmp( strCommand, L"usemtl" ) )
{
// Material
WCHAR strName[MAX_PATH] = {0};
InFile >> strName;
bool bFound = false;
for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
{
Material* pCurMaterial = m_Materials.GetAt( iMaterial );
if( 0 == wcscmp( pCurMaterial->strName, strName ) )
{
bFound = true;
dwCurSubset = iMaterial;
break;
}
}
if( !bFound )
{
pMaterial = new Material();
if( pMaterial == NULL )
return E_OUTOFMEMORY;
dwCurSubset = m_Materials.GetSize();
InitMaterial( pMaterial );
wcscpy_s( pMaterial->strName, MAX_PATH - 1, strName );
m_Materials.Add( pMaterial );
}
}
else
{
// Unimplemented or unrecognized command
}
InFile.ignore( 1000, '\n' );
}
// Cleanup
InFile.close();
DeleteCache();
// If an associated material file was found, read that in as well.
if( strMaterialFilename[0] )
{
V_RETURN( LoadMaterialsFromMTL( strMaterialFilename ) );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
DWORD CMeshLoader::AddVertex( UINT hash, VERTEX* pVertex )
{
// If this vertex doesn't already exist in the Vertices list, create a new entry.
// Add the index of the vertex to the Indices list.
bool bFoundInList = false;
DWORD index = 0;
// Since it's very slow to check every element in the vertex list, a hashtable stores
// vertex indices according to the vertex position's index as reported by the OBJ file
if( ( UINT )m_VertexCache.GetSize() > hash )
{
CacheEntry* pEntry = m_VertexCache.GetAt( hash );
while( pEntry != NULL )
{
VERTEX* pCacheVertex = m_Vertices.GetData() + pEntry->index;
// If this vertex is identical to the vertex already in the list, simply
// point the index buffer to the existing vertex
if( 0 == memcmp( pVertex, pCacheVertex, sizeof( VERTEX ) ) )
{
bFoundInList = true;
index = pEntry->index;
break;
}
pEntry = pEntry->pNext;
}
}
// Vertex was not found in the list. Create a new entry, both within the Vertices list
// and also within the hashtable cache
if( !bFoundInList )
{
// Add to the Vertices list
index = m_Vertices.GetSize();
m_Vertices.Add( *pVertex );
// Add this to the hashtable
CacheEntry* pNewEntry = new CacheEntry;
if( pNewEntry == NULL )
return (DWORD)-1;
pNewEntry->index = index;
pNewEntry->pNext = NULL;
// Grow the cache if needed
while( ( UINT )m_VertexCache.GetSize() <= hash )
{
m_VertexCache.Add( NULL );
}
// Add to the end of the linked list
CacheEntry* pCurEntry = m_VertexCache.GetAt( hash );
if( pCurEntry == NULL )
{
// This is the head element
m_VertexCache.SetAt( hash, pNewEntry );
}
else
{
// Find the tail
while( pCurEntry->pNext != NULL )
{
pCurEntry = pCurEntry->pNext;
}
pCurEntry->pNext = pNewEntry;
}
}
return index;
}
//--------------------------------------------------------------------------------------
void CMeshLoader::DeleteCache()
{
// Iterate through all the elements in the cache and subsequent linked lists
for( int i = 0; i < m_VertexCache.GetSize(); i++ )
{
CacheEntry* pEntry = m_VertexCache.GetAt( i );
while( pEntry != NULL )
{
CacheEntry* pNext = pEntry->pNext;
SAFE_DELETE( pEntry );
pEntry = pNext;
}
}
m_VertexCache.RemoveAll();
}
//--------------------------------------------------------------------------------------
HRESULT CMeshLoader::LoadMaterialsFromMTL( const WCHAR* strFileName )
{
HRESULT hr;
// Set the current directory based on where the mesh was found
WCHAR wstrOldDir[MAX_PATH] = {0};
GetCurrentDirectory( MAX_PATH, wstrOldDir );
SetCurrentDirectory( m_strMediaDir );
// Find the file
WCHAR strPath[MAX_PATH];
char cstrPath[MAX_PATH];
V_RETURN( DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strFileName ) );
WideCharToMultiByte( CP_ACP, 0, strPath, -1, cstrPath, MAX_PATH, NULL, NULL );
// File input
WCHAR strCommand[256] = {0};
wifstream InFile( cstrPath );
if( !InFile )
return DXTRACE_ERR( L"wifstream::open", E_FAIL );
// Restore the original current directory
SetCurrentDirectory( wstrOldDir );
Material* pMaterial = NULL;
for(; ; )
{
InFile >> strCommand;
if( !InFile )
break;
if( 0 == wcscmp( strCommand, L"newmtl" ) )
{
// Switching active materials
WCHAR strName[MAX_PATH] = {0};
InFile >> strName;
pMaterial = NULL;
for( int i = 0; i < m_Materials.GetSize(); i++ )
{
Material* pCurMaterial = m_Materials.GetAt( i );
if( 0 == wcscmp( pCurMaterial->strName, strName ) )
{
pMaterial = pCurMaterial;
break;
}
}
}
// The rest of the commands rely on an active material
if( pMaterial == NULL )
continue;
if( 0 == wcscmp( strCommand, L"#" ) )
{
// Comment
}
else if( 0 == wcscmp( strCommand, L"Ka" ) )
{
// Ambient color
float r, g, b;
InFile >> r >> g >> b;
pMaterial->vAmbient = D3DXVECTOR3( r, g, b );
}
else if( 0 == wcscmp( strCommand, L"Kd" ) )
{
// Diffuse color
float r, g, b;
InFile >> r >> g >> b;
pMaterial->vDiffuse = D3DXVECTOR3( r, g, b );
}
else if( 0 == wcscmp( strCommand, L"Ks" ) )
{
// Specular color
float r, g, b;
InFile >> r >> g >> b;
pMaterial->vSpecular = D3DXVECTOR3( r, g, b );
}
else if( 0 == wcscmp( strCommand, L"d" ) ||
0 == wcscmp( strCommand, L"Tr" ) )
{
// Alpha
InFile >> pMaterial->fAlpha;
}
else if( 0 == wcscmp( strCommand, L"Ns" ) )
{
// Shininess
int nShininess;
InFile >> nShininess;
pMaterial->nShininess = nShininess;
}
else if( 0 == wcscmp( strCommand, L"illum" ) )
{
// Specular on/off
int illumination;
InFile >> illumination;
pMaterial->bSpecular = ( illumination == 2 );
}
else if( 0 == wcscmp( strCommand, L"map_Kd" ) )
{
// Texture
InFile >> pMaterial->strDiffuseTex;
}
else if (0 == wcscmp( strCommand, L"map_Ks" ))
{
InFile >> pMaterial->strSpecularTex;
}
else if (0 == wcscmp( strCommand, L"map_bump" ))
{
WCHAR buf[100];
InFile >> buf;
InFile >> pMaterial->bumpVal;
InFile >> pMaterial->strNormalTex;
}
else
{
// Unimplemented or unrecognized command
}
InFile.ignore( 1000, L'\n' );
}
InFile.close();
return S_OK;
}
//--------------------------------------------------------------------------------------
void CMeshLoader::InitMaterial( Material* pMaterial )
{
ZeroMemory( pMaterial, sizeof( Material ) );
pMaterial->vAmbient = D3DXVECTOR3( 0.2f, 0.2f, 0.2f );
pMaterial->vDiffuse = D3DXVECTOR3( 0.8f, 0.8f, 0.8f );
pMaterial->vSpecular = D3DXVECTOR3( 1.0f, 1.0f, 1.0f );
pMaterial->nShininess = 0;
pMaterial->fAlpha = 1.0f;
pMaterial->bSpecular = false;
pMaterial->pDiffuseTex = NULL;
pMaterial->pSpecularTex = NULL;
pMaterial->pNormalTex = NULL;
pMaterial->bumpVal = 0.3;
pMaterial->strDiffuseTex[0] = 0;
pMaterial->strSpecularTex[0] = 0;
pMaterial->strNormalTex[0] = 0;
}
void CMeshLoader::ComputeTBN( VERTEX& v0, VERTEX& v1, VERTEX& v2 )
{
D3DXVECTOR3 p10 = v1.position - v0.position;
D3DXVECTOR3 p20 = v2.position - v0.position;
D3DXVECTOR2 t10 = v1.texcoord - v0.texcoord;
D3DXVECTOR2 t20 = v2.texcoord - v0.texcoord;
float a11 = t10.x, a12 = t10.y;
float a21 = t20.x, a22 = t20.y;
float delta = a11 * a22 - a21 * a12;
float b1, b2, x1, x2;
D3DXVECTOR3 tangent, bitangent;
b1 = p10.x; b2 = p20.x;
x1 = abs(delta) > 1e-6 ? (b1*a22 - b2*a12)/delta : 0;
x2 = abs(delta) > 1e-6 ? (a11*b2 - a21*b1)/delta : 0;
tangent.x = x1;
bitangent.x = x2;
b1 = p10.y; b2 = p20.y;
x1 = abs(delta) > 1e-6 ? (b1*a22 - b2*a12)/delta : 0;
x2 = abs(delta) > 1e-6 ? (a11*b2 - a21*b1)/delta : 0;
tangent.y = x1;
bitangent.y = x2;
b1 = p10.z; b2 = p20.z;
x1 = abs(delta) > 1e-6 ? (b1*a22 - b2*a12)/delta : 0;
x2 = abs(delta) > 1e-6 ? (a11*b2 - a21*b1)/delta : 0;
tangent.z = x1;
bitangent.z = x2;
v0.tangent = v1.tangent = v2.tangent = tangent;
v0.bitangent = v1.bitangent = v2.bitangent = bitangent;
}
void CMeshLoader::FillTBN()
{
for (int i = 0; i < m_Indices.GetSize(); i+=3)
{
DWORD id0 = m_Indices[i];
DWORD id1 = m_Indices[i+1];
DWORD id2 = m_Indices[i+2];
ComputeTBN(m_Vertices[id0], m_Vertices[id1], m_Vertices[id2]);
}
}