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

  1. //-----------------------------------------------------------------------------
  2. // File: BumpLens.cpp
  3. //
  4. // Desc: Code to simulate a magnifying glass using bumpmapping.
  5. //
  6. // Note: Based on a sample from the Matrox web site
  7. //
  8. // Copyright (c) 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. #include "resource.h"
  20.  
  21.  
  22.  
  23.  
  24. //-----------------------------------------------------------------------------
  25. // Function prototypes and global (or static) variables
  26. //-----------------------------------------------------------------------------
  27. inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
  28.  
  29. struct BUMPVERTEX          // Vertex type used for bumpmap lens effect
  30. {
  31.     D3DXVECTOR3 p;
  32.     FLOAT       tu1, tv1;
  33.     FLOAT       tu2, tv2;
  34. };
  35.  
  36. struct BACKGROUNDVERTEX    // Vertex type used for rendering background
  37. {
  38.     D3DXVECTOR4 p;
  39.     DWORD       color;
  40.     FLOAT       tu, tv;
  41. };
  42.  
  43. #define D3DFVF_BUMPVERTEX       (D3DFVF_XYZ|D3DFVF_TEX2)
  44. #define D3DFVF_BACKGROUNDVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
  45.  
  46.  
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Name: class CMyD3DApplication
  51. // Desc: Application class. The base class (CD3DApplication) provides the 
  52. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  53. //       adds functionality specific to this sample program.
  54. //-----------------------------------------------------------------------------
  55. class CMyD3DApplication : public CD3DApplication
  56. {
  57.     CD3DFont*               m_pFont;
  58.  
  59.     LPDIRECT3DVERTEXBUFFER8 m_pBackgroundVB;
  60.     LPDIRECT3DVERTEXBUFFER8 m_pLensVB;
  61.  
  62.     FLOAT                   m_fLensX;
  63.     FLOAT                   m_fLensY;
  64.  
  65.     LPDIRECT3DTEXTURE8      m_pBumpMapTexture;
  66.     LPDIRECT3DTEXTURE8      m_pBackgroundTexture;
  67.  
  68.     BOOL                    m_bDeviceValidationFailed;
  69.  
  70.     HRESULT CreateBumpMap( UINT iWidth, UINT iHeight );
  71.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  72.  
  73. protected:
  74.     HRESULT OneTimeSceneInit();
  75.     HRESULT InitDeviceObjects();
  76.     HRESULT RestoreDeviceObjects();
  77.     HRESULT InvalidateDeviceObjects();
  78.     HRESULT DeleteDeviceObjects();
  79.     HRESULT Render();
  80.     HRESULT FrameMove();
  81.     HRESULT FinalCleanup();
  82.  
  83. public:
  84.     CMyD3DApplication();
  85. };
  86.  
  87.  
  88.  
  89.  
  90. //-----------------------------------------------------------------------------
  91. // Name: WinMain()
  92. // Desc: Entry point to the program. Initializes everything, and goes into a
  93. //       message-processing loop. Idle time is used to render the scene.
  94. //-----------------------------------------------------------------------------
  95. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  96. {
  97.     CMyD3DApplication d3dApp;
  98.  
  99.     if( FAILED( d3dApp.Create( hInst ) ) )
  100.         return 0;
  101.  
  102.     return d3dApp.Run();
  103. }
  104.  
  105.  
  106.  
  107.  
  108. //-----------------------------------------------------------------------------
  109. // Name: CMyD3DApplication()
  110. // Desc: Application constructor. Sets attributes for the app.
  111. //-----------------------------------------------------------------------------
  112. CMyD3DApplication::CMyD3DApplication()
  113. {
  114.     m_strWindowTitle     = _T("BumpLens: Lens Effect Using BumpMapping");
  115.     m_bUseDepthBuffer    = FALSE;
  116.  
  117.     m_pFont              = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  118.  
  119.     m_pBumpMapTexture    = NULL;
  120.     m_pBackgroundTexture = NULL;
  121.  
  122.     m_pBackgroundVB      = NULL;
  123.     m_pLensVB            = NULL;
  124.     m_bDeviceValidationFailed = FALSE;
  125. }
  126.  
  127.  
  128.  
  129.  
  130. //-----------------------------------------------------------------------------
  131. // Name: OneTimeSceneInit()
  132. // Desc: Called during initial app startup, this function performs all the
  133. //       permanent initialization.
  134. //-----------------------------------------------------------------------------
  135. HRESULT CMyD3DApplication::OneTimeSceneInit()
  136. {
  137.     return S_OK;
  138. }
  139.  
  140.  
  141.  
  142.  
  143. //-----------------------------------------------------------------------------
  144. // Name: FrameMove()
  145. // Desc: Called once per frame, the call is the entry point for animating
  146. //       the scene.
  147. //-----------------------------------------------------------------------------
  148. HRESULT CMyD3DApplication::FrameMove()
  149. {
  150.     // Get a triangle wave between -1 and 1
  151.     m_fLensX = 2 * fabsf( 2 * ( (m_fTime/2) - floorf(m_fTime/2) ) - 1 ) - 1;
  152.  
  153.     // Get a regulated sine wave between -1 and 1
  154.     m_fLensY = 2 * fabsf( sinf( m_fTime ) ) - 1;
  155.  
  156.     return S_OK;
  157. }
  158.  
  159.  
  160.  
  161.  
  162. //-----------------------------------------------------------------------------
  163. // Name: Render()
  164. // Desc: Called once per frame, the call is the entry point for 3d
  165. //       rendering. This function sets up render states, clears the
  166. //       viewport, and renders the scene.
  167. //-----------------------------------------------------------------------------
  168. HRESULT CMyD3DApplication::Render()
  169. {
  170.     // Begin the scene
  171.     if( FAILED( m_pd3dDevice->BeginScene() ) )
  172.         return S_OK;
  173.  
  174.     // Render the background
  175.     m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
  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.     m_pd3dDevice->SetVertexShader( D3DFVF_BACKGROUNDVERTEX );
  181.     m_pd3dDevice->SetStreamSource( 0, m_pBackgroundVB, sizeof(BACKGROUNDVERTEX) );
  182.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  183.  
  184.     // Render the lens
  185.     m_pd3dDevice->SetTexture( 0, m_pBumpMapTexture );
  186.     m_pd3dDevice->SetTexture( 1, m_pBackgroundTexture );
  187.  
  188.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_BUMPENVMAP );
  189.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  190.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_CURRENT );
  191.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  192.  
  193.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT00,   F2DW(0.2f) );
  194.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT01,   F2DW(0.0f) );
  195.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT10,   F2DW(0.0f) );
  196.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVMAT11,   F2DW(0.2f) );
  197.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVLSCALE,  F2DW(1.0f) );
  198.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f) );
  199.  
  200.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  201.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  202.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
  203.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  204.  
  205.     // Generate texture coords depending on objects camera space position
  206.     if( m_d3dCaps.TextureCaps & D3DPTEXTURECAPS_PROJECTED )
  207.     {
  208.         D3DXMATRIX mat;
  209.         mat._11 = 0.5f; mat._12 = 0.0f; mat._13 = 0.0f;
  210.         mat._21 = 0.0f; mat._22 =-0.5f; mat._23 = 0.0f;
  211.         mat._31 = 0.5f; mat._32 = 0.5f; mat._33 = 1.0f;
  212.         mat._41 = 0.0f; mat._42 = 0.0f; mat._43 = 0.0f;
  213.  
  214.         // Set up a transform for projected textures
  215.         D3DXMATRIX matProj;
  216.         m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
  217.         mat._11 *= matProj._11;
  218.         mat._22 *= matProj._22;
  219.         mat._31 *= matProj._33;
  220.         mat._32 *= matProj._33;
  221.         mat._33 *= matProj._33;
  222.  
  223.         m_pd3dDevice->SetTransform( D3DTS_TEXTURE1, &mat );
  224.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3|D3DTTFF_PROJECTED );
  225.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION | 1);
  226.     }
  227.     else
  228.     {
  229.         D3DXMATRIX mat;
  230.         mat._11 = 0.5f; mat._12 = 0.0f;
  231.         mat._21 = 0.0f; mat._22 =-0.5f;
  232.         mat._31 = 0.0f; mat._32 = 0.0f;
  233.         mat._41 = 0.5f; mat._42 = 0.5f;
  234.  
  235.         // Since we can't do projected textures, scale-by-z here
  236.         D3DXMATRIX matView, matProj;
  237.         m_pd3dDevice->GetTransform( D3DTS_VIEW,       &matView );
  238.         m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
  239.         D3DXVECTOR3 vEyePt( matView._41, matView._42, matView._43 );
  240.         FLOAT       z = D3DXVec3Length( &vEyePt );
  241.         mat._11 *= ( matProj._11 / ( matProj._33 * z + matProj._34 ) );
  242.         mat._22 *= ( matProj._22 / ( matProj._33 * z + matProj._34 ) );
  243.  
  244.         m_pd3dDevice->SetTransform( D3DTS_TEXTURE1, &mat );
  245.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
  246.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION | 1);
  247.     }
  248.  
  249.     // Position the lens
  250.     D3DXMATRIX matWorld;
  251.     D3DXMatrixTranslation( &matWorld, 0.7f * (1000.0f-256.0f)*m_fLensX,
  252.                                       0.7f * (1000.0f-256.0f)*m_fLensY,
  253.                                       0.0f );
  254.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  255.  
  256.     m_pd3dDevice->SetVertexShader( D3DFVF_BUMPVERTEX );
  257.     m_pd3dDevice->SetStreamSource( 0, m_pLensVB, sizeof(BUMPVERTEX) );
  258.  
  259.     // Verify that the texture operations are possible on the device
  260.     DWORD dwNumPasses;
  261.     if( FAILED( m_pd3dDevice->ValidateDevice( &dwNumPasses ) ) )
  262.     {
  263.         // The right thing to do when device validation fails is to try
  264.         // a different rendering technique.  This sample just warns the user.
  265.         m_bDeviceValidationFailed = TRUE;
  266.     }
  267.  
  268.     // Render the lens
  269.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  270.  
  271.     // Output statistics
  272.     m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  273.     m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  274.  
  275.     if( m_bDeviceValidationFailed )
  276.     {
  277.         m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,0,0), 
  278.             _T("Warning: Device validation failed.  Rendering may not look right.") );
  279.     }
  280.  
  281.     // End the scene.
  282.     m_pd3dDevice->EndScene();
  283.  
  284.     return S_OK;
  285. }
  286.  
  287.  
  288.  
  289.  
  290. //-----------------------------------------------------------------------------
  291. // Name: CreateBumpmap()
  292. // Desc: Create a bump map texture and fill its content to BUMPDUDV format
  293. //-----------------------------------------------------------------------------
  294. HRESULT CMyD3DApplication::CreateBumpMap( UINT iWidth, UINT iHeight )
  295. {
  296.     // Create the bumpmap's surface and texture objects
  297.     if( FAILED( m_pd3dDevice->CreateTexture( iWidth, iHeight, 1, 0, 
  298.         D3DFMT_V8U8, D3DPOOL_MANAGED, &m_pBumpMapTexture ) ) )
  299.     {
  300.         return E_FAIL;
  301.     }
  302.  
  303.     // Fill the bumpmap texels to simulate a lens
  304.     D3DLOCKED_RECT d3dlr;
  305.     m_pBumpMapTexture->LockRect( 0, &d3dlr, 0, 0 );
  306.     DWORD dwDstPitch = (DWORD)d3dlr.Pitch;
  307.     BYTE* pDst       = (BYTE*)d3dlr.pBits;
  308.     UINT  mid        = iWidth/2;
  309.  
  310.     for( DWORD y0 = 0; y0 < iHeight; y0++ )
  311.     {
  312.         CHAR* pDst = (CHAR*)d3dlr.pBits + y0*d3dlr.Pitch;
  313.  
  314.         for( DWORD x0 = 0; x0 < iWidth; x0++ )
  315.         {
  316.             DWORD x1 = ( (x0==iWidth-1)  ? x0 : x0+1 );
  317.             DWORD y1 = ( (x0==iHeight-1) ? y0 : y0+1 );
  318.  
  319.             FLOAT fDistSq00 = (FLOAT)( (x0-mid)*(x0-mid) + (y0-mid)*(y0-mid) );
  320.             FLOAT fDistSq01 = (FLOAT)( (x1-mid)*(x1-mid) + (y0-mid)*(y0-mid) );
  321.             FLOAT fDistSq10 = (FLOAT)( (x0-mid)*(x0-mid) + (y1-mid)*(y1-mid) );
  322.  
  323.             FLOAT v00 = ( fDistSq00 > (mid*mid) ) ? 0.0f : sqrtf( (mid*mid) - fDistSq00 );
  324.             FLOAT v01 = ( fDistSq01 > (mid*mid) ) ? 0.0f : sqrtf( (mid*mid) - fDistSq01 );
  325.             FLOAT v10 = ( fDistSq10 > (mid*mid) ) ? 0.0f : sqrtf( (mid*mid) - fDistSq10 );
  326.  
  327.             FLOAT iDu = (128/D3DX_PI)*atanf(v00-v01); // The delta-u bump value
  328.             FLOAT iDv = (128/D3DX_PI)*atanf(v00-v10); // The delta-v bump value
  329.  
  330.             *pDst++ = (CHAR)(iDu);
  331.             *pDst++ = (CHAR)(iDv);
  332.         }
  333.     }
  334.  
  335.     m_pBumpMapTexture->UnlockRect(0);
  336.  
  337.     return S_OK;
  338. }
  339.  
  340.  
  341.  
  342.  
  343. //-----------------------------------------------------------------------------
  344. // Name: InitDeviceObjects()
  345. // Desc: Initialize scene objects
  346. //-----------------------------------------------------------------------------
  347. HRESULT CMyD3DApplication::InitDeviceObjects()
  348. {
  349.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  350.  
  351.     // Load the texture for the background image
  352.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"),
  353.                                        &m_pBackgroundTexture ) ) )
  354.         return E_FAIL;
  355.  
  356.     // Create the bump map texture
  357.     if( FAILED( CreateBumpMap( 256, 256 ) ) )
  358.        return E_FAIL;
  359.  
  360.     // Create a square for rendering the background
  361.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(BACKGROUNDVERTEX),
  362.                                                   D3DUSAGE_WRITEONLY, D3DFVF_BACKGROUNDVERTEX,
  363.                                                   D3DPOOL_MANAGED, &m_pBackgroundVB ) ) )
  364.         return E_FAIL;
  365.  
  366.     // Create a square for rendering the lens
  367.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(BUMPVERTEX),
  368.                                                   D3DUSAGE_WRITEONLY, D3DFVF_BUMPVERTEX,
  369.                                                   D3DPOOL_MANAGED, &m_pLensVB ) ) )
  370.         return E_FAIL;
  371.  
  372.     BUMPVERTEX* vLens;
  373.     m_pLensVB->Lock( 0, 0, (BYTE**)&vLens, 0 );
  374.     vLens[0].p = D3DXVECTOR3(-256.0f,-256.0f, 0.0f );
  375.     vLens[1].p = D3DXVECTOR3(-256.0f, 256.0f, 0.0f );
  376.     vLens[2].p = D3DXVECTOR3( 256.0f,-256.0f, 0.0f );
  377.     vLens[3].p = D3DXVECTOR3( 256.0f, 256.0f, 0.0f );
  378.     vLens[0].tu1 = 0.0f; vLens[0].tv1 = 1.0f;
  379.     vLens[1].tu1 = 0.0f; vLens[1].tv1 = 0.0f;
  380.     vLens[2].tu1 = 1.0f; vLens[2].tv1 = 1.0f;
  381.     vLens[3].tu1 = 1.0f; vLens[3].tv1 = 0.0f;
  382.     m_pLensVB->Unlock();
  383.  
  384.     m_bDeviceValidationFailed = FALSE;
  385.  
  386.     return S_OK;
  387. }
  388.  
  389.  
  390.  
  391.  
  392. //-----------------------------------------------------------------------------
  393. // Name: RestoreDeviceObjects()
  394. // Desc: Initialize scene objects
  395. //-----------------------------------------------------------------------------
  396. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  397. {
  398.     m_pFont->RestoreDeviceObjects();
  399.  
  400.     // Set the transform matrices
  401.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, -2000.0f );
  402.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f,     0.0f );
  403.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f,     0.0f );
  404.     D3DXMATRIX matWorld, matView, matProj;
  405.  
  406.     D3DXMatrixIdentity( &matWorld );
  407.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  408.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  409.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 3000.0f );
  410.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  411.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  412.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  413.  
  414.     // Set any appropiate state
  415.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  416.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  417.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP );
  418.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP );
  419.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  420.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  421.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MIPFILTER, D3DTEXF_NONE );
  422.  
  423.     // Size the background image
  424.     BACKGROUNDVERTEX* vBackground;
  425.     m_pBackgroundVB->Lock( 0, 0, (BYTE**)&vBackground, 0 );
  426.     for( UINT i=0; i<4; i ++ )
  427.     {
  428.         vBackground[i].p = D3DXVECTOR4( 0.0f, 0.0f, 0.9f, 1.0f );
  429.         vBackground[i].color = 0xffffffff;
  430.     }
  431.     vBackground[0].p.y = (FLOAT)m_d3dsdBackBuffer.Height;
  432.     vBackground[2].p.y = (FLOAT)m_d3dsdBackBuffer.Height;
  433.     vBackground[2].p.x = (FLOAT)m_d3dsdBackBuffer.Width;
  434.     vBackground[3].p.x = (FLOAT)m_d3dsdBackBuffer.Width;
  435.     vBackground[0].tu = 0.0f; vBackground[0].tv = 1.0f;
  436.     vBackground[1].tu = 0.0f; vBackground[1].tv = 0.0f;
  437.     vBackground[2].tu = 1.0f; vBackground[2].tv = 1.0f;
  438.     vBackground[3].tu = 1.0f; vBackground[3].tv = 0.0f;
  439.     m_pBackgroundVB->Unlock();
  440.  
  441.     return S_OK;
  442. }
  443.  
  444.  
  445.  
  446.  
  447. //-----------------------------------------------------------------------------
  448. // Name: InvalidateDeviceObjects()
  449. // Desc:
  450. //-----------------------------------------------------------------------------
  451. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  452. {
  453.     m_pFont->InvalidateDeviceObjects();
  454.     return S_OK;
  455. }
  456.  
  457.  
  458.  
  459.  
  460. //-----------------------------------------------------------------------------
  461. // Name: DeleteDeviceObjects()
  462. // Desc: Called when the app is exiting, or the device is being changed,
  463. //       this function deletes any device dependent objects.
  464. //-----------------------------------------------------------------------------
  465. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  466. {
  467.     m_pFont->DeleteDeviceObjects();
  468.     SAFE_RELEASE( m_pBackgroundTexture );
  469.     SAFE_RELEASE( m_pBumpMapTexture );
  470.     SAFE_RELEASE( m_pBackgroundVB );
  471.     SAFE_RELEASE( m_pLensVB );
  472.  
  473.     return S_OK;
  474. }
  475.  
  476.  
  477.  
  478.  
  479. //-----------------------------------------------------------------------------
  480. // Name: FinalCleanup()
  481. // Desc: Called before the app exits, this function gives the app the chance
  482. //       to cleanup after itself.
  483. //-----------------------------------------------------------------------------
  484. HRESULT CMyD3DApplication::FinalCleanup()
  485. {
  486.     SAFE_DELETE( m_pFont );
  487.     return S_OK;
  488. }
  489.  
  490.  
  491.  
  492.  
  493. //-----------------------------------------------------------------------------
  494. // Name: ConfirmDevice()
  495. // Desc: Called during device intialization, this code checks the device
  496. //       for some minimum set of capabilities
  497. //-----------------------------------------------------------------------------
  498. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  499.                                           D3DFORMAT Format )
  500. {
  501.     if( dwBehavior & D3DCREATE_PUREDEVICE )
  502.         return E_FAIL; // GetTransform doesn't work on PUREDEVICE
  503.  
  504.     // Device must be able to do bumpmapping
  505.     if( 0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP ) )
  506.         return E_FAIL;
  507.  
  508.     // Accept devices that can create D3DFMT_V8U8 textures
  509.     if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  510.                                               pCaps->DeviceType, Format,
  511.                                               0, D3DRTYPE_TEXTURE,
  512.                                               D3DFMT_V8U8 ) ) )
  513.         return S_OK;
  514.  
  515.     return E_FAIL;
  516. }
  517.  
  518.  
  519.  
  520.