home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / direct3d / skinnedmesh / skinnedmesh.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-22  |  9.5 KB  |  421 lines

  1. #pragma once
  2.  
  3. #ifndef __MVIEW_H__
  4. #define __MVIEW_H__
  5.  
  6.  
  7.  
  8. /*//////////////////////////////////////////////////////////////////////////////
  9. //
  10. // File: mview.h
  11. //
  12. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  13. //
  14. //
  15. //////////////////////////////////////////////////////////////////////////////*/
  16.  
  17. #define GXRELEASE(_p) do { if ((_p) != NULL) {(_p)->Release(); (_p) = NULL;} } while (0)
  18.  
  19. enum METHOD {
  20.     D3DNONINDEXED,
  21.     D3DINDEXED,
  22.     SOFTWARE,
  23.     NONE
  24. };
  25.  
  26. struct SMeshContainer
  27. {
  28.     ID3DXMesh *pMesh;
  29.     ID3DXMesh* pMeshHW;
  30.     ID3DXMesh* pMeshSW;
  31.     D3DMATERIAL8 *rgMaterials;
  32.     LPDIRECT3DTEXTURE8 *pTextures;
  33.     DWORD cpattr;
  34.     DWORD cMaterials;
  35.     DWORD iAttrSplit;
  36.  
  37.     SMeshContainer *pmcNext;
  38.  
  39.     char *szName;
  40.  
  41.     // Skin info
  42.     LPD3DXSKINMESH m_pSkinMesh;
  43.     D3DXATTRIBUTERANGE *m_pAttrTable;
  44.     D3DXMATRIX** m_pBoneMatrix;
  45.     LPD3DXBUFFER m_pBoneNamesBuf;
  46.     LPD3DXBUFFER m_pBoneOffsetBuf;
  47.     D3DXMATRIX* m_pBoneOffsetMat;
  48.     DWORD* m_rgiAdjacency;
  49.     DWORD m_numBoneComb;
  50.     DWORD m_maxFaceInfl;
  51.     LPD3DXBUFFER m_pBoneCombinationBuf;
  52.     METHOD  m_Method;
  53.  
  54.     SMeshContainer()
  55.         :pMesh(NULL),
  56.             pMeshHW(NULL),
  57.             pMeshSW(NULL),
  58.             rgMaterials(NULL),
  59.             pTextures(NULL),
  60.             cpattr(0),
  61.             iAttrSplit(0),
  62.             cMaterials(0),
  63.             pmcNext(NULL),
  64.             szName(NULL),
  65.             m_pSkinMesh(NULL),
  66.             m_pAttrTable(NULL),
  67.             m_pBoneMatrix(NULL),
  68.             m_pBoneNamesBuf(NULL),
  69.             m_pBoneOffsetBuf(NULL),
  70.             m_pBoneOffsetMat(NULL),
  71.             m_rgiAdjacency(NULL),
  72.             m_numBoneComb(0),
  73.             m_maxFaceInfl(0),
  74.             m_pBoneCombinationBuf(NULL),
  75.             m_Method(NONE)
  76.     {
  77.     }
  78.  
  79.     ~SMeshContainer()
  80.     {
  81.         delete []rgMaterials;
  82.  
  83.         if (pTextures)
  84.         {
  85.             for (DWORD i = 0; i < cMaterials; ++i)
  86.             {
  87.                 GXRELEASE(pTextures[i]);
  88.             }
  89.             delete []pTextures;
  90.         }
  91.  
  92.         GXRELEASE(pMesh);
  93.         GXRELEASE(pMeshHW);
  94.         GXRELEASE(pMeshSW);
  95.         GXRELEASE(m_pSkinMesh);
  96.         GXRELEASE(m_pBoneNamesBuf);
  97.         GXRELEASE(m_pBoneOffsetBuf);
  98.         GXRELEASE(m_pBoneCombinationBuf);
  99.  
  100.         delete[] m_pBoneMatrix;
  101.  
  102.         delete[] m_pAttrTable;
  103.  
  104.         delete []szName;
  105.  
  106.         delete[] m_rgiAdjacency;
  107.  
  108.         delete pmcNext;
  109.     }
  110. };
  111.  
  112. // X File formation rotate key
  113. struct SRotateKeyXFile
  114. {
  115.     DWORD            dwTime;
  116.     DWORD            dwFloats;    
  117.     float            w;
  118.     float            x;
  119.     float            y;
  120.     float            z;
  121. };
  122.  
  123. struct SScaleKeyXFile
  124. {
  125.     DWORD    dwTime;
  126.     DWORD    dwFloats;    
  127.     D3DXVECTOR3    vScale;    
  128. };
  129.  
  130.  
  131. struct SPositionKeyXFile
  132. {
  133.     DWORD    dwTime;
  134.     DWORD    dwFloats;    
  135.     D3DXVECTOR3    vPos;    
  136. };
  137.  
  138. struct SMatrixKeyXFile
  139. {
  140.     DWORD    dwTime;
  141.     DWORD    dwFloats;    
  142.     D3DXMATRIX    mat;    
  143. };
  144.  
  145. // in memory versions
  146.  
  147. struct SRotateKey
  148. {
  149.     DWORD            dwTime;
  150.     D3DXQUATERNION    quatRotate;    
  151. };
  152.  
  153. struct SPositionKey
  154. {
  155.     DWORD    dwTime;
  156.     D3DXVECTOR3    vPos;    
  157. };
  158.  
  159. struct SScaleKey
  160. {
  161.     DWORD    dwTime;
  162.     D3DXVECTOR3    vScale;    
  163. };
  164.  
  165. struct SMatrixKey
  166. {
  167.     DWORD    dwTime;
  168.     D3DXMATRIX    mat;    
  169. };
  170.  
  171. struct SFrame
  172. {
  173.     SMeshContainer *pmcMesh;
  174.     D3DXMATRIX matRot;
  175.     D3DXMATRIX matTrans;
  176.     D3DXMATRIX matRotOrig;
  177.     D3DXMATRIX matCombined;
  178.  
  179.     // animation information
  180.     SPositionKey *m_pPositionKeys;
  181.     UINT m_cPositionKeys;
  182.     SRotateKey *m_pRotateKeys;
  183.     UINT m_cRotateKeys;
  184.     SScaleKey *m_pScaleKeys;
  185.     UINT m_cScaleKeys;
  186.     SMatrixKey *m_pMatrixKeys;
  187.     UINT m_cMatrixKeys;
  188.  
  189.     SFrame *pframeAnimNext;
  190.     SFrame *pframeToAnimate;
  191.  
  192.     SFrame *pframeSibling;
  193.     SFrame *pframeFirstChild;
  194.  
  195.     bool bAnimationFrame;
  196.     char *szName;
  197.  
  198.     SFrame()
  199.         :
  200.             pmcMesh(NULL),
  201.             m_pPositionKeys(NULL),
  202.             m_cPositionKeys(0),
  203.             m_pScaleKeys(NULL),
  204.             m_cScaleKeys(0),
  205.             m_pRotateKeys(NULL),
  206.             m_cRotateKeys(0),
  207.             m_pMatrixKeys(NULL),
  208.             m_cMatrixKeys(0),
  209.             pframeAnimNext(NULL),
  210.             pframeToAnimate(NULL),
  211.             pframeSibling(NULL),
  212.             pframeFirstChild(NULL),
  213.             bAnimationFrame(false),
  214.             szName(NULL)
  215.     {
  216.         D3DXMatrixIdentity(&matRot);
  217.         D3DXMatrixIdentity(&matRotOrig);
  218.         D3DXMatrixIdentity(&matTrans);
  219.     }
  220.  
  221.     ~SFrame()
  222.     {
  223.         delete []szName;
  224.         delete pmcMesh;        
  225.         delete pframeFirstChild;
  226.         delete pframeSibling;
  227.  
  228.         delete []m_pPositionKeys;
  229.         delete []m_pRotateKeys;
  230.         delete []m_pScaleKeys;
  231.         delete []m_pMatrixKeys;
  232.  
  233.         // do NOT delete pframeAnimNext
  234.         // do NOT delete pframeToAnimate
  235.     }
  236.  
  237.     void SetTime(float fTime);
  238.  
  239.     SFrame *FindFrame(char *szFrame)
  240.     {
  241.         SFrame *pframe;
  242.  
  243.         if ((szName != NULL) && (strcmp(szName, szFrame) == 0))
  244.             return this;
  245.  
  246.         if (pframeFirstChild != NULL)
  247.         {
  248.             pframe = pframeFirstChild->FindFrame(szFrame);
  249.             if (pframe != NULL)
  250.                 return pframe;
  251.         }
  252.  
  253.         if (pframeSibling != NULL)
  254.         {
  255.             pframe = pframeSibling->FindFrame(szFrame);
  256.             if (pframe != NULL)
  257.                 return pframe;
  258.         }
  259.  
  260.         return NULL;
  261.     }
  262.  
  263.     void ResetMatrix()
  264.     {
  265.         matRot = matRotOrig;
  266.         D3DXMatrixIdentity(&matTrans);        
  267.  
  268.         if (pframeFirstChild != NULL)
  269.         {
  270.             pframeFirstChild->ResetMatrix();
  271.         }
  272.  
  273.         if (pframeSibling != NULL)
  274.         {
  275.             pframeSibling->ResetMatrix();
  276.         }
  277.     }
  278.  
  279.     void AddFrame(SFrame *pframe)
  280.     {
  281.         if (pframeFirstChild == NULL)
  282.         {
  283.             pframeFirstChild = pframe;
  284.         }
  285.         else
  286.         {
  287.             pframe->pframeSibling = pframeFirstChild->pframeSibling;
  288.             pframeFirstChild->pframeSibling = pframe;
  289.         }
  290.     }
  291.  
  292.     void AddMesh(SMeshContainer *pmc)
  293.     {
  294.         pmc->pmcNext = pmcMesh;
  295.         pmcMesh = pmc;
  296.     }
  297. };
  298.  
  299. struct SDrawElement
  300. {
  301.     SFrame *pframeRoot;
  302.  
  303.     D3DXVECTOR3 vCenter;
  304.     float fRadius;
  305.  
  306.     // name of element for selection purposes
  307.     char *szName;
  308.  
  309.     // animation list
  310.     SFrame *pframeAnimHead;
  311.  
  312.     // next element in list
  313.     SDrawElement *pdeNext;
  314.  
  315.     float fCurTime;
  316.     float fMaxTime;
  317.  
  318.     SDrawElement()
  319.         :vCenter(0.0,0.0,0.0),
  320.             fRadius(1.0),
  321.             szName(NULL),
  322.             pframeRoot(NULL),
  323.             pframeAnimHead(NULL),
  324.             pdeNext(NULL)
  325.     {
  326.     }
  327.  
  328.     ~SDrawElement()
  329.     {
  330.         delete pframeRoot;
  331.         delete pdeNext;
  332.         delete [] szName;
  333.  
  334.         // do NOT delete pframeAnimHead;
  335.     }
  336.  
  337.     void AddAnimationFrame(SFrame *pframeAnim)
  338.     {
  339.         pframeAnim->pframeAnimNext = pframeAnimHead;
  340.         pframeAnimHead = pframeAnim;
  341.     }
  342.  
  343.     SFrame *FindFrame(char *szName)
  344.     {
  345.         if (pframeRoot == NULL)
  346.             return NULL;
  347.         else
  348.             return pframeRoot->FindFrame(szName);
  349.     }
  350. };
  351.  
  352. HRESULT CalculateBoundingSphere(SDrawElement *pdeCur);
  353.  
  354.  
  355.  
  356. extern DWORD      g_dwNumDeviceTypes;
  357. //-----------------------------------------------------------------------------
  358. // Name: class CMyD3DApplication
  359. // Desc: Application class. The base class (CD3DApplication) provides the 
  360. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  361. //       adds functionality specific to this sample program.
  362. //-----------------------------------------------------------------------------
  363. class CMyD3DApplication : public CD3DApplication
  364. {
  365.     CD3DFont* m_pFont;
  366.  
  367.     METHOD m_method;
  368.  
  369.     DWORD m_dwFVF;
  370.  
  371.     CD3DArcBall   m_ArcBall;
  372.  
  373.     SMeshContainer *m_pmcSelectedMesh;
  374.     SFrame *m_pframeSelected;
  375.     SDrawElement *m_pdeSelected;
  376.     SDrawElement *m_pdeHead;
  377.  
  378.     TCHAR m_szPath[MAX_PATH];
  379.  
  380. protected:
  381.     HRESULT OneTimeSceneInit();
  382.     HRESULT InitDeviceObjects();
  383.     HRESULT RestoreDeviceObjects();
  384.     HRESULT InvalidateDeviceObjects();
  385.     HRESULT DeleteDeviceObjects();
  386.     HRESULT Render();
  387.     HRESULT FrameMove();
  388.     HRESULT FinalCleanup();
  389.     HRESULT ConfirmDevice(D3DCAPS8*,DWORD,D3DFORMAT);
  390.  
  391.     HRESULT FindBones(SFrame *pframeCur, SDrawElement *pde);
  392.     HRESULT LoadMeshHierarchy();
  393.     HRESULT LoadAnimationSet(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  394.                              DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  395.                              SFrame *pframeParent);
  396.     HRESULT LoadAnimation(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  397.                           DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  398.                           SFrame *pframeParent);
  399.     HRESULT LoadFrames(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  400.                        DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  401.                        SFrame *pframeParent);
  402.     HRESULT LoadMesh(LPDIRECTXFILEDATA pxofobjCur,
  403.                      DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  404.                      SFrame *pframeParent);
  405.     HRESULT SetProjectionMatrix();
  406.     HRESULT DeleteSelectedMesh();
  407.     HRESULT DrawMeshContainer(SMeshContainer *pmcMesh);
  408.     HRESULT UpdateFrames(SFrame *pframeCur, D3DXMATRIX &matCur);
  409.     HRESULT GenerateMesh(SMeshContainer* pmcMesh);
  410.     HRESULT DrawFrames(SFrame *pframeCur, UINT &cTriangles);
  411.  
  412. public:
  413.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  414.     CMyD3DApplication();
  415.  
  416. };
  417.  
  418.  
  419. #endif
  420.  
  421.