
Introduction to 3D Game Programming with DirectX.9.0 - F. D. Luna
.pdf
|
|
Y |
|
L |
|
|
F |
|
|
M |
|
A |
|
|
E |
|
|
T |
|
|
Introduction to 3D Game Programming with DirectX® 9.0
Frank D. Luna
Technical review by Rod Lopez
Wordware Publishing, Inc.
Library of Congress Cataloging-in-Publication Data
Luna, Frank D.
Introduction to 3D game programming with DirectX 9.0 / by Frank D. Luna.
p.cm.
ISBN 1-55622-913-5 (pbk.)
1. Computer games--Programming. 2. DirectX. I. Title. QA76.76.C672L83 2003
794.8'15268--dc21
© 2003, Wordware Publishing, Inc.
All Rights Reserved
2320 Los Rios Boulevard
Plano, Texas 75074
No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc.
Printed in the United States of America
ISBN 1-55622-913-5
10 9 8 7 6 5 4 3 2 1
0305
DirectX is a registered trademark of Microsoft Corporation in the United States and/or other countries.
All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks should not be regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products.
All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling:
(972) 423-0090
To my parents, Frank and Kathryn
iii
This page intentionally left blank.
Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Part I Mathematical Prerequisites . . . . . . . . . . . . . . 1
Vectors in 3-Space . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Vector Equality . . . . . . . . . . . . . . . . . . . . . . . . . 5 Computing the Magnitude of a Vector. . . . . . . . . . . . . 6 Normalizing a Vector . . . . . . . . . . . . . . . . . . . . . . 7 Vector Addition . . . . . . . . . . . . . . . . . . . . . . . . . 7 Vector Subtraction . . . . . . . . . . . . . . . . . . . . . . . 8 Scalar Multiplication . . . . . . . . . . . . . . . . . . . . . . 9 Dot Products . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Cross Products . . . . . . . . . . . . . . . . . . . . . . . . 10
Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Equality, Scalar Multiplication, and Addition. . . . . . . . . 12 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Identity Matrix . . . . . . . . . . . . . . . . . . . . . . 14 Inverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Transpose of a Matrix . . . . . . . . . . . . . . . . . . 15 D3DX Matrices . . . . . . . . . . . . . . . . . . . . . . . . 16
Basic Transformations . . . . . . . . . . . . . . . . . . . . . . . 18 The Translation Matrix . . . . . . . . . . . . . . . . . . . . 20 The Rotation Matrices . . . . . . . . . . . . . . . . . . . . 21 The Scaling Matrix . . . . . . . . . . . . . . . . . . . . . . 22 Combining Transformations . . . . . . . . . . . . . . . . . 23 Some Functions to Transform Vectors . . . . . . . . . . . . 25
Planes (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . 25 D3DXPLANE . . . . . . . . . . . . . . . . . . . . . . . . . 26 Point and Plane Spatial Relation . . . . . . . . . . . . . . . 27 Construction . . . . . . . . . . . . . . . . . . . . . . . . . 27 Normalizing a Plane. . . . . . . . . . . . . . . . . . . . . . 28 Transforming a Plane . . . . . . . . . . . . . . . . . . . . . 29 Nearest Point on a Plane to a Particular Point . . . . . . . . 29
Rays (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Rays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Ray/Plane Intersection . . . . . . . . . . . . . . . . . . . . 31 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
v

vi Contents
Part II |
Direct3D Fundamentals. . . . . . . . . |
. . . . . . . 33 |
|
Chapter 1 |
Direct3D Initialization . . . . . . . . . |
. . . . . . . 35 |
|
|
1.1 |
Direct3D Overview . . . . . . . . . . . . . |
. . . . . . . . . 35 |
|
|
1.1.1 The REF Device . . . . . . . . . . . |
. . . . . . . . . 36 |
|
|
1.1.2 D3DDEVTYPE . . . . . . . . . . . . |
. . . . . . . . . 37 |
|
1.2 |
COM . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . 37 |
|
1.3 |
Some Preliminaries . . . . . . . . . . . . . |
. . . . . . . . . 37 |
1.3.1Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.3.2Multisampling . . . . . . . . . . . . . . . . . . . . . . 39
1.3.3Pixel Formats . . . . . . . . . . . . . . . . . . . . . . 40
1.3.4Memory Pools. . . . . . . . . . . . . . . . . . . . . . 41
1.3.5 The Swap Chain and Page Flipping . . . . . . . . . . 42
1.3.6Depth Buffers . . . . . . . . . . . . . . . . . . . . . . 43
1.3.7Vertex Processing. . . . . . . . . . . . . . . . . . . . 44
1.3.8Device Capabilities . . . . . . . . . . . . . . . . . . . 44
1.4Initializing Direct3D . . . . . . . . . . . . . . . . . . . . . . 45
1.4.1Acquiring an IDirect3D9 Interface . . . . . . . . . . . 46
1.4.2Checking for Hardware Vertex Processing . . . . . . 47
1.4.3Filling Out the D3DPRESENT_PARAMETERS Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
1.4.4Creating the IDirect3DDevice9 Interface . . . . . . . 50
1.5Sample Application: Initializing Direct3D . . . . . . . . . . . 51
1.5.1d3dUtility.h/cpp . . . . . . . . . . . . . . . . . . . . . 52
1.5.2Sample Framework . . . . . . . . . . . . . . . . . . . 54
1.5.3Sample: D3D Init . . . . . . . . . . . . . . . . . . . . 54
1.6Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Chapter 2 The Rendering Pipeline. . . . . . . . . . . . . . . . 59
2.1Model Representation . . . . . . . . . . . . . . . . . . . . . 60
2.1.1Vertex Formats . . . . . . . . . . . . . . . . . . . . . 61
2.1.2Triangles. . . . . . . . . . . . . . . . . . . . . . . . . 62
2.1.3Indices . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.2The Virtual Camera . . . . . . . . . . . . . . . . . . . . . . 63
2.3The Rendering Pipeline . . . . . . . . . . . . . . . . . . . . 64
2.3.1Local Space . . . . . . . . . . . . . . . . . . . . . . . 65
2.3.2World Space . . . . . . . . . . . . . . . . . . . . . . . 65
2.3.3View Space . . . . . . . . . . . . . . . . . . . . . . . 66
2.3.4 Backface Culling . . . . . . . . . . . . . . . . . . . . 68
2.3.5Lighting . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.3.6Clipping . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.3.7Projection . . . . . . . . . . . . . . . . . . . . . . . . 70
2.3.8 Viewport Transform . . . . . . . . . . . . . . . . . . 72
2.3.9Rasterization . . . . . . . . . . . . . . . . . . . . . . 73
2.4Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Contents vii
Chapter 3 Drawing in Direct3D . . . . . . . . . . . . . . . . . 75
3.1Vertex/Index Buffers . . . . . . . . . . . . . . . . . . . . . . 75
3.1.1Creating a Vertex and Index Buffer . . . . . . . . . . 76
3.1.2Accessing a Buffer’s Memory . . . . . . . . . . . . . 78
3.1.3Retrieving Information about a Vertex and
Index Buffer . |
. |
. |
. |
. . . . |
. |
. |
. . |
. |
. . . . . |
. |
. |
. |
. |
. |
. |
. |
. |
79 |
3.2 Render States |
. |
. |
. |
. . . . |
. |
. |
. . |
. |
. . . . . |
. |
. |
. |
. |
. |
. |
. |
. |
80 |
3.3Drawing Preparations . . . . . . . . . . . . . . . . . . . . . 81
3.4Drawing with Vertex/Index Buffers . . . . . . . . . . . . . . 82
3.4.1IDirect3DDevice9::DrawPrimitive. . . . . . . . . . . 82
3.4.2IDirect3DDevice9::DrawIndexedPrimitive . . . . . . 82
3.4.3Begin/End Scene . . . . . . . . . . . . . . . . . . . . 84
3.5D3DX Geometric Objects . . . . . . . . . . . . . . . . . . . 84
3.6Sample Applications: Triangle, Cube, Teapot,
D3DXCreate* . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Chapter 4 Color . . . . . . . . . |
. . . . . . . . . |
. . . . . . . 91 |
4.1 Color Representation |
. . . . . . . . . . . . |
. . . . . . . . . 91 |
4.2Vertex Colors . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.3Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.4Sample Application: Colored Triangle . . . . . . . . . . . . . 95
4.5Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Chapter 5 Lighting . . . . . . . . . . . . . . . . . . . . . . . . 98
5.1Light Components . . . . . . . . . . . . . . . . . . . . . . . 98
5.2Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.3Vertex Normals . . . . . . . . . . . . . . . . . . . . . . . . 101
5.4Light Sources . . . . . . . . . . . . . . . . . . . . . . . . . 104
5.5Sample Application: Lighting . . . . . . . . . . . . . . . . . 107
5.6Additional Samples . . . . . . . . . . . . . . . . . . . . . . 109
5.7Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Chapter 6 Texturing. . . . . . . . . . . . . . . . . . . . . . . 111
6.1Texture Coordinates . . . . . . . . . . . . . . . . . . . . . 112
6.2Creating and Enabling a Texture . . . . . . . . . . . . . . . 113
6.3Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.4Mipmaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.4.1Mipmap Filter . . . . . . . . . . . . . . . . . . . . . 116
6.4.2Using Mipmaps with Direct3D . . . . . . . . . . . . 116
6.5Address Modes . . . . . . . . . . . . . . . . . . . . . . . . 116
6.6Sample Application: Textured Quad . . . . . . . . . . . . . 118
6.7Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

viii Contents
Chapter 7 Blending. . . . . . . . . . . . . . . . . . . . . . . 121
7.1The Blending Equation . . . . . . . . . . . . . . . . . . . . 121
7.2Blend Factors . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.3Transparency . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.3.1 Alpha Channels . . . . . . . . . . . . . . . . . . . . 125
7.3.2Specifying the Source of Alpha . . . . . . . . . . . . 125
7.4Creating an Alpha Channel Using the DirectX
Texture Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
7.5Sample Application: Transparency . . . . . . . . . . . . . . 127
7.6Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Chapter 8 Stenciling . . . . . . . . . . . . . . . . . . . . . . 131
8.1Using the Stencil Buffer . . . . . . . . . . . . . . . . . . . 132
8.1.1Requesting a Stencil Buffer . . . . . . . . . . . . . . 133
8.1.2The Stencil Test . . . . . . . . . . . . . . . . . . . . 133
8.1.3Controlling the Stencil Test . . . . . . . . . . . . . . 134
8.1.3.1Stencil Reference Value . . . . . . . . . . . 134
8.1.3.2Stencil Mask . . . . . . . . . . . . . . . . . 134
8.1.3.3Stencil Value . . . . . . . . . . . . . . . . . 135
8.1.3.4Comparison Operation . . . . . . . . . . . . 135
8.1.3 |
Updating the Stencil Buffer . |
. |
. |
. |
. |
. |
. . . . . |
. |
. |
. |
135 |
8.1.4 |
Stencil Write Mask . . . . . |
. |
. |
. |
. |
. |
. . . . . |
. |
. |
. |
137 |
8.2Sample Application: Mirrors . . . . . . . . . . . . . . . . . 137
8.2.1The Mathematics of Reflection . . . . . . . . . . . . 137
8.2.2Mirror Implementation Overview . . . . . . . . . . 139
8.2.3Code and Explanation . . . . . . . . . . . . . . . . . 140
8.2.3.1Part I . . . . . . . . . . . . . . . . . . . . . 141
8.2.3.2Part II . . . . . . . . . . . . . . . . . . . . . 141
8.2.3.3Part III . . . . . . . . . . . . . . . . . . . . 142
8.2.3.4 Part IV . . . . . . . . . . . . . . . . . . . . 143
8.2.3.5Part V . . . . . . . . . . . . . . . . . . . . . 143
8.3Sample Application: Planar Shadows. . . . . . . . . . . . . 144
8.3.1 Parallel Light Shadows . . . . . . . . . . . . . . . . 145
8.3.2Point Light Shadows. . . . . . . . . . . . . . . . . . 146
8.3.3The Shadow Matrix . . . . . . . . . . . . . . . . . . 146
8.3.4Using the Stencil Buffer to Prevent Double
Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
8.3.5Code and Explanation . . . . . . . . . . . . . . . . . 148
8.4Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Part III |
Applied Direct3D . . . . . . . . . . . . . |
. . . . |
. |
151 |
Chapter 9 |
Fonts . . . . . . . . . . . . . . . . . . . . |
. . . . |
. |
153 |
9.1ID3DXFont . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9.1.1Creating an ID3DXFont . . . . . . . . . . . . . . . . 153

Contents ix
9.1.2 Drawing Text . . . . . . . . . . . . . . . . . . . . . 154
9.1.3Computing the Frames Rendered Per Second . . . . 155
9.2CD3DFont . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
9.2.1Constructing a CD3DFont. . . . . . . . . . . . . . . 156
9.2.2 |
Drawing Text |
. . |
. |
. |
. . . |
. |
. |
. |
. |
. |
. |
. . . . . |
. |
. |
. |
156 |
9.2.3 |
Cleanup . . |
. . . |
. |
. |
. . . |
. |
. |
. |
. |
. |
. |
. . . . . |
. |
. |
. |
157 |
9.3D3DXCreateText . . . . . . . . . . . . . . . . . . . . . . . 157
9.4Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Chapter 10 Meshes Part I . . . . . . . . . . . . . . . . . . . . 160
10.1Geometry Info . . . . . . . . . . . . . . . . . . . . . . . . 160
10.2Subsets and the Attribute Buffer . . . . . . . . . . . . . . 161
10.3Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
10.4Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . 163
10.5The Attribute Table . . . . . . . . . . . . . . . . . . . . . 165
10.6Adjacency Info . . . . . . . . . . . . . . . . . . . . . . . . 167
10.7Cloning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
10.8Creating a Mesh (D3DXCreateMeshFVF) . . . . . . . . . 170
10.9Sample Application: Creating and Rendering a Mesh . . . 171
10.10Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Chapter 11 Meshes Part II . . . . . . . . . . . . . . . . . . . . 178
11.1ID3DXBuffer. . . . . . . . . . . . . . . . . . . . . . . . . 178
11.2XFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
11.2.1Loading an XFile . . . . . . . . . . . . . . . . . . . 180
11.2.2XFile Materials . . . . . . . . . . . . . . . . . . . . 181
11.2.3Sample Application: XFile . . . . . . . . . . . . . . 181
11.2.4Generating Vertex Normals . . . . . . . . . . . . . 184
11.3Progressive Meshes . . . . . . . . . . . . . . . . . . . . . 185
11.3.1Generating a Progressive Mesh . . . . . . . . . . . 186
11.3.2 Vertex Attribute Weights . . . . . . . . . . . . . . 187
11.3.3ID3DXPMesh Methods . . . . . . . . . . . . . . . 188
11.3.4Sample Application: Progressive Mesh . . . . . . . 190
11.4Bounding Volumes. . . . . . . . . . . . . . . . . . . . . . 193
11.4.1Some New Special Constants . . . . . . . . . . . . 194
11.4.2Bounding Volume Types . . . . . . . . . . . . . . . 195
11.4.3Sample Application: Bounding Volumes. . . . . . . 196
11.5Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Chapter 12 Building a Flexible Camera Class . . . . . . . . . . 199
12.1 Camera Design . . . . . . . . . . . . . . . . . . . . . . . 199
12.2Implementation Details . . . . . . . . . . . . . . . . . . . 201 12.2.1 Computing the View Matrix . . . . . . . . . . . . . 201
12.2.1.1 |
Part 1: |
Translation . |
. |
. |
. |
. |
. . . . . |
. |
. |
. |
202 |
12.2.1.2 |
Part 2: |
Rotation . . . |
. |
. |
. |
. |
. . . . . |
. |
. |
. |
203 |