Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Introduction to 3D Game Programming with DirectX.9.0 - F. D. Luna

.pdf
Скачиваний:
243
Добавлен:
24.05.2014
Размер:
6.94 Mб
Скачать

374 Appendix

destroyed. Our code is quite simple; when we receive a WM_LBUTTONDOWN message, we display a message box that prints out “Hello, World”:

case WM_LBUTTONDOWN:

::MessageBox(0, "Hello, World", "Hello", MB_OK); return 0;

When our window gets a WM_KEYDOWN message, we test what key was

pressed. The wParam passed into the window procedure specifies the virtual key code of the specific key that wasYpressed. Think of virtual key codes as an identifier for a particularLkey. The Windows header files

F Note: Remember, theAwParam and lParam parameters are used to

have a list of virtual key code constants that we can use to then test for a particular key (for example, to test if the Escape key was pressed, we use the virtual key code constant VK ESCAPE).

specify extra informationEaboutMa particular message. For the WM_KEYDOWN message, the wParam specifies the virtual key code of the specific key that was pressed.TThe MSDN library will specify the information that the wParam and lParam parameters carry for each Windows message.

case WM_KEYDOWN:

if( wParam == VK ESCAPE )

::DestroyWindow(MainWindowHandle); return 0;

When our window gets destroyed, we post a quit message (which terminates the message loop).

case WM_DESTROY: ::PostQuitMessage(0); return 0;

At the end of our window procedure, we call the DefWindowProc function. This function is a default window procedure. In our Hello World application, we only handle three messages; we use the default behavior specified in DefWindowProc for all the other messages that we receive but don’t necessarily need to handle ourselves. For example, Hello World can be minimized, maximized, resized, and closed. This functionality is provided to us through the default window procedure, as we do not handle the window messages to perform this functionality. Note that DefWindowProc is a Win32 API function.

Team-Fly®

An Introduction to Windows Programming

The MessageBox Function

There is one last API function that we have not yet covered, and that is the MessageBox function. This function is a very handy way to provide the user with information and get some quick input. The declaration for the MessageBox function looks like this:

int MessageBox(

 

HWND hWnd,

// Handle of owner window, may specify null.

LPCTSTR lpText,

// Text to put in the message box.

LPCTSTR lpCaption, // Text to put for the title of the message

 

// box.

UINT uType

// Style of the message box.

);

 

The return value for the MessageBox function depends on the type of message box. See the MSDN library for a list of possible return values and styles.

375

A p p e n d i x

A Better Message Loop

Games are very different applications than traditional Windows applications, such as office type applications and web browsers. Games are not typically event driven in the usual sense, and they must be updated constantly. For this reason, when we actually start writing our 3D programs we will, for the most part, not deal with Windows messages. Therefore, we will want to modify the message loop so that if there is a message, we will process it. But if there is not a message, then we want to run our game code. Our new message loop is as follows:

int Run()

{

MSG msg;

while(true)

{

if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))

{

if(msg.message == WM_QUIT) break;

::TranslateMessage(&msg);

::DispatchMessage(&msg);

}

else

// run game code

}

return msg.wParam;

}

376 Appendix

After we instantiate msg, we enter into an endless loop. We first call the API function PeekMessage, which checks the message queue for a message. See MSDN for the argument descriptions. If there is a message, it returns true and we handle the message. If PeekMessage returns false, then we handle our own specific game code.

Summary

To use Direct3D, we must create a Windows application that has a main window onto which we can render our 3D scenes. Furthermore, for games we create a special message loop that checks for messages, and if there are messages, it processes them; otherwise it executes our game logic.

Several Windows applications can be running concurrently; therefore Windows must manage resources between them and direct messages to the applications that they were intended for. Messages are sent to an application’s message queue when an event (keypress, mouse click, timer, etc.) has occurred for that application.

Every Windows application has a message queue where the messages that an application receives are stored. The application’s message loop constantly checks the queue for messages and dispatches them to their intended window procedure. Note that a single application can have several windows within it.

The window procedure is a special callback function we implement that Windows calls when a window in our application receives a message. In the window procedure, we write the code that we want to be executed when a window in our application receives a particular message. Messages that we do not specifically handle are forwarded to a default window procedure for default handling.

Bibliography

Angel, Edward. Interactive Computer Graphics: A Top-Down Approach with OpenGL. 2nd ed. Addison-Wesley, 2000.

Blinn, Jim. Jim Blinn’s Corner: A Trip Down the Graphics Pipeline, pp. 53-61. San Francisco: Morgan Kaufmann Publishers, Inc., 1996.

Eberly, David H. 3D Game Engine Design. San Francisco: Morgan Kaufmann Publishers, Inc., 2001.

Engel, Wolfgang F., ed. Direct3D ShaderX: Vertex and Pixel Shader Tips and Tricks. Plano, Texas: Wordware Publishing, 2002.

Fraleigh and Beauregard. Linear Algebra. 3rd ed. Addison-Wesley, 1995. Kilgard, Mark J. “Creating Reflections and Shadows Using Stencil

Buffers,” Game Developers Conference, NVIDIA slide presentation, 1999. (http://developer.nvidia.com/docs/IO/1407/ATT/stencil.ppt)

Lander, Jeff. “Shades of Disney: Opaquing a 3D World.” Game Developer Magazine, March 2000.

Lengyel, Eric. Mathematics for 3D Game Programming & Computer Graphics. Hingham, Mass.: Charles River Media, Inc., 2002.

Microsoft Corporation. Microsoft DirectX 9.0 SDK documentation. Microsoft Corporation, 2002.

Möller, Tomas, and Eric Haines. Real-TimeRendering. 2nd ed. Natick, Mass.: A K Peters, Ltd., 2002.

Mortenson, M.E. Mathematics for Computer Graphics Applications. 2nd ed. New York: Industrial Press, Inc., 1999.

Petzold, Charles. Programming Windows. 5th ed. Redmond, Wash.: Microsoft Press, 1999.

Prosise, Jeff. Programming Windows with MFC. 2nd ed. Redmond, Wash.: Microsoft Press, 1999.

Savchenko, Sergei. 3D Graphics Programming: Games and Beyond, pp. 153-156, 253-294. Sams Publishing, 2000.

van der Burg, John. “Building an Advanced Particle System.” Gamasutra, June 2000. (http://www.gamasutra.com/features/20000623/vanderburg_01.htm)

377

378 Bibliography

Watt, Alan, and Fabio Policarpo. 3D Games: Real-time Rendering and Software Technology. Addison-Wesley, 2001.

Watt, Alan. 3D Computer Graphics. 3rd ed. Addison-Wesley, 2000. Weinreich, Gabriel. Geometrical Vectors, pp. 1-11. Chicago: The Univer-

sity of Chicago Press, 1998.

Wilt, Nicholas. Object-Oriented Ray Tracing in C++, pp. 56-58. New York: John Wiley & Sons, Inc., 1994.

Index

2D array, see matrix

2D representation, 70

3D modelers, 179

3D Studio MAX, 179

A

abs function, 290

addition, see matrix and vectors address modes, 116-117

border color, 116 clamp, 116 mirror, 116 wrap, 116

Adobe Photoshop, 214 alpha

blending, see blending channels, 125 sources, 125-126

ambient lighting, 98

angle between two vectors, 9 anisotropic filtering, 115 antialiasing, 39 anticommutative

cross products, 10 matrix multiplication 13

aspect ratio, 71 attenuation, 105

B

back buffer, 42 back-facing polygons, 68 backface culling, 68-69 billboard, 235

blending, 121-123 destination blend, 123-124 factors, 123-124

formula, 122

source blend, 123-124

bool variable type, 280

border color, see address modes bounding

box structure, 195 sphere structure, 195 volumes, 193-194

C

callback function, 373 camera, 63-64, 199-201

look vector 199-200 right vector, 199-200 up vector, 67, 199-200 vectors, 199

cartoon

shading, 308-309 vertex shader, 309-311

CD3DFont, 155-156 creating, 156 drawing, 156-157

ceil function, 290

clamp, see address modes clamp function, 290 clipping, 64, 69-70

color

ranges, 91-92 representation, 91-92 RGBA vector, 93 shade modes, 94-95

column vector, 12 COM, 37

combining transformations, 23-25 const variable prefix, 285 constants, see ID3DXConstantTable coordinate systems, 3

cos function, 290 CreateWindow function, 370-371 cross function, 290

379

380 Index

D

D3DBLEND, 123-124 D3DCAPS9, 44 D3DCLEAR_* flags, 55 D3DCMPFUNC, 135 D3DCOLOR, 91 D3DCOLOR_ARGB macro, 92 D3DCOLOR_XRGB macro, 92 D3DCOLORVALUE, 92 D3DCULL, 69 D3DDECLMETHOD, 296 D3DDECLTYPE, 295-296 D3DDECLUSAGE, 296 D3DDEVTYPE, 37 D3DFORMAT, 40-41 D3DFVF, 61

D3DINDEXBUFFER_DESC, 80 D3DLIGHT9, 105-106 D3DLIGHTTYPE, 105 D3DLOCK flags, 79 D3DLOCK_DISCARD, 79, 246

D3DLOCK_NOOVERWRITE, 79, 246 D3DLOCK_READONLY, 79 D3DLOCKED_RECT, 39 D3DMATERIAL9, 99

D3DMATRIX, 17 D3DMULTISAMPLE_TYPE, 40 D3DPOOL, 41-42 D3DPRESENT, 50

D3DPRESENT_PARAMETERS, 48-50 D3DPRESENTFLAG, 49 D3DPRIMITIVETYPE, 82 D3DRENDERSTATETYPE, 80 D3DRS_ALPHABLENDENABLE, 123 D3DRS_DESTBLEND, 123 D3DRS_FILLMODE, 80 D3DRS_LIGHTING, 108 D3DRS_MAXANISOTROPY, 115 D3DRS_NORMALIZENORMALS, 104 D3DRS_POINTSCALE_A, 238 D3DRS_POINTSCALE_B, 238 D3DRS_POINTSCALE_C, 238 D3DRS_POINTSIZE_MAX, 238 D3DRS_POINTSIZE_MIN, 237 D3DRS_POINTSPRITEENABLE, 236

D3DRS_POINTSPRITESCALEENABLE, 237

D3DRS_POINTSPRITESIZE, 237 D3DRS_SHADEMODE, 95 D3DRS_SPECULARENABLE, 99 D3DRS_SRCBLEND, 123 D3DRS_STENCILENABLE, 132 D3DRS_STENCILFAIL, 136 D3DRS_STENCILMASK, 134 D3DRS_STENCILPASS, 136 D3DRS_STENCILREF, 134 D3DRS_STENCILWRITEMASK, 137 D3DRS_STENCILZFAIL, 136 D3DRS_ZENABLE, 150 D3DRS_ZWRITEENABLE, 141 D3DSAMP_ADDRESSU, 117 D3DSAMP_ADDRESSV, 117 D3DSAMP_MAGFILTER, 114-115 D3DSAMP_MINFILTER, 114-115 D3DSAMP_MIPFILTER, 116 D3DSHADEMODE, 95 D3DSTENCILOP_*, 136 D3DSWAPEFFECT, 49 D3DTEXF_LINEAR, see

D3DTEXTUREFILTERTYPE D3DTEXF_NONE, see

D3DTEXTUREFILTERTYPE D3DTEXF_POINT, see

D3DTEXTUREFILTERTYPE D3DTEXTUREADDRESS, 116 D3DTEXTUREFILTERTYPE, 114 D3DTS_PROJECTION, 71 D3DTS_VIEW, 67 D3DTS_WORLD, 66 D3DTSS_ALPHAARG1, 125 D3DTSS_ALPHAOP, 125 D3DUSAGE flags, 76 D3DVECTOR, 5 D3DVERTEXBUFFER_DESC, 80 D3DVERTEXELEMENT9, 295 D3DVIEWPORT9, 72

D3DX Library, xxii D3DXATTRIBUTERANGE, 166 D3DXATTRIBUTEWEIGHTS, 187-188 D3DXCOLOR, 92-93

Index 381

D3DXCompileShaderFromFile, 278-279

D3DXComputeBoundingBox, 194

D3DXComputeBoundingSphere, 194

D3DXComputeNormals, 185

D3DXCreate*, 84-85

D3DXCreateBuffer, 179

D3DXCreateEffectFromFile, 341

D3DXCreateMesh, 171

D3DXCreateMeshFVF, 170

D3DXCreateText, 157-159

D3DXCreateTexture, 222

D3DXCreateTextureFromFile, 113

D3DXDeclaratorFromFVF, 171

D3DXFilterTexture, 223

D3DXGeneratePMesh, 186

D3DXHANDLE, 275

D3DXLoadMeshFromX, 180

D3DXMATERIAL, 181

D3DXMATRIX, 16-17

D3DXMatrixIdentity, 18

D3DXMatrixInverse, 18

D3DXMatrixLookAtLH, 67

D3DXMatrixPerspectiveFovLH, 71

D3DXMatrixReflect, 138

D3DXMatrixRotationAxis, 205

D3DXMatrixRotationX, 21

D3DXMatrixRotationY, 22

D3DXMatrixRotationZ, 22

D3DXMatrixScaling, 23

D3DXMatrixShadow, 147

D3DXMatrixTranslation, 21

D3DXMatrixTranspose, 18

D3DXMESH, 169

D3DXMESHOPT, 164

D3DXPLANE, 26-27

D3DXPlaneDotCoord, 27

D3DXPlaneFromPointNormal, 28

D3DXPlaneFromPoints, 28

D3DXPlaneNormalize, 29

D3DXPlaneTransform, 29

D3DXSHADER_DEBUG, 279

D3DXSHADER_SKIPOPTIMIZATION,

279

D3DXSHADER_SKIPVALIDATION,

279

D3DXSplitMesh, 233

D3DXVec3Cross, 11

D3DXVec3Dot, 10 D3DXVec3Length, 7 D3DXVec3Normalize, 7 D3DXVec3TransformCoord, 25 D3DXVec3TransformNormal, 25 D3DXVECTOR3, 4-5 D3DXVECTOR4, 16 DefWindowProc function, 374 degenerate

quad, 312 triangle, 312

degrees function, 290 depth bias, 149 depth buffer, 43-44

enabling/disabling, 150 enabling/disabling writes to, 141 formats, 44

determinant function, 290

device capabilities, checking, 44-45 device enumeration, 46

diffuse lighting, 98, 224-228

diffuse lighting vertex shader, 301-303 Direct3D

initialization, 54-57 overview 35-36 pipeline, 64-65

Direct3DCreate9, 46 directional lights, 104 DirectX 9.0

documentation, xxiii installing, xix-xx

DispatchMessage function, 373 distance function, 290 do…while loop, 286

dot function, 290

double blending, 147-148 double variable type, 281 drawing preparations, 81 DX Texture Tool, 126-127

dynamic index buffer, see index buffer dynamic vertex buffer, see vertex buffer

382 Index

E

effect file, 335 annotations, 339-340 creating, 341-342 device states, 340-341 enabling, 344-347

parameters/constants, 342-344 passes, 336-337

pixel shader objects, 338-339 sampler states, 337-338 string objects, 339 techniques, 336-337

texture objects, 337

vertex shader objects, 338-339 EffectEdit, 356

EPSILON, 6, 194

event-driven programming model, 360 extern variable prefix, 284

F

face normal, 101 face remap, 164 falloff, 105 filtering, 114 flat shading, 94

flexible vertex formats, see D3DFVF float variable type, 281

float*x* variable type, 282 float2 variable type, 281 float3 variable type, 281 float4 variable type, 281

floating-point formats, see D3DFORMAT floor function, 229, 290

fog, 352-353

fonts, see CD3DFont, D3DXCreateText, and ID3DXFont

for loop, 286 foreshortening, 70

frames rendered per second, 155 front buffer, 42

front-facing polygons, 68 frustum, 63-64

G

GetMessage function, 373 global space, see world space Gouraud shading, 94

GUI, 362

H

HAL, 36

half variable type, 280 handle

to application, see HINSTANCE to brush, see HBRUSH

to cursor, see HCURSOR

to HLSL variable, see D3DXHANDLE to icon, see HICON

to menu, see HMENU to window, see HWND

hardware abstraction layer, see HAL hardware vertex processing, see vertex

processing HBRUSH, 369 HCURSOR, 369 heightmaps, 213-214

creating, 214-215 loading, 215

HICON, 369

High-Level Shading Language, see HLSL HINSTANCE, 367

HLSL, 269-270 array types, 283 casting, 286

constants, see ID3DXConstantTable entry point, 273-274

functions, 288-290

input/output structures, 272-273 intrinsic functions, 290-291 keywords, 285

matrix types, 282-283 operators, 287-288 scalar types, 280-281 semantics, 273 statements, 285-286 structures, 283-284 typedef keyword, 284

variable prefixes, 284-285

Index 383

vector types, 281-282 writing a shader, 270-272

HMENU, 371

homogeneous coordinates, 19 homogeneous vector, 19 HWND, 367

I

ID3DXBuffer, 178-179

ID3DXBuffer::GetBufferPointer, 178

ID3DXBuffer::GetBufferSize, 178

ID3DXConstantTable, 275

ID3DXConstantTable::GetConstant-

ByName, 275

ID3DXConstantTable::Set*, 275-278

ID3DXCreateFontIndirect, 153

ID3DXEffect::Begin, 345

ID3DXEffect::End, 346

ID3DXEffect::GetTechniqueByName,

345

ID3DXEffect::Pass, 346

ID3DXEffect::Set*, 343

ID3DXEffect::SetTechnique, 345

ID3DXEffect::ValidateTechnique, 345

ID3DXFont, 153

creating, 153-154

drawing, 154-155

ID3DXInclude, 279

ID3DXMesh, 84-85, 160

adjacency info, 167-169

attribute, 161

attribute buffer, 162

attribute ID, 162

attribute table, 165-167

cloning, 169

creating, 170-171

data, 160-163

drawing, 163

optimizing, 163-165

subsets, 161

ID3DXMesh::CloneMeshFVF, 169

ID3DXMesh::DrawSubset, 163

ID3DXMesh::GenerateAdjacency, 168

ID3DXMesh::GetAttributeTable, 167

ID3DXMesh::GetFVF, 161

ID3DXMesh::GetIndexBuffer, 160

ID3DXMesh::GetNumBytesPerVertex,

161

ID3DXMesh::GetNumFaces, 161

ID3DXMesh::GetNumVertices, 161

ID3DXMesh::GetVertexBuffer, 160

ID3DXMesh::LockIndexBuffer, 161

ID3DXMesh::LockVertexBuffer, 161

ID3DXMesh::Optimize, 165

ID3DXMesh::OptimizeInplace, 163

ID3DXMesh::UnlockIndexBuffer, 161

ID3DXMesh::UnlockVertexBuffer, 161

ID3DXPMesh, 185

creating, 186-188

methods, 188-189

ID3DXPMesh::GetMaxFaces, 188

ID3DXPMesh::GetMaxVertices, 188

ID3DXPMesh::GetMinFaces, 188

ID3DXPMesh::GetMinVertices, 188

ID3DXPMesh::SetNumFaces, 188

ID3DXPMesh::SetNumVertices, 188

ID3DXPMesh::TrimByFaces, 189

ID3DXPMesh::TrimByVertices, 189

IDirect3D9, 45

IDirect3D9::CheckDeviceMultiSample-

Type, 40

IDirect3D9::GetDeviceCaps, 47

IDirect3DDevice9, 45-46

IDirect3DDevice9::BeginScene, 84

IDirect3DDevice9::Clear, 55

IDirect3DDevice9::CreateDevice, 50-51

IDirect3DDevice9::CreateIndexBuffer,

78

IDirect3DDevice9::CreatePixelShader,

324

IDirect3DDevice9::CreateVertexBuffer,

77

IDirect3DDevice9::CreateVertex-

Declaration, 297

IDirect3DDevice9::CreateVertexShader,

300

IDirect3DDevice9::DrawIndexed-

Primitive, 82

IDirect3DDevice9::DrawPrimitive, 82

IDirect3DDevice9::EndScene, 84