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

游戏

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////////////
  2. // SkinMesh.h: interface for the CSkinMesh class.
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5. #include <d3d9.h>
  6. #include <d3dx9.h>
  7. #include <windows.h>
  8. #include <mmsystem.h>
  9. #include "Game.h"
  10. #if !defined(AFX_SKINMESH_H)
  11. #define AFX_SKINMESH_H
  12. #if _MSC_VER > 1000
  13. #pragma once
  14. #endif // _MSC_VER > 1000
  15. #include <tchar.h>
  16. #include <d3dx9.h>
  17. #include <d3dx9mesh.h>
  18. #define SAFE_DELETE(p)       { if(p) { delete (p);     (p)=NULL; } }
  19. #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p);   (p)=NULL; } }
  20. #define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }
  21. //-----------------------------------------------------------------------------
  22. // Name: struct D3DXFRAME_DERIVED
  23. // Desc: Structure derived from D3DXFRAME so we can add some app-specific
  24. //       info that will be stored with each frame
  25. //-----------------------------------------------------------------------------
  26. struct D3DXFRAME_DERIVED: public D3DXFRAME
  27. {
  28.     D3DXMATRIXA16        CombinedTransformationMatrix;
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Name: struct D3DXMESHCONTAINER_DERIVED
  32. // Desc: Structure derived from D3DXMESHCONTAINER so we can add some app-specific
  33. //       info that will be stored with each mesh
  34. //-----------------------------------------------------------------------------
  35. struct D3DXMESHCONTAINER_DERIVED: public D3DXMESHCONTAINER
  36. {
  37.     LPDIRECT3DTEXTURE9*  ppTextures;       // array of textures, entries are NULL if no texture specified    
  38.                                 
  39.     // SkinMesh info             
  40.     LPD3DXMESH           pOrigMesh;
  41.     LPD3DXATTRIBUTERANGE pAttributeTable;
  42.     DWORD                NumAttributeGroups; 
  43.     DWORD                NumInfl;
  44.     LPD3DXBUFFER         pBoneCombinationBuf;
  45.     D3DXMATRIX**         ppBoneMatrixPtrs;
  46.     D3DXMATRIX*          pBoneOffsetMatrices;
  47.     DWORD                NumPaletteEntries;
  48.     bool                 UseSoftwareVP;
  49.     DWORD                iAttributeSW;     // used to denote the split between SW and HW if necessary for non-indexed skinning
  50. };
  51. // enum for various skinning modes possible
  52. enum METHOD 
  53. {
  54.     D3DNONINDEXED,
  55.     D3DINDEXED,
  56.     SOFTWARE,
  57.     NONE
  58. };
  59. class CSkinMesh;
  60. //-----------------------------------------------------------------------------
  61. // Name: class CAllocateHierarchy
  62. // Desc: Custom version of ID3DXAllocateHierarchy with custom methods to create
  63. //       frames and meshcontainers.
  64. //-----------------------------------------------------------------------------
  65. //这里是Directx9.0a的
  66. class CAllocateHierarchy: public ID3DXAllocateHierarchy
  67. {
  68. public:
  69.     STDMETHOD(CreateFrame)(THIS_ LPCTSTR Name, LPD3DXFRAME *ppNewFrame);
  70. #if (D3D_SDK_VERSION & 31) //这里是Directx9.0b的
  71.     STDMETHOD(CreateMeshContainer)(THIS_ LPCTSTR Name, 
  72. LPD3DXMESHDATA pMeshData,
  73.                             LPD3DXMATERIAL pMaterials, 
  74. LPD3DXEFFECTINSTANCE pEffectInstances, 
  75. DWORD NumMaterials, 
  76.                             DWORD *pAdjacency, LPD3DXSKININFO pSkinInfo, 
  77.                             LPD3DXMESHCONTAINER *ppNewMeshContainer);
  78. #else //这里是Directx9.0c的
  79.     STDMETHOD(CreateMeshContainer)(THIS_ 
  80.         LPCSTR Name, 
  81.         CONST D3DXMESHDATA *pMeshData,
  82.         CONST D3DXMATERIAL *pMaterials, 
  83.         CONST D3DXEFFECTINSTANCE *pEffectInstances, 
  84.         DWORD NumMaterials, 
  85.         CONST DWORD *pAdjacency, 
  86.         LPD3DXSKININFO pSkinInfo, 
  87.         LPD3DXMESHCONTAINER *ppNewMeshContainer);
  88. #endif
  89.     STDMETHOD(DestroyFrame)(THIS_ LPD3DXFRAME pFrameToFree);
  90.     STDMETHOD(DestroyMeshContainer)(THIS_ LPD3DXMESHCONTAINER pMeshContainerBase);
  91.     CAllocateHierarchy(CSkinMesh *pSkinMesh) :m_pSkinMesh(pSkinMesh) {}
  92. public:
  93. CSkinMesh* m_pSkinMesh;
  94. };
  95. //这里是DirectX9.0C的
  96. //蒙皮动画
  97. class CSkinMesh  
  98. {
  99. public:
  100.     void Translation(POSITION ppos);
  101.      LPD3DXMESH pMesh;
  102. D3DXMATRIX matWorld;
  103. VOID SetAnim(BOOL bAnim);
  104. BOOL CSkinMesh::InterSect( D3DVECTOR *pRayOrig,
  105.  D3DVECTOR *pRayDir,D3DVECTOR* pVRet);
  106. VOID UpdateFrameMatrices( LPD3DXFRAME pFrameBase, LPD3DXMATRIX pParentMatrix );
  107. LPDIRECT3DVERTEXSHADER9 m_pIndexedVertexShader[4];
  108. D3DXMATRIXA16* m_pBoneMatrices;
  109. UINT m_NumBoneMatricesMax;
  110. METHOD m_SkinningMethod;
  111. D3DCAPS9  m_d3dCaps;
  112.     D3DXVECTOR3                 m_vObjectCenter;    // Center of bounding sphere of object
  113.     FLOAT                       m_fObjectRadius;    // Radius of bounding sphere of object
  114. D3DXVECTOR3                 m_vMin;    // Center of bounding sphere of object
  115. D3DXVECTOR3                 m_vMax;    // Center of bounding sphere of object
  116. LPD3DXANIMATIONCONTROLLER GetAnimController()
  117. {return m_pAnimController;};
  118. BOOL SetAnimationName(char *strAnimName);
  119. // void SetPosition(D3DXVECTOR3 &vPos) {m_vPos=vPos; };
  120. // void SetPosition(float x,float y,float z) {m_vPos.x=x;m_vPos.y=y;m_vPos.z=z;};
  121. VOID Render(float fTimeFromLastFrame,D3DXVECTOR3 vPos,float angle,float scale=1.0f);
  122. HRESULT LoadFromXFile(char* strFileName);
  123. HRESULT GenerateSkinnedMesh(D3DXMESHCONTAINER_DERIVED *pMeshContainer);
  124. CSkinMesh(LPDIRECT3DDEVICE9 pD3DDevice);
  125. // CSkinMesh(LPDIRECT3DDEVICE9 pD3DDevice,char* strFileName);
  126.    
  127. HRESULT CalculateBondingBox(LPD3DXFRAME pFrameParent, D3DXVECTOR3 *pVmin,D3DXVECTOR3 *pVmax)
  128. {
  129. static LPVOID pV; 
  130. //static LPD3DXMESH pMesh;
  131. static LPD3DXMATRIX pMat;
  132. static D3DXVECTOR3 vMin,vMax; 
  133. static D3DXVECTOR3 vTransFormed;
  134. if(pFrameParent->pMeshContainer)
  135. {
  136. pMesh=pFrameParent->pMeshContainer->MeshData.pMesh;    
  137. pMat=&(((D3DXFRAME_DERIVED*)pFrameParent)->CombinedTransformationMatrix);
  138. pMesh->LockVertexBuffer(0,&pV); 
  139. D3DXComputeBoundingBox((LPD3DXVECTOR3)pV,pMesh->GetNumVertices(),
  140. pMesh->GetNumBytesPerVertex(),&vMin,&vMax); 
  141. vTransFormed.x=pMat->_11*vMin.x+pMat->_21*vMin.y+pMat->_31*vMin.z+pMat->_41;
  142. vTransFormed.y=pMat->_12*vMin.x+pMat->_22*vMin.y+pMat->_32*vMin.z+pMat->_42;
  143. vTransFormed.z=pMat->_13*vMin.x+pMat->_23*vMin.y+pMat->_33*vMin.z+pMat->_43;
  144. if(vTransFormed.x<pVmin->x) pVmin->x=vTransFormed.x;
  145. if(vTransFormed.y<pVmin->y) pVmin->y=vTransFormed.y;
  146. if(vTransFormed.z<pVmin->z) pVmin->z=vTransFormed.z;
  147. vTransFormed.x=pMat->_11*vMax.x+pMat->_21*vMax.y+pMat->_31*vMax.z+pMat->_41;
  148. vTransFormed.y=pMat->_12*vMax.x+pMat->_22*vMax.y+pMat->_32*vMax.z+pMat->_42;
  149. vTransFormed.z=pMat->_13*vMax.x+pMat->_23*vMax.y+pMat->_33*vMax.z+pMat->_43;
  150. if(vTransFormed.x>pVmax->x) pVmax->x=vTransFormed.x;
  151. if(vTransFormed.y>pVmax->y) pVmax->y=vTransFormed.y;
  152. if(vTransFormed.z>pVmax->z) pVmax->z=vTransFormed.z;
  153. pMesh->UnlockVertexBuffer(); 
  154. }
  155. //else return S_OK;
  156. if(pFrameParent->pFrameSibling)
  157. CalculateBondingBox(pFrameParent->pFrameSibling,pVmin,pVmax);
  158. if(pFrameParent->pFrameFirstChild)
  159. CalculateBondingBox(pFrameParent->pFrameFirstChild,pVmin,pVmax);
  160. return S_OK;
  161. }
  162. virtual ~CSkinMesh();
  163. protected:
  164. // FLOAT m_fRotateAngle;
  165. BOOL m_bMoving;
  166. LPDIRECT3DDEVICE9 m_pD3DDevice;
  167. LPD3DXFRAME m_pFrameRoot;
  168. LPD3DXANIMATIONCONTROLLER m_pAnimController;
  169.     FLOAT m_fElapsedTime;      // Time elapsed since last frame
  170. VOID DrawMeshContainer(LPD3DXMESHCONTAINER pMeshContainerBase, LPD3DXFRAME pFrameBase);
  171. VOID DrawFrame(LPD3DXFRAME pFrame);
  172. HRESULT SetupBoneMatrixPointersOnMesh( LPD3DXMESHCONTAINER pMeshContainerBase );
  173. HRESULT SetupBoneMatrixPointers( LPD3DXFRAME pFrame );
  174. };
  175. #endif // !defined(AFX_SKINMESH_H__AE3ADAAF_4BE2_4B23_9A1D_9379FA1BAFD9__INCLUDED_)