Beginning Direct3d Game Programming, 2Nd Edition
Total Page:16
File Type:pdf, Size:1020Kb
Beginning Direct3D® Game Programming 2nd Edition Beginning Direct3D® Game Programming 2nd Edition Wolfgang F. Engel © 2003 by Premier Press, a division of Course Technology. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Premier Press, except for the inclusion of brief quotations in a review. The Premier Press logo and related trade dress are trademarks of Premier Press and may not be used without written permission. Publisher: Stacy L. Hiquet Senior Marketing Manager: Martine Edwards Marketing Manager: Heather Hurley Associate Marketing Manager: Kristin Eisenzopf Manager of Editorial Services: Heather Talbot Acquisitions Editor: Mitzi Foster-Koontz Project Editor/Copy Editor: Cathleen D. Snyder Technical Reviewer: André LaMothe Retail Market Coordinator: Sarah Dubois Interior Layout: Shawn Morningstar Cover Designer: Mike Tanamachi CD-ROM Producer: Brandon Penticuff Indexer: Katherine Stimson Proofreader: Lorraine Gunter DirectDraw, DirectMusic, DirectPlay, DirectSound, DirectX, Microsoft, Visual Basic, Visual C++, Windows, Windows NT, Xbox, and/or other Microsoft products are registered trademarks or trade- marks of Microsoft Corporation in the U.S. and/or other countries. All other trademarks are the property of their respective owners. Important: Premier Press cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. Premier Press and the author have attempted throughout this book to distinguish proprietary trade- marks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Premier Press from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of the fact that the Internet is an ever-chang- ing entity. Some facts may have changed since this book went to press. ISBN: 1-931841-39-x Library of Congress Catalog Card Number: 2003101212 Printed in the United States of America 03 04 05 06 07 BH 10 9 8 7 6 5 4 3 2 1 Premier Press, a division of Course Technology 25 Thomson Place Boston, MA 02210 Für meine Frau Katja und unsere Tochter Anja Acknowledgments his book couldn’t have been completed without the help of many people. In Tparticular, I want to thank my parents, who gave me a wonderful and warm child- hood; my wife, Katja, for being patient with a spare-time author; and our nearly two-year-old daughter, Anna, for showing me the important things in life. Additionally I would like to thank those people who also helped to make this book possible: Heather Hurley, Mitzi Koontz, Emi Smith, Cathleen Snyder, Heather Talbot, and André LaMothe. Thanks for your patience with me and for a great time at GDC 2003. About the Author Wolfgang F. Engel is the editor and coauthor of several programming books. He has also written several online tutorials published on http://www.gamedev.net, http://www.direct3d.info, and other Web sites. He held lectures at GDC 2003 and at Vision Days in Copenhagen. Wolfgang is also a faculty advisor for the Academy of Game Entertainment Technology (http://www.academyofget.com/html/advisors.html). Contents at a Glance Introduction . xx Part One DirectX Graphics: Don’t Hurt Me . 1 Chapter 1 The History of Direct3D/DirectX Graphics . 3 Chapter 2 Overview of HAL and COM . 9 Chapter 3 Programming Conventions. 19 Chapter 4 3D Fundamentals, Gouraud Shading, and Texture-Mapping Basics . 27 Chapter 5 The Basics. 37 Chapter 6 First Steps to Animation. 77 Part Two Knee-Deep in DirectX Graphics Programming . 123 Chapter 7 Texture-Mapping Fundamentals . 125 Chapter 8 Using Multiple Textures . 149 Part Three Hard-Core DirectX Graphics Programming . 175 Chapter 9 Shader Programming with the High-Level Shader Language. 177 Chapter 10 More Advanced Shader Effects. 197 Contents at a Glance ix Chapter 11 Working with Files . 227 Chapter 12 Using *.md3 Files . 253 Part Four Appendixes . 299 Appendix A Windows Game Programming Foundation . 301 Appendix B C++ Primer . 327 Appendix C Mathematics Primer . 353 Appendix D Creating a Texture with D3DXCreateTextureFromFileEx(). 371 Appendix E Game Programming Resources . 375 Appendix F What’s on the CD . 377 Index . 381 Contents Letter from the Series Editor . xix Introduction . xx Part One DirectX Graphics: Don’t Hurt Me . 1 Chapter 1 The History of Direct3D/DirectX Graphics. 3 DirectX 2.0 . 4 DirectX 6/7 . 5 DirectX 8. 5 Point Sprites . 6 3D Textures. 6 Direct3DX Utility Library . 6 Vertex and Pixel Shaders . 6 DirectX 9. 7 Summary. 8 Chapter 2 Overview of HAL and COM . 9 Hardware Abstraction Layer . 10 Pluggable Software Devices. 13 Reference Rasterizer . 14 Controlling Devices . 14 COM . 15 Summary. 17 Contents xi Chapter 3 Programming Conventions . 19 Accessing COM Objects . 20 Naming Conventions . 22 Debugging DirectX. 24 Return Codes . 25 Summary. 26 Chapter 4 3D Fundamentals, Gouraud Shading, and Texture-Mapping Basics . 27 3D Fundamentals . 28 Understanding Vertices . 30 Working with Orientation . 31 Understanding Faces . 31 Understanding Polygons . 33 Understanding Normals. 33 Understanding Normals and Gouraud Shading . 33 Te x t ure-Mapping Basics . 34 Summary. 36 Chapter 5 The Basics . 37 Compiling the Examples . 38 The DirectX Graphics Common Architecture . 40 The Basic Example . 41 The ConfirmDevice(), OneTimeSceneInit(), and InitDeviceObjects() Functions . 44 The RestoreDeviceObjects() Method . 45 The FrameMove() Function . 53 The Render() Function . 53 xii Contents The InvalidateDeviceObjects() Function . 57 The DeleteDeviceObjects() Function . 58 The FinalCleanup() Function . 58 The Basic2 Example. 58 The InitDeviceObjects() Function . 62 The RestoreDeviceObjects() Function . 62 The Render() Function . 64 The InvalidateDeviceObjects() Function . 66 The DeleteDeviceObjects() Function . ..