home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / MFCTex / mtexture.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  18.4 KB  |  507 lines

  1. //-----------------------------------------------------------------------------
  2. // File: mtexture.cpp
  3. //
  4. // Desc: Example code showing how to do multitexturing in D3D
  5. //
  6. //       Note: This code uses the D3D Framework helper library.
  7. //
  8. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include "stdafx.h"
  12. #include <tchar.h>
  13. #include <math.h>
  14. #include <time.h>
  15. #include <stdio.h>
  16. #include <D3DX8.h>
  17. #include "D3DApp.h"
  18. #include "D3DFont.h"
  19. #include "D3DFile.h"
  20. #include "D3DUtil.h"
  21. #include "DXUtil.h"
  22. #include "MFCTex.h"
  23.  
  24.  
  25.  
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Define a custom vertex that uses multiple sets of tex coords
  29. //-----------------------------------------------------------------------------
  30. struct WALLVERTEX
  31. {
  32.     D3DXVECTOR3 p;
  33.     DWORD dwColor;
  34.     FLOAT tu1, tv1;
  35.     FLOAT tu2, tv2;
  36.     FLOAT tu3, tv3;
  37. };
  38.  
  39. #define D3DFVF_WALLVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX3 )
  40.  
  41. #define FILL_WALLVERTEX( v, ax, ay, az, acolor, atu1, atv1, atu2, atv2, atu3, atv3 )  \
  42. {   v.p.x = ax; v.p.y = ay; v.p.z = az; v.dwColor  = acolor; \
  43.     v.tu1 = atu1; v.tv1 = atv1; v.tu2 = atu2; v.tv2 = atv2;\
  44.     v.tu3 = atu3; v.tv3 = atv3;\
  45. }
  46.  
  47. #define WALL_VERT_NUM 10
  48.  
  49.  
  50. struct FLOORCEILINGVERTEX
  51. {
  52.     D3DXVECTOR3 p;
  53.     D3DXVECTOR3 n;
  54.     FLOAT tu, tv;
  55. };
  56.  
  57. #define D3DFVF_FLOORCEILINGVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 )
  58.  
  59. #define FILL_FLOORCEILINGVERTEX( v, ax, ay, az, nx, ny, nz, atu, atv )  \
  60. {   v.p.x = ax; v.p.y = ay; v.p.z = az; v.n.x = nx; v.n.y = ny; v.n.z = nz; \
  61.     v.tu = atu; v.tv = atv; \
  62. }
  63.  
  64. #define FLOORCEILING_VERT_NUM 12
  65.  
  66.  
  67.  
  68. //-----------------------------------------------------------------------------
  69. // Storage of the texture stage states
  70. //-----------------------------------------------------------------------------
  71. WORD  g_wT0COp,   g_wT1COp,   g_wT2COp;
  72. WORD  g_wT0CArg1, g_wT1CArg1, g_wT2CArg1;
  73. WORD  g_wT0CArg2, g_wT1CArg2, g_wT2CArg2;
  74. WORD  g_wT0AOp,   g_wT1AOp,   g_wT2AOp;
  75. WORD  g_wT0AArg1, g_wT1AArg1, g_wT2AArg1;
  76. WORD  g_wT0AArg2, g_wT1AArg2, g_wT2AArg2;
  77. DWORD g_dwTextureFactor         = 0xffffffff;
  78. DWORD g_dwDiffuseColor          = 0xffffffff;
  79. DWORD g_dwMaxTextureBlendStages = 0;
  80.  
  81.  
  82.  
  83.  
  84.  
  85. //-----------------------------------------------------------------------------
  86. // Function prototypes and global (or static) variables
  87. //-----------------------------------------------------------------------------
  88. LPDIRECT3DTEXTURE8 g_pFloorTexture = NULL;
  89. LPDIRECT3DTEXTURE8 g_pTexture0     = NULL;
  90. LPDIRECT3DTEXTURE8 g_pTexture1     = NULL;
  91. LPDIRECT3DTEXTURE8 g_pTexture2     = NULL;
  92.  
  93. // Filenames for the textures (one for each texture stage)
  94. BOOL  g_bTexturesChanged = FALSE;
  95. TCHAR g_strTexture0[256];
  96. TCHAR g_strTexture1[256];
  97. TCHAR g_strTexture2[256];
  98.  
  99.  
  100.  
  101.  
  102. //-----------------------------------------------------------------------------
  103. // Name: OneTimeSceneInit()
  104. // Desc: Called during initial app startup, this function performs all the
  105. //       permanent initialization.
  106. //-----------------------------------------------------------------------------
  107. HRESULT CAppForm::OneTimeSceneInit()
  108. {
  109.     m_pFont = new CD3DFont( _T("Arial"), 9, D3DFONT_BOLD );
  110.  
  111.     // Create some textures
  112.     _tcscpy( g_strTexture0, _T("env2.bmp") );
  113.     _tcscpy( g_strTexture1, _T("spotlite.bmp") );
  114.     _tcscpy( g_strTexture2, _T("env3.bmp") );
  115.  
  116.     g_pFloorTexture = NULL;
  117.     g_pTexture0     = NULL;
  118.     g_pTexture1     = NULL;
  119.     g_pTexture2     = NULL;
  120.  
  121.     return S_OK;
  122. }
  123.  
  124.  
  125.  
  126.  
  127. //-----------------------------------------------------------------------------
  128. // Name: FrameMove()
  129. // Desc: Called once per frame, the call is the entry point for animating
  130. //       the scene.
  131. //-----------------------------------------------------------------------------
  132. HRESULT CAppForm::FrameMove()
  133. {
  134.     // Setup the world spin matrix
  135.     D3DXMATRIX matWorldSpin;
  136.     D3DXMatrixRotationY( &matWorldSpin, -m_fTime/9 );
  137.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorldSpin );
  138.  
  139.     // keep texture coordinates in a reasonable range
  140.     static FLOAT fTimeKeySub = 0.0f;
  141.     FLOAT fTexTimeKey = m_fTime + fTimeKeySub;
  142.     if( fTexTimeKey > 160.0f )
  143.     {
  144.         fTimeKeySub -= 160.0f;
  145.     }
  146.  
  147.     WALLVERTEX* pVertices;
  148.     m_pVBWalls->Lock( 0, 0, (BYTE**)&pVertices, 0 );
  149.     // Rotate the light map around the walls each frame
  150.     for( int i=0; i<(WALL_VERT_NUM/2); i++ )
  151.     {
  152.         pVertices[2*i+0].tu2 = fTexTimeKey/(WALL_VERT_NUM/2)+i;
  153.         pVertices[2*i+1].tu2 = fTexTimeKey/(WALL_VERT_NUM/2)+i;
  154.     }
  155.     for( i=0; i<WALL_VERT_NUM; i++ )
  156.         pVertices[i].dwColor = g_dwDiffuseColor;
  157.     m_pVBWalls->Unlock();
  158.  
  159.     return S_OK;
  160. }
  161.  
  162.  
  163.  
  164.  
  165. //-----------------------------------------------------------------------------
  166. // Name: SetTextureStageStatesForRendering()
  167. // Desc: Sets up the texture stage, as per the global variables defining each
  168. //       stage.
  169. //-----------------------------------------------------------------------------
  170. HRESULT SetTextureStageStatesForRendering( LPDIRECT3DDEVICE8 pd3dDevice )
  171. {
  172.     // If new textures were selected, restore them now
  173.     if( g_bTexturesChanged )
  174.     {
  175.         SAFE_RELEASE( g_pFloorTexture );
  176.         SAFE_RELEASE( g_pTexture0 );
  177.         SAFE_RELEASE( g_pTexture1 );
  178.         SAFE_RELEASE( g_pTexture2 );
  179.         if( FAILED( D3DUtil_CreateTexture( pd3dDevice, _T("floor.bmp"), &g_pFloorTexture ) ) )
  180.             g_pFloorTexture = NULL;
  181.         if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture0, &g_pTexture0 ) ) )
  182.             g_pTexture0 = NULL;
  183.         if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture1, &g_pTexture1 ) ) )
  184.             g_pTexture1 = NULL;
  185.         if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture2, &g_pTexture2 ) ) )
  186.             g_pTexture2 = NULL;
  187.         g_bTexturesChanged = FALSE;
  188.     }
  189.  
  190.     pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, g_dwTextureFactor );
  191.  
  192.     pd3dDevice->SetTexture( 0, g_pTexture0 );
  193.     pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
  194.     pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  195.     pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, g_wT0CArg1 );
  196.     pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   g_wT0COp   );
  197.     pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, g_wT0CArg2 );
  198.     pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, g_wT0AArg1 );
  199.     pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   g_wT0AOp   );
  200.     pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, g_wT0AArg2 );
  201.  
  202.     if( g_dwMaxTextureBlendStages > 1 )
  203.     {
  204.         pd3dDevice->SetTexture( 1, g_pTexture1 );
  205.         pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
  206.         pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  207.         pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, g_wT1CArg1 );
  208.         pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   g_wT1COp   );
  209.         pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, g_wT1CArg2 );
  210.         pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, g_wT1AArg1 );
  211.         pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   g_wT1AOp   );
  212.         pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, g_wT1AArg2 );
  213.     }
  214.  
  215.     if( g_dwMaxTextureBlendStages > 2)
  216.     {
  217.         pd3dDevice->SetTexture( 2, g_pTexture2 );
  218.         pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );
  219.         pd3dDevice->SetTextureStageState( 2, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  220.         pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, g_wT2CArg1 );
  221.         pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP,   g_wT2COp   );
  222.         pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, g_wT2CArg2 );
  223.         pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG1, g_wT2AArg1 );
  224.         pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,   g_wT2AOp   );
  225.         pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG2, g_wT2AArg2 );
  226.     }
  227.  
  228.     return S_OK;
  229. }
  230.  
  231.  
  232.  
  233.  
  234. //-----------------------------------------------------------------------------
  235. // Name: Render()
  236. // Desc: Called once per frame, the call is the entry point for 3d
  237. //       rendering. This function sets up render states, clears the
  238. //       viewport, and renders the scene.
  239. //-----------------------------------------------------------------------------
  240. HRESULT CAppForm::Render()
  241. {
  242.     HRESULT hr;
  243.  
  244.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  245.                        0x000000ff, 1.0f, 0L );
  246.  
  247.     // Begin the scene
  248.     if( FAILED( m_pd3dDevice->BeginScene() ) )
  249.         return E_FAIL;
  250.  
  251.     // Render the floor and ceiling (using single-textured vertices)
  252.     m_pd3dDevice->SetTexture( 0, g_pFloorTexture );
  253.     m_pd3dDevice->SetTexture( 1, NULL );
  254.     m_pd3dDevice->SetTexture( 2, NULL );
  255.  
  256.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  257.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  258.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  259.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
  260.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  261.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP,   D3DTOP_DISABLE );
  262.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  263.  
  264.     m_pd3dDevice->SetVertexShader( D3DFVF_FLOORCEILINGVERTEX );
  265.     m_pd3dDevice->SetStreamSource( 0, m_pVBFloorCeiling, sizeof(FLOORCEILINGVERTEX) );
  266.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 4 );
  267.  
  268.     // Setup the texture stages' state
  269.     SetTextureStageStatesForRendering( m_pd3dDevice );
  270.  
  271.     // Validate the device. This checks to see if the texture stage states we
  272.     // set up are valid for the device. If so, render the object.
  273.     DWORD dwNumPasses;
  274.     if( SUCCEEDED( hr = m_pd3dDevice->ValidateDevice( &dwNumPasses ) ) )
  275.     {
  276.         // Render the multi-textured object
  277.         m_pd3dDevice->SetVertexShader( D3DFVF_WALLVERTEX );
  278.         m_pd3dDevice->SetStreamSource( 0, m_pVBWalls, sizeof(WALLVERTEX) );
  279.         m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 8);
  280.     }
  281.  
  282.     TCHAR* pstr;
  283.     switch( hr )
  284.     {
  285.     case D3DERR_UNSUPPORTEDCOLOROPERATION:
  286.         pstr = _T("Unsupported color op");
  287.         break;
  288.     case D3DERR_UNSUPPORTEDCOLORARG:
  289.         pstr = _T("Unsupported color arg");
  290.         break;
  291.     case D3DERR_UNSUPPORTEDALPHAOPERATION:
  292.         pstr = _T("Unsupported alpha op");
  293.         break;
  294.     case D3DERR_UNSUPPORTEDALPHAARG:
  295.         pstr = _T("Unsupported alpha arg");
  296.         break;
  297.     case D3DERR_TOOMANYOPERATIONS:
  298.         pstr = _T("Too many texture ops");
  299.         break;
  300.     case D3DERR_WRONGTEXTUREFORMAT:
  301.         pstr = _T("Incompatible texture formats");
  302.         break;
  303.     case D3DERR_CONFLICTINGRENDERSTATE:
  304.         pstr = _T("Conflicting render state");
  305.         break;
  306.     case S_OK:
  307.         pstr = _T("Device validated OK");
  308.         break;
  309.     default:
  310.         pstr = _T("Using DX5 driver");
  311.         break;
  312.     }
  313.     m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), pstr );
  314.  
  315.     // End the scene.
  316.     m_pd3dDevice->EndScene();
  317.  
  318.     return S_OK;
  319. }
  320.  
  321.  
  322.  
  323.  
  324. //-----------------------------------------------------------------------------
  325. // Name: InitDeviceObjects()
  326. // Desc: Initialize scene objects.
  327. //-----------------------------------------------------------------------------
  328. HRESULT CAppForm::InitDeviceObjects()
  329. {
  330.     UpdateUIForDeviceCapabilites();
  331.  
  332.     // Initialize the font's internal textures
  333.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  334.  
  335.     // Create the walls vertex buffer
  336.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( WALL_VERT_NUM * sizeof(WALLVERTEX),
  337.         0, D3DFVF_WALLVERTEX, D3DPOOL_MANAGED, &m_pVBWalls ) ) )
  338.     {
  339.         return E_FAIL;
  340.     }
  341.  
  342.     WALLVERTEX* pVertices;
  343.     m_pVBWalls->Lock( 0, 0, (BYTE**)&pVertices, 0 );
  344.  
  345.     FILL_WALLVERTEX( pVertices[ 0], -5.0f,-5.0f, 5.0f, g_dwDiffuseColor, 0.00f, 1.0f, 1.00, 1.0f, 1.00, 1.0f );
  346.     FILL_WALLVERTEX( pVertices[ 1], -5.0f, 5.0f, 5.0f, g_dwDiffuseColor, 0.00f, 0.0f, 1.00, 0.0f, 1.00, 0.0f );
  347.     FILL_WALLVERTEX( pVertices[ 2],  5.0f,-5.0f, 5.0f, g_dwDiffuseColor, 1.00f, 1.0f, 0.00, 1.0f, 0.00, 1.0f );
  348.     FILL_WALLVERTEX( pVertices[ 3],  5.0f, 5.0f, 5.0f, g_dwDiffuseColor, 1.00f, 0.0f, 0.00, 0.0f, 0.00, 0.0f );
  349.     FILL_WALLVERTEX( pVertices[ 4],  5.0f,-5.0f,-5.0f, g_dwDiffuseColor, 2.00f, 1.0f, 1.00, 1.0f, 1.00, 1.0f );
  350.     FILL_WALLVERTEX( pVertices[ 5],  5.0f, 5.0f,-5.0f, g_dwDiffuseColor, 2.00f, 0.0f, 1.00, 0.0f, 1.00, 0.0f );
  351.     FILL_WALLVERTEX( pVertices[ 6], -5.0f,-5.0f,-5.0f, g_dwDiffuseColor, 3.00f, 1.0f, 1.00, 1.0f, 1.00, 1.0f );
  352.     FILL_WALLVERTEX( pVertices[ 7], -5.0f, 5.0f,-5.0f, g_dwDiffuseColor, 3.00f, 0.0f, 1.00, 0.0f, 1.00, 0.0f );
  353.     FILL_WALLVERTEX( pVertices[ 8], -5.0f,-5.0f, 5.0f, g_dwDiffuseColor, 4.00f, 1.0f, 0.00, 1.0f, 0.00, 1.0f );
  354.     FILL_WALLVERTEX( pVertices[ 9], -5.0f, 5.0f, 5.0f, g_dwDiffuseColor, 4.00f, 0.0f, 0.00, 0.0f, 0.00, 0.0f );
  355.  
  356.     m_pVBWalls->Unlock();
  357.  
  358.     // Create the floor/ceiling vertex buffer
  359.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( FLOORCEILING_VERT_NUM * sizeof(FLOORCEILINGVERTEX),
  360.         0, D3DFVF_FLOORCEILINGVERTEX, D3DPOOL_MANAGED, &m_pVBFloorCeiling ) ) )
  361.     {
  362.         return E_FAIL;
  363.     }
  364.  
  365.     FLOORCEILINGVERTEX* pFloorVertices;
  366.     m_pVBFloorCeiling->Lock( 0, 0, (BYTE**)&pFloorVertices, 0 );
  367.     FILL_FLOORCEILINGVERTEX( pFloorVertices[0],  -5,-5, 5, 0, 1, 0, 0.0f, 0.0f );
  368.     FILL_FLOORCEILINGVERTEX( pFloorVertices[1],   5,-5, 5, 0, 1, 0, 0.0f, 1.0f );
  369.     FILL_FLOORCEILINGVERTEX( pFloorVertices[2],   5,-5,-5, 0, 1, 0, 1.0f, 1.0f );
  370.     FILL_FLOORCEILINGVERTEX( pFloorVertices[3],   5,-5,-5, 0, 1, 0, 1.0f, 1.0f );
  371.     FILL_FLOORCEILINGVERTEX( pFloorVertices[4],  -5,-5,-5, 0, 1, 0, 1.0f, 0.0f );
  372.     FILL_FLOORCEILINGVERTEX( pFloorVertices[5],  -5,-5, 5, 0, 1, 0, 0.0f, 0.0f );
  373.     FILL_FLOORCEILINGVERTEX( pFloorVertices[6],   5, 5,-5, 0,-1, 0, 1.0f, 1.0f );
  374.     FILL_FLOORCEILINGVERTEX( pFloorVertices[7],   5, 5, 5, 0,-1, 0, 0.0f, 1.0f );
  375.     FILL_FLOORCEILINGVERTEX( pFloorVertices[8],  -5, 5, 5, 0,-1, 0, 0.0f, 0.0f );
  376.     FILL_FLOORCEILINGVERTEX( pFloorVertices[9],  -5, 5, 5, 0,-1, 0, 0.0f, 0.0f );
  377.     FILL_FLOORCEILINGVERTEX( pFloorVertices[10], -5, 5,-5, 0,-1, 0, 1.0f, 0.0f );
  378.     FILL_FLOORCEILINGVERTEX( pFloorVertices[11],  5, 5,-5, 0,-1, 0, 1.0f, 1.0f );
  379.     m_pVBFloorCeiling->Unlock();
  380.  
  381.     g_bTexturesChanged = TRUE; // Force texture reload
  382.  
  383.     g_dwMaxTextureBlendStages = m_d3dCaps.MaxTextureBlendStages;
  384.  
  385.     return S_OK;
  386. }
  387.  
  388.  
  389.  
  390.  
  391. //-----------------------------------------------------------------------------
  392. // Name: RestoreDeviceObjects()
  393. // Desc: Initialize scene objects.
  394. //-----------------------------------------------------------------------------
  395. HRESULT CAppForm::RestoreDeviceObjects()
  396. {
  397.     m_pFont->RestoreDeviceObjects();
  398.  
  399.     // Create and set up the shine materials w/ textures
  400.     D3DMATERIAL8 mtrl;
  401.     D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
  402.     m_pd3dDevice->SetMaterial( &mtrl );
  403.  
  404.     // Set the transform matrices
  405.     D3DXMATRIX matWorld, matView, matProj;
  406.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, -4.0f );
  407.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f,  0.0f );
  408.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f,  0.0f );
  409.  
  410.     D3DXMatrixIdentity( &matWorld );
  411.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  412.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/2, 1.0f, 1.0f, 1000.0f );
  413.  
  414.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  415.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  416.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  417.  
  418.     // Set any appropiate state
  419.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,  TRUE );
  420.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  421.  
  422.     return S_OK;
  423. }
  424.  
  425.  
  426.  
  427.  
  428. //-----------------------------------------------------------------------------
  429. // Name: InvalidateDeviceObjects()
  430. // Desc: Called when the device-dependent objects are about to be lost.
  431. //-----------------------------------------------------------------------------
  432. HRESULT CAppForm::InvalidateDeviceObjects()
  433. {
  434.     m_pFont->InvalidateDeviceObjects();
  435.     return S_OK;
  436. }
  437.  
  438.  
  439.  
  440.  
  441. //-----------------------------------------------------------------------------
  442. // Name: DeleteDeviceObjects()
  443. // Desc: Called when the app is exiting, or the device is being changed,
  444. //       this function deletes any device dependent objects.
  445. //-----------------------------------------------------------------------------
  446. HRESULT CAppForm::DeleteDeviceObjects()
  447. {
  448.     m_pFont->DeleteDeviceObjects();
  449.     SAFE_RELEASE( m_pVBWalls );
  450.     SAFE_RELEASE( m_pVBFloorCeiling );
  451.     SAFE_RELEASE( g_pFloorTexture );
  452.     SAFE_RELEASE( g_pTexture0 );
  453.     SAFE_RELEASE( g_pTexture1 );
  454.     SAFE_RELEASE( g_pTexture2 );
  455.  
  456.     return S_OK;
  457. }
  458.  
  459.  
  460.  
  461.  
  462. //-----------------------------------------------------------------------------
  463. // Name: FinalCleanup()
  464. // Desc: Called before the app exits, this function gives the app the chance
  465. //       to cleanup after itself.
  466. //-----------------------------------------------------------------------------
  467. HRESULT CAppForm::FinalCleanup()
  468. {
  469.     SAFE_DELETE( m_pFont );
  470.     return S_OK;
  471. }
  472.  
  473.  
  474.  
  475.  
  476. //-----------------------------------------------------------------------------
  477. // Name: ConfirmDevice()
  478. // Desc: Called during device intialization, this code checks the device
  479. //       for some minimum set of capabilities
  480. //-----------------------------------------------------------------------------
  481. HRESULT CAppForm::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, 
  482.                                  D3DFORMAT Format )
  483. {
  484.     return S_OK;
  485. }
  486.  
  487.  
  488.  
  489.  
  490. //-----------------------------------------------------------------------------
  491. // Name: SetTextureMaps()
  492. // Desc: Changes the texture maps used during rendering of the room.
  493. //-----------------------------------------------------------------------------
  494. VOID CAppForm::SetTextureMaps( const TCHAR* strTexture0, const TCHAR* strTexture1,
  495.                          const TCHAR* strTexture2 )
  496. {
  497.     _tcscpy( g_strTexture0, strTexture0 );
  498.     _tcscpy( g_strTexture1, strTexture1 );
  499.     _tcscpy( g_strTexture2, strTexture2 );
  500.  
  501.     g_bTexturesChanged = TRUE;
  502. }
  503.  
  504.  
  505.  
  506.  
  507.