1 | //-----------------------------------------------------------------------
|
---|
2 | // File: D3DUtil.cpp
|
---|
3 | //
|
---|
4 | // Desc: Shortcut macros and functions for using DX objects
|
---|
5 | //
|
---|
6 | //
|
---|
7 | // Copyright (c) 1997-1998 Microsoft Corporation. All rights reserved
|
---|
8 | //-----------------------------------------------------------------------
|
---|
9 |
|
---|
10 | #define D3D_OVERLOADS
|
---|
11 | #define STRICT
|
---|
12 | #include "D3DUtil.h"
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 | //-----------------------------------------------------------------------
|
---|
17 | // Name: D3DUtil_InitDeviceDesc()
|
---|
18 | // Desc: Helper function called to initialize a D3DDEVICEDESC7 structure,
|
---|
19 | //-----------------------------------------------------------------------
|
---|
20 | VOID D3DUtil_InitDeviceDesc( D3DDEVICEDESC7& ddDevDesc )
|
---|
21 | {
|
---|
22 | ZeroMemory( &ddDevDesc, sizeof(D3DDEVICEDESC7) );
|
---|
23 | ddDevDesc.dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
|
---|
24 | ddDevDesc.dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | //-----------------------------------------------------------------------
|
---|
31 | // Name: D3DUtil_InitSurfaceDesc()
|
---|
32 | // Desc: Helper function called to build a DDSURFACEDESC2 structure,
|
---|
33 | // typically before calling CreateSurface() or GetSurfaceDesc()
|
---|
34 | //-----------------------------------------------------------------------
|
---|
35 | VOID D3DUtil_InitSurfaceDesc( DDSURFACEDESC2& ddsd, DWORD dwFlags,
|
---|
36 | DWORD dwCaps )
|
---|
37 | {
|
---|
38 | ZeroMemory( &ddsd, sizeof(DDSURFACEDESC2) );
|
---|
39 | ddsd.dwSize = sizeof(DDSURFACEDESC2);
|
---|
40 | ddsd.dwFlags = dwFlags;
|
---|
41 | ddsd.ddsCaps.dwCaps = dwCaps;
|
---|
42 | ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | //-----------------------------------------------------------------------
|
---|
49 | // Name: D3DUtil_InitViewport()
|
---|
50 | // Desc: Helper function called to build a D3DVIEWPORT3 structure
|
---|
51 | //-----------------------------------------------------------------------
|
---|
52 | VOID D3DUtil_InitViewport( D3DVIEWPORT2& vp, DWORD dwWidth, DWORD dwHeight )
|
---|
53 | {
|
---|
54 | ZeroMemory( &vp, sizeof(D3DVIEWPORT2) );
|
---|
55 | vp.dwSize = sizeof(D3DVIEWPORT2);
|
---|
56 | vp.dwWidth = dwWidth;
|
---|
57 | vp.dwHeight = dwHeight;
|
---|
58 | vp.dvMaxZ = 1.0f;
|
---|
59 |
|
---|
60 | vp.dvClipX = -1.0f;
|
---|
61 | vp.dvClipWidth = 2.0f;
|
---|
62 | vp.dvClipY = 1.0f;
|
---|
63 | vp.dvClipHeight = 2.0f;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | //-----------------------------------------------------------------------
|
---|
70 | // Name: D3DUtil_InitMaterial()
|
---|
71 | // Desc: Helper function called to build a D3DMATERIAL structure
|
---|
72 | //-----------------------------------------------------------------------
|
---|
73 | VOID D3DUtil_InitMaterial( D3DMATERIAL& mtrl, FLOAT r, FLOAT g, FLOAT b )
|
---|
74 | {
|
---|
75 | ZeroMemory( &mtrl, sizeof(D3DMATERIAL) );
|
---|
76 | mtrl.dwSize = sizeof(D3DMATERIAL);
|
---|
77 | mtrl.dcvDiffuse.r = mtrl.dcvAmbient.r = r;
|
---|
78 | mtrl.dcvDiffuse.g = mtrl.dcvAmbient.g = g;
|
---|
79 | mtrl.dcvDiffuse.b = mtrl.dcvAmbient.b = b;
|
---|
80 | mtrl.dwRampSize = 16L; // A default ramp size
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 | //-----------------------------------------------------------------------
|
---|
87 | // Name: D3DUtil_InitLight()
|
---|
88 | // Desc: Initializes a D3DLIGHT structure
|
---|
89 | //-----------------------------------------------------------------------
|
---|
90 | VOID D3DUtil_InitLight( D3DLIGHT& light, D3DLIGHTTYPE ltType,
|
---|
91 | FLOAT x, FLOAT y, FLOAT z )
|
---|
92 | {
|
---|
93 | ZeroMemory( &light, sizeof(D3DLIGHT) );
|
---|
94 | light.dwSize = sizeof(D3DLIGHT);
|
---|
95 | light.dltType = ltType;
|
---|
96 | light.dcvColor.r = 1.0f;
|
---|
97 | light.dcvColor.g = 1.0f;
|
---|
98 | light.dcvColor.b = 1.0f;
|
---|
99 | light.dvPosition.x = light.dvDirection.x = x;
|
---|
100 | light.dvPosition.y = light.dvDirection.y = y;
|
---|
101 | light.dvPosition.z = light.dvDirection.z = z;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | //-----------------------------------------------------------------------
|
---|
108 | // Name: D3DUtil_GetDirectDrawFromDevice()
|
---|
109 | // Desc: Get the DDraw interface from a D3DDevice.
|
---|
110 | //-----------------------------------------------------------------------
|
---|
111 | LPDIRECTDRAW7 D3DUtil_GetDirectDrawFromDevice( LPDIRECT3DDEVICE7 pd3dDevice )
|
---|
112 | {
|
---|
113 | LPDIRECTDRAW7 pDD = NULL;
|
---|
114 | LPDIRECTDRAWSURFACE7 pddsRender;
|
---|
115 |
|
---|
116 | if( pd3dDevice )
|
---|
117 | {
|
---|
118 | // Get the current render target
|
---|
119 | if( SUCCEEDED( pd3dDevice->GetRenderTarget( &pddsRender ) ) )
|
---|
120 | {
|
---|
121 | // Get the DDraw4 interface from the render target
|
---|
122 | pddsRender->GetDDInterface( (VOID**)&pDD );
|
---|
123 | pddsRender->Release();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | return pDD;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 | /* No longer work in Dx7
|
---|
132 | //-----------------------------------------------------------------------
|
---|
133 | // Name: D3DUtil_GetDeviceMemoryType()
|
---|
134 | // Desc: Retreives the default memory type used for the device.
|
---|
135 | //-----------------------------------------------------------------------
|
---|
136 | DWORD D3DUtil_GetDeviceMemoryType( LPDIRECT3DDEVICE7 pd3dDevice )
|
---|
137 | {
|
---|
138 | D3DDEVICEDESC7 ddHwDesc;
|
---|
139 | if( FAILED( pd3dDevice->GetCaps( &ddHwDesc ) ) )
|
---|
140 | return 0L;
|
---|
141 |
|
---|
142 | if( ddHwDesc.dwFlags )
|
---|
143 | return DDSCAPS_VIDEOMEMORY;
|
---|
144 |
|
---|
145 | return DDSCAPS_SYSTEMMEMORY;
|
---|
146 | }
|
---|
147 | */
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | //-----------------------------------------------------------------------
|
---|
152 | // Name: D3DUtil_SetViewMatrix()
|
---|
153 | // Desc: Given an eye point, a lookat point, and an up vector, this
|
---|
154 | // function builds a 4x4 view matrix.
|
---|
155 | //-----------------------------------------------------------------------
|
---|
156 | HRESULT D3DUtil_SetViewMatrix( D3DMATRIX& mat, D3DVECTOR& vFrom,
|
---|
157 | D3DVECTOR& vAt, D3DVECTOR& vWorldUp )
|
---|
158 | {
|
---|
159 | // Get the z basis vector, which points straight ahead. This is the
|
---|
160 | // difference from the eyepoint to the lookat point.
|
---|
161 | D3DVECTOR vView = vAt - vFrom;
|
---|
162 |
|
---|
163 | FLOAT fLength = Magnitude( vView );
|
---|
164 | if( fLength < 1e-6f )
|
---|
165 | return E_INVALIDARG;
|
---|
166 |
|
---|
167 | // Normalize the z basis vector
|
---|
168 | vView /= fLength;
|
---|
169 |
|
---|
170 | // Get the dot product, and calculate the projection of the z basis
|
---|
171 | // vector onto the up vector. The projection is the y basis vector.
|
---|
172 | FLOAT fDotProduct = DotProduct( vWorldUp, vView );
|
---|
173 |
|
---|
174 | D3DVECTOR vUp = vWorldUp - fDotProduct * vView;
|
---|
175 |
|
---|
176 | // If this vector has near-zero length because the input specified a
|
---|
177 | // bogus up vector, let's try a default up vector
|
---|
178 | if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
|
---|
179 | {
|
---|
180 | vUp = D3DVECTOR( 0.0f, 1.0f, 0.0f ) - vView.y * vView;
|
---|
181 |
|
---|
182 | // If we still have near-zero length, resort to a different axis.
|
---|
183 | if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
|
---|
184 | {
|
---|
185 | vUp = D3DVECTOR( 0.0f, 0.0f, 1.0f ) - vView.z * vView;
|
---|
186 |
|
---|
187 | if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
|
---|
188 | return E_INVALIDARG;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | // Normalize the y basis vector
|
---|
193 | vUp /= fLength;
|
---|
194 |
|
---|
195 | // The x basis vector is found simply with the cross product of the y
|
---|
196 | // and z basis vectors
|
---|
197 | D3DVECTOR vRight = CrossProduct( vUp, vView );
|
---|
198 |
|
---|
199 | // Start building the matrix. The first three rows contains the basis
|
---|
200 | // vectors used to rotate the view to point at the lookat point
|
---|
201 | D3DUtil_SetIdentityMatrix( mat );
|
---|
202 | mat._11 = vRight.x; mat._12 = vUp.x; mat._13 = vView.x;
|
---|
203 | mat._21 = vRight.y; mat._22 = vUp.y; mat._23 = vView.y;
|
---|
204 | mat._31 = vRight.z; mat._32 = vUp.z; mat._33 = vView.z;
|
---|
205 |
|
---|
206 | // Do the translation values (rotations are still about the eyepoint)
|
---|
207 | mat._41 = - DotProduct( vFrom, vRight );
|
---|
208 | mat._42 = - DotProduct( vFrom, vUp );
|
---|
209 | mat._43 = - DotProduct( vFrom, vView );
|
---|
210 |
|
---|
211 | return S_OK;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 | //-----------------------------------------------------------------------
|
---|
218 | // Name: D3DUtil_SetProjectionMatrix()
|
---|
219 | // Desc: Sets the passed in 4x4 matrix to a perpsective projection matrix built
|
---|
220 | // from the field-of-view (fov, in y), aspect ratio, near plane (D),
|
---|
221 | // and far plane (F). Note that the projection matrix is normalized for
|
---|
222 | // element [3][4] to be 1.0. This is performed so that W-based range fog
|
---|
223 | // will work correctly.
|
---|
224 | //-----------------------------------------------------------------------
|
---|
225 | HRESULT D3DUtil_SetProjectionMatrix( D3DMATRIX& mat, FLOAT fFOV, FLOAT fAspect,
|
---|
226 | FLOAT fNearPlane, FLOAT fFarPlane )
|
---|
227 | {
|
---|
228 | if( fabs(fFarPlane-fNearPlane) < 0.01f )
|
---|
229 | return E_INVALIDARG;
|
---|
230 | if( fabs(sin(fFOV/2)) < 0.01f )
|
---|
231 | return E_INVALIDARG;
|
---|
232 |
|
---|
233 | FLOAT w = fAspect * (FLOAT)( cos(fFOV/2)/sin(fFOV/2) );
|
---|
234 | FLOAT h = 1.0f * (FLOAT)( cos(fFOV/2)/sin(fFOV/2) );
|
---|
235 | FLOAT Q = fFarPlane / ( fFarPlane - fNearPlane );
|
---|
236 |
|
---|
237 | ZeroMemory( &mat, sizeof(D3DMATRIX) );
|
---|
238 | mat._11 = w;
|
---|
239 | mat._22 = h;
|
---|
240 | mat._33 = Q;
|
---|
241 | mat._34 = 1.0f;
|
---|
242 | mat._43 = -Q*fNearPlane;
|
---|
243 |
|
---|
244 | return S_OK;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 | //-----------------------------------------------------------------------
|
---|
251 | // Name: D3DUtil_SetRotateXMatrix()
|
---|
252 | // Desc: Create Rotation matrix about X axis
|
---|
253 | //-----------------------------------------------------------------------
|
---|
254 | VOID D3DUtil_SetRotateXMatrix( D3DMATRIX& mat, FLOAT fRads )
|
---|
255 | {
|
---|
256 | D3DUtil_SetIdentityMatrix( mat );
|
---|
257 | mat._22 = (FLOAT)cos( fRads );
|
---|
258 | mat._23 = (FLOAT)sin( fRads );
|
---|
259 | mat._32 = -(FLOAT)sin( fRads );
|
---|
260 | mat._33 = (FLOAT)cos( fRads );
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 |
|
---|
266 | //-----------------------------------------------------------------------
|
---|
267 | // Name: D3DUtil_SetRotateYMatrix()
|
---|
268 | // Desc: Create Rotation matrix about Y axis
|
---|
269 | //-----------------------------------------------------------------------
|
---|
270 | VOID D3DUtil_SetRotateYMatrix( D3DMATRIX& mat, FLOAT fRads )
|
---|
271 | {
|
---|
272 | D3DUtil_SetIdentityMatrix( mat );
|
---|
273 | mat._11 = (FLOAT)cos( fRads );
|
---|
274 | mat._13 = -(FLOAT)sin( fRads );
|
---|
275 | mat._31 = (FLOAT)sin( fRads );
|
---|
276 | mat._33 = (FLOAT)cos( fRads );
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|
282 | //-----------------------------------------------------------------------
|
---|
283 | // Name: D3DUtil_SetRotateZMatrix()
|
---|
284 | // Desc: Create Rotation matrix about Z axis
|
---|
285 | //-----------------------------------------------------------------------
|
---|
286 | VOID D3DUtil_SetRotateZMatrix( D3DMATRIX& mat, FLOAT fRads )
|
---|
287 | {
|
---|
288 | D3DUtil_SetIdentityMatrix( mat );
|
---|
289 | mat._11 = (FLOAT)cos( fRads );
|
---|
290 | mat._12 = (FLOAT)sin( fRads );
|
---|
291 | mat._21 = -(FLOAT)sin( fRads );
|
---|
292 | mat._22 = (FLOAT)cos( fRads );
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 |
|
---|
297 |
|
---|
298 | //-----------------------------------------------------------------------
|
---|
299 | // Name: D3DUtil_SetRotationMatrix
|
---|
300 | // Desc: Create a Rotation matrix about vector direction
|
---|
301 | //-----------------------------------------------------------------------
|
---|
302 | VOID D3DUtil_SetRotationMatrix( D3DMATRIX& mat, D3DVECTOR& vDir, FLOAT fRads )
|
---|
303 | {
|
---|
304 | FLOAT fCos = (FLOAT)cos( fRads );
|
---|
305 | FLOAT fSin = (FLOAT)sin( fRads );
|
---|
306 | D3DVECTOR v = Normalize( vDir );
|
---|
307 |
|
---|
308 | mat._11 = ( v.x * v.x ) * ( 1.0f - fCos ) + fCos;
|
---|
309 | mat._12 = ( v.x * v.y ) * ( 1.0f - fCos ) - (v.z * fSin);
|
---|
310 | mat._13 = ( v.x * v.z ) * ( 1.0f - fCos ) + (v.y * fSin);
|
---|
311 |
|
---|
312 | mat._21 = ( v.y * v.x ) * ( 1.0f - fCos ) + (v.z * fSin);
|
---|
313 | mat._22 = ( v.y * v.y ) * ( 1.0f - fCos ) + fCos ;
|
---|
314 | mat._23 = ( v.y * v.z ) * ( 1.0f - fCos ) - (v.x * fSin);
|
---|
315 |
|
---|
316 | mat._31 = ( v.z * v.x ) * ( 1.0f - fCos ) - (v.y * fSin);
|
---|
317 | mat._32 = ( v.z * v.y ) * ( 1.0f - fCos ) + (v.x * fSin);
|
---|
318 | mat._33 = ( v.z * v.z ) * ( 1.0f - fCos ) + fCos;
|
---|
319 |
|
---|
320 | mat._14 = mat._24 = mat._34 = 0.0f;
|
---|
321 | mat._41 = mat._42 = mat._43 = 0.0f;
|
---|
322 | mat._44 = 1.0f;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 |
|
---|
327 |
|
---|
328 | //-----------------------------------------------------------------------
|
---|
329 | // Name: D3DUtil_GetDisplayDepth()
|
---|
330 | // Desc: Returns the depth of the current display mode.
|
---|
331 | //-----------------------------------------------------------------------
|
---|
332 | DWORD D3DUtil_GetDisplayDepth( LPDIRECTDRAW7 pDD4 )
|
---|
333 | {
|
---|
334 | // If the caller did not supply a DDraw object, just create a temp one.
|
---|
335 | if( NULL == pDD4 )
|
---|
336 | {
|
---|
337 | LPDIRECTDRAW pDD1;
|
---|
338 | if( FAILED( DirectDrawCreate( NULL, &pDD1, NULL ) ) )
|
---|
339 | return 0L;
|
---|
340 |
|
---|
341 | HRESULT hr = pDD1->QueryInterface( IID_IDirectDraw7, (VOID**)&pDD4 );
|
---|
342 | pDD1->Release();
|
---|
343 | if( FAILED(hr) )
|
---|
344 | return 0L;
|
---|
345 | }
|
---|
346 | else
|
---|
347 | pDD4->AddRef();
|
---|
348 |
|
---|
349 | // Get the display mode description
|
---|
350 | DDSURFACEDESC2 ddsd;
|
---|
351 | ZeroMemory( &ddsd, sizeof(DDSURFACEDESC2) );
|
---|
352 | ddsd.dwSize = sizeof(DDSURFACEDESC2);
|
---|
353 | pDD4->GetDisplayMode( &ddsd );
|
---|
354 | pDD4->Release();
|
---|
355 |
|
---|
356 | // Return the display mode's depth
|
---|
357 | return ddsd.ddpfPixelFormat.dwRGBBitCount;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 |
|
---|
362 |
|
---|
363 | //-----------------------------------------------------------------------
|
---|
364 | // Name: _DbgOut()
|
---|
365 | // Desc: Outputs a message to the debug stream
|
---|
366 | //-----------------------------------------------------------------------
|
---|
367 | HRESULT _DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
|
---|
368 | {
|
---|
369 | TCHAR buffer[256];
|
---|
370 | sprintf( buffer, "%s(%ld): ", strFile, dwLine );
|
---|
371 | OutputDebugString( buffer );
|
---|
372 | OutputDebugString( strMsg );
|
---|
373 |
|
---|
374 | if( hr )
|
---|
375 | {
|
---|
376 | sprintf( buffer, "(hr=%08lx)\n", hr );
|
---|
377 | OutputDebugString( buffer );
|
---|
378 | }
|
---|
379 |
|
---|
380 | OutputDebugString( "\n" );
|
---|
381 |
|
---|
382 | return hr;
|
---|
383 | }
|
---|
384 |
|
---|