home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / SkinnedMesh / mload.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  37.7 KB  |  1,225 lines

  1. //-----------------------------------------------------------------------------
  2. // File: mload.cpp
  3. //
  4. // Copyright (c) 1999-2001 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.     D3DXMATRIXA16 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.     D3DXMATRIXA16 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.     D3DXMATRIXA16 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.  
  705. HRESULT CMyD3DApplication::GenerateMesh(SMeshContainer *pmcMesh)
  706. {
  707.     // ASSUMPTION:  pmcMesh->m_rgiAdjacency contains the current adjacency
  708.     HRESULT hr = S_OK;
  709.     LPDIRECT3DDEVICE8 pDevice = NULL;
  710.     DWORD cFaces = pmcMesh->m_pSkinMesh->GetNumFaces();
  711.  
  712.     hr  = pmcMesh->m_pSkinMesh->GetDevice(&pDevice);
  713.     if (FAILED(hr))
  714.         goto e_Exit;
  715.  
  716.     GXRELEASE(pmcMesh->pMesh);
  717.     delete [] m_pBoneMatrices;
  718.  
  719.     pmcMesh->pMesh      = NULL;
  720.     m_pBoneMatrices     = NULL;
  721.     
  722.     if (m_method == D3DNONINDEXED)
  723.     {
  724.         LPD3DXBONECOMBINATION   rgBoneCombinations;
  725.  
  726.         hr = pmcMesh->m_pSkinMesh->ConvertToBlendedMesh
  727.                                    (
  728.                                        D3DXMESH_MANAGED|D3DXMESHOPT_VERTEXCACHE, 
  729.                                        pmcMesh->m_rgiAdjacency, 
  730.                                        NULL, 
  731.                                        &pmcMesh->cpattr, 
  732.                                        &pmcMesh->m_pBoneCombinationBuf, 
  733.                                        NULL,
  734.                                        NULL,
  735.                                        &pmcMesh->pMesh
  736.                                    );
  737.         if (FAILED(hr))
  738.             goto e_Exit;
  739.  
  740.         // calculate the max face influence count
  741.  
  742.         if ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) != D3DFVF_XYZ)
  743.         {
  744.             pmcMesh->m_maxFaceInfl = 1 + ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) - D3DFVF_XYZRHW) / 2;
  745.         }
  746.         else
  747.         {
  748.             pmcMesh->m_maxFaceInfl = 1;
  749.         }
  750.  
  751.         /* If the device can only do 2 matrix blends, ConvertToBlendedMesh cannot approximate all meshes to it
  752.            Thus we split the mesh in two parts: The part that uses at most 2 matrices and the rest. The first is
  753.            drawn using the device's HW vertex processing and the rest is drawn using SW vertex processing. */
  754.         if (m_d3dCaps.MaxVertexBlendMatrices <= 2)       
  755.         {
  756.             // calculate the index of the attribute table to split on
  757.             rgBoneCombinations  = reinterpret_cast<LPD3DXBONECOMBINATION>(pmcMesh->m_pBoneCombinationBuf->GetBufferPointer());
  758.  
  759.             for (pmcMesh->iAttrSplit = 0; pmcMesh->iAttrSplit < pmcMesh->cpattr; pmcMesh->iAttrSplit++)
  760.             {
  761.                 DWORD   cInfl   = 0;
  762.  
  763.                 for (DWORD iInfl = 0; iInfl < pmcMesh->m_maxFaceInfl; iInfl++)
  764.                 {
  765.                     if (rgBoneCombinations[pmcMesh->iAttrSplit].BoneId[iInfl] != UINT_MAX)
  766.                     {
  767.                         ++cInfl;
  768.                     }
  769.                 }
  770.  
  771.                 if (cInfl > m_d3dCaps.MaxVertexBlendMatrices)
  772.                 {
  773.                     break;
  774.                 }
  775.             }
  776.  
  777.             // if there is both HW and SW, add the Software Processing flag
  778.             if (pmcMesh->iAttrSplit < pmcMesh->cpattr)
  779.             {
  780.                 LPD3DXMESH pMeshTmp;
  781.  
  782.                 hr = pmcMesh->pMesh->CloneMeshFVF(D3DXMESH_SOFTWAREPROCESSING|pmcMesh->pMesh->GetOptions(), 
  783.                                                     pmcMesh->pMesh->GetFVF(),
  784.                                                     pDevice, &pMeshTmp);
  785.                 if (FAILED(hr))
  786.                 {
  787.                     goto e_Exit;
  788.                 }
  789.  
  790.                 pmcMesh->pMesh->Release();
  791.                 pmcMesh->pMesh = pMeshTmp;
  792.                 pMeshTmp = NULL;
  793.             }
  794.         }
  795.         else
  796.         {
  797.             pmcMesh->iAttrSplit = pmcMesh->cpattr;
  798.         }
  799.     }
  800.     else if (m_method == D3DINDEXEDVS)
  801.     {
  802.         // Get palette size
  803.         pmcMesh->m_paletteSize = min(28, pmcMesh->m_pSkinMesh->GetNumBones());
  804.  
  805.         DWORD flags = D3DXMESHOPT_VERTEXCACHE;
  806.         if (m_d3dCaps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
  807.         {
  808.             pmcMesh->m_bUseSW = false;
  809.             flags |= D3DXMESH_MANAGED;
  810.         }
  811.         else
  812.         {
  813.             pmcMesh->m_bUseSW = true;
  814.             flags |= D3DXMESH_SYSTEMMEM;
  815.         }
  816.  
  817.         hr = pmcMesh->m_pSkinMesh->ConvertToIndexedBlendedMesh(flags, pmcMesh->m_rgiAdjacency, pmcMesh->m_paletteSize, NULL,
  818.             &pmcMesh->cpattr, &pmcMesh->m_pBoneCombinationBuf, NULL, NULL, &pmcMesh->pMesh);
  819.         if (FAILED(hr))
  820.             goto e_Exit;
  821.  
  822.         if ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) != D3DFVF_XYZ)
  823.         {
  824.             pmcMesh->m_maxFaceInfl = ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) - D3DFVF_XYZRHW) / 2;
  825.         }
  826.         else
  827.         {
  828.             pmcMesh->m_maxFaceInfl = 1;
  829.         }
  830.  
  831.         // FVF has to match our declarator. Vertex shaders are not as forgiving as FF pipeline
  832.         DWORD newFVF = (pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_LASTBETA_UBYTE4;
  833.         if (newFVF != pmcMesh->pMesh->GetFVF())
  834.         {
  835.             LPD3DXMESH pMesh;
  836.             hr = pmcMesh->pMesh->CloneMeshFVF(pmcMesh->pMesh->GetOptions(), newFVF, pDevice, &pMesh);
  837.             if (!FAILED(hr))
  838.             {
  839.                 pmcMesh->pMesh->Release();
  840.                 pmcMesh->pMesh = pMesh;
  841.                 pMesh = NULL;
  842.             }
  843.         }
  844.     }
  845.     else if (m_method == D3DINDEXED)
  846.     {
  847.         DWORD maxFaceInfl;
  848.         DWORD flags = D3DXMESHOPT_VERTEXCACHE;
  849.  
  850.         hr = pmcMesh->m_pSkinMesh->GetMaxFaceInfluences(&maxFaceInfl);
  851.         if (FAILED(hr))
  852.             goto e_Exit;
  853.  
  854.         // 12 entry palette guarantees that any triangle (4 independent influences per vertex of a tri)
  855.         // can be handled
  856.         maxFaceInfl = min(maxFaceInfl, 12);
  857.  
  858.         if (m_d3dCaps.MaxVertexBlendMatrixIndex + 1 < maxFaceInfl)
  859.         {
  860.             // HW does not support indexed vertex blending. Use SW instead
  861.             pmcMesh->m_paletteSize = min(256, pmcMesh->m_pSkinMesh->GetNumBones());
  862.             pmcMesh->m_bUseSW = true;
  863.             flags |= D3DXMESH_SYSTEMMEM;
  864.         }
  865.         else
  866.         {
  867.             pmcMesh->m_paletteSize = min(m_d3dCaps.MaxVertexBlendMatrixIndex + 1, pmcMesh->m_pSkinMesh->GetNumBones());
  868.             pmcMesh->m_bUseSW = false;
  869.             flags |= D3DXMESH_MANAGED;
  870.         }
  871.  
  872.         hr = pmcMesh->m_pSkinMesh->ConvertToIndexedBlendedMesh(flags, pmcMesh->m_rgiAdjacency, pmcMesh->m_paletteSize, NULL,
  873.             &pmcMesh->cpattr, &pmcMesh->m_pBoneCombinationBuf, NULL, NULL, &pmcMesh->pMesh);
  874.         if (FAILED(hr))
  875.             goto e_Exit;
  876.  
  877.         // Here we are talking of max vertex influence which we determine from 
  878.         // the FVF of the returned mesh
  879.         if ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) != D3DFVF_XYZ)
  880.         {
  881.             pmcMesh->m_maxFaceInfl = ((pmcMesh->pMesh->GetFVF() & D3DFVF_POSITION_MASK) - D3DFVF_XYZRHW) / 2;
  882.         }
  883.         else
  884.         {
  885.             pmcMesh->m_maxFaceInfl = 1;
  886.         }
  887.     }
  888.     else if (m_method == SOFTWARE)
  889.     {
  890.         hr = pmcMesh->m_pSkinMesh->GenerateSkinnedMesh
  891.                                    (
  892.                                        D3DXMESH_WRITEONLY,          // options
  893.                                        0.0f,                        // minimum bone weight allowed
  894.                                        pmcMesh->m_rgiAdjacency,     // adjacency of in-mesh
  895.                                        NULL,     // adjacency of out-mesh
  896.                                        NULL,     // face remap array
  897.                                        NULL,     // vertex remap buffer
  898.                                        &pmcMesh->pMesh              // out-mesh
  899.                                    );
  900.         if (FAILED(hr))
  901.             goto e_Exit;
  902.  
  903.         hr = pmcMesh->pMesh->GetAttributeTable(NULL, &pmcMesh->cpattr);
  904.         if (FAILED(hr))
  905.             goto e_Exit;
  906.  
  907.         delete[] pmcMesh->m_pAttrTable;
  908.         pmcMesh->m_pAttrTable  = new D3DXATTRIBUTERANGE[pmcMesh->cpattr];
  909.         if (pmcMesh->m_pAttrTable == NULL)
  910.         {
  911.             hr = E_OUTOFMEMORY;
  912.             goto e_Exit;
  913.         }
  914.  
  915.         hr = pmcMesh->pMesh->GetAttributeTable(pmcMesh->m_pAttrTable, NULL);
  916.         if (FAILED(hr))
  917.             goto e_Exit;
  918.  
  919.         hr = pmcMesh->m_pSkinMesh->GetMaxFaceInfluences(&pmcMesh->m_maxFaceInfl);
  920.         if (FAILED(hr))
  921.             goto e_Exit;
  922.  
  923.         // Allocate space for blend matrices
  924.         m_pBoneMatrices  = new D3DXMATRIXA16[m_maxBones];
  925.         if (m_pBoneMatrices == NULL)
  926.         {
  927.             hr = E_OUTOFMEMORY;
  928.             goto e_Exit;
  929.         }
  930.     }
  931.     pmcMesh->m_Method = m_method;
  932.  
  933. e_Exit:
  934.  
  935.     GXRELEASE(pDevice);
  936.     return hr;
  937. }
  938.  
  939.  
  940.  
  941.  
  942. HRESULT CMyD3DApplication::LoadMesh(LPDIRECTXFILEDATA pxofobjCur,
  943.                                     DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  944.                                     SFrame *pframeParent)
  945. {
  946.     HRESULT hr = S_OK;
  947.     SMeshContainer *pmcMesh = NULL;
  948.     LPD3DXBUFFER pbufMaterials = NULL;
  949.     LPD3DXBUFFER pbufAdjacency = NULL;
  950.     DWORD cchName;
  951.     UINT cFaces;
  952.     UINT iMaterial;
  953.     LPDIRECT3DDEVICE8 m_pDevice = m_pd3dDevice;
  954.     LPDWORD pAdjacencyIn;
  955.     
  956.     pmcMesh = new SMeshContainer();
  957.     if (pmcMesh == NULL)
  958.     {
  959.         hr = E_OUTOFMEMORY;
  960.         goto e_Exit;
  961.     }
  962.     
  963.     hr = pxofobjCur->GetName(NULL, &cchName);
  964.     if (FAILED(hr))
  965.         goto e_Exit;
  966.     
  967.     if (cchName > 0)
  968.     {
  969.         pmcMesh->szName = new char[cchName];
  970.         if (pmcMesh->szName == NULL)
  971.         {
  972.             hr = E_OUTOFMEMORY;
  973.             goto e_Exit;
  974.         }
  975.         
  976.         hr = pxofobjCur->GetName(pmcMesh->szName, &cchName);
  977.         if (FAILED(hr))
  978.             goto e_Exit;
  979.     }
  980.     
  981.     hr = D3DXLoadSkinMeshFromXof(pxofobjCur, options, pD3DDevice, &pbufAdjacency, &pbufMaterials, &pmcMesh->cMaterials, 
  982.         &pmcMesh->m_pBoneNamesBuf, &pmcMesh->m_pBoneOffsetBuf, &pmcMesh->m_pSkinMesh);
  983.     if (FAILED(hr))
  984.     {
  985.         if (hr == D3DXERR_LOADEDMESHASNODATA)
  986.             hr = S_OK;
  987.         goto e_Exit;
  988.     }
  989.     
  990.     cFaces = pmcMesh->m_pSkinMesh->GetNumFaces();
  991.  
  992.     pAdjacencyIn = static_cast<LPDWORD>(pbufAdjacency->GetBufferPointer());
  993.  
  994.     pmcMesh->m_rgiAdjacency = new DWORD[cFaces * 3];
  995.  
  996.     if (pmcMesh->m_rgiAdjacency == NULL)
  997.     {
  998.         hr = E_OUTOFMEMORY;
  999.  
  1000.         goto e_Exit;
  1001.     }
  1002.  
  1003.     memcpy(pmcMesh->m_rgiAdjacency, pAdjacencyIn, cFaces * 3 * sizeof(DWORD));
  1004.  
  1005.     // Process skinning data
  1006.     if (pmcMesh->m_pSkinMesh->GetNumBones())
  1007.     {
  1008.         // Update max bones of any mesh in the app
  1009.         m_maxBones = max(pmcMesh->m_pSkinMesh->GetNumBones(), m_maxBones);
  1010.         
  1011.         pmcMesh->m_pBoneMatrix = new D3DXMATRIX*[pmcMesh->m_pSkinMesh->GetNumBones()];
  1012.         if (pmcMesh->m_pBoneMatrix == NULL)
  1013.             goto e_Exit;
  1014.         pmcMesh->m_pBoneOffsetMat = reinterpret_cast<D3DXMATRIX*>(pmcMesh->m_pBoneOffsetBuf->GetBufferPointer());
  1015.         
  1016.         hr = GenerateMesh(pmcMesh);
  1017.  
  1018.         if (FAILED(hr))
  1019.             goto e_Exit;
  1020.     }
  1021.     else
  1022.     {
  1023.         pmcMesh->m_pSkinMesh->GetOriginalMesh(&(pmcMesh->pMesh));
  1024.         pmcMesh->m_pSkinMesh->Release();
  1025.         pmcMesh->m_pSkinMesh = NULL;
  1026.         pmcMesh->cpattr = pmcMesh->cMaterials;
  1027.     }
  1028.     
  1029.     if ((pbufMaterials == NULL) || (pmcMesh->cMaterials == 0))
  1030.     {
  1031.         pmcMesh->rgMaterials = new D3DMATERIAL8[1];
  1032.         pmcMesh->pTextures = new LPDIRECT3DTEXTURE8[1];
  1033.         if (pmcMesh->rgMaterials == NULL || pmcMesh->pTextures == NULL)
  1034.         {
  1035.             hr = E_OUTOFMEMORY;
  1036.             goto e_Exit;
  1037.         }
  1038.         
  1039.         memset(pmcMesh->rgMaterials, 0, sizeof(D3DMATERIAL8));
  1040.         pmcMesh->rgMaterials[0].Diffuse.r = 0.5f;
  1041.         pmcMesh->rgMaterials[0].Diffuse.g = 0.5f;
  1042.         pmcMesh->rgMaterials[0].Diffuse.b = 0.5f;
  1043.         pmcMesh->rgMaterials[0].Specular = pmcMesh->rgMaterials[0].Diffuse;
  1044.         pmcMesh->pTextures[0] = NULL;
  1045.     }
  1046.     else
  1047.     {
  1048.         pmcMesh->rgMaterials = new D3DMATERIAL8[pmcMesh->cMaterials];
  1049.         pmcMesh->pTextures = new LPDIRECT3DTEXTURE8[pmcMesh->cMaterials];
  1050.         if (pmcMesh->rgMaterials == NULL || pmcMesh->pTextures == NULL)
  1051.         {
  1052.             hr = E_OUTOFMEMORY;
  1053.             goto e_Exit;
  1054.         }
  1055.         
  1056.         LPD3DXMATERIAL pMaterials = (LPD3DXMATERIAL)pbufMaterials->GetBufferPointer();
  1057.         
  1058.         for (iMaterial = 0; iMaterial < pmcMesh->cMaterials; iMaterial++)
  1059.         {
  1060.             
  1061.             pmcMesh->rgMaterials[iMaterial] = pMaterials[iMaterial].MatD3D;
  1062.             
  1063.             pmcMesh->pTextures[iMaterial] = NULL;
  1064.             if (pMaterials[iMaterial].pTextureFilename != NULL)
  1065.             {
  1066.                 TCHAR szPath[MAX_PATH];
  1067.                 DXUtil_FindMediaFile(szPath, pMaterials[iMaterial].pTextureFilename);
  1068.  
  1069.                 hr = D3DXCreateTextureFromFile(m_pDevice, szPath, &(pmcMesh->pTextures[iMaterial]));
  1070.                 if (FAILED(hr))
  1071.                     pmcMesh->pTextures[iMaterial] = NULL;
  1072.             }
  1073.         }
  1074.     }
  1075.     
  1076.     // add the mesh to the parent frame
  1077.     pframeParent->AddMesh(pmcMesh);
  1078.     pmcMesh = NULL;
  1079.     
  1080. e_Exit:
  1081.     delete pmcMesh;
  1082.     
  1083.     GXRELEASE(pbufAdjacency);
  1084.     GXRELEASE(pbufMaterials);
  1085.  
  1086.     return hr;
  1087. }
  1088.  
  1089.  
  1090.  
  1091.  
  1092. HRESULT CMyD3DApplication::LoadFrames(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  1093.                                       DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  1094.                                       SFrame *pframeParent)
  1095. {
  1096.     HRESULT hr = S_OK;
  1097.     LPDIRECTXFILEDATA pxofobjChild = NULL;
  1098.     LPDIRECTXFILEOBJECT pxofChild = NULL;
  1099.     const GUID *type;
  1100.     DWORD cbSize;
  1101.     D3DXMATRIX *pmatNew;
  1102.     SFrame *pframeCur;
  1103.     DWORD cchName;
  1104.     
  1105.     // Get the type of the object
  1106.     hr = pxofobjCur->GetType(&type);
  1107.     if (FAILED(hr))
  1108.         goto e_Exit;
  1109.     
  1110.     
  1111.     if (*type == TID_D3DRMMesh)
  1112.     {
  1113.         hr = LoadMesh(pxofobjCur, options, fvf, pD3DDevice, pframeParent);
  1114.         if (FAILED(hr))
  1115.             goto e_Exit;
  1116.     }
  1117.     else if (*type == TID_D3DRMFrameTransformMatrix)
  1118.     {
  1119.         hr = pxofobjCur->GetData(NULL, &cbSize, (PVOID*)&pmatNew);
  1120.         if (FAILED(hr))
  1121.             goto e_Exit;
  1122.         
  1123.         // update the parents matrix with the new one
  1124.         pframeParent->matRot = *pmatNew;
  1125.         pframeParent->matRotOrig = *pmatNew;
  1126.     }
  1127.     else if (*type == TID_D3DRMAnimationSet)
  1128.     {
  1129.         LoadAnimationSet(pxofobjCur, pde, options, fvf, pD3DDevice, pframeParent);
  1130.     }
  1131.     else if (*type == TID_D3DRMAnimation)
  1132.     {
  1133.         LoadAnimation(pxofobjCur, pde, options, fvf, pD3DDevice, pframeParent);
  1134.     }
  1135.     else if (*type == TID_D3DRMFrame)
  1136.     {
  1137.         pframeCur = new SFrame();
  1138.         if (pframeCur == NULL)
  1139.         {
  1140.             hr = E_OUTOFMEMORY;
  1141.             goto e_Exit;
  1142.         }
  1143.         
  1144.         hr = pxofobjCur->GetName(NULL, &cchName);
  1145.         if (FAILED(hr))
  1146.             goto e_Exit;
  1147.         
  1148.         if (cchName > 0)
  1149.         {
  1150.             pframeCur->szName = new char[cchName];
  1151.             if (pframeCur->szName == NULL)
  1152.             {
  1153.                 hr = E_OUTOFMEMORY;
  1154.                 goto e_Exit;
  1155.             }
  1156.             
  1157.             hr = pxofobjCur->GetName(pframeCur->szName, &cchName);
  1158.             if (FAILED(hr))
  1159.                 goto e_Exit;
  1160.         }
  1161.         
  1162.         pframeParent->AddFrame(pframeCur);
  1163.         
  1164.         // Enumerate child objects.
  1165.         // Child object can be data, data reference or binary.
  1166.         // Use QueryInterface() to find what type of object a child is.
  1167.         while (SUCCEEDED(pxofobjCur->GetNextObject(&pxofChild)))
  1168.         {
  1169.             // Query the child for it's FileData
  1170.             hr = pxofChild->QueryInterface(IID_IDirectXFileData,
  1171.                 (LPVOID *)&pxofobjChild);
  1172.             if (SUCCEEDED(hr))
  1173.             {
  1174.                 hr = LoadFrames(pxofobjChild, pde, options, fvf, pD3DDevice, pframeCur);
  1175.                 if (FAILED(hr))
  1176.                     goto e_Exit;
  1177.                 
  1178.                 GXRELEASE(pxofobjChild);
  1179.             }
  1180.             
  1181.             GXRELEASE(pxofChild);
  1182.         }
  1183.         
  1184.     }
  1185.     
  1186. e_Exit:
  1187.     GXRELEASE(pxofobjChild);
  1188.     GXRELEASE(pxofChild);
  1189.     return hr;
  1190. }
  1191.  
  1192.  
  1193.          
  1194.                                       
  1195. HRESULT  CMyD3DApplication::DeleteSelectedMesh()
  1196. {
  1197.     if (m_pdeSelected != NULL)
  1198.     {
  1199.         SDrawElement *pdeCur = m_pdeHead;
  1200.         SDrawElement *pdePrev = NULL;
  1201.         while ((pdeCur != NULL) && (pdeCur != m_pdeSelected))
  1202.         {
  1203.             pdePrev = pdeCur;
  1204.             pdeCur = pdeCur->pdeNext;
  1205.         }
  1206.  
  1207.         if (pdePrev == NULL)
  1208.         {
  1209.             m_pdeHead = m_pdeHead->pdeNext;
  1210.         }
  1211.         else
  1212.         {
  1213.             pdePrev->pdeNext = pdeCur->pdeNext;
  1214.         }
  1215.  
  1216.         m_pdeSelected->pdeNext = NULL;
  1217.         if (m_pdeHead == m_pdeSelected)
  1218.             m_pdeHead = NULL;
  1219.         delete m_pdeSelected;
  1220.         m_pdeSelected = NULL;
  1221.     }
  1222.  
  1223.     return S_OK;
  1224. }
  1225.