www.pudn.com > snow.rar > drawMesh.cpp


#include "drawMesh.h" 
 
 
 
DrawMesh::DrawMesh() 
{ 
	LPD3DXMESH              g_pMesh          = NULL;  
	D3DMATERIAL9*           g_pMeshMaterials = NULL;  
	LPDIRECT3DTEXTURE9*     g_pMeshTextures  = NULL;  
	DWORD                   g_dwNumMaterials = 0L;  
} 
 
 
//----------------------------------------------------------------------------- 
// Name: InitGeometry() 
// Desc: Load the mesh and build the material and texture arrays 
//----------------------------------------------------------------------------- 
HRESULT DrawMesh::InitGeometry(LPDIRECT3DDEVICE9 g_pd3dDevice,char* strMeshName,char*strTexName) 
{ 
    LPD3DXBUFFER pD3DXMtrlBuffer; 
 
    // Load the mesh from the specified file 
	D3DXLoadMeshFromX( strMeshName, D3DXMESH_SYSTEMMEM,g_pd3dDevice, NULL,  
                       &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,  
                      &g_pMesh ) ; 
 
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); 
    g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials]; 
    g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials]; 
 
    for( DWORD i=0; iRelease(); 
 
    return S_OK; 
} 
 
HRESULT DrawMesh::InitGeometry(LPDIRECT3DDEVICE9 g_pd3dDevice,char* strMeshName) 
{ 
    LPD3DXBUFFER pD3DXMtrlBuffer; 
 
    // Load the mesh from the specified file 
	D3DXLoadMeshFromX( strMeshName, D3DXMESH_SYSTEMMEM,g_pd3dDevice, NULL,  
                       &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,  
                      &g_pMesh ) ; 
 
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); 
    g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials]; 
    g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials]; 
 
    for( DWORD i=0; iRelease(); 
 
    return S_OK; 
} 
 
 
//----------------------------------------------------------------------------- 
// Name: Cleanup() 
// Desc: Releases all previously initialized objects 
//----------------------------------------------------------------------------- 
void DrawMesh::Cleanup() 
{ 
    if( g_pMeshMaterials != NULL )  
        delete[] g_pMeshMaterials; 
 
    if( g_pMeshTextures ) 
    { 
        for( DWORD i = 0; i < g_dwNumMaterials; i++ ) 
        { 
            if( g_pMeshTextures[i] ) 
                g_pMeshTextures[i]->Release(); 
        } 
        delete[] g_pMeshTextures; 
    } 
    if( g_pMesh != NULL ) 
        g_pMesh->Release(); 
 
} 
 
 
//----------------------------------------------------------------------------- 
// Name: Render() 
// Desc: Draws the scene 
//----------------------------------------------------------------------------- 
void DrawMesh::Render(LPDIRECT3DDEVICE9 g_pd3dDevice) 
{ 
      // Meshes are divided into subsets, one for each material. Render them in 
        // a loop 
        for( DWORD i=0; iSetMaterial( &g_pMeshMaterials[i] ); 
            g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] ); 
         
            // Draw the mesh subset 
            g_pMesh->DrawSubset( i ); 
        } 
}