logo资料库

Beginning DirectX 11 Game Programming 无水印pdf.pdf

第1页 / 共385页
第2页 / 共385页
第3页 / 共385页
第4页 / 共385页
第5页 / 共385页
第6页 / 共385页
第7页 / 共385页
第8页 / 共385页
资料共385页,剩余部分请下载后查看
Cover
Contents
Introduction
Chapter 1 The What, Why, and How of DirectX
What Is DirectX?
DirectX 10 versus DirectX 9
DirectX 11 versus DirectX 10
DirectX 11 versus OpenGL 4.0
Why Is DirectX Needed?
How DirectX Is Put Together
The Components of DirectX 11
Obsolete DirectX Components
Introducing Direct3D 11
Stages of Direct3D 11
Direct3D 11 Considerations
DirectX Tools
Sample Browser and Documentation
PIX
Caps Viewer
Diagnostic Tools
Texture Tool
Error Lookup
Control Panel
Cross-Platform Audio Creation Tool
Game Definition File Editor
Down-Level Hardware
Summary
Chapter Questions
Chapter 2 Your First DirectX Program
Creating the Project
Adding Windows Code
The Main Entry Point
Windows Initialization
Windows Callback Procedure
Time for Direct3D
Adding the DirectX Libraries
Initializing Direct3D
Driver Types and Features Levels
Device and Swap Chain Creation
Render Target View Creation
The Viewport
Clearing and Displaying the Screen
Cleaning Up
Formats
The Blank D3D Window
Design of the Template Framework
The Direct3D Class
The Blank Window Demo Class
Updating the Application Loop
DirectX Error Handling Library
Error Handling Functions
Error Handling Macros
Summary
Chapter Questions
Chapter 3 2D Rendering
2D Game Development
Textures
Sprites
2D Geometry
What Is a Vertex?
Definition of a Triangle
Vertex Buffers
Input Layout
Drawing a 2D Triangle
2D Triangle Demo
Loading the Geometry
Rendering the Geometry
Texture Mapping
Texture Interfaces
MIP Maps
Texture Details
Texture Mapping Demo
Sprites
Z-Ordering
Sprite Image
Getting Sprites to the Screen
Positioning and Scaling Sprites
The Game Sprite Demo
Creating and Rendering the Game Sprite
Summary
What You Have Learned
Chapter Questions
On Your Own
Chapter 4 Text and Font Rendering
Text in Games
Adding Text
Textured Fonts
A Font System Explained
Creating a Font System Using Sprites
Advanced Topics
In-Game Text Boxes
In-Game Console Window
Game Menus
Heads-Up Display
Summary
Chapter Questions
On Your Own
Chapter 5 Input Detection and Response
I Need Input
Input Options
Keyboard Input
Win32 Keyboard Input
DirectInput Keyboard Input
DirectInput Keyboard Demo
Mouse Input
DirectInput Mouse Input
XInput—Game Controllers
Setting Up XInput
Controller Vibrations
XInput for Input
Controller Capabilities
Battery Life
Keystrokes
Headset Sound
XInput Demo
Summary
What You Have Learned
Chapter Questions
On Your Own
Chapter 6 3D Primer
XNA Math
Utilities
Points
Vectors
Vector Arithmetic
Distance Between Vectors
Determining the Length of a Vector
Normalize a Vector
Cross Product
Dot Product
3D Space
Coordinate Systems
Transformations
World Transformations
View Transformations
Projection Transformations
Transforming an Object
Matrices
The Identity Matrix
Matrix Scaling
Matrix Translation
Matrix Rotation
Matrix Concatenation
Cube Demo
Additional XNA Math Topics
Compiler Directives
Constants
Macros
Structures and Types
Additional Functions
Additional Math Structures and Topics
Game Physics and Collision Detection
Summary
What You Have Learned
Chapter Questions
Chapter 7 Shaders and Effects
Shaders in Direct3D
History of Programmable Shaders
Effect Files
Effect File Layout
Loading an Effect File
External Variables and Constant Buffers
Input and Output Structures
Technique Blocks
Rasterizer States
High Level Shading Language
Variable Types
Semantics
Function Declarations
Vertex Shaders
Pixel Shaders
Texture Color Inversion
Color Shifting
Multitexturing
Geometry Shaders
Geometry Shader Function Declaration
The Geometry Shader Explained
Introduction to Lighting
Summary
What You Have Learned
Chapter Questions
On Your Own
Chapter 8 Cameras and Models in Direct3D
Cameras in Direct3D
Look-At Camera Demo
Arc-Ball Camera Demo
Meshes and Models
The OBJ File Format
Reading Tokens from a File
Loading Meshes from OBJ Files
Advanced Topics
Complex Cameras
3D Level Files
Summary
What You Have Learned
Chapter Questions
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Beginning DirectXR11 Game Programming Allen Sherrod and Wendy Jones Course Technology PTR A part of Cengage Learning Australia . Brazil . Japan . Korea . Mexico . Singapore . Spain . United Kingdom . United States
Beginning DirectX1 11 Game Programming Allen Sherrod and Wendy Jones Publisher and General Manager, Course Technology PTR: Stacy L. Hiquet Associate Director of Marketing: Sarah Panella Manager of Editorial Services: Heather Talbot Marketing Manager: Jordan Castellani Senior Acquisitions Editor: Emi Smith Project Editor: Dan Foster, Scribe Tribe Technical Reviewer: Wendy Jones Interior Layout Tech: MPS Limited, a Macmillan Company Cover Designer: Mike Tanamachi Indexer: Valerie Haynes Perry Proofreader: Gene Redding © 2012 Course Technology, a part of Cengage Learning. ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored, or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher. For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Further permissions questions can be emailed to permissionrequest@cengage.com DirectX is a registered trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. All images C Cengage Learning unless otherwise noted. Library of Congress Control Number: 2011920246 ISBN-13: 978-1-4354-5895-6 ISBN-10: 1-4354-5895-8 eISBN-10: 1-4354-5896-6 Course Technology, a part of Cengage Learning 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in the United States of America 1 2 3 4 5 6 7 13 12 11
A c k n o w l e d g m e n t s I would like to thank the men and women at Cengage Learning who helped make this book possible. I would also like to thank Wendy Jones for all her hard work on the previous editions of the book. And I would like to give a special thanks to Emi Smith, who has been very patient and helpful with this whole process. —Allen Sherrod Thanks to Allen Sherrod for taking over the writing duties for the DirectX 11 version of this book. I truly appreciate all the hard work and research he’s put into it. I’d also like to thank Emi Smith for giving me the opportunity to work with Cengage Learning on multiple books. —Wendy Jones
A b o u t t h e A u t h o r s Allen Sherrod is an experienced author in the field of video game development. Allen’s past works include two editions of Ultimate Game Programming with DirectX, Ultimate 3D Game Engine Design and Architecture, Game Graphics Programming, andData Structures and Algorithms for Game Developers. Allen has also contributed to Game Developer magazine, the Game Programming Gems 6 book, and the Gamasutra.com website. Allen is the creator of www. UltimateGameProgramming.com. Wendy Jones is co-founder and CTO of Kitty Code LLC, a games studio working on mobile and console platforms such as iPhone, Windows Phone 7, and the Xbox 360. Wendy’s past experience includes working on PC, console, and mobile game titles while working with Atari and Electronic Arts. Wendy also teaches DirectX at Full Sail University and is department chair of the interactive development department. Wendy can be reached through her website at www. fasterkittycodecode.com.
C o n t e n t s Chapter 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi The What, Why, and How of DirectX . . . . . . . . . . . . . . . . 1 What Is DirectX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 DirectX 10 versus DirectX 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 DirectX 11 versus DirectX 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 DirectX 11 versus OpenGL 4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Why Is DirectX Needed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 How DirectX Is Put Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 The Components of DirectX 11 . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Obsolete DirectX Components . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Introducing Direct3D 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Stages of Direct3D 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Direct3D 11 Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 DirectX Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Sample Browser and Documentation . . . . . . . . . . . . . . . . . . . . . 16 PIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Caps Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Diagnostic Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Texture Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Error Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Control Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Cross-Platform Audio Creation Tool . . . . . . . . . . . . . . . . . . . . . . 20 v
vi Contents Chapter 2 Game Definition File Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Down-Level Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Your First DirectX Program . . . . . . . . . . . . . . . . . . . . . . . 27 Creating the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Adding Windows Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 The Main Entry Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Windows Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Windows Callback Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Time for Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Adding the DirectX Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Initializing Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Driver Types and Features Levels . . . . . . . . . . . . . . . . . . . . . . . . 47 Device and Swap Chain Creation . . . . . . . . . . . . . . . . . . . . . . . . 49 Render Target View Creation . . . . . . . . . . . . . . . . . . . . . . . . . . 53 The Viewport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Clearing and Displaying the Screen . . . . . . . . . . . . . . . . . . . . . . 56 Cleaning Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Blank D3D Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Design of the Template Framework . . . . . . . . . . . . . . . . . . . . . . 60 The Direct3D Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 The Blank Window Demo Class . . . . . . . . . . . . . . . . . . . . . . . . . 67 Updating the Application Loop . . . . . . . . . . . . . . . . . . . . . . . . . 70 DirectX Error Handling Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Error Handling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Error Handling Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Chapter 3 2D Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 2D Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 2D Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 What Is a Vertex? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Definition of a Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Contents vii Vertex Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Input Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Drawing a 2D Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 2D Triangle Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Loading the Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Rendering the Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Texture Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 MIP Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Texture Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Texture Mapping Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Z-Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Sprite Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Getting Sprites to the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Positioning and Scaling Sprites . . . . . . . . . . . . . . . . . . . . . . . . 139 The Game Sprite Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Creating and Rendering the Game Sprite . . . . . . . . . . . . . . . . 143 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Chapter Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Text and Font Rendering . . . . . . . . . . . . . . . . . . . . . . . . 155 Text in Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Adding Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Textured Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 A Font System Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Creating a Font System Using Sprites . . . . . . . . . . . . . . . . . . . . . . 161 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 In-Game Text Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 In-Game Console Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Game Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Heads-Up Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Chapter Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Chapter 4
分享到:
收藏