/
Mesh.cpp
369 lines (308 loc) · 12.4 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
//-----------------------------------------------------------------------------
// File: ProgressiveMesh.cpp
//
// Desc: Sample of creating progressive meshes in D3D
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include "stdafx.h"
#include <Windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <commdlg.h>
#include <D3DX9.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DUtil.h"
#include "mesh.h"
//-----------------------------------------------------------------------------
// Name: CMeshRender()
// Desc: Constructor
//-----------------------------------------------------------------------------
CMeshRender::CMeshRender()
{
// Initialize member variables
DXUtil_GetDXSDKMediaPathCb( m_strInitialDir, sizeof(m_strInitialDir) );
_tcscpy( m_strMeshFilename, _T("Sphere.x") );//_T("Tiger.x") );
m_pPMeshes = NULL;
m_cPMeshes = 0;
m_pPMeshFull = NULL;
m_bShowOptimized = TRUE;
m_dwNumMaterials = 0L;
m_mtrlMeshMaterials = NULL;
m_pMeshTextures = NULL;
m_bDisplayHelp = FALSE;
m_bInitDone = FALSE;
m_pd3dDevice = NULL;
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMeshRender::Render()
{
if (!m_bInitDone)
return S_OK;
// Clear the backbuffer
//m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
// 0x000000ff, 1.0f, 0x00000000 );
{
if( m_pPMeshes )
{
// Set and draw each of the materials in the mesh
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
m_pd3dDevice->SetMaterial( &m_mtrlMeshMaterials[i] );
m_pd3dDevice->SetTexture( 0, m_pMeshTextures[i] );
if (m_bShowOptimized)
m_pPMeshes[m_iPMeshCur]->DrawSubset( i );
else
m_pPMeshFull->DrawSubset( i );
}
}
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMeshRender::InitDeviceObjects()
{
DWORD cVerticesPerMesh;
// Load mesh
LPD3DXBUFFER pAdjacencyBuffer = NULL;
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;
LPD3DXMESH pMesh = NULL;
LPD3DXPMESH pPMesh = NULL;
LPD3DXMESH pTempMesh;
LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
void* pVertices;
TCHAR strMediaPath[512];
HRESULT hr;
DWORD dw32BitFlag;
DWORD cVerticesMin;
DWORD cVerticesMax;
DWORD iPMesh;
D3DXWELDEPSILONS Epsilons;
DWORD i;
D3DXMATERIAL* d3dxMaterials;
// Find the path to the mesh
if( FAILED( DXUtil_FindMediaFileCb( strMediaPath, sizeof(strMediaPath), m_strMeshFilename ) ) )
return E_FAIL;//D3DAPPERR_MEDIANOTFOUND;
// Load the mesh from the specified file
if( FAILED( hr = D3DXLoadMeshFromX( strMediaPath, D3DXMESH_MANAGED, m_pd3dDevice,
&pAdjacencyBuffer, &pD3DXMtrlBuffer, NULL,
&m_dwNumMaterials, &pMesh ) ) )
{
// hide error so that device changes will not cause exit, shows blank screen instead
goto End;
}
dw32BitFlag = (pMesh->GetOptions() & D3DXMESH_32BIT);
// perform simple cleansing operations on mesh
if( FAILED( hr = D3DXCleanMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), &pTempMesh,
(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL ) ) )
{
m_dwNumMaterials = 0;
goto End;
}
SAFE_RELEASE(pMesh);
pMesh = pTempMesh;
// Perform a weld to try and remove excess vertices like the model bigship1.x in the DX9.0 SDK (current model is fixed)
// Weld the mesh using all epsilons of 0.0f. A small epsilon like 1e-6 works well too
memset(&Epsilons, 0, sizeof(D3DXWELDEPSILONS));
if( FAILED( hr = D3DXWeldVertices( pMesh, 0, &Epsilons,
(DWORD*)pAdjacencyBuffer->GetBufferPointer(),
(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL ) ) )
{
m_dwNumMaterials = 0;
goto End;
}
// verify validity of mesh for simplification
if( FAILED( hr = D3DXValidMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL ) ) )
{
m_dwNumMaterials = 0;
goto End;
}
// Allocate a material/texture arrays
d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
m_mtrlMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];
// Copy the materials and load the textures
for( i=0; i<m_dwNumMaterials; i++ )
{
m_mtrlMeshMaterials[i] = d3dxMaterials[i].MatD3D;
m_mtrlMeshMaterials[i].Ambient = m_mtrlMeshMaterials[i].Diffuse;
// Find the path to the texture and create that texture
DXUtil_FindMediaFileCb( strMediaPath, sizeof(strMediaPath), d3dxMaterials[i].pTextureFilename );
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, strMediaPath,
&m_pMeshTextures[i] ) ) )
m_pMeshTextures[i] = NULL;
}
pD3DXMtrlBuffer->Release();
pD3DXMtrlBuffer = NULL;
// Lock the vertex buffer, to generate a simple bounding sphere
hr = pMesh->GetVertexBuffer( &pVertexBuffer );
if( FAILED(hr) )
goto End;
hr = pVertexBuffer->Lock( 0, 0, &pVertices, D3DLOCK_NOSYSLOCK );
if( FAILED(hr) )
goto End;
hr = D3DXComputeBoundingSphere( (D3DXVECTOR3*)pVertices, pMesh->GetNumVertices(),
D3DXGetFVFVertexSize(pMesh->GetFVF()),
&m_vObjectCenter, &m_fObjectRadius );
pVertexBuffer->Unlock();
pVertexBuffer->Release();
if( FAILED(hr) || m_dwNumMaterials == 0 )
goto End;
if ( !(pMesh->GetFVF() & D3DFVF_NORMAL) )
{
hr = pMesh->CloneMeshFVF( dw32BitFlag|D3DXMESH_MANAGED, pMesh->GetFVF() | D3DFVF_NORMAL,
m_pd3dDevice, &pTempMesh );
if (FAILED(hr))
goto End;
D3DXComputeNormals( pTempMesh, NULL );
pMesh->Release();
pMesh = pTempMesh;
}
hr = D3DXGeneratePMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(),
NULL, NULL, 1, D3DXMESHSIMP_VERTEX, &pPMesh);
if( FAILED(hr) )
goto End;
cVerticesMin = pPMesh->GetMinVertices();
cVerticesMax = pPMesh->GetMaxVertices();
cVerticesPerMesh = (cVerticesMax - cVerticesMin) / 10;
m_cPMeshes = max(1, (DWORD)ceil((cVerticesMax - cVerticesMin) / (float)cVerticesPerMesh));
m_pPMeshes = new LPD3DXPMESH[m_cPMeshes];
if (m_pPMeshes == NULL)
{
hr = E_OUTOFMEMORY;
goto End;
}
memset(m_pPMeshes, 0, sizeof(LPD3DXPMESH) * m_cPMeshes);
// clone full size pmesh
hr = pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), m_pd3dDevice, &m_pPMeshFull );
if (FAILED(hr))
goto End;
// clone all the separate pmeshes
for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++)
{
hr = pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), m_pd3dDevice, &m_pPMeshes[iPMesh] );
if (FAILED(hr))
goto End;
// trim to appropriate space
hr = m_pPMeshes[iPMesh]->TrimByVertices(cVerticesMin + cVerticesPerMesh * iPMesh, cVerticesMin + cVerticesPerMesh * (iPMesh+1), NULL, NULL);
if (FAILED(hr))
goto End;
hr = m_pPMeshes[iPMesh]->OptimizeBaseLOD(D3DXMESHOPT_VERTEXCACHE, NULL);
if (FAILED(hr))
goto End;
}
// set current to be maximum number of vertices
m_iPMeshCur = m_cPMeshes - 1;
hr = m_pPMeshes[m_iPMeshCur]->SetNumVertices(cVerticesMax);
if (FAILED(hr))
goto End;
hr = m_pPMeshFull->SetNumVertices(cVerticesMax);
if (FAILED(hr))
goto End;
End:
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pD3DXMtrlBuffer );
SAFE_RELEASE( pMesh );
SAFE_RELEASE( pPMesh );
if (FAILED(hr))
{
for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++)
{
SAFE_RELEASE( m_pPMeshes[iPMesh] );
}
delete []m_pPMeshes;
m_cPMeshes = 0;
m_pPMeshes = NULL;
SAFE_RELEASE( m_pPMeshFull )
}
return hr;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMeshRender::RestoreDeviceObjects()
{
// Setup render state
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
// Setup the light
D3DLIGHT9 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 1.0f, -1.0f, 1.0f );
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
D3DLIGHT9 light1;
D3DUtil_InitLight( light1, D3DLIGHT_DIRECTIONAL, 1.0f, 10.0f, -1.0f );
m_pd3dDevice->SetLight( 1, &light1 );
m_pd3dDevice->LightEnable( 1, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00333333 );
m_bInitDone = TRUE;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMeshRender::DeleteDeviceObjects()
{
DWORD iPMesh;
if( m_pMeshTextures != NULL)
{
for( UINT i=0; i<m_dwNumMaterials; i++ )
SAFE_RELEASE( m_pMeshTextures[i] );
}
SAFE_DELETE_ARRAY( m_pMeshTextures );
SAFE_RELEASE( m_pPMeshFull );
for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++)
{
SAFE_RELEASE( m_pPMeshes[iPMesh] );
}
m_cPMeshes = 0;
delete []m_pPMeshes;
SAFE_DELETE_ARRAY( m_mtrlMeshMaterials );
m_dwNumMaterials = 0L;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: SetNumVertices()
// Desc: Sets the number of vertices to display on the current progressive mesh
//-----------------------------------------------------------------------------
void
CMeshRender::SetNumVertices(DWORD dwNumVertices)
{
m_pPMeshFull->SetNumVertices( dwNumVertices );
// if current pm valid for desired value, then set the number of vertices directly
if ((dwNumVertices >= m_pPMeshes[m_iPMeshCur]->GetMinVertices()) && (dwNumVertices <= m_pPMeshes[m_iPMeshCur]->GetMaxVertices()))
{
m_pPMeshes[m_iPMeshCur]->SetNumVertices( dwNumVertices );
}
else // search for the right one
{
m_iPMeshCur = m_cPMeshes - 1;
// look for the correct "bin"
while (m_iPMeshCur > 0)
{
// if number of vertices is less than current max then we found one to fit
if (dwNumVertices >= m_pPMeshes[m_iPMeshCur]->GetMinVertices())
break;
m_iPMeshCur -= 1;
}
// set the vertices on the newly selected mesh
m_pPMeshes[m_iPMeshCur]->SetNumVertices( dwNumVertices );
}
}