ebook img

Beginning DirectX 9 PDF

353 Pages·2004·5.107 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning DirectX 9

00 DX9_GP FM 3/15/04 1:05 PM Page i 00 DX9_GP FM 3/15/04 1:05 PM Page ii © 2004 by Premier Press, a division of Course Technology. All rights reserved. Senior Vice President, No part of this book may be reproduced or transmitted in any form or by any Course PTR Group: means, electronic or mechanical, including photocopying, recording, or by any Andy Shafran information storage or retrieval system without written permission from Publisher: Course PTR, except for the inclusion of brief quotations in a review. Stacy L. Hiquet The Premier Press logo and related trade dress are trademarks of Premier Press Senior Marketing Manager: and may not be used without written permission. Sarah O’Donnell DirectX is a registered trademark of Microsoft Corporation in the U.S. and/or Marketing Manager: other countries. Heather Hurley © Microsoft Corporation, 2002. All rights reserved. Manager of Editorial Services: All other trademarks are the property of their respective owners. Heather Talbot Important: Course PTR cannot provide software support. Please contact the Senior Acquisitions Editor: appropriate software manufacturer’s technical support line or Web site for Emi Smith assistance. Associate Marketing Manager: Course PTR and the author have attempted throughout this book to distin- Kristin Eisenzopf guish proprietary trademarks from descriptive terms by following the capital- Project/Copy Editor: ization style used by the manufacturer. Karen A. Gill Information contained in this book has been obtained by Course PTR from Technical Reviewer: sources believed to be reliable. However, because of the possibility of human or Joseph Hall mechanical error by our sources, Course PTR, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is Retail Market Coordinator: not responsible for any errors or omissions or the results obtained from use of Sarah Dubois such information. Readers should be particularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this Interior Layout: book went to press. Marian Hartsough Educational facilities, companies, and organizations interested in multiple Cover Designer: copies or licensing of this book should contact the publisher for quantity dis- Steve Deschene count information. Training manuals, CD-ROMs, and portions of this book are CD-ROM Producer: also available individually or can be tailored for specific needs. Brandon Penticuff ISBN: 1-59200-349-4 Indexer: Library of Congress Catalog Card Number: 2004090736 Sharon Shock Printed in the United States of America Proofreader: 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1 Sean Medlock Course PTR, a division of Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com 00 DX9_GP FM 3/15/04 1:05 PM Page iii To my children, Virginia, Elizabeth, and Ian and my forever, Ilene. 00 DX9_GP FM 3/15/04 1:05 PM Page iv Acknowledgments I ’d definitely like to thank Emi Smith and Karen Gill for working so patiently with me in the writing of this book. I’m very grateful to Joseph Hall for agreeing to do the technical editing. His comments regarding the code samples kept me sane. I’d also like to thank Course PTR for giving me the opportunity to present such a won- derful topic as DirectX. Finally, I’d like to send a heartfelt thank you to Albert James, my friend and mentor, who helped me embrace my love for programming and allow it to grow. iv 00 DX9_GP FM 3/15/04 1:05 PM Page v About the Author WENDY JONES devoted herself to computers the first time her eyes befell an Apple IIe in elementary school. From that point on, she spent every free moment learning BASIC and graphics programming, sketching out her ideas on graph paper to type in later. Other computer languages followed, including Pascal, C, Java, and C++. As Wendy’s career in computers took off, she branched out from DOS, teaching herself Windows programming and then jumping into the dot-com world for a bit. Although Internet companies provided cash, they didn’t provide fulfillment, so Wendy started expanding her programming skills to games, devoting any extra energy to its pursuit. Wendy’s true passion became apparent when she got the opportunity to work for Atari’s Humongous Entertainment as a game programmer. During her time at Atari, she worked on both PC and console titles, thrilled with the challenge they provided. Wendy is currently working with PocketPC software and handheld gaming devices. If you have any comments or questions about this book, you can reach Wendy at [email protected]. v 00 DX9_GP FM 3/15/04 1:05 PM Page vi Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv Part I Getting Down the Basics . . . . . . . . . . . . .1 Chapter 1 The What, Why, and How of DirectX . . . . . . . . . . . . . . . . . .3 Chapter 2 Your First DirectX Program . . . . . . . . . . . . . . . . . . . . . . . . . .9 Chapter 3 Surfaces, Sprites, and Salmon . . . . . . . . . . . . . . . . . . . . . . .35 Part II It’s a 3D World After All . . . . . . . . . . .63 Chapter 4 3D Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Chapter 5 Matrices, Transforms, and Rotations . . . . . . . . . . . . . . . . . .87 Chapter 6 Vertex Colors, Texture Mapping, and 3D Lighting . . . . . .117 Chapter 7 Meshes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Chapter 8 Point Sprites, Particles, and Pyrotechnics . . . . . . . . . . . . . .177 vi 00 DX9_GP FM 3/15/04 1:05 PM Page vii Contents at a Glance vii Part III Additional Needs . . . . . . . . . . . . . . . . .199 Chapter 9 Using DirectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Chapter 10 DirectSound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Chapter 11 The Final Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257 Part IV Appendixes . . . . . . . . . . . . . . . . . . . . . .293 Appendix A Answers to End-of-Chapter Exercises . . . . . . . . . . . . . . . .295 Appendix B Using the CD-ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 00 DX9_GP FM 3/15/04 1:05 PM Page viii Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv Part I Getting Down the Basics . . . . . . . . . . . . .1 Chapter 1 The What, Why, and How of DirectX . . . . . . . . . . . . . . . . . .3 What Is DirectX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 The Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Why Is DirectX Needed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 How Is DirectX Put Together? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 The Component Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 The Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Chapter 2 Your First DirectX Program . . . . . . . . . . . . . . . . . . . . . . . . . .9 Creating the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Adding the Windows Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 WinMain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 InitWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 WndProc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Time for DirectX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 The Direct3D Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 Creating the Rendering Device . . . . . . . . . . . . . . . . . . . . . . . . . . .17 Clearing the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Displaying the Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 Cleaning Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 viii 00 DX9_GP FM 3/15/04 1:05 PM Page ix Contents ix Updating the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Changing the Message Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 The Init Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 The Render Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 The cleanUp Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Adding the DirectX Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Taking the Game Full Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Video Modes and Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 Gathering Video Adapter and Driver Information . . . . . . . . . . . .29 Getting the Display Modes for an Adapter . . . . . . . . . . . . . . . . .30 A Code Example for Querying the Video Adapter . . . . . . . . . . . .32 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Chapter 3 Surfaces, Sprites, and Salmon . . . . . . . . . . . . . . . . . . . . . . .35 You’ve Just Touched the Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 The Display Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Offscreen Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Loading a Bitmap to a Surface . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Using DirectX to Render a Bitmap . . . . . . . . . . . . . . . . . . . . . . . .39 StretchRect Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 What Do Sprites Need? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Representing a Sprite in Code . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Creating Your First Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Moving Your Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Animating Your Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Displaying the Animated Sprites . . . . . . . . . . . . . . . . . . . . . . . . .55 Why Is It So Fast? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Timers: How to Animate on Time . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Timing Under Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Using QueryPerformanceCounter . . . . . . . . . . . . . . . . . . . . . . . .58 Getting the Time for Each Frame . . . . . . . . . . . . . . . . . . . . . . . . .58 Changing the Animation to Be Time Based . . . . . . . . . . . . . . . . .59 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Part II It’s a 3D World After All . . . . . . . . . . .63 Chapter 4 3D Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 3D Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Defining a Point in 2D Space . . . . . . . . . . . . . . . . . . . . . . . . . . . .66

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.