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

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "npc.h"
  3. CNpc::CNpc(LPDIRECT3DDEVICE9 d3dd,LPSTR str)
  4. {   
  5. //pos=poss;
  6. m_pD3DDevice=d3dd;
  7. LPD3DXBUFFER pBuffer;
  8. D3DXMATERIAL *  pMat;
  9.     //LPD3DXNpc pNpc = NULL;
  10. D3DXLoadMeshFromX(str,D3DXMESH_SYSTEMMEM,m_pD3DDevice,NULL,&pBuffer,NULL,&m_dwNumOfMaterials,&m_pNpc);
  11. pMat=(D3DXMATERIAL*) pBuffer->GetBufferPointer();
  12. m_pMaterial=new D3DMATERIAL9 [m_dwNumOfMaterials];
  13. m_pTexture=new LPDIRECT3DTEXTURE9 [m_dwNumOfMaterials];
  14. for(DWORD i=0;i<m_dwNumOfMaterials;i++)
  15. {
  16. m_pMaterial[i]=pMat[i].MatD3D;
  17. m_pMaterial[i].Ambient=m_pMaterial[i].Diffuse;
  18. D3DXCreateTextureFromFile(m_pD3DDevice,pMat[i].pTextureFilename,&m_pTexture[i]);
  19. }
  20. pBuffer->Release();
  21. //pNpc->CloneNpcFVF(D3DXNpc_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, m_pD3DDevice, &m_pMesh);
  22. //SafeRelease(pMesh);
  23.     DWORD * adjacencyInfo=new DWORD[m_pNpc->GetNumFaces()*3];
  24. m_pNpc->GenerateAdjacency(0.0f,adjacencyInfo);
  25. DWORD* optimizedAdjacencyInfo=new DWORD[m_pNpc->GetNumFaces()*3];
  26. m_pNpc->OptimizeInplace(D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_COMPACT|D3DXMESHOPT_VERTEXCACHE,adjacencyInfo,optimizedAdjacencyInfo,0,0);
  27. D3DXComputeNormals(m_pNpc,NULL);
  28. }
  29. CNpc::~CNpc()
  30. {
  31.     if(m_pMaterial!=NULL)
  32. {
  33. delete [] m_pMaterial;
  34. }
  35. if(m_pTexture!=NULL)
  36. {
  37. for(DWORD i=0;i<m_dwNumOfMaterials;i++)
  38. {
  39. if(m_pTexture[i]!=NULL)
  40. {
  41. m_pTexture[i]->Release();
  42. }
  43. }
  44. delete [] m_pTexture;
  45. }
  46. m_pNpc->Release();
  47. }
  48. void CNpc::RenderNpc()
  49. {
  50. for(DWORD i=0;i<m_dwNumOfMaterials;i++)
  51. {
  52. m_pD3DDevice->SetMaterial(&m_pMaterial[i]);
  53. m_pD3DDevice->SetTexture(0,m_pTexture[i]);
  54. m_pNpc->DrawSubset(i);
  55. }
  56. }
  57. void CNpc::Translate()
  58. {
  59.    
  60. D3DXMATRIX matWorld,matRotation;
  61. D3DXMatrixTranslation(&matRotation,pos.x,pos.y,pos.z);
  62. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matRotation);
  63. }
  64. void CNpc::Translation(float x,float y,float z,float a,float k)
  65. {
  66. float aa=D3DX_PI/180*a;
  67. D3DXMATRIX matRotate,matTrans ,matScal;
  68. D3DXMatrixTranslation(&matTrans,x,y,z);
  69. D3DXMatrixRotationY(&matRotate,aa);
  70. D3DXMatrixScaling(&matScal,k,k,k);
  71. D3DXMatrixMultiply(&matWorld,&matRotate,&matScal);
  72. D3DXMatrixMultiply(&matWorld,&matWorld,&matTrans);
  73. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
  74. }