Advanced 3D Game Programming with DirectX* 10.0 Peter Walsh Wordware Publishing, Inc.
Acknowledgments Introduction xiii xv Chapter I Windows I A Word about Windows I Hungarian Notation 3 General Windows Concepts 4 Message Handling in Windows 5 Processing Messages 6 Hello World Windows Style 7 Explaining the Code II Registering the Application 13 Initializing the Window 13 WndProc The Message Pump 17 Manipulating Window Geometry 17 Important Window Messages 20 Class Encapsulation 23 COM: The Component Object Model 30 Conclusion 33 Chapter 2 Getting Started with DirectX 10 35 What Is DirectX? 35 Installation 36 Setting Up VC+ + 36 What Happened to DirectDraw? 39 Direct3D 40 2D Graphics 101 41 Textures 44 Complex Textures 46 Describing Textures 46 The ID3D10Texture2D Interface 50 Texture Operations 51 Modifying the Contents of Textures 51 Creating Textures 52 Implementing Direct3D with cgraphicslayer 53 Creating the Graphics Layer 58
Initializing Direct3D 58 Step I: Creating a Device and Swap Chain 58 Step 2: Creating a Render Target View 62 Step 3: Putting It All Together 62 Shutting Down Direct3D 64 Sample Application: Direct3D Sample 64 Conclusion 66 Chapter 3 Input and Sound 67 Directlnput 67 Devices 68 Receiving Device States 70 Cooperative Levels 73 Application Focus and Devices 73 The Directlnput Object 74 Implementing Directlnput with cinputlayer 74 Additions to capplication 87 Sound 87 The Essentials of Sound 88 DirectSound Concepts 89 DirectSound Buffers 90 Operations on Sound Buffers 93 Loading WAV Files 96 Implementing DirectSound with csoundlayer 103 Creating the DirectSound Object 103 Setting the Cooperative Level 104 Grabbing the Primary Buffer 105 The csound Class 108 Additions to capplication I 14 Application: DirectSound Sample 114 Conclusion 119 Chapter 4 3D Math Foundations 121 Points 121 The point3 Structure 124 Basic point3 Functions 125 Assign 125 Mag and MagSquared 126 Normalize 126 Dist 126 point3 Operators 127 Addition/Subtraction 127 Vector-Scalar Multiplication/Division 129 Vector Equality 130 Dot Product 131 vi
Cross Product 134 Polygons 135 Triangles 138 Strips and Fans 139 Planes 141 Defining Locality with Relation to a Plane 144 Back-face Culling 147 Clipping Lines 148 Clipping Polygons 149 Object Representations 153 Transformations 156 Matrices 156 The matrix4 Structure 166 Translation 168 Basic Rotations 169 Axis-Angle Rotation 170 The LookAt Matrix 172 Perspective Projection Matrix 174 Inverse of a Matrix 174 Collision Detection with Bounding Spheres 175 Lighting 178 Representing Color 178 Lighting Models 180 Specular Reflection 182 Light Types 183 Parallel Lights (or Directional Lights) 183 Point Lights 184 Spotlights 185 Shading Models 186 Lambert 186 Gouraud 186 Phong 187 BSP Trees 187 BSP Tree Theory 188 BSP Tree Construction 189 BSP Tree Algorithms 194 Sorted Polygon Ordering 194 Testing Locality of a Point 196 Testing Line Segments 196 BSP Tree Code 197 Wrapping It Up 207 Chapter 5 Artificial Intelligence 209 Starting Point 210 Locomotion 210
Steering Basic Algorithms 211 Chasing 211 Evading 211 Pattern-based Al 212 Steering Advanced Algorithms 213 Potential Functions 214 The Good 215 The Bad 215 Application: potentialfunc 216 Path Following 218 Groundwork 220 Graph Theory 221 Using Graphs to Find Shortest Paths 225 Application: Path Planner 227 Motivation 230 Nondeterministic Finite Automata (NFAs) 230 Genetic Algorithms 233 Rule-Based Al 234 Neural Networks 235 A Basic Neuron 236 Simple Neural Networks 238 Training Neural Networks 240 Using Neural Networks in Games 241 Application: NeuralNet 241 Extending the System 253 Chapter 6 Multiplayer Internet Networking with UDP 255 Terminology 255 Endianness 255 Network Models 257 Protocols 258 Packets 259 Implementation I: MTUDP 260 Design Considerations 260 Things to Watch Out For 260 Mutexes 263 Threads, Monitor, and the Problem of the try/throw/catch Construction 264 MTUDP: The Early Years 265 MTUDP::Startup() and MTUDP::Cleanup() 266 MTUDP::MTUDP() and MTUDP::~MTUDP() 267 MTUDP::StartListening() 267 MTUDP::StartSending() 268 MTUDP::ThreadProc() 269 MTUDP::ProcesslncomingData() 270 viii
MTUDP::GetReliableData() 271 Reliable Communications 271 cdatapacket 271 cqueueln 272 chost 274 MTUDP::ReliableSendTo() 278 cunreliablequeueln 284 chost::addackmessage()/chost::processlncomingacks().. 285 cnetclock 290 Implementation 2: Smooth Network Play 292 Geographie and Temporal Independence 293 Timing Is Everything 294 Pick and Choose 295 Prediction and Extrapolation 295 Conclusion 297 Chapter 7 Direct3D Fundamentals 299 Introduction to D3D 299 Getting Started with Direct3D 300 Step I: Creating the ID3DI ODevice and Swap Chain 300 Step 2: Creating a Depth/Stencil Buffer 302 Bringing It All Together 306 Step 3: Creating a Viewport 308 Step 4: Creating a Default Shader 309 Introduction to Shaders 309 Your First HLSL Shader 311 The Vertex Shader 312 The Pixel Shader 313 The Technique 313 Setting Up the Shader in Code 315 More about Depth Buffers 322 Stencil Buffers 325 Vertex Buffers 325 Lighting with Shaders 328 Application: D3D View 330 The.o3d Format 330 The cmodel Class 331 Chapter 8 Advanced 3D Techniques 341 Animation Using Hierarchical Objects 341 Forward Kinematics 343 Inverse Kinematics 346 Application: InvKim 349 Parametric Curves and Surfaces 355 Bezier Curves and Surfaces 355 ix
Bezier Concepts 355 The Math 358 Finding the Basis Matrix 360 Calculating Bezier Curves 361 Forward Differencing 363 The cfwddifflterator Class 365 Drawing Curves 367 Drawing Surfaces 367 Application: Teapot 369 B-Spline Curves 376 Application: BSpline 377 Subdivision Surfaces 379 Subdivision Essentials 380 Triangles vs. Quads 382 Interpolating vs. Approximating 382 Uniform vs. Non-Uniform 383 Stationary vs. Non-Stationary 383 Modified Butterfly Method Subdivision Scheme 383 Application: SubDiv 387 Progressive Meshes 399 Progressive Mesh Basics 401 Choosing Our Edges 402 An Edge Selection Algorithm 403 Quadric Error Metrics 403 Implementing a Progressive Mesh Renderer 405 Radiosity 406 Radiosity Foundations 407 Progressive Radiosity 410 The Form Factor 41 I Application: Radiosity 412 Conclusion 416 Chapter 9 Advanced Direct3D 417 Alpha Blending 417 The Alpha Blending Equation 418 A Note on Depth Ordering 419 Enabling Alpha Blending 419 Using Alpha Blending from C++ 419 Using Alpha Blending from Shaders 422 Texture Mapping 101 423 Fundamentals 424 Affine vs. Perspective Mapping 425 Texture Addressing 426 Wrap 426 Mirror and Mirror Once 427
Clamp 427 Border Color 428 Texture Wrapping 429 Texture Aliasing 430 MIP Maps 432 Filtering 433 Point Sampling 433 Linear Filtering 434 Anisotropic Filtering 435 Textures in Direct3D 436 Texture Loading 437 DDS Format 437 The ctexture Class 438 Activating Textures 440 Creating a Shader View 441 Adding Textures to the Shader 441 Sending the Texture to the Shader 442 Texture Sampling 442 Texture Mapping 202 443 Texture Arrays 443 Effects Using Multiple Textures 443 Light Maps (a.k.a. Dark Maps) 444 Environment Maps 446 Spherical Environment Maps 446 Cubic Environment Maps 450 Specular Maps 452 Detail Maps 452 Application: Detail 455 Glow Maps 463 Gloss Maps 463 Other Effects 464 Application: MultiTex 465 Using the Stencil Buffer 483 Overdraw Counter 485 Dissolves and Wipes 485 Conclusion 485 Chapter 10 Scene Management 487 The Scene Management Problem and Solutions 487 Quadtrees/Octrees 488 Portal Rendering 490 Portal Rendering Concepts 491 Exact Portal Rendering 497 Approximative Portal Rendering 498 Portal Effects 499 xi
Mirrors 499 Translocators and Non-Euclidean Movement 502 Portal Generation 503 Precalculated Portal Rendering (with PVS) 505 Advantages/Disadvantages 506 Implementation Details 506 Application: Mobots Attack! 507 Interobject Communication 507 Network Communication 511 Code Structure 514 Closing Thoughts 515 Appendix An STL Primer 517 Templates 517 Containers 518 Iterators 519 Functors 521 Index 523 xii