
DirectX® 9 Graphics: The Definitive Guide to Direct3D® Alan Thorn Wordware Publishing, Inc. Library of Congress Cataloging-in-Publication Data Thorn, Alan. DirectX 9 graphics : the definitive guide to Direct3D / by Alan Thorn. p. cm. Includes bibliographical references and index. ISBN-13: 978-1-55622-229-7 ISBN-10: 1-55622-229-7 (pbk.) 1. Computer graphics. 2. DirectX. I. Title. T385.T49576 2005 006.6'93--dc22 2005003234 CIP © 2005, 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-13: 978-1-55622-229-7 ISBN-10: 1-55622-229-7 10987654321 0503 DirectX and Direct3D are registered trademarks of Microsoft Corporation in the United States and/or other countries. Other 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. This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this book and any disks or programs that may accompany it, including but not limited to implied warranties for the book’s quality, performance, merchantability, or fitness for any particular purpose. Neither Wordware Publishing, Inc. nor its dealers or distributors shall be liable to the purchaser or any other person or entity with respect to any liability,loss, or damage caused or alleged to have been caused directly or indirectly by this book. 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 Jean and Bill Mills, Peter Thorn, and Reginald Stokes This page intentionally left blank. Contents Acknowledgments ..........................xi Introduction .............................xiii Chapter 1 Getting Started with DirectX.................1 What Is DirectX? ...........................2 A More Precise Definition of DirectX ................4 Obtaining DirectX ...........................5 Installing DirectX ...........................5 Configuring DirectX..........................6 Exploring DirectX...........................7 DirectX Utilities............................9 Conclusion ..............................13 Chapter 2 Starting DirectX — Your First Program ...........15 Getting Started ...........................16 Step 1 — Create a Window .....................17 Step 2 — Create a Direct3D Object .................19 Step 3 — Create a Direct3D Device.................20 Creating a Device ........................21 More on Direct3D Devices ...................27 Step 4 — Configure the Message Loop ...............28 The Game Loop .........................29 Step 5 — Render and Display a Scene ...............30 Direct3D Surfaces — IDirect3DSurface9 ............31 Preparing to Present a Scene ..................32 Beginning and Ending a Scene .................34 Step 6 — Shut Down Direct3D ...................36 More on Surfaces ..........................37 Creating Surfaces ........................37 Loading Images onto Surfaces..................40 Copying Surfaces ........................42 Presenting Images with the Back Buffer ............44 Lost Devices .............................46 Conclusion ..............................49 v Contents Chapter 3 3D Mathematics......................51 Coordinate Systems .........................52 One-Dimensional (1D) Coordinate Systems ..........52 Two-Dimensional (2D) Coordinate Systems ..........53 More on 2D Coordinate Systems ................54 Three-Dimensional (3D) Coordinate Systems .........56 Geometric Transformations .....................57 Translation ............................58 Rotation .............................58 Scaling ..............................60 Vectors ................................61 Length of the Diagonal (Magnitude)...............63 Vector Addition..........................64 Vector Subtraction ........................65 Vector Multiplication.......................66 Vector Normalization ......................67 Vector Dot Product .......................67 Vector Cross Product ......................68 Matrices ...............................70 Matrix Components .......................71 Matrix Addition .........................72 Matrix Subtraction ........................72 Matrix Multiplication (Scalar) ..................72 Matrix by Matrix Multiplication .................73 Identity Matrix ..........................74 Inverse Matrix ..........................75 Matrices for Geometric Transformations ..............76 Matrix Translation ........................76 Matrix Rotation .........................78 Matrix Scaling ..........................81 Combining Transformations ...................82 Planes ................................83 Creating Planes from Three Points ...............84 Creating Planes from Point and Normal .............85 Classifying Points in 3D Using Planes..............86 Plane and Line Intersection ...................87 Conclusion ..............................89 Chapter 4 Direct3D for 3D Graphics .................91 Getting Started ...........................92 Create and Define a Vertex Format .................93 Using Vertices ..........................94 Create a Vertex Buffer ........................95 Fill the Vertex Buffer ......................97 Rendering the Triangle .......................99 View, Projection, and Transform Matrices............99 vi Contents Transformation Matrix ..................100 View Matrix .......................102 Projection Matrix .....................103 Setting the Stream Source ...................104 Setting the FVF ........................106 Drawing a Primitive ......................106 Animating the Triangle .......................109 Drawing Other Primitives .....................109 Indexed Primitives .........................111 Setting Up Index Buffers ....................113 Drawing Indexed Primitives ..................115 Conclusion .............................118 Chapter 5 Materials, Lights, and Textures ..............119 Lighting ...............................120 Turning the Lights On and Off .................121 More on Lights .........................122 Setting the Ambient Lighting ..............122 Getting Started with Direct Lights............123 Direct3D Shading Modes....................123 Materials ..............................124 Direct Lighting Types .......................126 Textures ..............................131 Creating Textures .......................132 Creating Blank Textures .................132 Creating Textures from Image Files ...........134 Texture Mapping ........................135 Setting the Active Texture ...................137 Texture Filtering ........................138 Texture Addressing Modes...................141 Texture Alpha Blending ....................144 2D Texturing ..........................145 ID3DXSprite — Drawing Textures in 2D .............147 Conclusion .............................150 Chapter 6 X Files — Loading and Saving Data............151 Introduction to X Files .......................152 Structural Overview ........................154 Header .............................154 Templates............................154 Data Objects ..........................156 Parent and Child Objects ....................157 Data Objects and References..................157 Standard Templates.........................159 Custom Templates .........................169 Reading X Files Using DirectX...................170 vii Contents Preparing ............................170 Registering Templates .....................171 Opening a File .........................173 Enumerating Top Objects ...................174 Enumerating Child Objects...................175 Processing Child Objects....................176 Enumeration Overview ....................176 Getting Object Data ......................178 Object Names.......................178 Object Types .......................179 Object Data ........................181 Saving Data to X Files — Save Object ...............182 Preparing ............................183 Saving Data ...........................184 Building the Tree ........................185 Committing the Data ......................186 Conclusion .............................186 Chapter 7 Meshes .........................187 What Are Meshes? .........................188 How to Make Meshes .......................188 How to Export Meshes.......................189 Testing Your Mesh .........................190 Meshes in Direct3D ........................191 Loading Meshes from X Files .................191 Loading Meshes from X File Data Objects...........193 Mesh Materials and Textures .................196 Rendering Meshes .......................199 Cleaning Up Meshes ......................200 More on Meshes ..........................201 Meshes and Vertex Buffers ..................201 Meshes and FVFs .......................202 Bounding Boxes and Spheres ...................204 Rays Intersecting Meshes .....................209 Vertex Interpolation ........................212 Conclusion .............................214 Chapter 8 Cameras — First-Person and More ............215
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages368 Page
-
File Size-