zfxMesh.cpp
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:2k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. //-------------------------------------------------------------------------------
  2. //zfxMesh.cpp
  3. //-------------------------------------------------------------------------------
  4. #include "zfxMesh.h"
  5. ///////////////////CZFXMesh class implementation////////////////////////////////////////////////////
  6. CZFXMesh::CZFXMesh()
  7. {
  8. m_dwMtrl = 0;
  9. m_pMeshMtrl = NULL;
  10. m_pMesh = NULL;
  11. m_ppTexture = NULL;
  12. }
  13. CZFXMesh::~CZFXMesh()
  14. {
  15. Release();
  16. }
  17. void CZFXMesh::Release()
  18. {
  19. SAFE_DELETE( m_pMeshMtrl );
  20. SAFE_RELEASE( m_pMesh );
  21. if(m_ppTexture)
  22. {
  23. for(DWORD i=0;i<m_dwMtrl;i++)
  24. SAFE_RELEASE( m_ppTexture[i] );
  25. SAFE_DELETE(m_ppTexture);
  26. }
  27. }
  28. HRESULT CZFXMesh::LoadMeshFromFile( LPDIRECT3DDEVICE9 pd3dDevice, LPCTSTR strFileName, 
  29. DWORD options )
  30. {
  31. HRESULT hr;
  32. ID3DXBuffer* pMtrlBuffer;
  33. if( FAILED( hr = D3DXLoadMeshFromX( strFileName, options, pd3dDevice,
  34. NULL, &pMtrlBuffer, NULL, &m_dwMtrl, &m_pMesh )))
  35. return hr;
  36. D3DXMATERIAL* pMtrlInBuf = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
  37. m_pMeshMtrl = new D3DMATERIAL9[m_dwMtrl];
  38. for(DWORD i=0;i<m_dwMtrl;i++)
  39. {
  40. m_pMeshMtrl[i] = pMtrlInBuf[i].MatD3D;
  41. }
  42. m_ppTexture = new LPDIRECT3DTEXTURE9[m_dwMtrl];
  43. for(DWORD i=0;i<m_dwMtrl;i++)
  44. {
  45. if( NULL != pMtrlInBuf[i].pTextureFilename )
  46. {
  47. D3DXCreateTextureFromFileA( pd3dDevice, pMtrlInBuf[i].pTextureFilename, &m_ppTexture[i] );
  48. }
  49. else
  50. {
  51. m_ppTexture[i] = NULL;
  52. }
  53. }
  54. return hr;
  55. }
  56. void CZFXMesh::Render( LPDIRECT3DDEVICE9 pd3dDevice )
  57. {
  58. for(DWORD i=0;i<m_dwMtrl;i++)
  59. {
  60. pd3dDevice->SetTexture( 0, m_ppTexture[i]);
  61. pd3dDevice->SetMaterial( &m_pMeshMtrl[i] );
  62. m_pMesh->DrawSubset(i);
  63. }
  64. }
  65. void CZFXMesh::Render( LPDIRECT3DDEVICE9 pd3dDevice, LPDIRECT3DTEXTURE9* ppTexture )
  66. {
  67. for(DWORD i=0;i<m_dwMtrl;i++)
  68. {
  69. pd3dDevice->SetTexture( 0, ppTexture[i]);
  70. pd3dDevice->SetMaterial( &m_pMeshMtrl[i] );
  71. m_pMesh->DrawSubset(i);
  72. }
  73. }