.Introduction Advanced 3D Game Programming with DirectX 9.0 by Peter Walsh Wordware Publishing © 2003 Companion Web Site Advanced 3D Game Programming Using DirectX 9.0 Peter Walsh Wordware Publishing, Inc Library of Congress Cataloging-in-Publication Data Walsh, Peter (Peter Andrew), 1980Advanced 3D game programming with DirectX 9.0 / by Peter Walsh p cm ISBN 1-55622-968-2 (pbk.) Computer games–Programming DirectX I Title QA76.76.C672W382 2003 794.8'167768–dc21 2003007140 CIP Copyright © 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 1-55622-968-2 10 0403 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 Dedications To my beautiful fiancée Lisa Sullivan I love you with all my heart Peter To my parents, Manny and Maria Adrian Original edition for DirectX version 7.0 written by Adrian Perez with Dan Royer Revised and updated by Peter Walsh Acknowledgments Like Adrian says below, this book, like any other, was not just the work of one (or two or three) people; there have been so many people over the years who have helped me in one way or another, and the result of all these efforts contributed to the knowledge contained in this book I will try to thank everyone I can My update of this book would not have occurred without the help of Tracy Williams, who has helped me many times with my books Not only did she get me going on my first book, but she got me hooked up with Wordware for this book, my third Of course, I must thank Jim Hill, Wes Beckwith, and Tim McEvoy of Wordware for being such great people to work with Thanks to Phil Taylor on the DirectX team at Microsoft for agreeing to the tech check and also to Wolfgang Engel and Bruno Sousa for their technical support Of course, thank you to my wonderful fiancee Lisa for helping to keep me motivated while working on the book, when I just wanted to give up and party! Where would I be without thanking all my friends and family, who keep me sane during the many months that I spent researching and writing these massive books So thank you Jon-Paul Keatley, Stewart Wright, Andrew McCall, Todd Fay, Mike Andrews, Laz Allen, and all my other friends around the world that I don't have room to list! Also, who would I be writing a book and not mentioning my soon-to-be family-in-law? So thank you Liam and Ann Sullivan for giving me permission to marry your beautiful daughter (also to Joanne, Pauline, Liam Jr., and the rest of the family) Of course, thanks to my parents Simon and Joy Walsh for being so supportive during my younger years and to this day The worst thing about writing acknowledgments is that you always forget someone who helped you until the day the book goes to print So thank you to everyone else I forgot—please accept my apologies; my poor brain is worn out after all this work! Peter Walsh This book couldn't have been completed without the help and guidance of a whole lot of people I'll try to remember them all here First, thanks to Wes Beckwith and Jim Hill at Wordware Publishing They were extremely forgiving of my hectic schedule, and they helped guide me to finishing this book I also must thank Alex Dunne for letting me write an article in 1998 for Game Developer magazine If I hadn't written that article, I never would have written this book Everything I know about the topics in this book I learned from other people Some of these people were mentors, others were bosses, and still others were professors and teachers Some were just cool people who took the time to sit and talk with me I can't thank them enough Paul Heckbert, Tom Funkhouser, Eric Petajan, Charles Boyd, Mike Toelle, Kent Griffin, David Baraff, Randy Pausch, Howie Choset, Michael Abrash, Hugues Hoppe, and Mark Stehlik: You guys rock Thank you Thanks to Microsoft, ATI, nVidia, id Software, and Lydia Choy for helping me with some of the images used in the text Many people helped assure the technical correctness and general sanity of this text Ian Parberry and his class at University of North Texas were immensely helpful: Thanks, guys Michael Krause was an indispensable help in assuring the correctness of the DirectX chapters Bob Gaines, Mikey Wetzel, and Jason Sandlin from the DirectX team at Microsoft helped make sure Chapters 2, 3, 4, 8, and 10 were shipshape: Mad props to them David Black was kind enough to look over Chapter 11 and help remove some errors and clarify a few points Finally, I need to thank all of the people who helped me get this thing done I know I won't be able to remember all of them, but here's a short list: Manual and Maria Perez, Katherin Peperzak, Lydia Choy (again), Mike Schuresko, Mike Breen (and the rest of the Originals), Vick Mukherjee, Patrick Nelson, Brian Sharp, and Marcin Krieger Adrian Perez About the author Peter Walsh is a professional game programmer at Visual Science Ltd., where he has worked on a number of titles including the Formula series of games, Harry Potter and the Chamber of Secrets, and others for Electronic Arts, the world's leading publisher of computer games He has studied for a degree in computer games development at Abertay University in Dundee, Scotland, and has worked with IC-CAVE, a think tank for the next generation of gaming technology The complete source code in C++, including a game demonstrating techniques covered in this book, can be downloaded from http://www.wordware.com/files/dx9 .Advanced 3D Game Programming with DirectX 9.0 ISBN:1556229682 by Peter Walsh Wordware Publishing © 2003 (525 pages) Designed for programmers who are new to graphics and game programming, this book covers Direct 3D, DirectInput, and DirectSound, as well as artificial intelligence, networking, multithreading, and scene management Companion Web Site Table of Contents Back Cover Table of Contents Advanced 3D Game Programming Using DirectX 9.0 Introduction Chapter - Windows Chapter - Getting Started with DirectX Chapter - Communicating with DirectInput Chapter - DirectSound Chapter - 3D Math Foundations Chapter - Artificial Intelligence Chapter - UDP Networking Chapter - Beginning Direct3D Chapter - Advanced 3D Programming Chapter 10 - Advanced Direct3D Chapter 11 - Scene Management Appendix - An STL Primer Introduction A wise man somewhere, somehow, at some point in history, may have said the best way to start a book is with an anecdote I would never question the words of a wise man who may or may not have existed, so here we go When I was a freshman in high school back in 1993, I took the required biology class that most kids my age end up having to take It involved experiments, lab reports, dissecting of various animals, and the like One of my lab partners was a fellow named Chris V We were both interested in computers and quickly became friends, to the point where talking about biology in class was second to techno-babble One night, in the middle of December, Chris called me up The lab report that was due the next day required results from the experiment we had done together in class, and he had lost his copy of our experiment results He wanted to know if I could copy mine and bring them over to his place so he could finish writing up the lab Of course, this was in those heinous pre-car days, so driving to his house required talking my parents into it, finding his address, and various other hardships While I was willing to him the favor, I wasn't willing to it for free So I asked him what he could to reciprocate my kind gesture "Well," he said, "I guess I can give you a copy of this game I just got." "Really? What's it called?" I said "Doom By the Wolf 3D guys." "It's called Doom? What kind of name is that??" After getting the results to his house and the game to mine, I fired the program up on my creaky old 386 DX-20 clone, burning rubber with a whopping MB of RAM As my space marine took his first tenuous steps down the corridors infested with hellspawn, my life changed I had done some programming before in school (Logo and Basic), but after I finished playing the first time, I had a clear picture in my head of what I wanted to with my life: I wanted to write games, something like Doom I popped onto a few local bulletinboards and asked two questions: What language was the game written in, and what compiler was used? Within a day or so, I purchased Watcom C 10.0 and got my first book on C programming My first C program was "Hello, World." My second was a slow, crash-happy, non-robust, wireframe spinning cube I tip my hat to John Carmack, John Romero, and the rest of the team behind Doom; my love for creating games was fully realized via their masterpiece It's because of them that I learned everything that I have about this exceptionally interesting and dynamic area of computer acquired programming The knowledge that I have is what I hope to fill these pages with, so other people can get into graphics and game programming I've found that the best way to get a lot of useful information down in a short amount of space is to use the tried-and-true FAQ (frequently asked questions) format I figured if people needed answers to some questions about this book as they stood in their local bookstore trying to decide whether or not to buy it, these would be them Who are you? What are you doing here? Well I, being Peter rather than Adrian, am a professional games programmer and have been for a quite a few years I started out like most people these days, getting extremely interested in how games worked after Doom came out After teaching myself programming, I moved on to study for a degree in computer games development at Abertay University in Dundee, Scotland After that I went on to work for a short while with IC-CAVE, which is a think tank for the next generation of gaming technology Over the years I've worked on games like F1 Career Challenge, Harry Potter and the Chamber of Secrets, SHOX, and the upcoming Medal of Honor: Rising Sun I've developed games for the PC, Game Boy, Dreamcast, PS2, Game Cube, and Xbox I've also written two other books over the last two years on DirectX programming I've also read so many programming books that I reckon I have personally wiped out half of the Amazon rainforest So hopefully all that material will help me write this book in a way that avoids all the pitfalls that other authors have fallen into I really hope you learn a lot from this book If you have any questions along the way that you just can't get to the bottom of, please email me at mrzen@msn.com Unfortunately, after printing that email in a previous book it was bombarded by junk mail from spammers and became almost unusable However, Hotmail has gotten better lately, so Functors The last thing we'll talk about in this short run through the STL are functors They are used by many of the generic algorithms and functions (like for_each, discussed above) They are classes that implement the parentheses operator This allows them to mimic the behavior of a regular function, but they can neat things like save function state (via member variables) Chapter uses a functor to search through a list of z-buffer formats for a good match using the generic algorithm find_if The algorithm runs the functor on each element in the container until either it runs out of elements or the functor returns true for one of the elements (in this case, the particular z-buffer format we wish to use) See the source code for Chapter to get an idea of how functors work 513 .Advanced 3D Game Programming with DirectX 9.0 ISBN:1556229682 by Peter Walsh Wordware Publishing © 2003 (525 pages) Designed for programmers who are new to graphics and game programming, this book covers Direct 3D, DirectInput, and DirectSound, as well as artificial intelligence, networking, multithreading, and scene management Companion Web Site Table of Contents Back Cover Back Cover This update to the best-selling Advanced 3-D Game Programming Using DirectX 8.0 focuses on implementation changes to DirectX 9.0 Designed for programmers who are new to graphics and game programming, this book covers Direct 3D DirectInput, and DirectSound, as well as artificial intelligence, networking, multithreading, and scene management Along with several sample applications that target specific algorithms, full source code is provided for a client-server networked 3D first-person game that demonstrates many of the techniques discussed in this book Develop a solid foundation in mathematics for use in the creation of 3D graphics Find out how to simulate intelligence in your game using basic and advanced steering algorithms and motivation engines Learn how to implement multithreaded UDP classes for the development of multiplayer networked games Explore a variety of 3D programming topics including forward and inverse kinematics, parametric curves, subdivision surfaces, and progressive meshes Discover how to use the components and tools in DirectX 9.0 to create computer games with cutting-edge 3D algorithms and effects About the Author Peter Walsh is a professional game programmer at Visual Science Ltd., where he has worked on a number of titles including the Formula series of games, Harry Potter and the Chamber of Secrets, and others for Electronic Arts, the world’s leading publisher of computer games He has studied for a degree in computer games development at Abertay University in Dundee, Scotland, and has worked with IC-CAVE, a think tank for the next generation of gaming technology 514 ... Foundations Chapter - Artificial Intelligence Chapter - UDP Networking Chapter - Beginning Direct3D Chapter - Advanced 3D Programming Chapter 10 - Advanced Direct3D Chapter 11 - Scene Management Appendix... Programming Using DirectX 9. 0 Introduction Chapter - Windows Chapter - Getting Started with DirectX Chapter - Communicating with DirectInput Chapter - DirectSound Chapter - 3D Math Foundations Chapter... this case, the particular z-buffer format we wish to use) See the source code for Chapter to get an idea of how functors work 513 .Advanced 3D Game Programming with DirectX 9. 0 ISBN :15 562 296 82