1
//-----------------------------------------------------------------------------
3
// ImageLib GL Test Source
4
// Copyright (C) 2000-2002 by Denton Woods
5
// Last modified: 04/28/2001 <--Y2K Compliant! =]
7
// Filename: testil/d3dtest/d3dtest.cpp
9
// Description: Sample implementation of a Direct3D 8.0a image viewer.
11
//-----------------------------------------------------------------------------
30
#pragma comment(lib, "d3d8.lib")
31
#pragma comment(lib, "d3dx8.lib")
32
#pragma comment(lib, "winmm.lib")
37
// Taken from the D3D 8.0a SDK Volumetric Texture Sample.
42
//-----------------------------------------------------------------------------
43
// Defines, constants, and global variables
44
//-----------------------------------------------------------------------------
52
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(0))
54
VERTEX g_vVertices[4] =
56
{ 1.0f,-1.0f, 0.0f, 0xffffffff, 1.0f, 1.0f },
57
{-1.0f,-1.0f, 0.0f, 0xffffffff, 0.0f, 1.0f },
58
{ 1.0f, 1.0f, 0.0f, 0xffffffff, 1.0f, 0.0f },
59
{-1.0f, 1.0f, 0.0f, 0xffffffff, 0.0f, 0.0f }
65
//-----------------------------------------------------------------------------
66
// Name: class CMyD3DApplication
67
// Desc: Application class. The base class (CD3DApplication) provides the
68
// generic functionality needed in all Direct3D samples. CMyD3DApplication
69
// adds functionality specific to this sample program.
70
//-----------------------------------------------------------------------------
71
class CMyD3DApplication : public CD3DApplication
74
LPDIRECT3DTEXTURE8 m_pTexture;
75
LPDIRECT3DVERTEXBUFFER8 m_pVB;
77
HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
80
HRESULT OneTimeSceneInit();
81
HRESULT InitDeviceObjects();
82
HRESULT RestoreDeviceObjects();
83
HRESULT InvalidateDeviceObjects();
84
HRESULT DeleteDeviceObjects();
85
HRESULT FinalCleanup();
96
//-----------------------------------------------------------------------------
98
// Desc: Entry point to the program. Initializes everything, and goes into a
99
// message-processing loop. Idle time is used to render the scene.
100
//-----------------------------------------------------------------------------
101
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
103
CMyD3DApplication d3dApp;
108
if( FAILED( d3dApp.Create( hInst ) ) )
117
//-----------------------------------------------------------------------------
118
// Name: CMyD3DApplication()
119
// Desc: Application constructor. Sets attributes for the app.
120
//-----------------------------------------------------------------------------
121
CMyD3DApplication::CMyD3DApplication()
123
m_strWindowTitle = _T("DevIL Direct3D Test");
124
m_bUseDepthBuffer = TRUE;
126
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
134
//-----------------------------------------------------------------------------
135
// Name: OneTimeSceneInit()
136
// Desc: Called during initial app startup, this function performs all the
137
// permanent initialization.
138
//-----------------------------------------------------------------------------
139
HRESULT CMyD3DApplication::OneTimeSceneInit()
147
//-----------------------------------------------------------------------------
149
// Desc: Called once per frame, the call is the entry point for animating
151
//-----------------------------------------------------------------------------
152
HRESULT CMyD3DApplication::FrameMove()
160
//-----------------------------------------------------------------------------
162
// Desc: Called once per frame, the call is the entry point for 3d
163
// rendering. This function sets up render states, clears the
164
// viewport, and renders the scene.
165
//-----------------------------------------------------------------------------
166
HRESULT CMyD3DApplication::Render()
168
// Clear the viewport
169
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
170
0x00000000, 1.0f, 0L );
173
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
175
// Draw the quad, with the volume texture
176
m_pd3dDevice->SetTexture( 0, m_pTexture );
177
m_pd3dDevice->SetVertexShader( D3DFVF_VERTEX );
178
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
179
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
182
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
183
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
186
m_pd3dDevice->EndScene();
195
//-----------------------------------------------------------------------------
196
// Name: InitDeviceObjects()
197
// Desc: Initialize scene objects.
198
//-----------------------------------------------------------------------------
199
HRESULT CMyD3DApplication::InitDeviceObjects()
203
m_pFont->InitDeviceObjects( m_pd3dDevice );
208
ilutD3D8TexFromFile(m_pd3dDevice, __argv[1], &m_pTexture);
209
//D3DXCreateTextureFromFile(m_pd3dDevice, __argv[1], &m_pTexture);
211
// Create a vertex buffer
213
if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX),
216
D3DPOOL_MANAGED, &m_pVB ) ) )
220
m_pVB->Lock( 0, 4*sizeof(VERTEX), (BYTE**)&pVertices, 0 );
221
memcpy( pVertices, g_vVertices, sizeof(VERTEX)*4 );
231
//-----------------------------------------------------------------------------
232
// Name: RestoreDeviceObjects()
233
// Desc: Initialize scene objects.
234
//-----------------------------------------------------------------------------
235
HRESULT CMyD3DApplication::RestoreDeviceObjects()
237
m_pFont->RestoreDeviceObjects();
240
D3DXVECTOR3 vEye( 0.0f, 0.0f,-3.0f );
241
D3DXVECTOR3 vAt( 0.0f, 0.0f, 0.0f );
242
D3DXVECTOR3 vUp( 0.0f, 1.0f, 0.0f );
243
D3DXMATRIX matWorld, matView, matProj;
244
D3DXMatrixIdentity( &matWorld );
245
D3DXMatrixLookAtLH( &matView, &vEye,&vAt, &vUp );
246
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
247
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
248
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
249
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
250
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
253
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
254
m_pd3dDevice->SetRenderState( D3DRS_CLIPPING, FALSE );
255
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
256
m_pd3dDevice->SetRenderState( D3DRS_CLIPPING, FALSE );
257
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
258
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
259
m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
261
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
262
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
263
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
264
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
265
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
266
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
267
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
268
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
276
//-----------------------------------------------------------------------------
277
// Name: InvalidateDeviceObjects()
279
//-----------------------------------------------------------------------------
280
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
282
m_pFont->InvalidateDeviceObjects();
289
//-----------------------------------------------------------------------------
290
// Name: DeleteDeviceObjects()
291
// Desc: Called when the app is exiting, or the device is being changed,
292
// this function deletes any device dependent objects.
293
//-----------------------------------------------------------------------------
294
HRESULT CMyD3DApplication::DeleteDeviceObjects()
296
m_pFont->DeleteDeviceObjects();
297
SAFE_RELEASE( m_pTexture );
298
SAFE_RELEASE( m_pVB );
306
//-----------------------------------------------------------------------------
307
// Name: FinalCleanup()
308
// Desc: Called before the app exits, this function gives the app the chance
309
// to cleanup after itself.
310
//-----------------------------------------------------------------------------
311
HRESULT CMyD3DApplication::FinalCleanup()
313
SAFE_DELETE( m_pFont );
320
//-----------------------------------------------------------------------------
321
// Name: ConfirmDevice()
322
// Desc: Called during device intialization, this code checks the device
323
// for some minimum set of capabilities
324
//-----------------------------------------------------------------------------
325
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,