Mesh.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:3k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Mesh.h"
  3. CMesh::CMesh(LPDIRECT3DDEVICE9 d3dd,LPSTR str)
  4. {   m_pD3DDevice=d3dd;
  5. LPD3DXBUFFER pBuffer;
  6. D3DXMATERIAL *  pMat;
  7.     LPD3DXMESH pMesh = NULL;
  8. D3DXLoadMeshFromX(str,D3DXMESH_SYSTEMMEM,m_pD3DDevice,NULL,&pBuffer,NULL,&m_dwNumOfMaterials,&m_pMesh);
  9. pMat=(D3DXMATERIAL*) pBuffer->GetBufferPointer();
  10. m_pMaterial=new D3DMATERIAL9 [m_dwNumOfMaterials];
  11. m_pTexture=new LPDIRECT3DTEXTURE9 [m_dwNumOfMaterials];
  12. for(DWORD i=0;i<m_dwNumOfMaterials;i++)
  13. {
  14. m_pMaterial[i]=pMat[i].MatD3D;
  15. m_pMaterial[i].Ambient=m_pMaterial[i].Diffuse;
  16. D3DXCreateTextureFromFile(m_pD3DDevice,pMat[i].pTextureFilename,&m_pTexture[i]);
  17. }
  18. pBuffer->Release();
  19. //pMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, m_pD3DDevice, &m_pMesh);
  20. //SafeRelease(pMesh);
  21. /*DWORD * adjacencyInfo=new DWORD[m_pMesh->GetNumFaces()*3];
  22. m_pMesh->GenerateAdjacency(0.0f,adjacencyInfo);
  23. DWORD* optimizedAdjacencyInfo=new DWORD[m_pMesh->GetNumFaces()*3];
  24. m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_DONOTSPLIT,adjacencyInfo,optimizedAdjacencyInfo,0,0);*/
  25. D3DXComputeNormals(m_pMesh,NULL);
  26. }
  27. CMesh::~CMesh()
  28. {
  29.     if(m_pMaterial!=NULL)
  30. {
  31. delete [] m_pMaterial;
  32. }
  33. if(m_pTexture!=NULL)
  34. {
  35. for(DWORD i=0;i<m_dwNumOfMaterials;i++)
  36. {
  37. if(m_pTexture[i]!=NULL)
  38. {
  39. m_pTexture[i]->Release();
  40. }
  41. }
  42. delete [] m_pTexture;
  43. }
  44. m_pMesh->Release();
  45. }
  46. void CMesh::RenderMesh()
  47. {// m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   TRUE );
  48.     //m_pD3DDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
  49.    // m_pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  50.      // m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,TRUE);
  51. for(DWORD i=0;i<m_dwNumOfMaterials;i++)
  52. {
  53. m_pD3DDevice->SetMaterial(&m_pMaterial[i]);
  54. m_pD3DDevice->SetTexture(0,m_pTexture[i]);
  55.        
  56. m_pMesh->DrawSubset(i);
  57.        
  58. }
  59.      // m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,FALSE);
  60. }
  61. void CMesh::Translate(POSITION ppos)
  62. {
  63.     
  64. D3DXMATRIX matRotation,matTranslate;
  65. D3DXMatrixRotationY(&matRotation,-D3DX_PI/180*(ppos.a+90.0f));
  66. D3DXMatrixTranslation(&matTranslate,ppos.x,ppos.y+65.0f,ppos.z);
  67. D3DXMatrixMultiply(&matWorld,&matRotation,&matTranslate);
  68. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
  69. }
  70. void CMesh::Translation(NPOSITION ppos)
  71. {
  72.     D3DXMATRIX matRotation,matTranslate;
  73. D3DXMatrixRotationY(&matRotation,-D3DX_PI/180*(ppos.a+90.0f));
  74. D3DXMatrixTranslation(&matTranslate,ppos.x,ppos.y+65.0f,ppos.z);
  75. D3DXMatrixMultiply(&matWorld,&matRotation,&matTranslate);
  76. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
  77. }