home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / direct3d / skinnedmesh / mload.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-22  |  43.9 KB  |  1,621 lines

  1. //-----------------------------------------------------------------------------
  2. // File: mload.cpp
  3. //
  4. // Copyright (c) 1999-2000 Microsoft Corporation. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6.  
  7. #include <windows.h>
  8. #include <mmsystem.h>
  9. #include <objbase.h>
  10. #include <malloc.h> // _alloca
  11. #include <stdio.h>
  12. #include <d3d8.h>
  13. #include <d3dx8.h>
  14. #include "resource.h"
  15. #include "D3DApp.h"
  16. #include "D3DFont.h"
  17. #include "D3DUtil.h"
  18. #include "DXUtil.h"
  19. #include "D3DRes.h"
  20. #include "rmxfguid.h"
  21. #include "rmxftmpl.h"
  22. #include "dxutil.h"
  23. #include "SkinnedMesh.h"
  24.  
  25.  
  26. HRESULT CalculateSum(SFrame *pframe, D3DXMATRIX *pmatCur, D3DXVECTOR3 *pvCenter, 
  27.                      UINT *pcVertices)
  28. {
  29.     HRESULT hr = S_OK;
  30.     PBYTE pbPoints = NULL;
  31.     UINT cVerticesLocal = 0;
  32.     PBYTE pbCur;
  33.     D3DXVECTOR3 *pvCur;
  34.     D3DXVECTOR3 vTransformedCur;
  35.     UINT iPoint;
  36.     SMeshContainer *pmcCur;
  37.     SFrame *pframeCur;
  38.     UINT cVertices;
  39.     D3DXMATRIX matLocal;
  40.     
  41.     D3DXMatrixMultiply(&matLocal, &pframe->matRot, pmatCur);
  42.     
  43.     pmcCur = pframe->pmcMesh;
  44.     while (pmcCur != NULL)
  45.     {
  46.         DWORD fvfsize = D3DXGetFVFVertexSize(pmcCur->pMesh->GetFVF());
  47.         
  48.         cVertices = pmcCur->pMesh->GetNumVertices();
  49.         
  50.         hr = pmcCur->pMesh->LockVertexBuffer(0, &pbPoints);
  51.         if (FAILED(hr))
  52.             goto e_Exit;
  53.         
  54.         for( iPoint=0, pbCur = pbPoints; iPoint < cVertices; iPoint++, pbCur += fvfsize)
  55.         {
  56.             pvCur = (D3DXVECTOR3*)pbCur;
  57.             
  58.             if ((pvCur->x != 0.0) || (pvCur->y != 0.0) || (pvCur->z != 0.0))
  59.             {
  60.                 cVerticesLocal++;
  61.                 
  62.                 D3DXVec3TransformCoord(&vTransformedCur, pvCur, &matLocal);
  63.                 
  64.                 pvCenter->x += vTransformedCur.x;
  65.                 pvCenter->y += vTransformedCur.y;
  66.                 pvCenter->z += vTransformedCur.z;
  67.             }
  68.         }
  69.         
  70.         
  71.         pmcCur->pMesh->UnlockVertexBuffer();
  72.         pbPoints = NULL;
  73.         
  74.         pmcCur = pmcCur->pmcNext;
  75.     }
  76.     
  77.     *pcVertices += cVerticesLocal;
  78.     
  79.     pframeCur = pframe->pframeFirstChild;
  80.     while (pframeCur != NULL)
  81.     {
  82.         hr = CalculateSum(pframeCur, &matLocal, pvCenter, pcVertices);
  83.         if (FAILED(hr))
  84.             goto e_Exit;
  85.         
  86.         pframeCur = pframeCur->pframeSibling;
  87.     }
  88.     
  89. e_Exit:
  90.     if (pbPoints != NULL)
  91.     {
  92.         pmcCur->pMesh->UnlockVertexBuffer();
  93.     }
  94.     
  95.     return hr;
  96. }
  97.  
  98.  
  99.  
  100.  
  101. HRESULT CalculateRadius(SFrame *pframe, D3DXMATRIX *pmatCur, D3DXVECTOR3 *pvCenter, 
  102.                         float *pfRadiusSq)
  103. {
  104.     HRESULT hr = S_OK;
  105.     PBYTE pbPoints = NULL;
  106.     PBYTE pbCur;
  107.     D3DXVECTOR3 *pvCur;
  108.     D3DXVECTOR3 vDist;;
  109.     UINT iPoint;
  110.     UINT cVertices;
  111.     SMeshContainer *pmcCur;
  112.     SFrame *pframeCur;
  113.     float fRadiusLocalSq;
  114.     float fDistSq;
  115.     D3DXMATRIX matLocal;
  116.     
  117.     D3DXMatrixMultiply(&matLocal, &pframe->matRot, pmatCur);
  118.     
  119.     pmcCur = pframe->pmcMesh;
  120.     fRadiusLocalSq = *pfRadiusSq;
  121.     while (pmcCur != NULL)
  122.     {
  123.         DWORD fvfsize = D3DXGetFVFVertexSize(pmcCur->pMesh->GetFVF());
  124.         
  125.         cVertices = pmcCur->pMesh->GetNumVertices();
  126.         
  127.         hr = pmcCur->pMesh->LockVertexBuffer(0, &pbPoints);
  128.         if (FAILED(hr))
  129.             goto e_Exit;
  130.         
  131.         for( iPoint=0, pbCur = pbPoints; iPoint < cVertices; iPoint++, pbCur += fvfsize )
  132.         {
  133.             pvCur = (D3DXVECTOR3*)pbCur;
  134.             
  135.             if ((pvCur->x == 0.0) && (pvCur->y == 0.0) && (pvCur->z == 0.0))
  136.                 continue;
  137.             
  138.             D3DXVec3TransformCoord(&vDist, pvCur, &matLocal);
  139.             
  140.             vDist -= *pvCenter;
  141.             
  142.             fDistSq = D3DXVec3LengthSq(&vDist);
  143.             
  144.             if( fDistSq > fRadiusLocalSq )
  145.                 fRadiusLocalSq = fDistSq;
  146.         }
  147.         
  148.         
  149.         pmcCur->pMesh->UnlockVertexBuffer();
  150.         pbPoints = NULL;
  151.         
  152.         pmcCur = pmcCur->pmcNext;
  153.     }
  154.     
  155.     *pfRadiusSq = fRadiusLocalSq;
  156.     
  157.     pframeCur = pframe->pframeFirstChild;
  158.     while (pframeCur != NULL)
  159.     {
  160.         hr = CalculateRadius(pframeCur, &matLocal, pvCenter, pfRadiusSq);
  161.         if (FAILED(hr))
  162.             goto e_Exit;
  163.         
  164.         pframeCur = pframeCur->pframeSibling;
  165.     }
  166.     
  167. e_Exit:
  168.     if (pbPoints != NULL)
  169.     {
  170.         pmcCur->pMesh->UnlockVertexBuffer();
  171.     }
  172.     
  173.     return hr;
  174. }
  175.  
  176.  
  177.  
  178.  
  179. HRESULT CalculateBoundingSphere(SDrawElement *pdeCur)
  180. {
  181.     HRESULT hr = S_OK;
  182.     D3DXVECTOR3 vCenter(0,0,0);
  183.     UINT cVertices = 0;
  184.     float fRadiusSq = 0;
  185.     D3DXMATRIX matCur;
  186.     
  187.     D3DXMatrixIdentity(&matCur);
  188.     hr = CalculateSum(pdeCur->pframeRoot, &matCur, &vCenter, &cVertices);
  189.     if (FAILED(hr))
  190.         goto e_Exit;
  191.     
  192.     if (cVertices > 0)
  193.     {
  194.         vCenter /= (float)cVertices;
  195.         
  196.         D3DXMatrixIdentity(&matCur);
  197.         hr = CalculateRadius(pdeCur->pframeRoot, &matCur, &vCenter, &fRadiusSq);
  198.         if (FAILED(hr))
  199.             goto e_Exit;
  200.     }
  201.     
  202.     pdeCur->fRadius = (float)sqrt((double)fRadiusSq);;
  203.     pdeCur->vCenter = vCenter;
  204. e_Exit:
  205.     return hr;
  206. }
  207.  
  208.  
  209.  
  210.  
  211. HRESULT CMyD3DApplication::FindBones(SFrame *pframeCur, SDrawElement *pde)
  212. {
  213.     HRESULT hr = S_OK;
  214.     SMeshContainer *pmcMesh;
  215.     SFrame *pframeChild;
  216.     
  217.     pmcMesh = pframeCur->pmcMesh;
  218.     while (pmcMesh != NULL)
  219.     {
  220.         if (pmcMesh->m_pSkinMesh)
  221.         {
  222.             char** pBoneName = static_cast<char**>(pmcMesh->m_pBoneNamesBuf->GetBufferPointer());
  223.             for (DWORD i = 0; i < pmcMesh->m_pSkinMesh->GetNumBones(); ++i)
  224.             {
  225.                 SFrame* pFrame = pde->FindFrame(pBoneName[i]);
  226.                 pmcMesh->m_pBoneMatrix[i] = &(pFrame->matCombined);
  227.             }
  228.         }
  229.         pmcMesh = pmcMesh->pmcNext;
  230.     }
  231.     
  232.     pframeChild = pframeCur->pframeFirstChild;
  233.     while (pframeChild != NULL)
  234.     {
  235.         hr = FindBones(pframeChild, pde);
  236.         if (FAILED(hr))
  237.             return hr;
  238.         
  239.         pframeChild = pframeChild->pframeSibling;
  240.     }
  241.     
  242.     return S_OK;
  243. }
  244.  
  245.  
  246.  
  247.  
  248. HRESULT CMyD3DApplication::LoadMeshHierarchy()
  249. {
  250.     TCHAR* pszFile = m_szPath;
  251.     SDrawElement *pdeMesh = NULL;
  252.     HRESULT hr = S_OK;
  253.     LPDIRECTXFILE pxofapi = NULL;
  254.     LPDIRECTXFILEENUMOBJECT pxofenum = NULL;
  255.     LPDIRECTXFILEDATA pxofobjCur = NULL;
  256.     DWORD dwOptions;
  257.     int cchFileName;
  258.  
  259.     if (pszFile == NULL)
  260.         return E_INVALIDARG;
  261.     
  262.     pdeMesh = new SDrawElement();
  263.     
  264.     delete pdeMesh->pframeRoot;
  265.     pdeMesh->pframeAnimHead = NULL;
  266.     
  267.     pdeMesh->pframeRoot = new SFrame();
  268.     if (pdeMesh->pframeRoot == NULL)
  269.     {
  270.         hr = E_OUTOFMEMORY;
  271.         goto e_Exit;
  272.     }
  273.     
  274.     
  275.     dwOptions = 0;
  276.     
  277.     cchFileName = strlen(pszFile);
  278.     if (cchFileName < 2)
  279.     {
  280.         hr = E_FAIL;
  281.         goto e_Exit;
  282.     }
  283.     
  284.     hr = DirectXFileCreate(&pxofapi);
  285.     if (FAILED(hr))
  286.         goto e_Exit;
  287.     
  288.     // Register templates for d3drm.
  289.     hr = pxofapi->RegisterTemplates((LPVOID)D3DRM_XTEMPLATES,
  290.         D3DRM_XTEMPLATE_BYTES);
  291.     if (FAILED(hr))
  292.         goto e_Exit;
  293.     
  294.     // Create enum object.
  295.     hr = pxofapi->CreateEnumObject((LPVOID)pszFile,
  296.         DXFILELOAD_FROMFILE,
  297.         &pxofenum);
  298.     if (FAILED(hr))
  299.         goto e_Exit;
  300.     
  301.     
  302.     // Enumerate top level objects.
  303.     // Top level objects are always data object.
  304.     while (SUCCEEDED(pxofenum->GetNextDataObject(&pxofobjCur)))
  305.     {
  306.         hr = LoadFrames(pxofobjCur, pdeMesh, dwOptions, m_dwFVF,
  307.             m_pd3dDevice,
  308.             pdeMesh->pframeRoot);
  309.         GXRELEASE(pxofobjCur);
  310.         
  311.         if (FAILED(hr))
  312.             goto e_Exit;
  313.     }
  314.     
  315.     hr = FindBones(pdeMesh->pframeRoot, pdeMesh);
  316.     if (FAILED(hr))
  317.         goto e_Exit;
  318.     
  319.     
  320.     delete []pdeMesh->szName;
  321.     pdeMesh->szName = new char[cchFileName+1];
  322.     if (pdeMesh->szName == NULL)
  323.     {
  324.         hr = E_OUTOFMEMORY;
  325.         goto e_Exit;
  326.     }
  327.     memcpy(pdeMesh->szName, pszFile, cchFileName+1);
  328.  
  329.     // delete the current mesh, now that the load has succeeded
  330.     DeleteSelectedMesh();
  331.  
  332.     // link into the draw list
  333.     pdeMesh->pdeNext = m_pdeHead;
  334.     m_pdeHead = pdeMesh;
  335.     
  336.     m_pdeSelected = pdeMesh;
  337.     m_pmcSelectedMesh = pdeMesh->pframeRoot->pmcMesh;
  338.     
  339.     
  340.     m_pframeSelected = pdeMesh->pframeRoot;
  341.     
  342.     hr = CalculateBoundingSphere(pdeMesh);
  343.     if (FAILED(hr))
  344.         goto e_Exit;
  345.     
  346.     SetProjectionMatrix();
  347.     
  348.     m_pdeSelected->fCurTime = 0.0f;
  349.     m_pdeSelected->fMaxTime = 200.0f;
  350.     
  351.     D3DXMatrixTranslation(&m_pdeSelected->pframeRoot->matRot,
  352.         -pdeMesh->vCenter.x, -pdeMesh->vCenter.y, -pdeMesh->vCenter.z);
  353.     m_pdeSelected->pframeRoot->matRotOrig = m_pdeSelected->pframeRoot->matRot;
  354.     
  355. e_Exit:
  356.     GXRELEASE(pxofobjCur);
  357.     GXRELEASE(pxofenum);
  358.     GXRELEASE(pxofapi);
  359.     
  360.     if (FAILED(hr))
  361.     {
  362.         delete pdeMesh;
  363.     }
  364.     
  365.     return hr;
  366. }
  367.  
  368.  
  369.  
  370.  
  371. HRESULT CMyD3DApplication::LoadAnimation(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  372.                                          DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  373.                                          SFrame *pframeParent)
  374. {
  375.     HRESULT hr = S_OK;
  376.     SRotateKeyXFile *pFileRotateKey;
  377.     SScaleKeyXFile *pFileScaleKey;
  378.     SPositionKeyXFile *pFilePosKey;
  379.     SMatrixKeyXFile *pFileMatrixKey;
  380.     SFrame *pframeCur;
  381.     LPDIRECTXFILEDATA pxofobjChild = NULL;
  382.     LPDIRECTXFILEOBJECT pxofChild = NULL;
  383.     LPDIRECTXFILEDATAREFERENCE pxofobjChildRef = NULL;
  384.     const GUID *type;
  385.     DWORD dwSize;
  386.     PBYTE pData;
  387.     DWORD dwKeyType;
  388.     DWORD cKeys;
  389.     DWORD iKey;
  390.     DWORD cchName;
  391.     char *szFrameName;
  392.     
  393.     pframeCur = new SFrame();
  394.     if (pframeCur == NULL)
  395.     {
  396.         hr = E_OUTOFMEMORY;
  397.         goto e_Exit;
  398.     }
  399.     pframeCur->bAnimationFrame = true;
  400.     
  401.     pframeParent->AddFrame(pframeCur);
  402.     pde->AddAnimationFrame(pframeCur);
  403.     
  404.     // Enumerate child objects.
  405.     // Child object can be data, data reference or binary.
  406.     // Use QueryInterface() to find what type of object a child is.
  407.     while (SUCCEEDED(pxofobjCur->GetNextObject(&pxofChild)))
  408.     {
  409.         // Query the child for it's FileDataReference
  410.         hr = pxofChild->QueryInterface(IID_IDirectXFileDataReference,
  411.             (LPVOID *)&pxofobjChildRef);
  412.         if (SUCCEEDED(hr))
  413.         {
  414.             hr = pxofobjChildRef->Resolve(&pxofobjChild);
  415.             if (SUCCEEDED(hr))
  416.             {
  417.                 hr = pxofobjChild->GetType(&type);
  418.                 if (FAILED(hr))
  419.                     goto e_Exit;
  420.                 
  421.                 if( TID_D3DRMFrame == *type )
  422.                 {
  423.                     if (pframeCur->pframeToAnimate != NULL)
  424.                     {
  425.                         hr = E_INVALIDARG;
  426.                         goto e_Exit;
  427.                     }
  428.                     
  429.                     hr = pxofobjChild->GetName(NULL, &cchName);
  430.                     if (FAILED(hr))
  431.                         goto e_Exit;
  432.                     
  433.                     if (cchName == 0)
  434.                     {
  435.                         hr = E_INVALIDARG;
  436.                         goto e_Exit;
  437.                         
  438.                     }
  439.                     
  440.                     szFrameName = (char*)_alloca(cchName);
  441.                     if (szFrameName == NULL)
  442.                     {
  443.                         hr = E_OUTOFMEMORY;
  444.                         goto e_Exit;
  445.                     }
  446.                     
  447.                     hr = pxofobjChild->GetName(szFrameName, &cchName);
  448.                     if (FAILED(hr))
  449.                         goto e_Exit;
  450.                     
  451.                     pframeCur->pframeToAnimate = pde->FindFrame(szFrameName);
  452.                     if (pframeCur->pframeToAnimate == NULL)
  453.                     {
  454.                         hr = E_INVALIDARG;
  455.                         goto e_Exit;
  456.                     }
  457.                 }
  458.                 
  459.                 GXRELEASE(pxofobjChild);
  460.             }
  461.             
  462.             GXRELEASE(pxofobjChildRef);
  463.         }
  464.         else
  465.         {
  466.             
  467.             // Query the child for it's FileData
  468.             hr = pxofChild->QueryInterface(IID_IDirectXFileData,
  469.                 (LPVOID *)&pxofobjChild);
  470.             if (SUCCEEDED(hr))
  471.             {
  472.                 hr = pxofobjChild->GetType(&type);
  473.                 if (FAILED(hr))
  474.                     goto e_Exit;
  475.                 
  476.                 if ( TID_D3DRMFrame == *type )
  477.                 {
  478.                     hr = LoadFrames(pxofobjChild, pde, options, fvf, pD3DDevice, pframeCur);
  479.                     if (FAILED(hr))
  480.                         goto e_Exit;
  481.                 }
  482.                 else if ( TID_D3DRMAnimationOptions == *type )
  483.                 {
  484.                     //ParseAnimOptions(pChildData,pParentFrame);
  485.                     //i=2;
  486.                 }
  487.                 else if ( TID_D3DRMAnimationKey == *type )
  488.                 {
  489.                     hr = pxofobjChild->GetData( NULL, &dwSize, (PVOID*)&pData );
  490.                     if (FAILED(hr))
  491.                         goto e_Exit;
  492.                     
  493.                     dwKeyType = ((DWORD*)pData)[0];
  494.                     cKeys = ((DWORD*)pData)[1];
  495.                     
  496.                     if (dwKeyType == 0)
  497.                     {
  498.                         if (pframeCur->m_pRotateKeys != NULL)
  499.                         {
  500.                             hr = E_INVALIDARG;
  501.                             goto e_Exit;
  502.                         }
  503.                         
  504.                         pframeCur->m_pRotateKeys = new SRotateKey[cKeys];
  505.                         if (pframeCur->m_pRotateKeys == NULL)
  506.                         {
  507.                             hr = E_OUTOFMEMORY;
  508.                             goto e_Exit;
  509.                         }
  510.                         
  511.                         pframeCur->m_cRotateKeys = cKeys;
  512.                         //NOTE x files are w x y z and QUATERNIONS are x y z w
  513.                         
  514.                         pFileRotateKey =  (SRotateKeyXFile*)(pData + (sizeof(DWORD) * 2));
  515.                         for (iKey = 0;iKey < cKeys; iKey++)
  516.                         {
  517.                             pframeCur->m_pRotateKeys[iKey].dwTime = pFileRotateKey->dwTime;
  518.                             pframeCur->m_pRotateKeys[iKey].quatRotate.x = pFileRotateKey->x;
  519.                             pframeCur->m_pRotateKeys[iKey].quatRotate.y = pFileRotateKey->y;
  520.                             pframeCur->m_pRotateKeys[iKey].quatRotate.z = pFileRotateKey->z;
  521.                             pframeCur->m_pRotateKeys[iKey].quatRotate.w = pFileRotateKey->w;
  522.                             
  523.                             pFileRotateKey += 1;
  524.                         }
  525.                     }
  526.                     else if (dwKeyType == 1)
  527.                     {
  528.                         if (pframeCur->m_pScaleKeys != NULL)
  529.                         {
  530.                             hr = E_INVALIDARG;
  531.                             goto e_Exit;
  532.                         }
  533.                         
  534.                         pframeCur->m_pScaleKeys = new SScaleKey[cKeys];
  535.                         if (pframeCur->m_pScaleKeys == NULL)
  536.                         {
  537.                             hr = E_OUTOFMEMORY;
  538.                             goto e_Exit;
  539.                         }
  540.                         
  541.                         pframeCur->m_cScaleKeys = cKeys;
  542.                         
  543.                         pFileScaleKey =  (SScaleKeyXFile*)(pData + (sizeof(DWORD) * 2));
  544.                         for (iKey = 0;iKey < cKeys; iKey++)
  545.                         {
  546.                             pframeCur->m_pScaleKeys[iKey].dwTime = pFileScaleKey->dwTime;
  547.                             pframeCur->m_pScaleKeys[iKey].vScale = pFileScaleKey->vScale;
  548.                             
  549.                             pFileScaleKey += 1;
  550.                         }
  551.                     }
  552.                     else if (dwKeyType == 2)
  553.                     {
  554.                         if (pframeCur->m_pPositionKeys != NULL)
  555.                         {
  556.                             hr = E_INVALIDARG;
  557.                             goto e_Exit;
  558.                         }
  559.                         
  560.                         pframeCur->m_pPositionKeys = new SPositionKey[cKeys];
  561.                         if (pframeCur->m_pPositionKeys == NULL)
  562.                         {
  563.                             hr = E_OUTOFMEMORY;
  564.                             goto e_Exit;
  565.                         }
  566.                         
  567.                         pframeCur->m_cPositionKeys = cKeys;
  568.                         
  569.                         pFilePosKey =  (SPositionKeyXFile*)(pData + (sizeof(DWORD) * 2));
  570.                         for (iKey = 0;iKey < cKeys; iKey++)
  571.                         {
  572.                             pframeCur->m_pPositionKeys[iKey].dwTime = pFilePosKey->dwTime;
  573.                             pframeCur->m_pPositionKeys[iKey].vPos = pFilePosKey->vPos;
  574.                             
  575.                             pFilePosKey += 1;
  576.                         }
  577.                     }
  578.                     else if (dwKeyType == 4)
  579.                     {
  580.                         if (pframeCur->m_pMatrixKeys != NULL)
  581.                         {
  582.                             hr = E_INVALIDARG;
  583.                             goto e_Exit;
  584.                         }
  585.                         
  586.                         pframeCur->m_pMatrixKeys = new SMatrixKey[cKeys];
  587.                         if (pframeCur->m_pMatrixKeys == NULL)
  588.                         {
  589.                             hr = E_OUTOFMEMORY;
  590.                             goto e_Exit;
  591.                         }
  592.                         
  593.                         pframeCur->m_cMatrixKeys = cKeys;
  594.                         
  595.                         pFileMatrixKey =  (SMatrixKeyXFile*)(pData + (sizeof(DWORD) * 2));
  596.                         for (iKey = 0;iKey < cKeys; iKey++)
  597.                         {
  598.                             pframeCur->m_pMatrixKeys[iKey].dwTime = pFileMatrixKey->dwTime;
  599.                             pframeCur->m_pMatrixKeys[iKey].mat = pFileMatrixKey->mat;
  600.                             
  601.                             pFileMatrixKey += 1;
  602.                         }
  603.                     }
  604.                     else
  605.                     {
  606.                         hr = E_INVALIDARG;
  607.                         goto e_Exit;
  608.                     }
  609.                 }
  610.                 
  611.                 GXRELEASE(pxofobjChild);
  612.             }
  613.         }
  614.         
  615.         GXRELEASE(pxofChild);
  616.     }
  617.     
  618. e_Exit:
  619.     GXRELEASE(pxofobjChild);
  620.     GXRELEASE(pxofChild);
  621.     GXRELEASE(pxofobjChildRef);
  622.     return hr;
  623. }
  624.  
  625.  
  626.  
  627.  
  628. HRESULT CMyD3DApplication::LoadAnimationSet(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  629.                                             DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  630.                                             SFrame *pframeParent)
  631. {
  632.     SFrame *pframeCur;
  633.     const GUID *type;
  634.     HRESULT hr = S_OK;
  635.     LPDIRECTXFILEDATA pxofobjChild = NULL;
  636.     LPDIRECTXFILEOBJECT pxofChild = NULL;
  637.     DWORD cchName;
  638.     
  639.     pframeCur = new SFrame();
  640.     if (pframeCur == NULL)
  641.     {
  642.         hr = E_OUTOFMEMORY;
  643.         goto e_Exit;
  644.     }
  645.     pframeCur->bAnimationFrame = true;
  646.     
  647.     pframeParent->AddFrame(pframeCur);
  648.     
  649.     hr = pxofobjCur->GetName(NULL, &cchName);
  650.     if (FAILED(hr))
  651.         goto e_Exit;
  652.     
  653.     if (cchName > 0)
  654.     {
  655.         pframeCur->szName = new char[cchName];
  656.         if (pframeCur->szName == NULL)
  657.         {
  658.             hr = E_OUTOFMEMORY;
  659.             goto e_Exit;
  660.         }
  661.         
  662.         hr = pxofobjCur->GetName(pframeCur->szName, &cchName);
  663.         if (FAILED(hr))
  664.             goto e_Exit;
  665.     }
  666.     
  667.     
  668.     // Enumerate child objects.
  669.     // Child object can be data, data reference or binary.
  670.     // Use QueryInterface() to find what type of object a child is.
  671.     while (SUCCEEDED(pxofobjCur->GetNextObject(&pxofChild)))
  672.     {
  673.         // Query the child for it's FileData
  674.         hr = pxofChild->QueryInterface(IID_IDirectXFileData,
  675.             (LPVOID *)&pxofobjChild);
  676.         if (SUCCEEDED(hr))
  677.         {
  678.             hr = pxofobjChild->GetType(&type);
  679.             if (FAILED(hr))
  680.                 goto e_Exit;
  681.             
  682.             if( TID_D3DRMAnimation == *type )
  683.             {
  684.                 hr = LoadAnimation(pxofobjChild, pde, options, fvf, pD3DDevice, pframeCur);
  685.                 if (FAILED(hr))
  686.                     goto e_Exit;
  687.             }
  688.             
  689.             GXRELEASE(pxofobjChild);
  690.         }
  691.         
  692.         GXRELEASE(pxofChild);
  693.     }
  694.     
  695. e_Exit:
  696.     GXRELEASE(pxofobjChild);
  697.     GXRELEASE(pxofChild);
  698.     return hr;
  699. }
  700.  
  701.  
  702.  
  703.  
  704. HRESULT SplitMesh
  705.         (
  706.             LPD3DXMESH  pMesh,              // ASSUMPTION:  *pMesh is attribute sorted & has a valid attribute table
  707.             DWORD       iAttrSplit,         // **ppMeshB gets the mesh comprising of this attribute range onward
  708.             DWORD*      rgiAdjacency, 
  709.             DWORD       optionsA, 
  710.             DWORD       optionsB, 
  711.             LPD3DXMESH* ppMeshA, 
  712.             LPD3DXMESH* ppMeshB
  713.         )
  714. {
  715.     *ppMeshA = NULL;
  716.  
  717.     *ppMeshB = NULL;
  718.  
  719.     
  720.     HRESULT hr  = S_OK;
  721.  
  722.     PBYTE   pbVerticesIn    = NULL;
  723.     PBYTE   pbIndicesIn     = NULL;
  724.     DWORD*  piAttribsIn     = NULL;
  725.  
  726.     LPD3DXMESH pMeshA   = NULL;
  727.     LPD3DXMESH pMeshB   = NULL;
  728.     
  729.     LPD3DXBUFFER pVertexRemapA  = NULL;
  730.     LPD3DXBUFFER pVertexRemapB  = NULL;
  731.         
  732.     DWORD*  rgiAdjacencyA   = NULL;
  733.     DWORD*  rgiAdjacencyB   = NULL;
  734.  
  735.  
  736.  
  737.  
  738.     LPDIRECT3DDEVICE8   pDevice     = NULL;
  739.  
  740.  
  741.     D3DXATTRIBUTERANGE* rgAttrTable = NULL;
  742.  
  743.     DWORD               cAttrTable  = 0;
  744.  
  745.  
  746.  
  747.     DWORD   cVerticesA;
  748.     DWORD   cVerticesB;
  749.  
  750.     DWORD   cFacesA;
  751.     DWORD   cFacesB;
  752.  
  753.     DWORD   cbVertexSize;
  754.  
  755.     DWORD   dw32bit;
  756.  
  757.  
  758.     dw32bit         = pMesh->GetOptions() & D3DXMESH_32BIT;
  759.  
  760.     cbVertexSize    = D3DXGetFVFVertexSize(pMesh->GetFVF());
  761.  
  762.     hr  = pMesh->GetDevice(&pDevice);
  763.  
  764.     if (FAILED(hr))
  765.         goto e_Exit;
  766.  
  767.  
  768.     hr  = pMesh->GetAttributeTable(NULL, &cAttrTable);
  769.  
  770.     if (FAILED(hr))
  771.         goto e_Exit;
  772.  
  773.     
  774.     rgAttrTable = new D3DXATTRIBUTERANGE[cAttrTable];
  775.  
  776.     if (rgAttrTable == NULL)
  777.     {
  778.         hr  = E_OUTOFMEMORY;
  779.  
  780.         goto e_Exit;
  781.     }
  782.  
  783.  
  784.     hr  = pMesh->GetAttributeTable(rgAttrTable, NULL);
  785.  
  786.     if (FAILED(hr))
  787.         goto e_Exit;
  788.  
  789.     if (iAttrSplit == 0)
  790.     {
  791.         cVerticesA  = 0;
  792.  
  793.         cFacesA     = 0;
  794.     }
  795.     else if (iAttrSplit >= cAttrTable)
  796.     {
  797.         cVerticesA  = pMesh->GetNumVertices();
  798.  
  799.         cFacesA     = pMesh->GetNumFaces();
  800.     }
  801.     else
  802.     {
  803.         cVerticesA  = rgAttrTable[iAttrSplit].VertexStart;
  804.  
  805.         cFacesA     = rgAttrTable[iAttrSplit].FaceStart;
  806.     }
  807.  
  808.     cVerticesB  = pMesh->GetNumVertices() - cVerticesA;
  809.  
  810.     cFacesB     = pMesh->GetNumFaces() - cFacesA;
  811.  
  812.  
  813.     hr  = pMesh->LockVertexBuffer(D3DLOCK_READONLY, &pbVerticesIn);
  814.  
  815.     if (FAILED(hr))
  816.         goto e_Exit;
  817.  
  818.  
  819.     hr  = pMesh->LockIndexBuffer(D3DLOCK_READONLY, &pbIndicesIn);
  820.  
  821.     if (FAILED(hr))
  822.         goto e_Exit;
  823.  
  824.  
  825.     hr  = pMesh->LockAttributeBuffer(D3DLOCK_READONLY, &piAttribsIn);
  826.  
  827.     if (FAILED(hr))
  828.         goto e_Exit;
  829.  
  830.  
  831.     if (cFacesA && cVerticesA)
  832.     {
  833.         PBYTE   pbVerticesOut   = NULL;
  834.         PBYTE   pbIndicesOut    = NULL;
  835.         DWORD*  piAttribsOut    = NULL;
  836.         DWORD i;
  837.  
  838.         hr  = D3DXCreateMeshFVF(cFacesA, cVerticesA, optionsA | dw32bit, pMesh->GetFVF(), pDevice, &pMeshA);
  839.  
  840.         if (FAILED(hr))
  841.             goto e_ExitA;
  842.  
  843.  
  844.         hr  = pMeshA->LockVertexBuffer(0, &pbVerticesOut);
  845.  
  846.         if (FAILED(hr))
  847.             goto e_ExitA;
  848.  
  849.  
  850.         hr  = pMeshA->LockIndexBuffer(0, (LPBYTE*)&pbIndicesOut);
  851.  
  852.         if (FAILED(hr))
  853.             goto e_ExitA;
  854.  
  855.  
  856.         hr  = pMeshA->LockAttributeBuffer(0, &piAttribsOut);
  857.  
  858.         if (FAILED(hr))
  859.             goto e_ExitA;
  860.  
  861.  
  862.         memcpy(pbVerticesOut, pbVerticesIn, cVerticesA * cbVertexSize * sizeof(BYTE));
  863.  
  864.         if (dw32bit)
  865.         {
  866.             memcpy(pbIndicesOut, pbIndicesIn, cFacesA * 3 * sizeof(DWORD));
  867.         }
  868.         else
  869.         {
  870.             memcpy(pbIndicesOut, pbIndicesIn, cFacesA * 3 * sizeof(WORD));
  871.         }
  872.  
  873.       
  874.         memcpy(piAttribsOut, piAttribsIn, cFacesA * sizeof(DWORD));
  875.  
  876.  
  877.         rgiAdjacencyA   = new DWORD[cFacesA * 3];
  878.  
  879.         if (rgiAdjacencyA == NULL)
  880.         {
  881.             hr  = E_OUTOFMEMORY;
  882.  
  883.             goto e_ExitA;
  884.         }
  885.  
  886.  
  887.         for (i = 0; i <  cFacesA * 3; i++)
  888.         {
  889.             rgiAdjacencyA[i]    = (rgiAdjacency[i] < cFacesA) ? rgiAdjacency[i] : 0xFFFFFFFF;
  890.         }
  891.  
  892.  
  893. e_ExitA:
  894.  
  895.         if (pbVerticesOut != NULL)
  896.         {
  897.             pMeshA->UnlockVertexBuffer();
  898.         }
  899.  
  900.  
  901.         if (pbIndicesOut != NULL)
  902.         {
  903.             pMeshA->UnlockIndexBuffer();
  904.         }
  905.  
  906.  
  907.         if (piAttribsOut != NULL)
  908.         {
  909.             pMeshA->UnlockAttributeBuffer();
  910.         }
  911.  
  912.  
  913.         if (FAILED(hr))
  914.             goto e_Exit;
  915.     }
  916.  
  917.  
  918.     // calculate Mesh A's attribute table
  919.  
  920.     if (pMeshA != NULL)
  921.     {
  922.         hr  = pMeshA->OptimizeInplace
  923.                       (
  924.                           D3DXMESHOPT_VERTEXCACHE,
  925.                           rgiAdjacencyA,
  926.                           NULL,
  927.                           NULL,
  928.                           &pVertexRemapA
  929.                       );
  930.  
  931.         if (FAILED(hr))
  932.             goto e_Exit;
  933.     }
  934.  
  935.  
  936.     if (cFacesB && cVerticesB)
  937.     {
  938.         PBYTE   pbVerticesOut   = NULL;
  939.         PBYTE   pbIndicesOut    = NULL;
  940.         DWORD*  piAttribsOut    = NULL;
  941.         DWORD i;
  942.  
  943.         hr  = D3DXCreateMeshFVF(cFacesB, cVerticesB, optionsB | dw32bit, pMesh->GetFVF(), pDevice, &pMeshB);
  944.  
  945.         if (FAILED(hr))
  946.             goto e_ExitB;
  947.  
  948.  
  949.         hr  = pMeshB->LockVertexBuffer(0, &pbVerticesOut);
  950.  
  951.         if (FAILED(hr))
  952.             goto e_ExitB;
  953.  
  954.  
  955.         hr  = pMeshB->LockIndexBuffer(0, &pbIndicesOut);
  956.  
  957.         if (FAILED(hr))
  958.             goto e_ExitB;
  959.  
  960.  
  961.         hr  = pMeshB->LockAttributeBuffer(0, &piAttribsOut);
  962.  
  963.         if (FAILED(hr))
  964.             goto e_ExitB;
  965.  
  966.  
  967.         memcpy(pbVerticesOut, pbVerticesIn + (cVerticesA * cbVertexSize), cVerticesB * cbVertexSize * sizeof(BYTE));
  968.  
  969.  
  970.         // copy & renumber indices
  971.  
  972.         if (dw32bit)
  973.         {
  974.             for (DWORD i = 0; i < cFacesB * 3; i++)
  975.             {
  976.                 ((DWORD*)pbIndicesOut)[i]    = ((DWORD*)pbIndicesIn)[(cFacesA * 3) + i]  - (DWORD)cVerticesA;
  977.             }
  978.         }
  979.         else
  980.         {
  981.             for (DWORD i = 0; i < cFacesB * 3; i++)
  982.             {
  983.                 ((WORD*)pbIndicesOut)[i]    = ((WORD*)pbIndicesIn)[(cFacesA * 3) + i]  - (WORD)cVerticesA;
  984.             }
  985.         }
  986.  
  987.         memcpy(piAttribsOut, piAttribsIn + cFacesA, cFacesB * sizeof(DWORD));
  988.  
  989.  
  990.         rgiAdjacencyB   = new DWORD[cFacesB * 3];
  991.  
  992.         if (rgiAdjacencyB == NULL)
  993.         {
  994.             hr  = E_OUTOFMEMORY;
  995.  
  996.             goto e_ExitB;
  997.         }
  998.  
  999.  
  1000.         // copy & renumber adjacency
  1001.  
  1002.         for (i = 0; i < cFacesB * 3; i++)
  1003.         {
  1004.             rgiAdjacencyB[i]    = (rgiAdjacency[(cFacesA * 3) + i] >= cFacesA && rgiAdjacency[(cFacesA * 3) + i] != 0xFFFFFFFF) ? rgiAdjacency[(cFacesA * 3) + i] - cFacesA : 0xFFFFFFFF;
  1005.         }
  1006.  
  1007.  
  1008. e_ExitB:
  1009.  
  1010.         if (pbVerticesOut != NULL)
  1011.         {
  1012.             pMeshB->UnlockVertexBuffer();
  1013.         }
  1014.  
  1015.  
  1016.         if (pbIndicesOut != NULL)
  1017.         {
  1018.             pMeshB->UnlockIndexBuffer();
  1019.         }
  1020.  
  1021.  
  1022.         if (piAttribsOut != NULL)
  1023.         {
  1024.             pMeshB->UnlockAttributeBuffer();
  1025.         }
  1026.  
  1027.  
  1028.         if (FAILED(hr))
  1029.             goto e_Exit;
  1030.     }
  1031.  
  1032.  
  1033.     // calculate Mesh B's attribute table
  1034.  
  1035.     if (pMeshB != NULL)
  1036.     {
  1037.         hr  = pMeshB->OptimizeInplace
  1038.                       (
  1039.                           D3DXMESHOPT_ATTRSORT,
  1040.                           rgiAdjacencyB,
  1041.                           NULL,
  1042.                           NULL,
  1043.                           &pVertexRemapB
  1044.                       );
  1045.  
  1046.         if (FAILED(hr))
  1047.             goto e_Exit;
  1048.     }
  1049.  
  1050.  
  1051. e_Exit:
  1052.     
  1053.     if (rgAttrTable != NULL)
  1054.     {
  1055.         delete[] rgAttrTable;
  1056.     }
  1057.  
  1058.     if (pbVerticesIn != NULL)
  1059.     {
  1060.         pMesh->UnlockVertexBuffer();
  1061.     }
  1062.  
  1063.     if (pbIndicesIn != NULL)
  1064.     {
  1065.         pMesh->UnlockIndexBuffer();
  1066.     }
  1067.  
  1068.     if (piAttribsIn != NULL)
  1069.     {
  1070.         pMesh->UnlockAttributeBuffer();
  1071.     }
  1072.     
  1073.     if (rgiAdjacencyA != NULL)
  1074.     {
  1075.         delete[] rgiAdjacencyA;
  1076.     }
  1077.  
  1078.     if (rgiAdjacencyB != NULL)
  1079.     {
  1080.         delete[] rgiAdjacencyB;
  1081.     }
  1082.  
  1083.     GXRELEASE(pDevice);
  1084.  
  1085.     GXRELEASE(pVertexRemapA);
  1086.  
  1087.     GXRELEASE(pVertexRemapB);
  1088.  
  1089.     if (FAILED(hr))
  1090.     {
  1091.         GXRELEASE(pMeshA);
  1092.  
  1093.         GXRELEASE(pMeshB);
  1094.  
  1095.         pMeshA  = NULL;
  1096.  
  1097.         pMeshB  = NULL;
  1098.     }
  1099.  
  1100.     *ppMeshA    = pMeshA;
  1101.  
  1102.     *ppMeshB    = pMeshB;
  1103.  
  1104.     return hr;
  1105. }
  1106.  
  1107.  
  1108.  
  1109.  
  1110. HRESULT CMyD3DApplication::GenerateMesh(SMeshContainer *pmcMesh)
  1111. {
  1112.     // ASSUMPTION:  pmcMesh->m_rgiAdjacency contains the current adjacency
  1113.  
  1114.     HRESULT hr  = S_OK;
  1115.  
  1116.     DWORD*  pAdjacencyIn    = NULL;
  1117.  
  1118.     DWORD   cFaces  = pmcMesh->m_pSkinMesh->GetNumFaces();
  1119.  
  1120.     pAdjacencyIn    = new DWORD[pmcMesh->m_pSkinMesh->GetNumFaces() * 3];
  1121.  
  1122.     if (pAdjacencyIn == NULL)
  1123.     {
  1124.         hr = E_OUTOFMEMORY;
  1125.  
  1126.         goto e_Exit;
  1127.     }
  1128.     
  1129.     memcpy(pAdjacencyIn, pmcMesh->m_rgiAdjacency, cFaces * 3 * sizeof(DWORD));
  1130.  
  1131.  
  1132.     GXRELEASE(pmcMesh->pMesh);
  1133.     GXRELEASE(pmcMesh->pMeshHW);
  1134.     GXRELEASE(pmcMesh->pMeshSW);
  1135.  
  1136.     pmcMesh->pMesh      = NULL;
  1137.     pmcMesh->pMeshHW    = NULL;
  1138.     pmcMesh->pMeshSW    = NULL;
  1139.     
  1140.     if (m_method == D3DNONINDEXED)
  1141.     {
  1142.         DWORD*  rgiAdjacency    = NULL;
  1143.         
  1144.         LPDIRECT3DDEVICE8       pDevice = NULL;
  1145.  
  1146.         
  1147.         LPD3DXBONECOMBINATION   rgBoneCombinations;
  1148.  
  1149.         D3DCAPS8                caps;
  1150.  
  1151.  
  1152.         rgiAdjacency    = new DWORD[cFaces * 3];
  1153.  
  1154.         if (rgiAdjacency == NULL)
  1155.         {
  1156.             hr  = E_OUTOFMEMORY;
  1157.  
  1158.             goto e_ExitNONINDEXED;
  1159.         }
  1160.  
  1161.  
  1162.         hr = pmcMesh->m_pSkinMesh->ConvertToBlendedMesh
  1163.                                    (
  1164.                                        0, 
  1165.                                        pAdjacencyIn, 
  1166.                                        rgiAdjacency, 
  1167.                                        &pmcMesh->cpattr, 
  1168.                                        &pmcMesh->m_pBoneCombinationBuf, 
  1169.                                        &pmcMesh->pMesh
  1170.                                    );
  1171.         if (FAILED(hr))
  1172.             goto e_ExitNONINDEXED;
  1173.  
  1174.  
  1175.  
  1176.         // calculate the max face influence count
  1177.  
  1178.         if ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) != D3DFVF_XYZ)
  1179.         {
  1180.             pmcMesh->m_maxFaceInfl = 1 + ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) - D3DFVF_XYZRHW) / 2;
  1181.         }
  1182.         else
  1183.         {
  1184.             pmcMesh->m_maxFaceInfl = 1;
  1185.         }
  1186.  
  1187.  
  1188.  
  1189.         hr  = pmcMesh->pMesh->GetDevice(&pDevice);
  1190.  
  1191.         if (FAILED(hr))
  1192.             goto e_ExitNONINDEXED;
  1193.  
  1194.  
  1195.         hr  = pDevice->GetDeviceCaps(&caps);
  1196.  
  1197.         if (FAILED(hr))
  1198.             goto e_ExitNONINDEXED;
  1199.  
  1200.         /* If the device can only do 2 matrix blends, ConvertToBlendedMesh cannot approximate all meshes to it
  1201.            Thus we split the mesh in two parts: The part that uses at most 2 matrices and the rest. The first is
  1202.            drawn using the device's HW vertex processing and the rest is drawn using SW vertex processing. */
  1203.         if (caps.MaxVertexBlendMatrices == 2)       
  1204.         {
  1205.       
  1206.             // calculate the index of the attribute table to split on
  1207.  
  1208.             rgBoneCombinations  = reinterpret_cast<LPD3DXBONECOMBINATION>(pmcMesh->m_pBoneCombinationBuf->GetBufferPointer());
  1209.  
  1210.             for (pmcMesh->iAttrSplit = 0; pmcMesh->iAttrSplit < pmcMesh->cpattr; pmcMesh->iAttrSplit++)
  1211.             {
  1212.                 DWORD   cInfl   = 0;
  1213.  
  1214.                 for (DWORD iInfl = 0; iInfl < pmcMesh->m_maxFaceInfl; iInfl++)
  1215.                 {
  1216.                     if (rgBoneCombinations[pmcMesh->iAttrSplit].BoneId[iInfl] != UINT_MAX)
  1217.                     {
  1218.                         ++cInfl;
  1219.                     }
  1220.                 }
  1221.  
  1222.                 if (cInfl > 2)
  1223.                 {
  1224.                     break;
  1225.                 }
  1226.             }
  1227.  
  1228.             // split the mesh
  1229.  
  1230.             hr  = SplitMesh(pmcMesh->pMesh, pmcMesh->iAttrSplit, rgiAdjacency, D3DXMESH_WRITEONLY,  pmcMesh->pMesh->GetOptions() | D3DXMESH_SYSTEMMEM, &pmcMesh->pMeshHW, &pmcMesh->pMeshSW);
  1231.  
  1232.             if (FAILED(hr))
  1233.                 goto e_ExitNONINDEXED;
  1234.         }
  1235.         else
  1236.         {
  1237.             // Vertex cache optimize the mesh
  1238.             LPD3DXMESH pMeshOpt;
  1239.             hr = pmcMesh->pMesh->Optimize(D3DXMESHOPT_VERTEXCACHE, rgiAdjacency, NULL, NULL, NULL, &pMeshOpt);
  1240.             if (!FAILED(hr))
  1241.             {
  1242.                 pmcMesh->pMesh->Release();
  1243.                 pmcMesh->pMesh = pMeshOpt;
  1244.                 pMeshOpt = NULL;
  1245.             }
  1246.  
  1247.             // Need to clone the mesh to be WRITEONLY since we will not now read back from it.
  1248.              LPD3DXMESH pMeshVid;
  1249.             hr = pmcMesh->pMesh->CloneMeshFVF(pmcMesh->pMesh->GetOptions() | D3DXMESH_WRITEONLY, pmcMesh->pMesh->GetFVF(),
  1250.                                               pDevice, &pMeshVid);
  1251.             if (!FAILED(hr))
  1252.             {
  1253.                 pmcMesh->pMesh->Release();
  1254.                 pmcMesh->pMesh = pMeshVid;
  1255.                 pMeshVid = NULL;
  1256.             }
  1257.         }
  1258.  
  1259.  
  1260. e_ExitNONINDEXED:
  1261.  
  1262.         GXRELEASE(pDevice);
  1263.  
  1264.         if (rgiAdjacency != NULL)
  1265.         {
  1266.             delete[] rgiAdjacency;
  1267.         }
  1268.  
  1269.         if (FAILED(hr))
  1270.             goto e_Exit;
  1271.  
  1272.     }
  1273.     else if (m_method == D3DINDEXED)
  1274.     {
  1275.         hr = pmcMesh->m_pSkinMesh->ConvertToIndexedBlendedMesh(D3DXMESH_SYSTEMMEM, pAdjacencyIn, 255, NULL,
  1276.             &pmcMesh->cpattr, &pmcMesh->m_pBoneCombinationBuf, &pmcMesh->pMesh);
  1277.         if (FAILED(hr))
  1278.             goto e_Exit;
  1279.  
  1280.         // Here we are talking of max vertex influence which we determine from 
  1281.         // the FVF of the returned mesh
  1282.         if ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) != D3DFVF_XYZ)
  1283.         {
  1284.             pmcMesh->m_maxFaceInfl = ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) - D3DFVF_XYZRHW) / 2;
  1285.         }
  1286.         else
  1287.         {
  1288.             pmcMesh->m_maxFaceInfl = 1;
  1289.         }
  1290.     }
  1291.     else if (m_method == SOFTWARE)
  1292.     {
  1293.         hr = pmcMesh->m_pSkinMesh->GenerateSkinnedMesh
  1294.                                    (
  1295.                                        D3DXMESH_WRITEONLY,          // options
  1296.                                        0.0f,                        // minimumm bone weight allowed
  1297.                                        pAdjacencyIn,                // adjacency of in-mesh
  1298.                                        pmcMesh->m_rgiAdjacency,     // adjacency of out-mesh
  1299.                                        &pmcMesh->pMesh              // out-mesh
  1300.                                    );
  1301.         if (FAILED(hr))
  1302.             goto e_Exit;
  1303.  
  1304.  
  1305.         hr = pmcMesh->pMesh->GetAttributeTable(NULL, &pmcMesh->cpattr);
  1306.  
  1307.         if (FAILED(hr))
  1308.             goto e_Exit;
  1309.  
  1310.  
  1311.         delete[] pmcMesh->m_pAttrTable;
  1312.  
  1313.         pmcMesh->m_pAttrTable  = new D3DXATTRIBUTERANGE[pmcMesh->cpattr];
  1314.  
  1315.         if (pmcMesh->m_pAttrTable == NULL)
  1316.         {
  1317.             hr = E_OUTOFMEMORY;
  1318.  
  1319.             goto e_Exit;
  1320.         }
  1321.  
  1322.  
  1323.         hr = pmcMesh->pMesh->GetAttributeTable(pmcMesh->m_pAttrTable, NULL);
  1324.  
  1325.         if (FAILED(hr))
  1326.             goto e_Exit;
  1327.  
  1328.  
  1329.         hr = pmcMesh->m_pSkinMesh->GetMaxFaceInfluences(&pmcMesh->m_maxFaceInfl);
  1330.  
  1331.         if (FAILED(hr))
  1332.             goto e_Exit;
  1333.     }
  1334.  
  1335.     pmcMesh->m_Method = m_method;
  1336.  
  1337. e_Exit:
  1338.  
  1339.     delete[] pAdjacencyIn;
  1340.  
  1341.     return hr;
  1342. }
  1343.  
  1344.  
  1345.  
  1346.  
  1347. HRESULT CMyD3DApplication::LoadMesh(LPDIRECTXFILEDATA pxofobjCur,
  1348.                                     DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  1349.                                     SFrame *pframeParent)
  1350. {
  1351.     HRESULT hr = S_OK;
  1352.     SMeshContainer *pmcMesh = NULL;
  1353.     LPD3DXBUFFER pbufMaterials = NULL;
  1354.     LPD3DXBUFFER pbufAdjacency = NULL;
  1355.     DWORD cchName;
  1356.     UINT cFaces;
  1357.     UINT iMaterial;
  1358.     LPDIRECT3DDEVICE8 m_pDevice = m_pd3dDevice;
  1359.     
  1360.     pmcMesh = new SMeshContainer();
  1361.     if (pmcMesh == NULL)
  1362.     {
  1363.         hr = E_OUTOFMEMORY;
  1364.         goto e_Exit;
  1365.     }
  1366.     
  1367.     hr = pxofobjCur->GetName(NULL, &cchName);
  1368.     if (FAILED(hr))
  1369.         goto e_Exit;
  1370.     
  1371.     if (cchName > 0)
  1372.     {
  1373.         pmcMesh->szName = new char[cchName];
  1374.         if (pmcMesh->szName == NULL)
  1375.         {
  1376.             hr = E_OUTOFMEMORY;
  1377.             goto e_Exit;
  1378.         }
  1379.         
  1380.         hr = pxofobjCur->GetName(pmcMesh->szName, &cchName);
  1381.         if (FAILED(hr))
  1382.             goto e_Exit;
  1383.     }
  1384.     
  1385.     hr = D3DXLoadSkinMeshFromXof(pxofobjCur, options, pD3DDevice, &pbufAdjacency, &pbufMaterials, &pmcMesh->cMaterials, 
  1386.         &pmcMesh->m_pBoneNamesBuf, &pmcMesh->m_pBoneOffsetBuf, &pmcMesh->m_pSkinMesh);
  1387.     if (FAILED(hr))
  1388.         goto e_Exit;
  1389.     
  1390.     cFaces = pmcMesh->m_pSkinMesh->GetNumFaces();
  1391.  
  1392.     // Process skinning data
  1393.     if (pmcMesh->m_pSkinMesh->GetNumBones())
  1394.     {
  1395.         pmcMesh->m_pBoneMatrix = new D3DXMATRIX*[pmcMesh->m_pSkinMesh->GetNumBones()];
  1396.         if (pmcMesh->m_pBoneMatrix == NULL)
  1397.             goto e_Exit;
  1398.         pmcMesh->m_pBoneOffsetMat = reinterpret_cast<D3DXMATRIX*>(pmcMesh->m_pBoneOffsetBuf->GetBufferPointer());
  1399.         LPDWORD pAdjacencyIn = static_cast<LPDWORD>(pbufAdjacency->GetBufferPointer());
  1400.  
  1401.         pmcMesh->m_rgiAdjacency = new DWORD[cFaces * 3];
  1402.  
  1403.         if (pmcMesh->m_rgiAdjacency == NULL)
  1404.         {
  1405.             hr = E_OUTOFMEMORY;
  1406.  
  1407.             goto e_Exit;
  1408.         }
  1409.  
  1410.         memcpy(pmcMesh->m_rgiAdjacency, pAdjacencyIn, cFaces * 3 * sizeof(DWORD));
  1411.         
  1412.         hr = GenerateMesh(pmcMesh);
  1413.  
  1414.         if (FAILED(hr))
  1415.             goto e_Exit;
  1416.     }
  1417.     else
  1418.     {
  1419.         pmcMesh->m_pSkinMesh->GetOriginalMesh(&(pmcMesh->pMesh));
  1420.         pmcMesh->m_pSkinMesh->Release();
  1421.         pmcMesh->m_pSkinMesh = NULL;
  1422.         pmcMesh->cpattr = pmcMesh->cMaterials;
  1423.     }
  1424.     
  1425.     if ((pbufMaterials == NULL) || (pmcMesh->cMaterials == 0))
  1426.     {
  1427.         pmcMesh->rgMaterials = new D3DMATERIAL8[1];
  1428.         pmcMesh->pTextures = new LPDIRECT3DTEXTURE8[1];
  1429.         if (pmcMesh->rgMaterials == NULL || pmcMesh->pTextures == NULL)
  1430.         {
  1431.             hr = E_OUTOFMEMORY;
  1432.             goto e_Exit;
  1433.         }
  1434.         
  1435.         memset(pmcMesh->rgMaterials, 0, sizeof(D3DXMATERIAL));
  1436.         pmcMesh->rgMaterials[0].Diffuse.r = 0.5f;
  1437.         pmcMesh->rgMaterials[0].Diffuse.g = 0.5f;
  1438.         pmcMesh->rgMaterials[0].Diffuse.b = 0.5f;
  1439.         pmcMesh->rgMaterials[0].Specular = pmcMesh->rgMaterials[0].Diffuse;
  1440.         pmcMesh->pTextures[0] = NULL;
  1441.     }
  1442.     else
  1443.     {
  1444.         pmcMesh->rgMaterials = new D3DMATERIAL8[pmcMesh->cMaterials];
  1445.         pmcMesh->pTextures = new LPDIRECT3DTEXTURE8[pmcMesh->cMaterials];
  1446.         if (pmcMesh->rgMaterials == NULL || pmcMesh->pTextures == NULL)
  1447.         {
  1448.             hr = E_OUTOFMEMORY;
  1449.             goto e_Exit;
  1450.         }
  1451.         
  1452.         LPD3DXMATERIAL pMaterials = (LPD3DXMATERIAL)pbufMaterials->GetBufferPointer();
  1453.         
  1454.         for (iMaterial = 0; iMaterial < pmcMesh->cMaterials; iMaterial++)
  1455.         {
  1456.             
  1457.             pmcMesh->rgMaterials[iMaterial] = pMaterials[iMaterial].MatD3D;
  1458.             
  1459.             pmcMesh->pTextures[iMaterial] = NULL;
  1460.             if (pMaterials[iMaterial].pTextureFilename != NULL)
  1461.             {
  1462.                 TCHAR szPath[MAX_PATH];
  1463.                 DXUtil_FindMediaFile(szPath, pMaterials[iMaterial].pTextureFilename);
  1464.  
  1465.                 hr = D3DXCreateTextureFromFile(m_pDevice, szPath, &(pmcMesh->pTextures[iMaterial]));
  1466.                 if (FAILED(hr))
  1467.                     pmcMesh->pTextures[iMaterial] = NULL;
  1468.             }
  1469.         }
  1470.     }
  1471.     
  1472.     // add the mesh to the parent frame
  1473.     pframeParent->AddMesh(pmcMesh);
  1474.     pmcMesh = NULL;
  1475.     
  1476. e_Exit:
  1477.     delete pmcMesh;
  1478.     
  1479.     GXRELEASE(pbufAdjacency);
  1480.     GXRELEASE(pbufMaterials);
  1481.  
  1482.     return hr;
  1483. }
  1484.  
  1485.  
  1486.  
  1487.  
  1488. HRESULT CMyD3DApplication::LoadFrames(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  1489.                                       DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  1490.                                       SFrame *pframeParent)
  1491. {
  1492.     HRESULT hr = S_OK;
  1493.     LPDIRECTXFILEDATA pxofobjChild = NULL;
  1494.     LPDIRECTXFILEOBJECT pxofChild = NULL;
  1495.     const GUID *type;
  1496.     DWORD cbSize;
  1497.     D3DXMATRIX *pmatNew;
  1498.     SFrame *pframeCur;
  1499.     DWORD cchName;
  1500.     
  1501.     // Get the type of the object
  1502.     hr = pxofobjCur->GetType(&type);
  1503.     if (FAILED(hr))
  1504.         goto e_Exit;
  1505.     
  1506.     
  1507.     if (*type == TID_D3DRMMesh)
  1508.     {
  1509.         hr = LoadMesh(pxofobjCur, options, fvf, pD3DDevice, pframeParent);
  1510.         if (FAILED(hr))
  1511.             goto e_Exit;
  1512.     }
  1513.     else if (*type == TID_D3DRMFrameTransformMatrix)
  1514.     {
  1515.         hr = pxofobjCur->GetData(NULL, &cbSize, (PVOID*)&pmatNew);
  1516.         if (FAILED(hr))
  1517.             goto e_Exit;
  1518.         
  1519.         // update the parents matrix with the new one
  1520.         pframeParent->matRot = *pmatNew;
  1521.         pframeParent->matRotOrig = *pmatNew;
  1522.     }
  1523.     else if (*type == TID_D3DRMAnimationSet)
  1524.     {
  1525.         LoadAnimationSet(pxofobjCur, pde, options, fvf, pD3DDevice, pframeParent);
  1526.     }
  1527.     else if (*type == TID_D3DRMAnimation)
  1528.     {
  1529.         LoadAnimation(pxofobjCur, pde, options, fvf, pD3DDevice, pframeParent);
  1530.     }
  1531.     else if (*type == TID_D3DRMFrame)
  1532.     {
  1533.         pframeCur = new SFrame();
  1534.         if (pframeCur == NULL)
  1535.         {
  1536.             hr = E_OUTOFMEMORY;
  1537.             goto e_Exit;
  1538.         }
  1539.         
  1540.         hr = pxofobjCur->GetName(NULL, &cchName);
  1541.         if (FAILED(hr))
  1542.             goto e_Exit;
  1543.         
  1544.         if (cchName > 0)
  1545.         {
  1546.             pframeCur->szName = new char[cchName];
  1547.             if (pframeCur->szName == NULL)
  1548.             {
  1549.                 hr = E_OUTOFMEMORY;
  1550.                 goto e_Exit;
  1551.             }
  1552.             
  1553.             hr = pxofobjCur->GetName(pframeCur->szName, &cchName);
  1554.             if (FAILED(hr))
  1555.                 goto e_Exit;
  1556.         }
  1557.         
  1558.         pframeParent->AddFrame(pframeCur);
  1559.         
  1560.         // Enumerate child objects.
  1561.         // Child object can be data, data reference or binary.
  1562.         // Use QueryInterface() to find what type of object a child is.
  1563.         while (SUCCEEDED(pxofobjCur->GetNextObject(&pxofChild)))
  1564.         {
  1565.             // Query the child for it's FileData
  1566.             hr = pxofChild->QueryInterface(IID_IDirectXFileData,
  1567.                 (LPVOID *)&pxofobjChild);
  1568.             if (SUCCEEDED(hr))
  1569.             {
  1570.                 hr = LoadFrames(pxofobjChild, pde, options, fvf, pD3DDevice, pframeCur);
  1571.                 if (FAILED(hr))
  1572.                     goto e_Exit;
  1573.                 
  1574.                 GXRELEASE(pxofobjChild);
  1575.             }
  1576.             
  1577.             GXRELEASE(pxofChild);
  1578.         }
  1579.         
  1580.     }
  1581.     
  1582. e_Exit:
  1583.     GXRELEASE(pxofobjChild);
  1584.     GXRELEASE(pxofChild);
  1585.     return hr;
  1586. }
  1587.  
  1588.  
  1589.          
  1590.                                       
  1591. HRESULT  CMyD3DApplication::DeleteSelectedMesh()
  1592. {
  1593.     if (m_pdeSelected != NULL)
  1594.     {
  1595.         SDrawElement *pdeCur = m_pdeHead;
  1596.         SDrawElement *pdePrev = NULL;
  1597.         while ((pdeCur != NULL) && (pdeCur != m_pdeSelected))
  1598.         {
  1599.             pdePrev = pdeCur;
  1600.             pdeCur = pdeCur->pdeNext;
  1601.         }
  1602.  
  1603.         if (pdePrev == NULL)
  1604.         {
  1605.             m_pdeHead = m_pdeHead->pdeNext;
  1606.         }
  1607.         else
  1608.         {
  1609.             pdePrev->pdeNext = pdeCur->pdeNext;
  1610.         }
  1611.  
  1612.         m_pdeSelected->pdeNext = NULL;
  1613.         if (m_pdeHead == m_pdeSelected)
  1614.             m_pdeHead = NULL;
  1615.         delete m_pdeSelected;
  1616.         m_pdeSelected = NULL;
  1617.     }
  1618.  
  1619.     return S_OK;
  1620. }
  1621.