Beginning DirectX 10 Game Programming 1st Edition Wendy Jones instant download
Beginning DirectX 10 Game Programming 1st Edition Wendy Jones instant download
https://ebookname.com/product/beginning-directx-10-game-
programming-1st-edition-wendy-jones/
https://ebookname.com/product/visual-basic-game-programming-with-
directx-1st-edition-jonathan-s-harbour/
https://ebookname.com/product/ultimate-game-programming-with-
directx-second-edition-allen-allen-sherrod-sherrod/
https://ebookname.com/product/isometric-game-programming-with-
directx-7-0-premier-press-game-development-software-1st-edition-
ernest-pazera/
https://ebookname.com/product/writing-poetry-2rev-ed-edition-
john-whitworth/
Automotive lubricants reference book 2nd ed Edition
Caines
https://ebookname.com/product/automotive-lubricants-reference-
book-2nd-ed-edition-caines/
https://ebookname.com/product/transition-to-triumph-indian-
navy-1965-1975-vice-adm-g-m-hiranandani/
https://ebookname.com/product/sustainability-and-poverty-
alleviation-confronting-environmental-threats-in-sindh-
pakistan-1st-edition-ernesto-sanchez-triana/
https://ebookname.com/product/the-disordered-couple-2nd-edition-
len-sperry-editor/
https://ebookname.com/product/simple-lie-algebras-over-fields-of-
positive-characteristic-i-2ed-edition-strade-h/
Beginning JavaScript Charts 1st Edition Fabio Nelli
https://ebookname.com/product/beginning-javascript-charts-1st-
edition-fabio-nelli/
Beginning
DirectX® 10
Game
Programming
Wendy Jones
ß 2008 Thomson Course Technology, a division of Thomson Learning Publisher and General Manager,
Inc. All rights reserved. No part of this book may be reproduced or Thomson Course Technology PTR:
transmitted in any form or by any means, electronic or mechanical, Stacy L. Hiquet
including photocopying, recording, or by any information storage or
retrieval system without written permission from Thomson Course Associate Director of Marketing:
Technology PTR, except for the inclusion of brief quotations in a review. Sarah O’Donnell
The Thomson Course Technology PTR logo and related trade dress are Manager of Editorial Services:
trademarks of Thomson Course Technology, a division of Thomson Heather Talbot
Learning Inc., and may not be used without written permission.
Marketing Manager:
DirectX is a registered trademark of Microsoft Corporation in the Jordan Casey
United States and other countries.
Senior Acquisitions Editor:
All other trademarks are the property of their respective owners. Emi Smith
Important: Thomson Course Technology PTR cannot provide software
Marketing Assistant:
support. Please contact the appropriate software manufacturer’s
Adena Flitt
technical support line or Web site for assistance.
Project Editor:
Thomson Course Technology PTR and the author have attempted
Jenny Davidson
throughout this book to distinguish proprietary trademarks from
descriptive terms by following the capitalization style used by the Technical Reviewer:
manufacturer. Jim Adams and Allen Sherrod
Information contained in this book has been obtained by Thomson
PTR Editorial Services Coordinator:
Course Technology PTR from sources believed to be reliable. However,
Erin Johnson
because of the possibility of human or mechanical error by our sources,
Thomson Course Technology PTR, or others, the Publisher does not Interior Layout Tech:
guarantee the accuracy, adequacy, or completeness of any information ICC Macmillan Inc.
and is not responsible for any errors or omissions or the results
obtained from use of such information. Readers should be particularly Cover Designer:
aware of the fact that the Internet is an ever-changing entity. Some facts Mike Tanamachi
may have changed since this book went to press.
CD-ROM Producer:
Educational facilities, companies, and organizations interested in Brandon Penticuff
multiple copies or licensing of this book should contact the Publisher
for quantity discount information. Training manuals, CD-ROMs, and Indexer:
portions of this book are also available individually or can be tailored Kelly D. Henthorne
for specific needs.
Proofreader:
ISBN-10: 1-59863-361-9 Sara Gullion
ISBN-13: 978-1-59863-361-0
eISBN-10: 1-59863-624-3
Library of Congress Catalog Card Number: 2006909688
Printed in the United States of America
08 09 10 11 12 TW 10 9 8 7 6 5 4 3 2 1
I’d like to thank Emi Smith for giving me the chance to work with Course PTR
again. My experiences writing for her and Course PTR have been very rewarding.
I’d also like to thank project editor Jenny Davidson for doing the editing and
bringing everything together.
I’d like to take a moment to give thanks to the entire gaming industry for
allowing me to be part of something so wonderful. Where else can you get paid to
have fun with friends and put out some amazing products at the same time?
About the Author
Wendy Jones devoted herself to computers the first time her eyes befell an Apple
IIe in elementary school. From that point on, she spent every free moment
learning BASIC and graphics programming, sketching out her ideas on graph
paper to type in later. Other computer languages followed, including Pascal, C,
C#, and C++.
As Wendy’s career in computers took off, she branched out, teaching herself
Windows programming and then jumping into the dot-com world for a bit.
Although Internet companies provided a challenge, they didn’t provide fulfill-
ment, so Wendy started expanding her programming skills to games, devoting
any extra energy to its pursuit.
Wendy’s true passion became apparent when she got the opportunity to work for
Atari’s Humongous Entertainment as a game programmer. During her time at
Atari, she worked on both PC and console titles, thrilled with the challenge they
provided.
Wendy can now be found at Electronic Art’s Tiburon studio in Orlando working
with some wonderfully talented people on Next Generation consoles.
If you have any comments or questions about this book, you can reach Wendy at
her website at http://www.fasterkittycodecode.com.
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
vi
Contents vii
The Viewport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Clearing the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Displaying the Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Cleaning Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Updating the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Changing the Message Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Init Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Render Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The ShutdownDirect3D Function. . . . . . . . . . . . . . . . . . . . . . . . 34
Adding the DirectX Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Determining the Length of a Vector . . . . . . . . . . . . . . . . . . . . 111
Normalize a Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Cross Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Dot Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The Geometry Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
World Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
View Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Projection Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Transforming an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
What Are Matrices? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Initializing a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Matrix Scaling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Matrix Translation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Matrix Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Rotate around the X Axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Rotate around the Y Axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Rotate around the Z Axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Multiply Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Point Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Line Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Line Strips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Triangle Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Triangle Strips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Triangle Fans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Introduction
xv
xvi Introduction
Requirements
In order to get the full use of this book, a few things are required: A computer
system running Windows Vista Retail with Visual Studio 2005 Professional
installed. Additionally, you should have the June 2007 DirectX SDK installed.
Later versions of the SDK may work with the samples in the book but are not
guaranteed to compile as Microsoft can change the API at any time. Finally,
you’ll need a video card that has been certified Direct3D10 compliant. The
samples and code in this book might work with the software renderer but might
run slowly if at all.
This page intentionally left blank
chapter 1
DirectX is the premier game API for the Windows platform. Just a few years ago,
game makers were struggling with problems stemming from hardware incom-
patibilities, making it almost impossible for everyone to enjoy the same games.
Then Microsoft came along with DirectX. It provided game makers with a single
clean API to write to that would almost guarantee compatibility across different
sets of PC hardware. Since DirectX’s release, the number of games running under
Windows has increased dramatically.
In this chapter, I’ll cover the following:
n Understanding what DirectX is
n Why DirectX is useful
n How data flows through the Direct3D 10 pipeline
n What is brand new for Direct3D 10
What Is DirectX?
DirectX, Microsoft’s collection of application programming interfaces (APIs), is
designed to give game developers a low-level interface to the PC hardware
running Windows. Each API component provides access to different aspects of
3
4 Chapter 1 n The What, Why, and How of DirectX
the hardware, including graphics, sound, and networking, all through a standard
interface. This interface allows developers to write their games using one set of
functions with little worry about the hardware it’s being run on.
The Components
The DirectX API is split into multiple components, each representing a different
aspect of the system. Each API can be used independently of one another, thereby
only adding in the functionality your game requires.
In the latest version of DirectX, some of the components were updated, such as
Direct3D, while others are now being maintained at their previous levels by
Microsoft. The components within DirectX can be upgraded individually as new
functionality is required.
n DirectX Graphics. The component that handles all graphics processing.
This API provides functions for handling 2D and 3D graphic drawing, as
well as initializing and setting the environment for your game.
n DirectInput. All user input is handled through this API. This component
includes support for devices like the keyboard, mouse, gamepad, and joy-
sticks. DirectInput also provides support for force-feedback.
n XInput. A new addition to the DirectX family of APIs. XInput allows for
easy interfacing with the new Xbox 360 controller.
n DirectSound. For adding in sound effects or background music, this is the
API to use. DirectSound’s functionality allows for loading and playing of
one or more sound files while providing complete control over how they’re
played.
n DirectSetup. After your game is complete, you’ll want to show it to others.
DirectSetup gives you the functionality to install the latest version of
DirectX on the user’s computer.
Note
In previous versions of DirectX, 2D drawing functionality was provided by a component called
DirectDraw. Because DirectDraw is no longer being updated, you should perform all drawing
using 3D.
6 Chapter 1 n The What, Why, and How of DirectX
Introducing Direct3D 10
With the latest version of Windows, called Vista, Microsoft is introducing a new
version of graphic API called Direct3D 10 (D3D10). With this latest release,
Direct3D has gone through a dramatic change, streamlining the API and
dropping a lot of the unnecessary code. The entire desktop in Windows Vista will
be based on D3D10 giving the user more than a two-dimensional workspace.
Each window on the desktop will actually be a 3D object with the ability to
behave as objects do in the real world. To allow this to happen, the GPU on the
video card now becomes a shared resource.
Video cards are also being upgraded to provide users with the functionality that
D3D10 will require. In earlier versions of DirectX, video card manufacturers had
the option of providing only partial Direct3D compatibility, causing all sorts of
headaches. When you bought the latest game, you were never sure that your
Introducing Direct3D 10 7
video card could fully support the features it required. Now with D3D10, all card
manufacturers must implement the full feature set to be considered compatible
either within the hardware or within their driver. This will greatly benefit
the buyers of video cards because companies won’t be able to only support the
functionality that they deem important.
Since the changes to Direct3D were extensive, the latest Direct3D will not work
on earlier versions of Windows. This will be the first time that DirectX will no
longer be backward compatible. You may want to keep this in mind when writing
games for D3D10, because the installed base of users may take a while to migrate
to Windows Vista.
Stages of Direct3D
Direct3D is more than just an API; it’s a tool for transforming simple geometric
shapes and images into a living, breathing world. Even though you can treat
many pieces of Direct3D as a black box and not have to worry about the
implementation details, it is a good idea to understand at least an overview of
how it works.
Direct3D processes your scene in multiple stages, each stage performing a specific
set of tasks resulting in a final image.
The first stage, called the Input-Assembler (IA) stage, can be thought of as the
building block stage. This is the stage where the basic building blocks that make
up your objects are put into the Direct3D pipeline.
Next, the vertices are passed into the Vertex Shader (VS) stage. Every vertex in the
scene is passed into the vertex shader stage individually and transformed by the
currently active shader. You’ll learn about shaders later in the book. This stage is
user configurable through shaders.
The third stage, the Geometry Shader (GS), is brand new to Direct3D 10. With
the introduction of the Geometry shader, full primitives such as triangles and
lines can be transformed as a whole. Previously, each vertex in the primitive was
required to have all processing done on an individual vertex basis using the vertex
shader stage. This stage is user configurable through shaders.
The fourth stage, the Rasterizer, has the job of transforming your 3D scene
description into actual pixels. Any pixels that lay outside of the viewport are
clipped before the image is drawn.
8 Chapter 1 n The What, Why, and How of DirectX
Figure 1.1
The pipeline stages of Direct3D.
The Pixel Shader (PS) stage is where pixels in your scene are processed. Like
vertices, every pixel that makes up a primitive is fed into the pixel shader. Pixel
shaders allow you to determine how objects in your scene are going to appear in
the final image. You can apply lighting effects, handle environment mapping, or
just pass the pixels through. This stage is user configurable through shaders.
The final stage, the Output Merger (OM) stage, is where it all comes together.
The OM takes all of the pieces from the other stages of the pipeline and builds up
the final image to send to the screen.
Figure 1.1 shows the stages of Direct3D.
Direct3D 10 Considerations
Some of you may already be familiar with writing games using DirectX, and if so,
there are a few things you need to be aware of when converting your game to the
latest version of Direct3D.
Random documents with unrelated
content Scribd suggests to you:
end of this edition there is a beautifully-designed cut of the printer’s
device, which is probably the work of the same artist. V.87
A painter, named Nicholas Emanuel Deutsch, a contemporary of
Urse Graff, and who resided at Bern, is said, by Sandrart, to have
been of a noble English family, and the same writer adds that he left
his own country on account of his religion. The latter statement,
however, is not likely to be correct, for there are wood-cuts, with this
artist’s mark, dated “Bern, 1518;” which was before the persecution
in England on account of the doctrines of Luther had commenced. In
J. R. Füssli’s Dictionary of Artists it is stated that he was of a French
family, of the name of Cholard, but that he was born at Bern in
1484, and died there in 1530. He was a poet as well as a painter,
and held one of the highest offices in the magistracy of Bern.
Within the first thirty years of the sixteenth century the practice
of illustrating books with wood-cuts seems to have been more
general than at any other period, scarcely excepting the present; for
though within the last eight or ten years an immense number of
wood-cuts have been executed in England and France, yet wood
engravings at the time referred to were introduced into a greater
variety of books, and the art was more generally practised
throughout Europe. In modern German and Dutch works wood 315
engravings are sparingly introduced; and in works printed in
Switzerland and Italy they are still more rarely to be found. In the
former period the art seems to have been very generally practised
throughout Europe, though to a greater extent, and with greater
skill, in Germany than in any other country. The wood-cuts which are
to be found in Italian books printed between 1500 and 1530 are
mostly meagre in design and very indifferently engraved; and for
many years after the German wood engravers had begun to give
variety of colour and richness of effect to their cuts by means of
cross-hatchings, their Italian contemporaries continued to adhere to
the old method of engraving their figures, chiefly in outline, with the
shadows and the folds of the draperies indicated by parallel lines.
These observations relate only to the ordinary wood engravings of
the period, printed in the same page with type, or printed separately
in the usual manner of surface printing at one impression. The
admirable chiaro-scuros of Ugo da Carpi, printed from two or more
blocks, are for effect and general excellence the most admirable
specimens of this branch of the art that ever have been executed;
they are as superior to the chiaro-scuros of German artists as the
usual wood engravings of the latter excel those executed in Italy
during the same period.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com