1. Trang chủ
  2. » Công Nghệ Thông Tin

introduction to 3d game programming with directx 9.0 (2003)

421 289 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 421
Dung lượng 6,62 MB

Nội dung

TEAMFLY Team-Fly ® Introduction to 3D Game Programming with DirectX ® 9.0 Frank D. Luna Technical review by Rod Lopez Wordware Publishing, Inc. Library of Congress Cataloging-in-Publication Data Luna, Frank D. Introduction to 3D game programming with DirectX 9.0 / by Frank D. Luna. p. cm. ISBN 1-55622-913-5 (pbk.) 1. Computer games Programming. 2. DirectX. I. Title. QA76.76.C672L83 2003 794.8'15268 dc21 2003005834 CIP © 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. Printed in the United States of America ISBN 1-55622-913-5 10987654321 0305 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 To my parents, Frank and Kathryn iii This page intentionally left blank. Contents Acknowledgments xv Introduction xvii Part I Mathematical Prerequisites . . 1 Vectors in 3-Space 2 Vector Equality 5 Computing the Magnitude of a Vector 6 Normalizing a Vector 7 Vector Addition 7 Vector Subtraction 8 Scalar Multiplication 9 Dot Products 9 Cross Products 10 Matrices 11 Equality, Scalar Multiplication, and Addition 12 Multiplication 13 The Identity Matrix 14 Inverses 15 The Transpose of a Matrix 15 D3DX Matrices 16 Basic Transformations 18 The Translation Matrix 20 The Rotation Matrices 21 The Scaling Matrix 22 Combining Transformations 23 Some Functions to Transform Vectors 25 Planes (Optional) 25 D3DXPLANE 26 Point and Plane Spatial Relation 27 Construction 27 Normalizing a Plane 28 Transforming a Plane 29 Nearest Point on a Plane to a Particular Point 29 Rays (Optional) 30 Rays 30 Ray/Plane Intersection 31 Summary 32 v Part II Direct3D Fundamentals. . . 33 Chapter 1 Direct3D Initialization . . . 35 1.1 Direct3D Overview 35 1.1.1 The REF Device 36 1.1.2 D3DDEVTYPE 37 1.2 COM 37 1.3 Some Preliminaries 37 1.3.1 Surfaces 38 1.3.2 Multisampling 39 1.3.3 Pixel Formats 40 1.3.4 Memory Pools 41 1.3.5 The Swap Chain and Page Flipping 42 1.3.6 Depth Buffers 43 1.3.7 Vertex Processing 44 1.3.8 Device Capabilities 44 1.4 Initializing Direct3D 45 1.4.1 Acquiring an IDirect3D9 Interface 46 1.4.2 Checking for Hardware Vertex Processing 47 1.4.3 Filling Out the D3DPRESENT_PARAMETERS Structure 48 1.4.4 Creating the IDirect3DDevice9 Interface 50 1.5 Sample Application: Initializing Direct3D 51 1.5.1 d3dUtility.h/cpp 52 1.5.2 Sample Framework 54 1.5.3 Sample: D3D Init 54 1.6 Summary 57 Chapter 2 The Rendering Pipeline. . . 59 2.1 Model Representation 60 2.1.1 Vertex Formats 61 2.1.2 Triangles 62 2.1.3 Indices 62 2.2 The Virtual Camera 63 2.3 The Rendering Pipeline 64 2.3.1 Local Space 65 2.3.2 World Space 65 2.3.3 View Space 66 2.3.4 Backface Culling 68 2.3.5 Lighting 69 2.3.6 Clipping 69 2.3.7 Projection 70 2.3.8 Viewport Transform 72 2.3.9 Rasterization 73 2.4 Summary 73 vi Contents Chapter 3 Drawing in Direct3D 75 3.1 Vertex/Index Buffers 75 3.1.1 Creating a Vertex and Index Buffer 76 3.1.2 Accessing a Buffer’s Memory 78 3.1.3 Retrieving Information about a Vertex and Index Buffer 79 3.2 Render States 80 3.3 Drawing Preparations 81 3.4 Drawing with Vertex/Index Buffers 82 3.4.1 IDirect3DDevice9::DrawPrimitive 82 3.4.2 IDirect3DDevice9::DrawIndexedPrimitive 82 3.4.3 Begin/End Scene 84 3.5 D3DX Geometric Objects 84 3.6 Sample Applications: Triangle, Cube, Teapot, D3DXCreate* 85 3.7 Summary 89 Chapter 4 Color 91 4.1 Color Representation 91 4.2 Vertex Colors 94 4.3 Shading 94 4.4 Sample Application: Colored Triangle 95 4.5 Summary 97 Chapter 5 Lighting 98 5.1 Light Components 98 5.2 Materials 99 5.3 Vertex Normals 101 5.4 Light Sources 104 5.5 Sample Application: Lighting 107 5.6 Additional Samples 109 5.7 Summary 110 Chapter 6 Texturing. . . 111 6.1 Texture Coordinates 112 6.2 Creating and Enabling a Texture 113 6.3 Filters 114 6.4 Mipmaps 115 6.4.1 Mipmap Filter 116 6.4.2 Using Mipmaps with Direct3D 116 6.5 Address Modes 116 6.6 Sample Application: Textured Quad 118 6.7 Summary 120 Contents vii Chapter 7 Blending. . . 121 7.1 The Blending Equation 121 7.2 Blend Factors 123 7.3 Transparency 124 7.3.1 Alpha Channels 125 7.3.2 Specifying the Source of Alpha 125 7.4 Creating an Alpha Channel Using the DirectX Texture Tool 126 7.5 Sample Application: Transparency 127 7.6 Summary 130 Chapter 8 Stenciling 131 8.1 Using the Stencil Buffer 132 8.1.1 Requesting a Stencil Buffer 133 8.1.2 The Stencil Test 133 8.1.3 Controlling the Stencil Test 134 8.1.3.1 Stencil Reference Value 134 8.1.3.2 Stencil Mask 134 8.1.3.3 Stencil Value 135 8.1.3.4 Comparison Operation 135 8.1.3 Updating the Stencil Buffer 135 8.1.4 Stencil Write Mask 137 8.2 Sample Application: Mirrors 137 8.2.1 The Mathematics of Reflection 137 8.2.2 Mirror Implementation Overview 139 8.2.3 Code and Explanation 140 8.2.3.1 PartI 141 8.2.3.2 PartII 141 8.2.3.3 PartIII 142 8.2.3.4 PartIV 143 8.2.3.5 PartV 143 8.3 Sample Application: Planar Shadows 144 8.3.1 Parallel Light Shadows 145 8.3.2 Point Light Shadows 146 8.3.3 The Shadow Matrix 146 8.3.4 Using the Stencil Buffer to Prevent Double Blending 147 8.3.5 Code and Explanation 148 8.4 Summary 150 Part III Applied Direct3D . . . 151 Chapter 9 Fonts 153 9.1 ID3DXFont 153 9.1.1 Creating an ID3DXFont 153 viii Contents 9.1.2 Drawing Text 154 9.1.3 Computing the Frames Rendered Per Second 155 9.2 CD3DFont 155 9.2.1 Constructing a CD3DFont 156 9.2.2 Drawing Text 156 9.2.3 Cleanup 157 9.3 D3DXCreateText 157 9.4 Summary 159 Chapter 10 Meshes Part I 160 10.1 Geometry Info 160 10.2 Subsets and the Attribute Buffer 161 10.3 Drawing 163 10.4 Optimizing 163 10.5 The Attribute Table 165 10.6 Adjacency Info 167 10.7 Cloning 169 10.8 Creating a Mesh (D3DXCreateMeshFVF) 170 10.9 Sample Application: Creating and Rendering a Mesh . . . 171 10.10 Summary 177 Chapter 11 Meshes Part II 178 11.1 ID3DXBuffer 178 11.2 XFiles 179 11.2.1 Loading an XFile 180 11.2.2 XFile Materials 181 11.2.3 Sample Application: XFile 181 11.2.4 Generating Vertex Normals 184 11.3 Progressive Meshes 185 11.3.1 Generating a Progressive Mesh 186 11.3.2 Vertex Attribute Weights 187 11.3.3 ID3DXPMesh Methods 188 11.3.4 Sample Application: Progressive Mesh 190 11.4 Bounding Volumes 193 11.4.1 Some New Special Constants 194 11.4.2 Bounding Volume Types 195 11.4.3 Sample Application: Bounding Volumes 196 11.5 Summary 198 Chapter 12 Building a Flexible Camera Class . . . 199 12.1 Camera Design 199 12.2 Implementation Details 201 12.2.1 Computing the View Matrix 201 12.2.1.1 Part 1: Translation 202 12.2.1.2 Part 2: Rotation 203 Contents ix [...]... operator FLOAT* (); operator CONST FLOAT* () const; // assignment operators D3DXVECTOR3& operator += D3DXVECTOR3& operator -= D3DXVECTOR3& operator *= D3DXVECTOR3& operator /= ( ( ( ( CONST CONST FLOAT FLOAT D3DXVECTOR3& ); D3DXVECTOR3& ); ); ); // unary operators D3DXVECTOR3 operator + () const; D3DXVECTOR3 operator - () const; // binary operators D3DXVECTOR3 operator D3DXVECTOR3 operator D3DXVECTOR3... D3DXVECTOR3 operator D3DXVECTOR3 operator + * / ( ( ( ( CONST CONST FLOAT FLOAT D3DXVECTOR3& ) const; D3DXVECTOR3& ) const; ) const; ) const; Team-Flyđ friend D3DXVECTOR3 operator * ( FLOAT, CONST struct D3DXVECTOR3& ); BOOL operator == ( CONST D3DXVECTOR3& ) const; BOOL operator != ( CONST D3DXVECTOR3& ) const; } D3DXVECTOR3, *LPD3DXVECTOR3; Note that D3DXVECTOR3 inherits its component data from D3DVECTOR, which... (0, 0, 1) A vector with a magnitude of one is called a unit vector Figure 4: The zero vector and the standard basis vectors for 3 TE 4 In the D3DX library, we can use the D3DXVECTOR3 class to represent a vector in 3-space Its class definition is: typedef struct D3DXVECTOR3 : public D3DVECTOR { public: D3DXVECTOR3() {}; D3DXVECTOR3( CONST FLOAT * ); D3DXVECTOR3( CONST D3DVECTOR& ); D3DXVECTOR3( FLOAT x,... functions and other important details about vectors Note: Although we are primarily concerned with 3D vectors, we sometimes work with 2D and 4D vectors in 3D graphics programming The D3DX library provides a D3DXVECTOR2 and D3DXVECTOR4 class for representing 2D and 4D vectors, respectively Vectors of different dimensions have the same properties as 3D vectors, namely they describe magnitudes and directions,... OpenGL) who would like an introduction to Direct3D 9.0 n Experienced Direct3D programmers who would like an up -to- date book covering the latest version of Direct3D, including vertex and pixel shaders, the High-Level Shading Language, and the effects framework Installing DirectX 9.0 To write and execute DirectX 9.0 programs, you need both the DirectX 9.0 runtime and the DirectX 9.0 SDK (Software Development... Introduction Figure I.7: A screen shot of the C++ SDK documentation viewer Note: In this book we may direct you to the documentation for further details from time to time The SDK documentation also contains some introductory tutorials at the URL \DirectX9 _c.chm:: /directx/ graphics/programmingguide/ tutorialsandsamplesandtoolsandtips/tutorials/tutorials.htm These tutorials correspond to some of the topics... 2ứ Using the D3DX library, we can normalize a vector using the following function: D3DXVECTOR3 *D3DXVec3Normalize( D3DXVECTOR3* pOut, // Result CONST D3DXVECTOR3* pV // The vector to normalize ); Note: This function returns a pointer to the result so that it can be passed as a parameter to another function For the most part, unless otherwise stated, a D3DX math function returns a pointer to the result... coincides with the head of u; then the sum is the vector originating at the tail of u and ending at the head of the translated v To add two vectors in code, we use the overloaded addition operator: D3DXVECTOR3 u(2.0f, 0.0f, 1.0f); D3DXVECTOR3 v(0.0f, -1.0f, 5.0f); // (2.0 + 0.0, 0.0 + (-1.0), 1.0 + 5.0) D3DXVECTOR3 sum = u + v; // = (2.0f, -1.0f, 6.0f) Vector Subtraction Similar to addition, two vectors... necessary In addition to the math explanations, we show the D3DX mathrelated classes used to model these mathematical objects and the functions used to execute a particular operation Objectives n n n n To learn the geometry and algebra of vectors and their applications to 3D computer graphics To learn about matrices, their algebra, and how we use them to transform 3D geometry To learn how to model planes... learn how to use the DirectX SDK documentation You can launch the C++ DirectX online documentation by executing the DirectX9 _c file in the \DXSDK\Doc \DirectX9 directory, where DXSDK is the directory to which you installed DirectX The DirectX documentation covers just about every part of the DirectX API; therefore, it is very useful as a reference, but because the documentation doesnt go into much depth, . Frank D. Introduction to 3D game programming with DirectX 9. 0 / by Frank D. Luna. p. cm. ISBN 1-55622 -91 3-5 (pbk.) 1. Computer games Programming. 2. DirectX. I. Title. QA76.76.C672L83 200 3 794 .8'15268. introduc- tion to 3D programming using the latest iteration of Direct3D— Direct3D 9. 0 n 3D programmers experienced with an API other than DirectX (e.g., OpenGL) who would like an introduction to Direct3D 9. 0 n Experienced. Language, and the effects framework Installing DirectX 9. 0 To write and execute DirectX 9. 0 programs, you need both the DirectX 9. 0 runtime and the DirectX 9. 0 SDK (Software Development Kit) installed

Ngày đăng: 26/10/2014, 20:34

TỪ KHÓA LIÊN QUAN