Mesh.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:3k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. // Mesh.cpp: implementation of the CMesh class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Mesh.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CMesh::CMesh(LPDIRECT3DDEVICE8 pD3DDevice, LPSTR pFilename)
  9. {
  10. LPD3DXBUFFER pMaterialsBuffer = NULL;
  11. LPD3DXMESH pMesh = NULL;
  12. m_pD3DDevice = pD3DDevice;
  13. if(FAILED(D3DXLoadMeshFromX(pFilename, D3DXMESH_SYSTEMMEM, m_pD3DDevice, NULL, 
  14.                                 &pMaterialsBuffer, &m_dwNumMaterials, &pMesh)))
  15. {
  16. m_pMesh = NULL;
  17. m_pMeshMaterials = NULL;
  18. m_pMeshTextures = NULL;
  19. LogError("<li>Mesh '%s' failed to load", pFilename);
  20. return;
  21. }
  22.     D3DXMATERIAL* matMaterials = (D3DXMATERIAL*)pMaterialsBuffer->GetBufferPointer();
  23.     
  24. //Create two arrays. One to hold the materials and only to hold the textures
  25. m_pMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];
  26.     m_pMeshTextures  = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
  27.     for(DWORD i = 0; i < m_dwNumMaterials; i++)
  28.     {
  29.         // Copy the material
  30.         m_pMeshMaterials[i] = matMaterials[i].MatD3D;
  31.         // Set the ambient color for the material (D3DX does not do this)
  32. m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
  33. // Create the texture
  34.         if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, 
  35.                                             matMaterials[i].pTextureFilename, 
  36.                                             &m_pMeshTextures[i])))
  37.         {
  38.             m_pMeshTextures[i] = NULL;
  39.         }
  40.     }
  41.     //We've finished with the material buffer, so release it
  42.     SafeRelease(pMaterialsBuffer);
  43. //Make sure that the normals are setup for our mesh
  44. pMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, m_pD3DDevice, &m_pMesh);
  45. SafeRelease(pMesh);
  46. D3DXComputeNormals(m_pMesh);
  47. LogInfo("<li>Mesh '%s' loaded OK", pFilename);
  48. }
  49. CMesh::~CMesh()
  50. {
  51. SafeDelete(m_pMeshMaterials);
  52. if(m_pMeshTextures != NULL)
  53. {
  54. for(DWORD i = 0; i < m_dwNumMaterials; i++)
  55. {
  56. if(m_pMeshTextures[i])
  57. {
  58. SafeRelease(m_pMeshTextures[i]);
  59. }
  60. }
  61. }
  62. SafeDelete(m_pMeshTextures);
  63. SafeRelease(m_pMesh);
  64.     
  65. LogInfo("<li>Mesh destroyed OK");
  66. }
  67. DWORD CMesh::Render()
  68. {
  69. if(m_pMesh != NULL)
  70. {
  71. for(DWORD i = 0; i < m_dwNumMaterials; i++)
  72. {
  73. m_pD3DDevice->SetMaterial(&m_pMeshMaterials[i]);
  74. m_pD3DDevice->SetTexture(0, m_pMeshTextures[i]);
  75.         
  76. m_pMesh->DrawSubset(i);
  77. }
  78. return m_pMesh->GetNumFaces();
  79. }
  80. else
  81. {
  82. return 0;
  83. }
  84. }