~ubuntu-branches/ubuntu/hoary/devil/hoary

« back to all changes in this revision

Viewing changes to examples/D3D Example/d3dtest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2005-01-03 19:57:42 UTC
  • Revision ID: james.westby@ubuntu.com-20050103195742-4ipkplcwygu3irv0
Tags: upstream-1.6.7
ImportĀ upstreamĀ versionĀ 1.6.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
13
 
 
14
#define STRICT
 
15
#include <stdio.h>
 
16
#include <math.h>
 
17
#include <D3DX8.h>
 
18
#include "D3DApp.h"
 
19
#include "D3DFile.h"
 
20
#include "D3DFont.h"
 
21
#include "D3DUtil.h"
 
22
#include "DXUtil.h"
 
23
 
 
24
#ifdef  _DEBUG
 
25
#define IL_DEBUG
 
26
#endif//_DEBUG
 
27
#include <il/ilut.h>
 
28
 
 
29
 
 
30
#pragma comment(lib, "d3d8.lib")
 
31
#pragma comment(lib, "d3dx8.lib")
 
32
#pragma comment(lib, "winmm.lib")
 
33
 
 
34
 
 
35
//
 
36
//
 
37
//  Taken from the D3D 8.0a SDK Volumetric Texture Sample.
 
38
//
 
39
//
 
40
 
 
41
 
 
42
//-----------------------------------------------------------------------------
 
43
// Defines, constants, and global variables
 
44
//-----------------------------------------------------------------------------
 
45
struct VERTEX
 
46
{
 
47
    FLOAT      x, y, z;
 
48
    DWORD      color;
 
49
    FLOAT      tu, tv;
 
50
};
 
51
 
 
52
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(0))
 
53
 
 
54
VERTEX g_vVertices[4] =
 
55
{
 
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 }
 
60
};
 
61
 
 
62
 
 
63
 
 
64
 
 
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
 
72
{
 
73
    CD3DFont* m_pFont;
 
74
    LPDIRECT3DTEXTURE8 m_pTexture;
 
75
    LPDIRECT3DVERTEXBUFFER8  m_pVB;
 
76
 
 
77
    HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
 
78
 
 
79
protected:
 
80
    HRESULT OneTimeSceneInit();
 
81
    HRESULT InitDeviceObjects();
 
82
    HRESULT RestoreDeviceObjects();
 
83
    HRESULT InvalidateDeviceObjects();
 
84
    HRESULT DeleteDeviceObjects();
 
85
    HRESULT FinalCleanup();
 
86
    HRESULT Render();
 
87
    HRESULT FrameMove();
 
88
 
 
89
public:
 
90
    CMyD3DApplication();
 
91
};
 
92
 
 
93
 
 
94
 
 
95
 
 
96
//-----------------------------------------------------------------------------
 
97
// Name: WinMain()
 
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 )
 
102
{
 
103
    CMyD3DApplication d3dApp;
 
104
 
 
105
        if (__argc <= 1)
 
106
                return 1;
 
107
 
 
108
    if( FAILED( d3dApp.Create( hInst ) ) )
 
109
        return 0;
 
110
 
 
111
    return d3dApp.Run();
 
112
}
 
113
 
 
114
 
 
115
 
 
116
 
 
117
//-----------------------------------------------------------------------------
 
118
// Name: CMyD3DApplication()
 
119
// Desc: Application constructor. Sets attributes for the app.
 
120
//-----------------------------------------------------------------------------
 
121
CMyD3DApplication::CMyD3DApplication()
 
122
{
 
123
    m_strWindowTitle    = _T("DevIL Direct3D Test");
 
124
    m_bUseDepthBuffer   = TRUE;
 
125
 
 
126
    m_pFont                             = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
 
127
    m_pTexture                  = NULL;
 
128
    m_pVB                               = NULL;
 
129
}
 
130
 
 
131
 
 
132
 
 
133
 
 
134
//-----------------------------------------------------------------------------
 
135
// Name: OneTimeSceneInit()
 
136
// Desc: Called during initial app startup, this function performs all the
 
137
//       permanent initialization.
 
138
//-----------------------------------------------------------------------------
 
139
HRESULT CMyD3DApplication::OneTimeSceneInit()
 
140
{
 
141
    return S_OK;
 
142
}
 
143
 
 
144
 
 
145
 
 
146
 
 
147
//-----------------------------------------------------------------------------
 
148
// Name: FrameMove()
 
149
// Desc: Called once per frame, the call is the entry point for animating
 
150
//       the scene.
 
151
//-----------------------------------------------------------------------------
 
152
HRESULT CMyD3DApplication::FrameMove()
 
153
{
 
154
    return S_OK;
 
155
}
 
156
 
 
157
 
 
158
 
 
159
 
 
160
//-----------------------------------------------------------------------------
 
161
// Name: Render()
 
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()
 
167
{
 
168
    // Clear the viewport
 
169
    m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
 
170
                         0x00000000, 1.0f, 0L );
 
171
 
 
172
    // Begin the scene
 
173
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
 
174
    {
 
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);
 
180
 
 
181
        // Output statistics
 
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 );
 
184
 
 
185
        // End the scene.
 
186
        m_pd3dDevice->EndScene();
 
187
    }
 
188
 
 
189
    return S_OK;
 
190
}
 
191
 
 
192
 
 
193
 
 
194
 
 
195
//-----------------------------------------------------------------------------
 
196
// Name: InitDeviceObjects()
 
197
// Desc: Initialize scene objects.
 
198
//-----------------------------------------------------------------------------
 
199
HRESULT CMyD3DApplication::InitDeviceObjects()
 
200
{
 
201
    HRESULT hr;
 
202
 
 
203
    m_pFont->InitDeviceObjects( m_pd3dDevice );
 
204
 
 
205
        ilInit();
 
206
        iluInit();
 
207
        ilutInit();
 
208
        ilutD3D8TexFromFile(m_pd3dDevice, __argv[1], &m_pTexture);
 
209
        //D3DXCreateTextureFromFile(m_pd3dDevice, __argv[1], &m_pTexture);
 
210
 
 
211
    // Create a vertex buffer
 
212
    {
 
213
        if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX),
 
214
                                           D3DUSAGE_WRITEONLY,
 
215
                                           D3DFVF_VERTEX,
 
216
                                           D3DPOOL_MANAGED, &m_pVB ) ) )
 
217
            return hr;
 
218
 
 
219
        VERTEX* pVertices;
 
220
        m_pVB->Lock( 0, 4*sizeof(VERTEX), (BYTE**)&pVertices, 0 );
 
221
        memcpy( pVertices, g_vVertices, sizeof(VERTEX)*4 );
 
222
        m_pVB->Unlock();
 
223
    }
 
224
 
 
225
    return S_OK;
 
226
}
 
227
 
 
228
 
 
229
 
 
230
 
 
231
//-----------------------------------------------------------------------------
 
232
// Name: RestoreDeviceObjects()
 
233
// Desc: Initialize scene objects.
 
234
//-----------------------------------------------------------------------------
 
235
HRESULT CMyD3DApplication::RestoreDeviceObjects()
 
236
{
 
237
    m_pFont->RestoreDeviceObjects();
 
238
 
 
239
    // Set the matrices
 
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 );
 
251
 
 
252
    // Set state
 
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 );
 
260
 
 
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 );
 
269
 
 
270
    return S_OK;
 
271
}
 
272
 
 
273
 
 
274
 
 
275
 
 
276
//-----------------------------------------------------------------------------
 
277
// Name: InvalidateDeviceObjects()
 
278
// Desc:
 
279
//-----------------------------------------------------------------------------
 
280
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
 
281
{
 
282
    m_pFont->InvalidateDeviceObjects();
 
283
    return S_OK;
 
284
}
 
285
 
 
286
 
 
287
 
 
288
 
 
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()
 
295
{
 
296
    m_pFont->DeleteDeviceObjects();
 
297
    SAFE_RELEASE( m_pTexture );
 
298
    SAFE_RELEASE( m_pVB );
 
299
 
 
300
    return S_OK;
 
301
}
 
302
 
 
303
 
 
304
 
 
305
 
 
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()
 
312
{
 
313
    SAFE_DELETE( m_pFont );
 
314
    return S_OK;
 
315
}
 
316
 
 
317
 
 
318
 
 
319
 
 
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,
 
326
                                          D3DFORMAT Format )
 
327
{
 
328
    return S_OK;
 
329
}