d3dtest.cpp
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 10k
Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib GL Test Source
  4. // Copyright (C) 2000-2002 by Denton Woods
  5. // Last modified:  04/28/2001 <--Y2K Compliant! =]
  6. //
  7. // Filename:  testil/d3dtest/d3dtest.cpp
  8. //
  9. // Description:  Sample implementation of a Direct3D 8.0a image viewer.
  10. //
  11. //
  12. // 20040801 XIX: DX9 update, hopefully :)
  13. //
  14. //-----------------------------------------------------------------------------
  15. #define STRICT
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <D3DX9.h>
  19. //#include "dxstdafx.h"
  20. //#include "D3DApp.h"
  21. //#include "D3DFile.h"
  22. //#include "D3DFont.h"
  23. //#include "D3DUtil.h"
  24. #ifdef  _DEBUG
  25. #define IL_DEBUG
  26. #endif//_DEBUG
  27. #include <il/ilut.h>
  28. #pragma comment(lib, "d3d9.lib")
  29. #pragma comment(lib, "d3dx9.lib")
  30. #pragma comment(lib, "winmm.lib")
  31. //
  32. //
  33. //  Taken from the D3D 8.0a SDK Volumetric Texture Sample.
  34. //
  35. //
  36. //-----------------------------------------------------------------------------
  37. // Defines, constants, and global variables
  38. //-----------------------------------------------------------------------------
  39. struct VERTEX
  40. {
  41.     FLOAT      x, y, z;
  42.     DWORD      color;
  43.     FLOAT      tu, tv;
  44. };
  45. #define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(0))
  46. VERTEX g_vVertices[4] =
  47. {
  48.     { 1.0f,-1.0f, 0.0f, 0xffffffff, 1.0f, 1.0f },
  49.     {-1.0f,-1.0f, 0.0f, 0xffffffff, 0.0f, 1.0f },
  50.     { 1.0f, 1.0f, 0.0f, 0xffffffff, 1.0f, 0.0f },
  51.     {-1.0f, 1.0f, 0.0f, 0xffffffff, 0.0f, 0.0f }
  52. };
  53. //-----------------------------------------------------------------------------
  54. // Name: class CMyD3DApplication
  55. // Desc: Application class. The base class (CD3DApplication) provides the 
  56. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  57. //       adds functionality specific to this sample program.
  58. //-----------------------------------------------------------------------------
  59. class CMyD3DApplication : public CD3DApplication
  60. {
  61.     CD3DFont* m_pFont;
  62.     LPDIRECT3DTEXTURE9 m_pTexture;
  63.     LPDIRECT3DVERTEXBUFFER9  m_pVB;
  64.     HRESULT ConfirmDevice( D3DCAPS9*, DWORD, D3DFORMAT );
  65. protected:
  66.     HRESULT OneTimeSceneInit();
  67.     HRESULT InitDeviceObjects();
  68.     HRESULT RestoreDeviceObjects();
  69.     HRESULT InvalidateDeviceObjects();
  70.     HRESULT DeleteDeviceObjects();
  71.     HRESULT FinalCleanup();
  72.     HRESULT Render();
  73.     HRESULT FrameMove();
  74. public:
  75.     CMyD3DApplication();
  76. };
  77. //-----------------------------------------------------------------------------
  78. // Name: WinMain()
  79. // Desc: Entry point to the program. Initializes everything, and goes into a
  80. //       message-processing loop. Idle time is used to render the scene.
  81. //-----------------------------------------------------------------------------
  82. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  83. {
  84.     CMyD3DApplication d3dApp;
  85. if (__argc <= 1)
  86. return 1;
  87.     if( FAILED( d3dApp.Create( hInst ) ) )
  88.         return 0;
  89.     return d3dApp.Run();
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Name: CMyD3DApplication()
  93. // Desc: Application constructor. Sets attributes for the app.
  94. //-----------------------------------------------------------------------------
  95. CMyD3DApplication::CMyD3DApplication()
  96. {
  97.     m_strWindowTitle    = _T("DevIL Direct3D Test");
  98. //    m_bUseDepthBuffer   = TRUE;
  99.     m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  100.     m_pTexture = NULL;
  101.     m_pVB = NULL;
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Name: OneTimeSceneInit()
  105. // Desc: Called during initial app startup, this function performs all the
  106. //       permanent initialization.
  107. //-----------------------------------------------------------------------------
  108. HRESULT CMyD3DApplication::OneTimeSceneInit()
  109. {
  110.     return S_OK;
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Name: FrameMove()
  114. // Desc: Called once per frame, the call is the entry point for animating
  115. //       the scene.
  116. //-----------------------------------------------------------------------------
  117. HRESULT CMyD3DApplication::FrameMove()
  118. {
  119.     return S_OK;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Name: Render()
  123. // Desc: Called once per frame, the call is the entry point for 3d
  124. //       rendering. This function sets up render states, clears the
  125. //       viewport, and renders the scene.
  126. //-----------------------------------------------------------------------------
  127. HRESULT CMyD3DApplication::Render()
  128. {
  129.     // Clear the viewport
  130.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  131.                          0x00000000, 1.0f, 0L );
  132.     // Begin the scene
  133.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  134.     {
  135.         // Draw the quad, with the volume texture
  136.         m_pd3dDevice->SetTexture( 0, m_pTexture );
  137. //        m_pd3dDevice->SetVertexShader( D3DFVF_VERTEX );
  138.         m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
  139.         m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
  140.         // Output statistics
  141.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  142.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  143.         // End the scene.
  144.         m_pd3dDevice->EndScene();
  145.     }
  146.     return S_OK;
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Name: InitDeviceObjects()
  150. // Desc: Initialize scene objects.
  151. //-----------------------------------------------------------------------------
  152. HRESULT CMyD3DApplication::InitDeviceObjects()
  153. {
  154.     HRESULT hr;
  155.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  156. ilInit();
  157. iluInit();
  158. ilutInit();
  159. ilutD3D8TexFromFile(m_pd3dDevice, __argv[1], &m_pTexture);
  160. //D3DXCreateTextureFromFile(m_pd3dDevice, __argv[1], &m_pTexture);
  161.     // Create a vertex buffer
  162.     {
  163.         if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX),
  164.                                            D3DUSAGE_WRITEONLY,
  165.                                            D3DFVF_VERTEX,
  166.                                            D3DPOOL_MANAGED, &m_pVB ) ) )
  167.             return hr;
  168.         VERTEX* pVertices;
  169.         m_pVB->Lock( 0, 4*sizeof(VERTEX), (BYTE**)&pVertices, 0 );
  170.         memcpy( pVertices, g_vVertices, sizeof(VERTEX)*4 );
  171.         m_pVB->Unlock();
  172.     }
  173.     return S_OK;
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Name: RestoreDeviceObjects()
  177. // Desc: Initialize scene objects.
  178. //-----------------------------------------------------------------------------
  179. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  180. {
  181.     m_pFont->RestoreDeviceObjects();
  182.     // Set the matrices
  183.     D3DXVECTOR3 vEye( 0.0f, 0.0f,-3.0f );
  184.     D3DXVECTOR3 vAt(  0.0f, 0.0f, 0.0f );
  185.     D3DXVECTOR3 vUp(  0.0f, 1.0f, 0.0f );
  186.     D3DXMATRIX matWorld, matView, matProj;
  187.     D3DXMatrixIdentity( &matWorld );
  188.     D3DXMatrixLookAtLH( &matView, &vEye,&vAt, &vUp );
  189.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  190.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
  191.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  192.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  193.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  194.     // Set state
  195.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
  196.     m_pd3dDevice->SetRenderState( D3DRS_CLIPPING,     FALSE );
  197.     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_NONE );
  198.     m_pd3dDevice->SetRenderState( D3DRS_CLIPPING,     FALSE );
  199.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     FALSE );
  200.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      FALSE );
  201.     m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
  202.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  203.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  204.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  205.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
  206.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
  207.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
  208.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  209.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  210.     return S_OK;
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Name: InvalidateDeviceObjects()
  214. // Desc:
  215. //-----------------------------------------------------------------------------
  216. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  217. {
  218.     m_pFont->InvalidateDeviceObjects();
  219.     return S_OK;
  220. }
  221. //-----------------------------------------------------------------------------
  222. // Name: DeleteDeviceObjects()
  223. // Desc: Called when the app is exiting, or the device is being changed,
  224. //       this function deletes any device dependent objects.
  225. //-----------------------------------------------------------------------------
  226. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  227. {
  228.     m_pFont->DeleteDeviceObjects();
  229.     SAFE_RELEASE( m_pTexture );
  230.     SAFE_RELEASE( m_pVB );
  231.     return S_OK;
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Name: FinalCleanup()
  235. // Desc: Called before the app exits, this function gives the app the chance
  236. //       to cleanup after itself.
  237. //-----------------------------------------------------------------------------
  238. HRESULT CMyD3DApplication::FinalCleanup()
  239. {
  240.     SAFE_DELETE( m_pFont );
  241.     return S_OK;
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Name: ConfirmDevice()
  245. // Desc: Called during device intialization, this code checks the device
  246. //       for some minimum set of capabilities
  247. //-----------------------------------------------------------------------------
  248. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  249.                                           D3DFORMAT Format )
  250. {
  251.     return S_OK;
  252. }