3DModel.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
源码类别:

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: 3DModel.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: C3DModel()
  9. // Desc:
  10. //-----------------------------------------------------------------------------
  11. C3DModel::C3DModel() 
  12. {
  13.     m_pMesh = NULL;
  14.     m_vBoundingMax = D3DXVECTOR3(0,0,0);
  15.     m_vBoundingMin = D3DXVECTOR3(0,0,0);
  16.     m_vCenter      = D3DXVECTOR3(0,0,0);
  17.     m_fRadius      = 0.0f;
  18.     m_dwNumFaces   = 0;
  19.     m_pBoundingVB  = NULL;
  20. }
  21. //-----------------------------------------------------------------------------
  22. // Name: C3DModel()
  23. // Desc:
  24. //-----------------------------------------------------------------------------
  25. C3DModel::~C3DModel()
  26. {
  27.     FinalCleanup();
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Name: OneTimeSceneInit()
  31. // Desc:
  32. //-----------------------------------------------------------------------------
  33. HRESULT C3DModel::OneTimeSceneInit()
  34. {
  35.     return S_OK;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Name: InitDeviceObjects()
  39. // Desc:
  40. //-----------------------------------------------------------------------------
  41. HRESULT C3DModel::InitDeviceObjects( TCHAR* strModelName, const BOOL bCreateTest, float fBoundingScale ) 
  42. {
  43.     HRESULT hr;
  44.     m_pMesh = new CD3DMesh();
  45.     if( NULL == m_pMesh )
  46.         return E_OUTOFMEMORY;
  47.     if( bCreateTest ) 
  48.     {
  49.         ID3DXMesh* pD3DXMesh; 
  50.         D3DXCreateSphere( g_pd3dDevice, 1.0f, 16, 16, &pD3DXMesh, NULL );
  51.         m_pMesh->m_pSysMemMesh = pD3DXMesh;
  52.         m_pMesh->m_dwNumMaterials = 1;
  53.         m_pMesh->m_pMaterials = new D3DMATERIAL9[1];
  54.         if( NULL == m_pMesh->m_pMaterials )
  55.             return E_OUTOFMEMORY;
  56.         ZeroMemory( &m_pMesh->m_pMaterials[0], sizeof(D3DMATERIAL9) );
  57.         m_pMesh->m_pMaterials[0].Diffuse.r = m_pMesh->m_pMaterials[0].Ambient.r = 1.0f;
  58.         m_pMesh->m_pMaterials[0].Diffuse.g = m_pMesh->m_pMaterials[0].Ambient.g = 0.0f;
  59.         m_pMesh->m_pMaterials[0].Diffuse.b = m_pMesh->m_pMaterials[0].Ambient.b = 0.0f;
  60.         m_pMesh->m_pMaterials[0].Diffuse.a = m_pMesh->m_pMaterials[0].Ambient.a = 1.0f;
  61.         m_pMesh->m_pTextures  = new LPDIRECT3DTEXTURE9[1];
  62.         if( NULL == m_pMesh->m_pTextures )
  63.             return E_OUTOFMEMORY;
  64.         m_pMesh->m_pTextures[0] = NULL;
  65.     }
  66.     else
  67.     {
  68.         if( FAILED( hr = m_pMesh->Create( g_pd3dDevice, strModelName ) ) )
  69.             return hr;
  70.     }
  71.     // Fix the m_pMesh->m_pMaterials[i].Ambient since the .x file doesn't update this value
  72.     if( m_pMesh )
  73.     {
  74.         for( DWORD i=0; i<m_pMesh->m_dwNumMaterials; i++ )
  75.             m_pMesh->m_pMaterials[i].Ambient = m_pMesh->m_pMaterials[i].Diffuse;
  76.     }
  77.     if( m_pMesh->m_pSysMemMesh )
  78.     {
  79.         m_dwNumFaces = m_pMesh->m_pSysMemMesh->GetNumFaces();
  80.         BYTE* pData = NULL;
  81.         if( SUCCEEDED( m_pMesh->m_pSysMemMesh->LockVertexBuffer( D3DLOCK_READONLY, (void**) &pData ) ) )
  82.         {           
  83.             DWORD dwStride = D3DXGetFVFVertexSize( m_pMesh->m_pSysMemMesh->GetFVF() );
  84.             D3DXComputeBoundingSphere( (D3DXVECTOR3*)pData, m_pMesh->m_pSysMemMesh->GetNumVertices(), 
  85.                                        dwStride, 
  86.                                        &m_vCenter, &m_fRadius );
  87.             D3DXComputeBoundingBox( (D3DXVECTOR3*)pData, m_pMesh->m_pSysMemMesh->GetNumVertices(), 
  88.                                     dwStride, 
  89.                                     &m_vBoundingMin, &m_vBoundingMax );
  90.             
  91.             float fLargest = max(max(m_vBoundingMax.x,m_vBoundingMax.y),m_vBoundingMax.z);
  92.             fLargest *= fBoundingScale;
  93.             m_vBoundingMin = D3DXVECTOR3(-fLargest,-fLargest,-fLargest);
  94.             m_vBoundingMax = D3DXVECTOR3(fLargest,fLargest,fLargest);
  95.             m_pMesh->m_pSysMemMesh->UnlockVertexBuffer();
  96.             m_vBoundingVertex[0] = D3DXVECTOR3( m_vBoundingMin.x, m_vBoundingMin.y, m_vBoundingMin.z ); // xyz
  97.             m_vBoundingVertex[1] = D3DXVECTOR3( m_vBoundingMax.x, m_vBoundingMin.y, m_vBoundingMin.z ); // Xyz
  98.             m_vBoundingVertex[2] = D3DXVECTOR3( m_vBoundingMin.x, m_vBoundingMax.y, m_vBoundingMin.z ); // xYz
  99.             m_vBoundingVertex[3] = D3DXVECTOR3( m_vBoundingMax.x, m_vBoundingMax.y, m_vBoundingMin.z ); // XYz
  100.             m_vBoundingVertex[4] = D3DXVECTOR3( m_vBoundingMin.x, m_vBoundingMin.y, m_vBoundingMax.z ); // xyZ
  101.             m_vBoundingVertex[5] = D3DXVECTOR3( m_vBoundingMax.x, m_vBoundingMin.y, m_vBoundingMax.z ); // XyZ
  102.             m_vBoundingVertex[6] = D3DXVECTOR3( m_vBoundingMin.x, m_vBoundingMax.y, m_vBoundingMax.z ); // xYZ
  103.             m_vBoundingVertex[7] = D3DXVECTOR3( m_vBoundingMax.x, m_vBoundingMax.y, m_vBoundingMax.z ); // XYZ
  104.         }
  105.         hr = g_pd3dDevice->CreateVertexBuffer( 24 * sizeof(LINELIST_VERTEX),
  106.                                             D3DUSAGE_WRITEONLY,
  107.                                             LINELIST_FVF, D3DPOOL_MANAGED,
  108.                                             &m_pBoundingVB, NULL );
  109.         if( SUCCEEDED(hr) )
  110.         {
  111.             LINELIST_VERTEX* pVertexBuffer = NULL;
  112.             hr = m_pBoundingVB->Lock( 0, 0, (void**) &pVertexBuffer, 0 );
  113.             if( SUCCEEDED(hr) )
  114.             {
  115.                 DWORD dw[24] = { 0,1, 2,3, 0,2, 1,3, 4,5, 6,7, 4,6, 5,7, 0,4, 2,6, 1,5, 3,7 };
  116.                 for( int i=0; i<24; i++ )
  117.                 {
  118.                     pVertexBuffer->p = m_vBoundingVertex[ dw[i] ];
  119.                     pVertexBuffer->c = 0xFFFFFFFF;
  120.                     pVertexBuffer++;
  121.                 }
  122.                 m_pBoundingVB->Unlock(); 
  123.             }
  124.         }
  125.     }
  126.     return S_OK;
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Name: RestoreDeviceObjects()
  130. // Desc:
  131. //-----------------------------------------------------------------------------
  132. HRESULT C3DModel::RestoreDeviceObjects()
  133. {
  134.     if( m_pMesh )
  135.         return m_pMesh->RestoreDeviceObjects( g_pd3dDevice ); 
  136.     return S_OK;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Name: Render()
  140. // Desc:
  141. //-----------------------------------------------------------------------------
  142. HRESULT C3DModel::Render( DWORD* pdwNumVerts )
  143. {
  144.     *pdwNumVerts = m_dwNumFaces;
  145.     if( m_pMesh )
  146.         return m_pMesh->Render( g_pd3dDevice ); 
  147.     return S_OK;
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Name: Render()
  151. // Desc:
  152. //-----------------------------------------------------------------------------
  153. HRESULT C3DModel::RenderBoundingBox()
  154. {
  155.     D3DMATERIAL9 mtrl;
  156.     g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  157.     g_pd3dDevice->SetTexture( 0, NULL );
  158.     ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
  159.     mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
  160.     mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
  161.     mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
  162.     mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
  163.     g_pd3dDevice->SetMaterial( &mtrl );
  164.     g_pd3dDevice->SetFVF( LINELIST_FVF );
  165.     g_pd3dDevice->SetStreamSource( 0, m_pBoundingVB, 0, sizeof(LINELIST_VERTEX) );
  166.     g_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 12 );
  167.     g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  168.     return S_OK;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Name: InvalidateDeviceObjects()
  172. // Desc:
  173. //-----------------------------------------------------------------------------
  174. HRESULT C3DModel::InvalidateDeviceObjects()
  175. {
  176.     if( m_pMesh )
  177.         return m_pMesh->InvalidateDeviceObjects(); 
  178.     return S_OK;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Name: DeleteDeviceObjects()
  182. // Desc:
  183. //-----------------------------------------------------------------------------
  184. HRESULT C3DModel::DeleteDeviceObjects()
  185. {
  186.     SAFE_RELEASE( m_pBoundingVB );
  187.     return S_OK;
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Name: FinalCleanup()
  191. // Desc:
  192. //-----------------------------------------------------------------------------
  193. VOID C3DModel::FinalCleanup()
  194. {
  195.     if( m_pMesh )
  196.     {
  197.         m_pMesh->Destroy(); 
  198.         SAFE_DELETE( m_pMesh );
  199.     }
  200. }