home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / direct3d / bumpmapping / bumpwaves / bumpwaves.cpp next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  19.0 KB  |  514 lines

  1. //-----------------------------------------------------------------------------
  2. // File: BumpWaves.cpp
  3. //
  4. // Desc: Code to simulate reflections off waves using bumpmapping.
  5. //
  6. //       Note: This code uses the D3D Framework helper library.
  7. //
  8. // Copyright (c) 1998-2000 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <tchar.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <D3DX8.h>
  15. #include "D3DApp.h"
  16. #include "D3DFont.h"
  17. #include "D3DUtil.h"
  18. #include "DXUtil.h"
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Function prototypes and global (or static) variables
  25. //-----------------------------------------------------------------------------
  26. inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
  27.  
  28. struct VERTEX
  29. {
  30.     D3DXVECTOR3 p;
  31.     D3DXVECTOR3 n;
  32.     FLOAT       tu, tv;
  33. };
  34.  
  35. #define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
  36.  
  37.  
  38.  
  39.  
  40. //-----------------------------------------------------------------------------
  41. // Name: class CMyD3DApplication
  42. // Desc: Application class. The base class (CD3DApplication) provides the 
  43. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  44. //       adds functionality specific to this sample program.
  45. //-----------------------------------------------------------------------------
  46. class CMyD3DApplication : public CD3DApplication
  47. {
  48.     CD3DFont*               m_pFont;
  49.     LPDIRECT3DVERTEXBUFFER8 m_pBackgroundVB;
  50.     LPDIRECT3DTEXTURE8      m_pBackgroundTexture;
  51.  
  52.     LPDIRECT3DVERTEXBUFFER8 m_pWaterVB;
  53.     LPDIRECT3DTEXTURE8      m_psBumpMap;
  54.     D3DXMATRIX              m_matBumpMat;
  55.  
  56.     LPDIRECT3DTEXTURE8 CreateBumpMap( DWORD, DWORD, D3DFORMAT );
  57.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  58.  
  59. protected:
  60.     HRESULT OneTimeSceneInit();
  61.     HRESULT InitDeviceObjects();
  62.     HRESULT RestoreDeviceObjects();
  63.     HRESULT InvalidateDeviceObjects();
  64.     HRESULT DeleteDeviceObjects();
  65.     HRESULT Render();
  66.     HRESULT FrameMove();
  67.     HRESULT FinalCleanup();
  68.  
  69.     UINT m_n;               // Number of vertices in the ground grid along X
  70.     UINT m_m;               // Number of vertices in the ground grid along Z
  71.     UINT m_nTriangles;      // Number of triangles in the ground grid
  72.  
  73. public:
  74.     CMyD3DApplication();
  75. };
  76.  
  77.  
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Name: WinMain()
  82. // Desc: Entry point to the program. Initializes everything, and goes into a
  83. //       message-processing loop. Idle time is used to render the scene.
  84. //-----------------------------------------------------------------------------
  85. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  86. {
  87.     CMyD3DApplication d3dApp;
  88.  
  89.     if( FAILED( d3dApp.Create( hInst ) ) )
  90.         return 0;
  91.  
  92.     return d3dApp.Run();
  93. }
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: CMyD3DApplication()
  100. // Desc: Application constructor. Sets attributes for the app.
  101. //-----------------------------------------------------------------------------
  102. CMyD3DApplication::CMyD3DApplication()
  103. {
  104.     m_strWindowTitle    = _T("BumpWaves: Using BumpMapping For Waves");
  105.     m_bUseDepthBuffer   = FALSE;
  106.  
  107.     m_pFont              = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  108.     m_psBumpMap          = NULL;
  109.     m_pBackgroundTexture = NULL;
  110.     m_pBackgroundVB      = NULL;
  111.     m_pWaterVB           = NULL;
  112.  
  113.     // The following are set in InitDeviceObjects
  114.     m_n = 0;
  115.     m_m = 0;
  116.     m_nTriangles = 0;
  117. }
  118.  
  119.  
  120.  
  121.  
  122. //-----------------------------------------------------------------------------
  123. // Name: OneTimeSceneInit()
  124. // Desc: Called during initial app startup, this function performs all the
  125. //       permanent initialization.
  126. //-----------------------------------------------------------------------------
  127. HRESULT CMyD3DApplication::OneTimeSceneInit()
  128. {
  129.     return S_OK;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. //-----------------------------------------------------------------------------
  136. // Name: FrameMove()
  137. // Desc: Called once per frame, the call is the entry point for animating
  138. //       the scene.
  139. //-----------------------------------------------------------------------------
  140. HRESULT CMyD3DApplication::FrameMove()
  141. {
  142.     // Setup the bump matrix
  143.     // Min r is 0.04 if amplitude is 32 to miss temporal aliasing
  144.     // Max r is 0.16 for amplitude is 8 to miss spatial aliasing
  145.     FLOAT r = 0.04f;
  146.     m_matBumpMat._11 =  r * cosf( m_fTime * 9.0f );
  147.     m_matBumpMat._12 = -r * sinf( m_fTime * 9.0f );
  148.     m_matBumpMat._21 =  r * sinf( m_fTime * 9.0f );
  149.     m_matBumpMat._22 =  r * cosf( m_fTime * 9.0f );
  150.  
  151.     return S_OK;
  152. }
  153.  
  154.  
  155.  
  156.  
  157. //-----------------------------------------------------------------------------
  158. // Name: Render()
  159. // Desc: Called once per frame, the call is the entry point for 3d
  160. //       rendering. This function sets up render states, clears the
  161. //       viewport, and renders the scene.
  162. //-----------------------------------------------------------------------------
  163. HRESULT CMyD3DApplication::Render()
  164. {
  165.     // Clear the render target
  166.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0L );
  167.     
  168.     // Begin the scene
  169.     if( FAILED( m_pd3dDevice->BeginScene() ) )
  170.         return S_OK;
  171.  
  172.     // Set up texture stage states for the background
  173.     m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
  174.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
  175.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
  176.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  177.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  178.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
  179.  
  180.     // Render the background
  181.     m_pd3dDevice->SetVertexShader( D3DFVF_VERTEX );
  182.     m_pd3dDevice->SetStreamSource( 0, m_pBackgroundVB, sizeof(VERTEX) );
  183.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  184.  
  185.     // Set up texture stage 0's states for the bumpmap
  186.     m_pd3dDevice->SetTexture( 0, m_psBumpMap );
  187.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
  188.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP );
  189.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP );
  190.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT00,   F2DW( m_matBumpMat._11 ) );
  191.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT01,   F2DW( m_matBumpMat._12 ) );
  192.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT10,   F2DW( m_matBumpMat._21 ) );
  193.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT11,   F2DW( m_matBumpMat._22 ) );
  194.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_BUMPENVMAP );
  195.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  196.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_CURRENT );
  197.  
  198.     // Set up texture stage 1's states for the environment map
  199.     m_pd3dDevice->SetTexture( 1, m_pBackgroundTexture );
  200.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
  201.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  202.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  203.  
  204.     // Set up projected texture coordinates
  205.     // tu = (0.8x + 0.5z) / z
  206.     // tv = (0.8y - 0.5z) / z
  207.     D3DXMATRIX mat;
  208.     mat._11 = 0.8f; mat._12 = 0.0f; mat._13 = 0.0f;
  209.     mat._21 = 0.0f; mat._22 = 0.8f; mat._23 = 0.0f;
  210.     mat._31 = 0.5f; mat._32 =-0.5f; mat._33 = 1.0f;
  211.     mat._41 = 0.0f; mat._42 = 0.0f; mat._43 = 0.0f;
  212.  
  213.     m_pd3dDevice->SetTransform( D3DTS_TEXTURE1, &mat );
  214.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3|D3DTTFF_PROJECTED );
  215.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION | 1 );
  216.  
  217.     // Render the water
  218.     m_pd3dDevice->SetVertexShader( D3DFVF_VERTEX );
  219.     m_pd3dDevice->SetStreamSource( 0, m_pWaterVB, sizeof(VERTEX) );
  220.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_nTriangles );
  221.  
  222.     // Output statistics
  223.     m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  224.     m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  225.  
  226.     // End the scene.
  227.     m_pd3dDevice->EndScene();
  228.  
  229.     return S_OK;
  230. }
  231.  
  232.  
  233.  
  234.  
  235. //-----------------------------------------------------------------------------
  236. // Name: CreateBumpMap()
  237. // Desc: Creates a bumpmap
  238. //-----------------------------------------------------------------------------
  239. LPDIRECT3DTEXTURE8 CMyD3DApplication::CreateBumpMap( DWORD dwWidth, DWORD dwHeight,
  240.                                                      D3DFORMAT d3dBumpFormat )
  241. {
  242.     LPDIRECT3DTEXTURE8 psBumpMap;
  243.  
  244.     // Check if the device can create the format
  245.     if( FAILED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  246.                                            m_d3dCaps.DeviceType,
  247.                                            m_d3dsdBackBuffer.Format,
  248.                                            0, D3DRTYPE_TEXTURE, d3dBumpFormat ) ) )
  249.         return NULL;
  250.  
  251.     // Create the bump map texture
  252.     if( FAILED( m_pd3dDevice->CreateTexture( dwWidth, dwHeight, 1, 0 /* Usage */,
  253.                                              d3dBumpFormat, D3DPOOL_MANAGED,
  254.                                              &psBumpMap ) ) )
  255.         return NULL;
  256.  
  257.     // Lock the surface and write in some bumps for the waves
  258.     D3DLOCKED_RECT d3dlr;
  259.     psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
  260.     CHAR* pDst = (CHAR*)d3dlr.pBits;
  261.     CHAR  iDu, iDv;
  262.  
  263.     for( DWORD y=0; y<dwHeight; y++ )
  264.     {
  265.         CHAR* pPixel = pDst;
  266.  
  267.         for( DWORD x=0; x<dwWidth; x++ )
  268.         {
  269.             FLOAT fx = x/(FLOAT)dwWidth - 0.5f;
  270.             FLOAT fy = y/(FLOAT)dwHeight - 0.5f;
  271.  
  272.             FLOAT r = sqrtf( fx*fx + fy*fy );
  273.  
  274.             iDu  = (CHAR)( 64 * cosf( 300.0f * r ) * expf( -r * 5.0f ) );
  275.             iDu += (CHAR)( 32 * cosf( 150.0f * ( fx + fy ) ) );
  276.             iDu += (CHAR)( 16 * cosf( 140.0f * ( fx * 0.85f - fy ) ) );
  277.  
  278.             iDv  = (CHAR)( 64 * sinf( 300.0f * r ) * expf( -r * 5.0f ) );
  279.             iDv += (CHAR)( 32 * sinf( 150.0f * ( fx + fy ) ) );
  280.             iDv += (CHAR)( 16 * sinf( 140.0f * ( fx * 0.85f - fy ) ) );
  281.  
  282.             *pPixel++ = iDu;
  283.             *pPixel++ = iDv;
  284.         }
  285.         pDst += d3dlr.Pitch;
  286.     }
  287.     psBumpMap->UnlockRect(0);
  288.  
  289.     return psBumpMap;
  290. }
  291.  
  292.  
  293.  
  294.  
  295. //-----------------------------------------------------------------------------
  296. // Name: InitDeviceObjects()
  297. // Desc: Initialize scene objects
  298. //-----------------------------------------------------------------------------
  299. HRESULT CMyD3DApplication::InitDeviceObjects()
  300. {
  301.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  302.  
  303.     // Load the texture for the background image
  304.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"),
  305.                                        &m_pBackgroundTexture, D3DFMT_R5G6B5 ) ) )
  306.         return D3DAPPERR_MEDIANOTFOUND;
  307.  
  308.     // Create the bumpmap. 
  309.     m_psBumpMap = CreateBumpMap( 256, 256, D3DFMT_V8U8 );
  310.     if( NULL == m_psBumpMap )
  311.         return E_FAIL;
  312.  
  313.     // Create a square for rendering the background
  314.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX),
  315.                                                   D3DUSAGE_WRITEONLY, D3DFVF_VERTEX,
  316.                                                   D3DPOOL_MANAGED, &m_pBackgroundVB ) ) )
  317.         return E_FAIL;
  318.     VERTEX* v;
  319.     m_pBackgroundVB->Lock( 0, 0, (BYTE**)&v, 0 );
  320.     v[0].p  = D3DXVECTOR3(-1000.0f,    0.0f, 0.0f );
  321.     v[1].p  = D3DXVECTOR3(-1000.0f, 1000.0f, 0.0f );
  322.     v[2].p  = D3DXVECTOR3( 1000.0f,    0.0f, 0.0f );
  323.     v[3].p  = D3DXVECTOR3( 1000.0f, 1000.0f, 0.0f );
  324.     v[0].n  = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
  325.     v[1].n  = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
  326.     v[2].n  = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
  327.     v[3].n  = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
  328.     v[0].tu = 0.0f; v[0].tv = 147/256.0f;
  329.     v[1].tu = 0.0f; v[1].tv = 0.0f;
  330.     v[2].tu = 1.0f; v[2].tv = 147/256.0f;
  331.     v[3].tu = 1.0f; v[3].tv = 0.0f;
  332.     m_pBackgroundVB->Unlock();
  333.  
  334.     // If D3DPTEXTURECAPS_PROJECTED is set, projected textures are computed
  335.     // per pixel, so this sample will work fine with just a quad for the water
  336.     // model.  If it's not set, textures are projected per vertex rather than 
  337.     // per pixel, so distortion will be visible unless we use more vertices.
  338.     if( m_d3dCaps.TextureCaps & D3DPTEXTURECAPS_PROJECTED )
  339.     {
  340.         m_n = 2;               // Number of vertices in the ground grid along X
  341.         m_m = 2;               // Number of vertices in the ground grid along Z
  342.     }
  343.     else
  344.     {
  345.         m_n = 8;               // Number of vertices in the ground grid along X
  346.         m_m = 8;               // Number of vertices in the ground grid along Z
  347.     }
  348.     m_nTriangles = (m_n-1)*(m_m-1)*2;   // Number of triangles in the ground
  349.  
  350.     // Create a square grid m_n*m_m for rendering the water
  351.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_nTriangles*3*sizeof(VERTEX),
  352.                                                   D3DUSAGE_WRITEONLY, D3DFVF_VERTEX,
  353.                                                   D3DPOOL_MANAGED, &m_pWaterVB ) ) )
  354.         return E_FAIL;
  355.     m_pWaterVB->Lock( 0, 0, (BYTE**)&v, 0 );
  356.     float dX = 2000.0f/(m_n-1);
  357.     float dZ = 1250.0f/(m_m-1);
  358.     float x0 = -1000;
  359.     float z0 = -1250;
  360.     float dU = 1.0f/(m_n-1);
  361.     float dV = 0.7f/(m_m-1);
  362.     UINT k = 0;
  363.     for (UINT z=0; z < (m_m-1); z++)
  364.     {
  365.         for (UINT x=0; x < (m_n-1); x++)
  366.         {
  367.             v[k].p  = D3DXVECTOR3(x0 + x*dX, 0.0f, z0 + z*dZ );
  368.             v[k].n  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  369.             v[k].tu = x*dU; 
  370.             v[k].tv = z*dV;
  371.             k++;
  372.             v[k].p  = D3DXVECTOR3(x0 + x*dX, 0.0f, z0 + (z+1)*dZ );
  373.             v[k].n  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  374.             v[k].tu = x*dU; 
  375.             v[k].tv = (z+1)*dV;
  376.             k++;
  377.             v[k].p  = D3DXVECTOR3(x0 + (x+1)*dX, 0.0f, z0 + (z+1)*dZ );
  378.             v[k].n  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  379.             v[k].tu = (x+1)*dU; 
  380.             v[k].tv = (z+1)*dV;
  381.             k++;
  382.             v[k].p  = D3DXVECTOR3(x0 + x*dX, 0.0f, z0 + z*dZ );
  383.             v[k].n  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  384.             v[k].tu = x*dU; 
  385.             v[k].tv = z*dV;
  386.             k++;
  387.             v[k].p  = D3DXVECTOR3(x0 + (x+1)*dX, 0.0f, z0 + (z+1)*dZ );
  388.             v[k].n  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  389.             v[k].tu = (x+1)*dU; 
  390.             v[k].tv = (z+1)*dV;
  391.             k++;
  392.             v[k].p  = D3DXVECTOR3(x0 + (x+1)*dX, 0.0f, z0 + z*dZ );
  393.             v[k].n  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  394.             v[k].tu = (x+1)*dU; 
  395.             v[k].tv = z*dV;
  396.             k++;
  397.         }
  398.     }
  399.     m_pWaterVB->Unlock();
  400.  
  401.     return S_OK;
  402. }
  403.  
  404. //-----------------------------------------------------------------------------
  405. // Name: RestoreDeviceObjects()
  406. // Desc: Initialize scene objects
  407. //-----------------------------------------------------------------------------
  408. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  409. {
  410.     m_pFont->RestoreDeviceObjects();
  411.  
  412.     // Set the transform matrices
  413.     D3DXVECTOR3 vEyePt(    0.0f, 400.0f, -1650.0f );
  414.     D3DXVECTOR3 vLookatPt( 0.0f,   0.0f,     0.0f );
  415.     D3DXVECTOR3 vUpVec(    0.0f,   1.0f,     0.0f );
  416.     D3DXMATRIX matWorld, matView, matProj;
  417.  
  418.     D3DXMatrixIdentity( &matWorld );
  419.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  420.     D3DXMatrixPerspectiveFovLH( &matProj, 1.00f, 1.0f, 1.0f, 10000.0f );
  421.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  422.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  423.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  424.  
  425.     // Set any appropiate state
  426.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0xffffffff );
  427.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   TRUE );
  428.     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
  429.  
  430.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  431.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  432.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  433.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  434.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  435.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  436.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  437.  
  438.     return S_OK;
  439. }
  440.  
  441.  
  442.  
  443.  
  444. //-----------------------------------------------------------------------------
  445. // Name: InvalidateDeviceObjects()
  446. // Desc:
  447. //-----------------------------------------------------------------------------
  448. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  449. {
  450.     m_pFont->InvalidateDeviceObjects();
  451.     return S_OK;
  452. }
  453.  
  454.  
  455.  
  456.  
  457. //-----------------------------------------------------------------------------
  458. // Name: DeleteDeviceObjects()
  459. // Desc: Called when the app is exiting, or the device is being changed,
  460. //       this function deletes any device dependent objects.
  461. //-----------------------------------------------------------------------------
  462. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  463. {
  464.     m_pFont->DeleteDeviceObjects();
  465.     SAFE_RELEASE( m_pBackgroundTexture );
  466.     SAFE_RELEASE( m_psBumpMap );
  467.     SAFE_RELEASE( m_pBackgroundVB );
  468.     SAFE_RELEASE( m_pWaterVB );
  469.  
  470.     return S_OK;
  471. }
  472.  
  473.  
  474.  
  475.  
  476. //-----------------------------------------------------------------------------
  477. // Name: FinalCleanup()
  478. // Desc: Called before the app exits, this function gives the app the chance
  479. //       to cleanup after itself.
  480. //-----------------------------------------------------------------------------
  481. HRESULT CMyD3DApplication::FinalCleanup()
  482. {
  483.     SAFE_DELETE( m_pFont );
  484.     return S_OK;
  485. }
  486.  
  487.  
  488.  
  489.  
  490. //-----------------------------------------------------------------------------
  491. // Name: ConfirmDevice()
  492. // Desc: Called during device intialization, this code checks the device
  493. //       for some minimum set of capabilities
  494. //-----------------------------------------------------------------------------
  495. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  496.                                           D3DFORMAT Format )
  497. {
  498.     // Device must be able to do bumpmapping
  499.     if( 0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP ) )
  500.         return E_FAIL;
  501.  
  502.     // Accept devices that can create D3DFMT_V8U8 textures
  503.     if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  504.                                               pCaps->DeviceType, Format,
  505.                                               0, D3DRTYPE_TEXTURE,
  506.                                               D3DFMT_V8U8 ) ) )
  507.         return S_OK;
  508.  
  509.     // Else, reject the device
  510.     return E_FAIL;
  511. }
  512.  
  513.  
  514.