Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
Introduction to
3D GAME PROGRAMMING
WITH DIRECTX® 11
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_000.html[2012/7/10 11:08:28]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY
By purchasing or using this book (the “Work”), you agree that this license grants permission to use the
contents contained herein, but does not give you the right of ownership to any of the textual content in the
book or ownership to any of the information or products contained in it. This license does not permit
uploadingofthe Work onto the Internet or on a network (of any kind) without the written
consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc.
contained herein is limited to and subject to licensing terms for the respective products, and permission
must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any
portion of the textual material (in any media) that is contained in the Work.
MERCURY LEARNING AND INFORMATION LLC (“MLI” or “the Publisher”) and anyone involved in the
creation, writing, or production of the companion disc, accompanying algorithms, code, or computer
programs (“the software”), and any accompanying Web site or software of the Work, cannot and do not
warrant the performance or results that might be obtained by using the contents of the Work. The author,
developers, and the Publisher have used their best efforts to insure the accuracy and functionality of the
textual material and/or programs contained in this package; we, however, make no warranty of any kind,
express or implied, regarding the performance of these contents or programs. The Work is sold “as is”
without warranty (except for defective materials used in manufacturing the book or due to faulty
workmanship).
The author, developers, and the publisher ofany accompanying content, and anyone involved in the
composition, production, and manufacturing of this work will not be liable for damages of any kind arising
out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual
material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other
incidental, physical, or consequential damages arising out of the use of this Work.
The sole remedy in the event ofa claim ofany kind is expressly limited to replacement of the book, and
only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” vary from state
to state, and might not apply to the purchaser of this product.
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_001.html[2012/7/10 11:08:28]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
Introduction to
3D GAME PROGRAMMING
WITH DIRECTX® 11
Frank D. Luna
Mercury Learning and Informatoin
Dulles, Virginia
Boston, Massachusetts
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_002.html[2012/7/10 11:08:29]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
Copyright ©2012 by MERCURY LEARNING AND INFORMATION LLC. All rights reserved.
This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval
system ofany type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited
to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher.
Publisher: David Pallai
MERCURY LEARNING AND INFORMATION
22841 Quicksilver Drive
Dulles, VA 20166
info@merclearning.com
www.merclearning.com
1-800-758-3756
This book is printed on acid-free paper.
Frank D. Luna. Introduction to 3D GAME PROGRAMMING WITH DIRECTX 11 ISBN: 978-1-9364202-2-3
The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their
products. 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, etc. is not an attempt to infringe on the property of others.
Library of Congress Control Number: 2012931119
121314321
Printed in Canada
Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact
the Customer Service Dept. at 1-800-758-3756 (toll free).
The sole obligation of MERCURY LEARNING AND INFORMATION to the purchaser is to replace the disc, based on defective materials or
faulty workmanship, but not based on the operation or functionality of the product.
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_003.html[2012/7/10 11:08:29]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
To my nieces and nephews,
Marrick, Hans, Max, Anna, Augustus, and Presley
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_004.html[2012/7/10 11:08:29]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
CONTENTS
Acknowledgments
Introduction
Intended Audience
Prerequisites
Required Development Tools and Hardware
Use of the D3DX Library
Using the DirectX SDK Documentation and SDK Samples
Clarity
Sample Programs and Online Supplements
Demo Project Setup in Visual Studio 2010
Create a Win32 Project
Linking the DirectX Libraries
Setting up the Search Paths
Adding the Source Code and Building the Project
PART I MATHEMATICAL PREREQUISITES
Chapter 1 Vector Algebra
1.1 Vectors
1.1.1 Vectors and Coordinate Systems
1.1.2 Left-Handed Versus Right-Handed Coordinate Systems
1.1.3 Basic Vector Operations
1.2 Length and Unit Vectors
1.3 The Dot Product
1.3.1 Orthogonalization
1.4 The Cross Product
1.4.1 Pseudo 2D Cross Product
1.4.2 Orthogonalization with the Cross Product
1.5 Points
1.6 XNA Math Vectors
1.6.1 Vector Types
1.6.2 Loading and Storage Methods
1.6.3 Parameter Passing
1.6.4 Constant Vectors
1.6.5 Overloaded Operators
1.6.6 Miscellaneous
1.6.7 Setter Functions
1.6.8 Vector Functions
1.6.9 Floating-Point Error
1.7 Summary
1.8 Exercises
Chapter 2 Matrix Algebra
2.1 Definition
2.2 Matrix Multiplication
2.2.1 Definition
2.2.2 Vector-Matrix Multiplication
2.2.3 Associativity
2.3 The Transpose of a Matrix
2.4 The Identity Matrix
2.5 The Determinant of a Matrix
2.5.1 Matrix Minors
2.5.2 Definition
2.6 The Adjoint of a Matrix
2.7 The Inverse of a Matrix
2.8 XNA Matrices
2.8.1 Matrix Types
2.8.2 Matrix Functions
2.8.3 XNA Matrix Sample Program
2.9 Summary
2.10 Exercises
Chapter 3 Transformations
3.1 Linear Transformations
3.1.1 Definition
3.1.2 Matrix Representation
3.1.3 Scaling
3.1.4 Rotation
3.2 Affine Transformations
3.2.1 Homogeneous Coordinates
3.2.2 Definition and Matrix Representation
3.2.2 Translation
3.2.3 Affine Matrices for Scaling and Rotation
3.2.4 Geometric Interpretation of an Affine Transformation Matrix.
3.3 Composition of Transformations
3.4 Change of Coordinate Transformations
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_005.html[2012/7/10 11:08:30]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
3.4.1 Vectors
3.4.2 Points
3.4.3 Matrix Representation
3.4.4 Associativity and Change of Coordinate Matrices
3.4.5 Inverses and Change of Coordinate Matrices
3.5 Transformation Matrix versus Change of Coordinate Matrix
3.6 XNA Math Transformation Functions
3.7 Summary
3.8 Exercises
PART II DIRECT 3D FOUNDATIONS
Chapter 4 Direct3D Initialization
4.1 Preliminaries
4.1.1 Direct3D Overview
4.1.2 COM
4.1.3 Textures and Data Resource Formats
4.1.4 The Swap Chain and Page Flipping
4.1.5 Depth Buffering
4.1.6 Texture Resource Views
4.1.7 Multisampling Theory
4.1.8 Multisampling in Direct3D
4.1.9 Feature Levels
4.2 Initializing Direct3D
4.2.1 Create the Device and Context
4.2.2 Check 4X MSAA Quality Support
4.2.3 Describe the Swap Chain
4.2.4 Create the Swap Chain
4.2.5 Create the Render Target View
4.2.6 Create the Depth/Stencil Buffer and View
4.2.7 Bind the Views to the Output Merger Stage
4.2.8 Set the Viewport
4.3 Timing and Animation
4.3.1 The Performance Timer
4.3.2 Game Timer Class
4.3.3 Time Elapsed Between Frames
4.3.4 Total Time
4.4 The Demo Application Framework
4.4.1 D3DApp
4.4.2 Non-Framework Methods
4.4.3 Framework Methods
4.4.4 Frame Statistics
4.4.5 The Message Handler
4.4.6 Going Full Screen
4.4.7 The “Init Direct3D” Demo
4.5 Debugging Direct3D Applications
4.6 Summary
4.7 Exercises
Chapter 5 The Rendering Pipeline
5.1 The 3D Illusion
5.2 Model Representation
5.3 Basic Computer Color
5.3.1 Color Operations
5.3.2 128-Bit Color
5.3.3 32-Bit Color
5.4 Overview of the Rendering Pipeline
5.5 The Input Assembler Stage
5.5.1 Vertices
5.5.2 Primitive Topology
5.5.2.1 Point List
5.5.2.2 Line Strip
5.5.2.3 Line List
5.5.2.4 Triangle Strip
5.5.2.5 Triangle List
5.5.2.6 Primitives with Adjacency
5.5.2.7 Control Point Patch List
5.5.3 Indices
5.6 The Vertex Shader Stage
5.6.1 Local Space and World Space
5.6.2 View Space
5.6.3 Projection and Homogeneous Clip Space
5.6.3.1 Denning a Frustum
5.6.3.2 Projecting Vertices
5.6.3.3 Normalized Device Coordinates (NDC)
5.6.3.4 Writing the Projection Equation with a Matrix
5.6.3.5 Normalized Depth Value
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_005.html[2012/7/10 11:08:30]
Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11
5.6.3.6 XMMatrixPerspectiveFovLH
5.7 The Tessellation Stages
5.8 The Geometry Shader Stage
5.9 Clipping
5.10 The Rasterization Stage
5.10.1 Viewport Transform
5.10.2 Backface Culling
5.10.3 Vertex Attribute Interpolation
5.11 The Pixel Shader Stage
5.12 The Output Merger Stage
5.13 Summary
5.14 Exercises
Chapter 6 Drawing in Direct3D
6.1 Vertices and Input Layouts
6.2 Vertex Buffers
6.3 Indices and Index Buffers
6.4 Example Vertex Shader
6.5 Constant Buffers
6.6 Example Pixel Shader
6.7 Render States
6.8 Effects
6.8.1 Effect Files
6.8.2 Compiling Shaders
6.8.3 Interfacing with Effects from the C++ Application
6.8.4 Using Effects to Draw
6.8.5 Compiling an Effect at Build Time
6.8.6 The Effects Framework as a "Shader Generator"
6.8.7 What the Assembly Looks Like
6.9 Box Demo
6.10 Hills Demo
6.10.1 Generating the Grid Vertices
6.10.2 Generating the Grid Indices
6.10.3 Applying the Height Function
6.11 Shapes Demo
6.11.1 Generating a Cylinder Mesh
6.11.1.1 Cylinder Side Geometry
6.11.1.2 Cap Geometry
6.11.2 Generating a Sphere Mesh
6.11.3 Generating a Geosphere Mesh
6.11.4 Demo Code
6.12 Loading Geometry from the File
6.13 Dynamic Vertex Buffers
6.14 Summary
6.15 Exercises
Chapter 7 Lighting
7.1 Light and Material Interaction
7.2 Normal Vectors
7.2.1 Computing Normal Vectors
7.2.2 Transforming Normal Vectors
7.3 Lambert’s Cosine Law
7.4 Diffuse Lighting
7.5 Ambient Lighting
7.6 Specular Lighting
7.7 Brief Recap
7.8 Specifying Materials
7.9 Parallel Lights
7.10 Point Lights
7.10.1 Attenuation
7.10.2 Range
7.11 Spotlights
7.12 Implementation
7.12.1 Lighting Structures
7.12.2 Structure Packing
7.12.3 Implementing Directional Lights
7.12.4 Implementing Point Lights
7.12.5 Implementing Spotlights
7.13 Lighting Demo
7.13.1 Effect File
7.13.2 C++ Application Code
7.13.3 Normal Computation
7.14 Lit Skull Demo
7.15 Summary
7.16 Exercises
Chapter 8 Texturing
file:///C|/Users/zck/Desktop/Introduction+to+3D+Game+Programming+with/CR!E92V5R6GC96Y94XVJG67CTGJ0ARA_split_005.html[2012/7/10 11:08:30]