22
Advanced 3-D Game Programming using DirectX 7.0 By Adrian Perez, with Dan Royer Wordware Publishing, Inc.

Advanced 3-D Game Pro gramming using DirectX 7.0 - Computer

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Advanced 3-D GamePro gramming using

DirectX 7.0

By Adrian Perez, with Dan Royer

Wordware Pub lishing, Inc.

Library of Congress Cataloging-in-Publication Data

Perez, Adrian. Advanced 3-D game programming using DirectX 7.0 / by Adrian Perez, with Dan Royer. p. cm. ISBN 1-55622-721-3 (pbk.) 1. Computer games—Programming. 2. DirectX. I. Royer, Dan. II. Title.

QA76.76.C672 P47 2000 794.8'15265--dc21 00-032086 CIP

© 2000, Wordware Publishing, Inc.

All Rights Reserved

2320 Los Rios BoulevardPlano, Texas 75074

No part of this book may be reproduced in any form or byany means without permission in writing from

Wordware Publishing, Inc.

Printed in the United States of America

ISBN 1-55622-721-310 9 8 7 6 5 4 3 2 10005

DirectX is a registered trademark of Microsoft Corp.Other product names mentioned are used for identification purposes only and may be trademarks of their respectivecompanies.

All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., atthe above address. Telephone inquiries may be made by calling:

(972) 423-0090

To my par ents, Manny and Maria.

Adrian

The woman who would n’t love me back,The head bully,

My par ents:

Well look who’s laugh ing now!

Dan

iii

Ac knowl edg ments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xivIn tro duc tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Chap ter 1 Win dows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1A Word About Win dows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Hun gar ian No ta tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Gen eral Win dows Con cepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Mes sage Han dling in Win dows . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Ex plaining Mes sage Pro cessing . . . . . . . . . . . . . . . . . . . . . . . . 5Hello World—Win dows Style . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Ex plaining the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Reg is tering Our Ap pli ca tion . . . . . . . . . . . . . . . . . . . . . . 11Ini tial izing Our Win dow . . . . . . . . . . . . . . . . . . . . . . . . 11WndProc—The Mes sage Pump . . . . . . . . . . . . . . . . . . . . 14

Ma nip u lating Win dow Ge om e try . . . . . . . . . . . . . . . . . . . . . . . . . 15Im por tant Win dow Mes sages . . . . . . . . . . . . . . . . . . . . . . . . . . . 17MFC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Class En cap su la tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23COM: The Com po nent Ob ject Model . . . . . . . . . . . . . . . . . . . . . . 30References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Chap ter 2 DirectX—The Ba sic Com po nents . . . . . . . . . . . . . . . . . . . . . 35What is DirectX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35In stal la tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Set ting up VC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36DirectDraw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2-D Graph ics—A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . 39Sur faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Com plex Sur faces . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Describing Sur faces . . . . . . . . . . . . . . . . . . . . . . . . . . 45The IDirectDrawSurface7 Inter face. . . . . . . . . . . . . . . . . . 50Sur face Oper a tions . . . . . . . . . . . . . . . . . . . . . . . . . . 51Sur faces and Mem ory . . . . . . . . . . . . . . . . . . . . . . . . . 53Mod ifying the Con tents of Sur faces . . . . . . . . . . . . . . . . . . 53Draw ing on Sur faces with GDI . . . . . . . . . . . . . . . . . . . . 54

v

The DirectDraw Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . 55Win dowed vs. Full-screen . . . . . . . . . . . . . . . . . . . . . . . 56

The DirectDraw In ter face . . . . . . . . . . . . . . . . . . . . . . . . . 57Cre ating DirectDraw Sur faces . . . . . . . . . . . . . . . . . . . . 58DirectDraw Device Capa bil ities. . . . . . . . . . . . . . . . . . . . 58

Im ple menting DirectDraw with cGraphicsLayer . . . . . . . . . . . . . . 59Cre ating the Graph ics Layer . . . . . . . . . . . . . . . . . . . . . 66Enu mer ating DirectDraw Devices . . . . . . . . . . . . . . . . . . 67Full-screen Ini tial iza tion . . . . . . . . . . . . . . . . . . . . . . . . 69Win dowed Ini tial iza tion . . . . . . . . . . . . . . . . . . . . . . . . 75Shutting Down DirectDraw . . . . . . . . . . . . . . . . . . . . . . 81Changes to cApplication. . . . . . . . . . . . . . . . . . . . . . . . 81

Ap pli ca tion: DirectDraw Sam ple . . . . . . . . . . . . . . . . . . . . . . 85DirectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Why Should We Use DirectInput? . . . . . . . . . . . . . . . . . . . . . 88De vices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Receiving Device States . . . . . . . . . . . . . . . . . . . . . . . . 90Coop er a tive Levels . . . . . . . . . . . . . . . . . . . . . . . . . . 93Appli ca tion Focus and Devices . . . . . . . . . . . . . . . . . . . . 94

The DirectInput Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Im ple menting DirectInput with cInputLayer . . . . . . . . . . . . . . . . 95

Addi tions to cApplication . . . . . . . . . . . . . . . . . . . . . . 110DirectSound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

The Es sen tials of Sound . . . . . . . . . . . . . . . . . . . . . . . . . . 111DirectSound Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 113

DirectSound Buffers . . . . . . . . . . . . . . . . . . . . . . . . . 113Op er a tions on Sound Buffers . . . . . . . . . . . . . . . . . . . . . . . 116

Loading WAV Files . . . . . . . . . . . . . . . . . . . . . . . . . . 120Im ple menting DirectSound with cSoundLayer . . . . . . . . . . . . . . 121

Cre ating the DirectSound Object . . . . . . . . . . . . . . . . . . 121Set ting the Coop er a tive Level . . . . . . . . . . . . . . . . . . . . 122Grabbing the Pri mary Buffer . . . . . . . . . . . . . . . . . . . . . 123

The cSound Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Addi tions to cApplication . . . . . . . . . . . . . . . . . . . . . . 133

Ap pli ca tion: DirectSound Sam ple . . . . . . . . . . . . . . . . . . . . . 133References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

Chap ter 3 3-D Math Foun da tions . . . . . . . . . . . . . . . . . . . . . . . . . . 139Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

The point3 Struc ture . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Ba sic point3 Func tions . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

Assign. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Mag and MagSquared . . . . . . . . . . . . . . . . . . . . . . . . 145Nor mal ize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

Con tents

vi

Dist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146point3 Op er a tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

Addi tion/Sub trac tion . . . . . . . . . . . . . . . . . . . . . . . . . 146Vec tor-sca lar Mul ti pli ca tion/Divi sion. . . . . . . . . . . . . . . . . 148Vec tor Equal ity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149Dot Prod uct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Cross Prod uct . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Poly gons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Tri an gles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Strips and Fans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

De fining Lo cal ity with Re la tion to a Plane. . . . . . . . . . . . . . . . . 163Back-face Culling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Clip ping Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Clip ping Poly gons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Ob ject Rep re sen ta tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Trans for ma tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

Ma trices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175The matrix4 Struc ture . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Trans la tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Basic Rota tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Axis-Angle Rota tion . . . . . . . . . . . . . . . . . . . . . . . . . 191The LookAt Matrix. . . . . . . . . . . . . . . . . . . . . . . . . . 193Per spec tive Pro jec tion Matrix . . . . . . . . . . . . . . . . . . . . 195Inverse of a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Col li sion De tec tion with Bounding Spheres. . . . . . . . . . . . . . . . . . . 197Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

Rep re senting Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Lighting Mod els . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Spec u lar Reflec tion . . . . . . . . . . . . . . . . . . . . . . . . . . 204Light Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Par al lel Lights (or Direc tional Lights) . . . . . . . . . . . . . . . . 205Point Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Spot Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

Shad ing Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Lam bert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Gourad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Phong. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

BSP Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211BSP Tree The ory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212BSP Tree Con struc tion . . . . . . . . . . . . . . . . . . . . . . . . . . 213BSP Tree Al go rithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

Sorted Poly gon Order ing . . . . . . . . . . . . . . . . . . . . . . 218Testing Local ity of a Point . . . . . . . . . . . . . . . . . . . . . . 219Testing Line Seg ments . . . . . . . . . . . . . . . . . . . . . . . . 220

Con tents

vii

BSP Tree Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Wrapping It Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Ref er ences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

Chap ter 4 Ar ti fi cial In tel li gence . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Starting Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Lo co mo tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Steering—Ba sic Al go rithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

Chasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Evading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236Pat tern-based AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Steering—Ad vanced Al go rithms. . . . . . . . . . . . . . . . . . . . . . . . . 237Po ten tial Func tions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

The Good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240The Bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Appli ca tion: potentialFunc . . . . . . . . . . . . . . . . . . . . . . 241

Path Fol low ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243Ground work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244Graph The ory Done Really, Really Quickly . . . . . . . . . . . . . 246Using Graphs to Find Shortest Paths. . . . . . . . . . . . . . . . . 249Appli ca tion: Path Plan ner . . . . . . . . . . . . . . . . . . . . . . 251

Mo ti va tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255Non-De ter min is tic Fi nite Au tom ata (NFAs) . . . . . . . . . . . . . . . 255Ge netic Al go rithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Rule-Based AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Neu ral Net works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

A Basic Neu ron . . . . . . . . . . . . . . . . . . . . . . . . . . . 261Sim ple Neu ral Net works . . . . . . . . . . . . . . . . . . . . . . 263Training Neu ral Net works . . . . . . . . . . . . . . . . . . . . . . 265Using Neu ral Net works in Games . . . . . . . . . . . . . . . . . . 266Appli ca tion: NeuralNet . . . . . . . . . . . . . . . . . . . . . . . 266

Some Closing Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Works Cited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

Chap ter 5 UDP Net working . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Ter mi nol ogy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

Endianness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Net work Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Pro to cols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281The Right Tool for the Job (aka “DirectPlay and why we’re not us ing it”) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Im ple men ta tion 1: MTUDP . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Con tents

viii

De sign Con sid er ations. . . . . . . . . . . . . . . . . . . . . . . . . . . 282Things that go “aurgh, my kid ney!” in the night. . . . . . . . . . . . . . 282Mutexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Threads, Mon i tor, and the Prob lem of the try/throw/catch Con struc tion . . . . . . . . . . . . . . . . . . . . . . . 286MTUDP: The Early Years . . . . . . . . . . . . . . . . . . . . . . . . . 286

MTUDP::Startup() and MTUDP::Cleanup() . . . . . . . . . . . . . 287MTUDP::MTUDP() and MTUDP::~MTUDP() . . . . . . . . . . . 288MTUDP::StartListening(). . . . . . . . . . . . . . . . . . . . . . . 289MTUDP::StartSending() . . . . . . . . . . . . . . . . . . . . . . . 290MTUDP::ThreadProc(). . . . . . . . . . . . . . . . . . . . . . . . 290MTUDP::ProcessIncomingData() . . . . . . . . . . . . . . . . . . 292MTUPD::GetReliableData() . . . . . . . . . . . . . . . . . . . . . 293MTUDP::ReliableSendTo().. . . . . . . . . . . . . . . . . . . . . . 293

cDataPacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293cQueueIn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294cHost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

MTUDP::ReliableSendTo() . . . . . . . . . . . . . . . . . . . . . . 300cUnreliableQueueIn . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307cUnreliableQueueOut . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

cHost::AddACKMessage() / cHost::ProcessIncomingACKs() . . . . 308cNetClock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

Im ple men ta tion 2: Smooth Net work Play . . . . . . . . . . . . . . . . . . . 316Geo graphic and Tem po ral In de pend ence . . . . . . . . . . . . . . . . . 316Tim ing is Everything . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Pick and Choose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Pre dic tion and Ex trap o la tion . . . . . . . . . . . . . . . . . . . . . . . 319

Con clu sion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

Chap ter 6 Be gin ning Direct3D—Im me di ate Mode . . . . . . . . . . . . . . . . . 323In tro duc tion to D3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323The Direct3D7 Ob ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324The Direct3DDevice7 Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . 325

De vice Se man tics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326De vice Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

HAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326TnLHal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327RGB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328RefRast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328Ramp (and Other Leg acy Devices). . . . . . . . . . . . . . . . . . 329

De ter mining De vice Ca pa bil ities . . . . . . . . . . . . . . . . . . . . . 329The D3DPRIMCAPS Struc ture . . . . . . . . . . . . . . . . . . . 332

Set ting De vice Ren der States . . . . . . . . . . . . . . . . . . . . . . . 334Fun da men tal Direct3D Struc tures . . . . . . . . . . . . . . . . . . . . . . . 341

D3DCOLOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

Con tents

ix

D3DCOLORVALUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342D3DVECTOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343D3DMATRIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

The Depth Prob lem (and How Direct3D Solves It) . . . . . . . . . . . . . . 343W-Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Enu mer ating Z-buffer For mats . . . . . . . . . . . . . . . . . . . . . . 347

Sten cil Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Ver tex Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Tex ture Map ping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354Ma te rials and Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

Using Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354D3D Lighting Seman tics . . . . . . . . . . . . . . . . . . . . . . . 356The sLight Helper Class . . . . . . . . . . . . . . . . . . . . . . . 357

Using Ma te rials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358D3D Mate rial Seman tics . . . . . . . . . . . . . . . . . . . . . . . 359The sMaterial Helper Class . . . . . . . . . . . . . . . . . . . . . 360

The Ge om e try Pipe line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360Clip ping and Viewports . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363Ver tex-based Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Pixel-based Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Using Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

Draw ing with the De vice . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Direct3D Ver tex Struc tures . . . . . . . . . . . . . . . . . . . . . . . . 367

D3DVERTEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367D3DLVERTEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368D3DTLVERTEX . . . . . . . . . . . . . . . . . . . . . . . . . . . 369Flex i ble Ver tex Format Flags . . . . . . . . . . . . . . . . . . . . . 370

Strided Ver tex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373Prim i tive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374The DrawPrimitive Func tions . . . . . . . . . . . . . . . . . . . . . . . 375

DrawPrimitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375DrawPrimitiveStrided . . . . . . . . . . . . . . . . . . . . . . . . 375DrawPrimitiveVB. . . . . . . . . . . . . . . . . . . . . . . . . . . 376DrawIndexedPrimitive . . . . . . . . . . . . . . . . . . . . . . . . 376DrawIndexedPrimitiveStrided . . . . . . . . . . . . . . . . . . . . 377DrawIndexedPrimitiveVB . . . . . . . . . . . . . . . . . . . . . . 378

Adding Direct3D to the Graph ics Layer . . . . . . . . . . . . . . . . . . . . 379Direct3D Ini tial iza tion . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

Acquire an IDirect3D7 Inter face from Our IDirectDraw7 Inter face . . . . . . . . . . . . . . . . . . . . . 380Enu mer ate and Con firm Devices . . . . . . . . . . . . . . . . . . 380Enu mer ate Z-buffer For mats and Build the Z-buffer . . . . . . . . 384Cre ate a Device and Attach It to the Back Buffer . . . . . . . . . . 388Cre ate a Viewport and Pro jec tion Matrix . . . . . . . . . . . . . . 388

Con tents

x

Putting It All To gether . . . . . . . . . . . . . . . . . . . . . . . . . . . 389Resizing Wor ries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392

Fur ther Ad di tions to the GameLib . . . . . . . . . . . . . . . . . . . . . . . 393The D3DX Li brary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Ap pli ca tion: D3D View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

The .o3d For mat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395The cModel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396

Chap ter 7 Ad vanced 3-D Pro gramming . . . . . . . . . . . . . . . . . . . . . . . 407An i ma tion us ing Hi er ar chi cal Ob jects . . . . . . . . . . . . . . . . . . . . . . 407

For ward Ki ne mat ics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409In verse Ki ne mat ics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

Appli ca tion: InvKim . . . . . . . . . . . . . . . . . . . . . . . . . 414Para met ric Curves and Sur faces . . . . . . . . . . . . . . . . . . . . . . . . . 417

Bezier Curves and Sur faces . . . . . . . . . . . . . . . . . . . . . . . . 417Bezier Con cepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 417The Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420Find ing the Ba sis Ma trix . . . . . . . . . . . . . . . . . . . . . . . 422Cal cu lating Bezier Curves . . . . . . . . . . . . . . . . . . . . . . 423For ward Dif fer enc ing . . . . . . . . . . . . . . . . . . . . . . . . 425Draw ing Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . 429Draw ing Sur faces. . . . . . . . . . . . . . . . . . . . . . . . . . . 430Ap pli ca tion: Tea pot. . . . . . . . . . . . . . . . . . . . . . . . . . 431

B-Spline Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436Ap pli ca tion: BSpline . . . . . . . . . . . . . . . . . . . . . . . . . 438

Sub di vi sion Sur faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439Sub di vi sion Es sen tials . . . . . . . . . . . . . . . . . . . . . . . . . . . 440

Tri an gles vs. Quads . . . . . . . . . . . . . . . . . . . . . . . . . . 442Inter po lating vs. Approx i mating . . . . . . . . . . . . . . . . . . . 442Uni form vs. Non-Uni form . . . . . . . . . . . . . . . . . . . . . . 442Sta tion ary vs. Non-Sta tion ary . . . . . . . . . . . . . . . . . . . . 443

Mod ified But ter fly Method Sub di vi sion Sur faces . . . . . . . . . . . . . 443Ap pli ca tion: SubDiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446

Pro gres sive Meshes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459Pro gres sive Mesh Ba sics . . . . . . . . . . . . . . . . . . . . . . . . . . 460Choosing Our Edges. . . . . . . . . . . . . . . . . . . . . . . . . . . . 461

Stan Melax’s Edge Selec tion Algo rithm . . . . . . . . . . . . . . . 462Quad ric Error Met rics . . . . . . . . . . . . . . . . . . . . . . . . 462

Im ple menting a Pro gres sive Mesh Ren derer . . . . . . . . . . . . . . . 464Radiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

Radiosity Foundations . . . . . . . . . . . . . . . . . . . . . . . . . . . 467Pro gres sive Radiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . 470The Form Fac tor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470Ap pli ca tion: Radiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . 472

Ref er ences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476

Con tents

xi

For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

Chap ter 8 Ad vanced Direct3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . 479Al pha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

The Al pha Blending Equa tion . . . . . . . . . . . . . . . . . . . . . . . 480A Note on Depth Or der ing . . . . . . . . . . . . . . . . . . . . . . . . 481En abling Al pha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . 481Blending Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481

Tex ture Map ping 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483Fun da men tals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484Af fine Ver sus Per spec tive Map ping . . . . . . . . . . . . . . . . . . . . 486Tex ture Ad dressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487

Wrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487Mir ror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488Clamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488Bor der Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489

Tex ture Wrapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490Tex ture Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490MIP Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492Fil tering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

Point Sam pling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493Bilinear Fil tering . . . . . . . . . . . . . . . . . . . . . . . . . . . 494Trilinear Fil tering . . . . . . . . . . . . . . . . . . . . . . . . . . . 495Anisotropic Fil tering . . . . . . . . . . . . . . . . . . . . . . . . . 496

Tex tures in Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . 497Tex ture Man age ment. . . . . . . . . . . . . . . . . . . . . . . . . 497Tex ture Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

DDS For mat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499The cTexture Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

Acti vating Tex tures . . . . . . . . . . . . . . . . . . . . . . . . . . 508Tex ture Map ping 202 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

Mul ti ple Tex tures Per Prim i tive . . . . . . . . . . . . . . . . . . . . . . 509Tex ture Trans forms. . . . . . . . . . . . . . . . . . . . . . . . . . 518Effects Using Mul ti ple Tex tures . . . . . . . . . . . . . . . . . . . 519Light Maps (a.k.a. Dark Maps) . . . . . . . . . . . . . . . . . . . . 520

En vi ron ment Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522Spher i cal En vi ron ment Maps. . . . . . . . . . . . . . . . . . . . . 522Cu bic En vi ron ment Maps . . . . . . . . . . . . . . . . . . . . . . 525Spec u lar Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Detail Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

Ap pli ca tion: De tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535Glow Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536Gloss Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538Other Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540

Ap pli ca tion: MultiTex . . . . . . . . . . . . . . . . . . . . . . . . . . . 540

Con tents

xii

Pass 1: Base Map . . . . . . . . . . . . . . . . . . . . . . . . . . . 540Pass 2: Detail Map . . . . . . . . . . . . . . . . . . . . . . . . . . 542Pass 3: Glow Map . . . . . . . . . . . . . . . . . . . . . . . . . . 544Pass 4: Envi ron ment Map . . . . . . . . . . . . . . . . . . . . . . 547Pass 5: Gloss Map . . . . . . . . . . . . . . . . . . . . . . . . . . 550Pass 6: Cloud Map . . . . . . . . . . . . . . . . . . . . . . . . . . 554Putting Them All Together . . . . . . . . . . . . . . . . . . . . . . 556

A Gen eral Pur pose Shader Li brary . . . . . . . . . . . . . . . . . . . . 558Using the Sten cil Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560

Over draw Coun ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562Dis solves and Wipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563Sten cil Shadows and Sten cil Mir rors . . . . . . . . . . . . . . . . . . . 564

Val i dating De vice Ca pa bil ities with ValidateDevice() . . . . . . . . . . . . . . 564For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566

Chap ter 9 Scene Man age ment. . . . . . . . . . . . . . . . . . . . . . . . . . . . 567The Scene Man age ment Prob lem . . . . . . . . . . . . . . . . . . . . . . . . 567So lu tions to the Scene Man age ment Prob lem . . . . . . . . . . . . . . . . . 568

Quadtrees/Octrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569Por tal Ren der ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570

Por tal Ren der ing Con cepts . . . . . . . . . . . . . . . . . . . . . 571Exact Por tal Ren der ing . . . . . . . . . . . . . . . . . . . . . . . . 578Approximative Por tal Ren der ing . . . . . . . . . . . . . . . . . . . 580

Por tal Ef fects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580Mir rors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580Translocators and Non-Eu clid ean Move ment . . . . . . . . . . . . 583

Por tal Gen er a tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585Pre cal cu lated Por tal Ren der ing (PVS) . . . . . . . . . . . . . . . . . . . 586

Advan tages/Dis ad van tages . . . . . . . . . . . . . . . . . . . . . . 587Imple men ta tion Details . . . . . . . . . . . . . . . . . . . . . . . 587

Ap pli ca tion: Mobots At tack! . . . . . . . . . . . . . . . . . . . . . . . . . . . 588Interobject Com mu ni ca tion . . . . . . . . . . . . . . . . . . . . . . . . 588Net work Com mu ni ca tion . . . . . . . . . . . . . . . . . . . . . . . . . 592Code Struc ture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

For Fur ther Read ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595Closing Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

Ap pen dix A An STL Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597Tem plates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597Con tainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600Func tors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602STL Re sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602

In dex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603

Con tents

xiii

This book could n’t have been com pleted with out the help and guid ance of awhole lot of peo ple. I’ll try to remem ber them all here. First, thanks go to WesBeckwith and Jim Hill at Wordware Pub lishing. They were extremely for giv ingof my hec tic sched ule, and they helped guide me to fin ish ing this book. I alsomust thank Alex Dunne for let ting me write an arti cle in 1998 for Game Devel -oper magazine. If I had n’t writ ten that arti cle, I never would have writ ten thisbook.

Every thing I know about the top ics in this book I learned from other peo ple. Some of these peo ple were men tors, oth ers were bosses, still oth ers were pro fes -sors and teach ers. Some were just cool peo ple who took the time to sit and talkwith me. I can’t thank them enough. Paul Heckbert, Tom Funkhouser, EricPetajan, Charles Boyd, Mike Toelle, Kent Grif fin, David Baraff, Randy Pausch,Howie Choset, Michael Abrash, Hugues Hoppe, and Mark Stehlik: You guysrock, thank you.

Thanks to Microsoft, ATI, nVidia, id Soft ware, and Lydia Choy for help ingme with some of the images used in the text.

A lot of peo ple helped assure the tech ni cal cor rect ness and gen eral san ity ofthis text. Ian Parberry and his class at Uni ver sity of North Texas were immensely help ful: Thanks, guys. Michael Krause was an indis pens able help in assur ing the cor rect ness of the DirectX chap ters. Bob Gaines, Mikey Wetzel, and JasonSandlin from the DirectX team at Microsoft helped make sure Chapters 2, 6, and 8 were ship-shape: Mad props to them. David Black was kind enough to lookover Chapter 9 and help remove some errors and clar ify a few points.

Finally I need to thank all of the peo ple who helped me get this thing done.I know I won’t be able to remem ber all of them, but here’s a short list: Man ualand Maria Perez, Katherin Peperzak, Lydia Choy (again), Mike Schuresko, MikeBreen (and the rest of the Orig i nals), Vick Mukherjee, Pat rick Nel son, BrianSharp, and Marcin Krieger.

xiv

A wise man some where, somehow, at some point in his tory, may have said thebest way to start a book is with an anec dote. Never to ques tion the words of awise man who may or may not have existed, here we go.

When I was a fresh man in high school back in 1993, I took the requiredbiol ogy class most kids of my age end up hav ing to take. It involved exper i -ments, lab reports, dis sect ing of var i ous ani mals, and the like. One of my labpart ners was a fel low named Chris V. We were both inter ested in com put ers, and quickly became friends, to the point where talk ing about biol ogy in class wassec ond to techno-bab ble.

One night, in the mid dle of Decem ber, Chris called me up. The lab reportthat was due the next day required results from the exper i ment we had donetogether in class, and he had lost his copy of our exper i ment results. He wantedto know if I could copy mine and bring them over to his place so he could fin ishwrit ing up the lab. Of course, this was in those hei nous pre-car days, so driv ingto his house required talk ing my par ents into it, find ing his address, and var i ous other hard ships. While I was will ing to do him the favor, I was n’t will ing to do it for free. So I asked him what he could do to recip ro cate my kind ges ture.

“Well,” he said, “I guess I can give you a copy of this game I just got.”“Really? What’s it called?” said I.“Doom. By the Wolf 3-D guys.”“It’s called Doom? What kind of name is that??”After get ting the results to his house and the game to mine, I fired the pro -

gram up on my creaky old 386 DX-20 clone, burn ing rub ber with a whop ping 4MB of RAM. As my space marine took his first ten u ous steps down the cor ri dorsinfested with hellspawn, my life changed. I had done some pro gram ming before in school (Logo and Basic), but after I fin ished play ing the first time, I had aclear pic ture in my head of what I wanted to do with my life: I wanted to writegames, write some thing like Doom. I popped onto a few local BBoards andasked two ques tions: what lan guage was the game writ ten in, and what com -piler was used.

Within a day or so I pur chased Watcom C 10.0 and got my first book on Cpro gram ming. My first C pro gram was “Hello, World.” My sec ond was a slow,crash-happy, non-robust, wireframe spin ning cube.

xv

I tip my hat to John Carmack, John Romero, and the rest of the team behind Doom: My love for cre at ing games was fully real ized via their mas ter piece. It’sbecause of them that I learned every thing that I have about this exceed inglyinter est ing and dynamic area of com puter acquired pro gram ming. The knowl -edge I have is what I hope to fill these pages with, so that other peo ple can getinto graph ics and game pro gram ming.

I’ve found that the best way to get a lot of use ful infor ma tion down in ashort amount of space is to use the tried-and-true FAQ (fre quently asked ques -tions) for mat. I fig ured if peo ple needed answers to some ques tions about thisbook as they stood in their local book store try ing to decide whether or not tobuy it, these would be them.

I’ll be the first to say it: I am not a pro fes sional game pro gram mer. I hope to beone even tu ally, but for right now I am but a sim ple col lege stu dent. A few yearsago I wrote an arti cle for Game Devel oper that led to the ini tial dis cus sion withWordware to write this book. Besides that, I’ve spent sum mers work ing atLucent (in their Graph ics depart ment) and at Microsoft (on the Direct3D, Imme -di ate Mode team). I’m not claim ing to be a “game pro gram ming guru,” but I’veread all I can on a lot of top ics and have a lot of prac ti cal expe ri ence in graph ics pro gram ming.

It was n’t too long ago that I was learn ing the ropes, so I’m hop ing thatexplain ing some of the pit falls that I’ve gone through can help other peo pleaspir ing to write games.

Another thing that I am not is a pro fes sional author, nor do I ever plan tobe. So I beg that you be quick to for give any tech ni cal and gram mat i cal errors; I assure you that I took every pos si ble means to elim i nate both. It seems to bealmost a cliché for game pro gram ming books to be writ ten by non-game pro -gram mers, but I hope I can buck the sta tus quo and cre ate a book that teachesyou a lot.

I’ve learned from a lot of amaz ingly bril liant peo ple, cov ered a lot of dif fi cultground, and asked a lot of dumb ques tions. One thing that I’ve found is that thegame devel op ment indus try is all about shar ing. If every one shares, every oneknows more neat stuff and the net knowl edge of the indus try increases. This is a good thing, because then we all get to play better games. No one per son coulddis cover all the prin ci ples behind com puter graph ics and game pro gram mingthem selves; no one can learn in a vac uum. Peo ple took the time to share what

xvi

Intro duc tion

they learned with me, and now I’m tak ing the time to share what I’ve learnedwith you.

This book was intended spe cif i cally for peo ple who know how to pro gramalready, but have taken only rudi men tary stabs at graph ics/game pro gram ming,or have never taken any stab at all. You may be a pro gram mer in another fieldor a col lege stu dent look ing to embark on some side pro jects.

This book was not designed for begin ners. I’m not try ing to sound aloof or any -thing; I’m sure if a begin ner picks up this book they’ll be able to trudge throughit if they feel up to it. How ever, since I’m so con strained for space, often times Ineed to breeze past cer tain con cepts (such as inher i tance in C++). If you’venever pro grammed before, you’ll have an exceed ingly dif fi cult time with thisbook.

On the other hand, this book isn’t really designed for pro fes sion als either.I’m sure that most peo ple who have pushed games out the door will only findone or two chap ters in this book have any mate rial they have n’t seen before.

The code was writ ten in C++, using Microsoft Visual C++ 6.0. The .DSPs and.DSWs are pro vided on the CD; the .DSPs will work with ver sions pre vi ous to6.0, and the .DSWs will work with 6.0 and up. If you choose to use a dif fer entcom piler, get ting the source code to work should be a fairly triv ial task. I spe cif i -cally wrote this code to use as lit tle non-stan dard C++ as pos si ble (as far as Iknow, the only non-stan dard C++ I use is name less struc tures within unions).

I chose Win32 as the API envi ron ment to use because 90% of com puter userscur rently work on Win dows. Win32 is not an easy API to under stand, espe ciallyafter using DOS cod ing con ven tions. It isn’t ter ri bly ele gant either, but I sup pose it could be worse. We could choose other plat forms to work on, but doing soreduces our tar get audi ence by a fac tor of 9 or more.

If you’ve never heard of Linux, Linux is an open source oper at ing sys tem.This means any one can down load the source to see how the sys tem works, and

Intro duc tion

xvii

any one can join teams that work on future ver sions and make con tri bu tions tothe oper at ing sys tem. The Linux com mu nity has a lot of pride for what it does,and as a result Linux is an incred i bly small, fast, and sta ble oper at ing sys tem.There are a vari ety of win dow man ag ers avail able for down load, some thatemu late other WMs like Win dows or MacOS, some that take new direc tions(like the ultra-sim plis tic Blackbox and the uber-com pli cated Enlight en ment).Check out www.linux.org.

For those of you who have never used it, OpenGL is another graph ics API. Sil i -con Graph ics designed it in the early ’90s for use on their high-end graph icsworkstations. It has been ported to count less plat forms and oper at ing sys tems.Out side of the games indus try, in areas like sim u la tion and aca demic research,OpenGL is the de facto stan dard for doing com puter graph ics. It is a sim ple, ele -gant, and fast API. Check out www.opengl.org for more infor ma tion.

But it isn’t per fect. First of all, OpenGL has a large amount of func tion al ityin it. Making the inter face so sim ple requires that the imple men ta tion take careof a lot of ugly details to make sure every thing works cor rectly. Because of theway driv ers are imple mented, each com pany that makes a 3-D card has to sup -port the entire OpenGL fea ture set in order to have a fully com pli ant OpenGLdriver. These driv ers are extremely dif fi cult to imple ment cor rectly, and the per -for mance on equal hard ware can vary wildly based on driver qual ity. Inaddi tion, DirectX has the added advan tage of being able to move quicker toaccom mo date new hard ware fea tures. DirectX is con trolled by Microsoft (which can be a good or bad thing, depend ing on your view of it) while OpenGLextensions need to be delib er ated by com mit tees.

My ini tial hope was to have two ver sions of the source code, one for Win -dows and Direct3D, the other for Linux and OpenGL. This ended up not beingpos si ble, so I had to choose one or the other; I chose Direct3D.

I had a few other lan guage choices I was kick ing around when plan ning thisbook. Although there are aco lytes out there for Delphi, VB, and even ML, theonly lan guages I seri ously con sid ered were C++, Java, and C. Java is designedby Sun Microsystems and is an inher ently object-ori ented lan guage, with somehigh-level lan guage fea tures like gar bage col lec tion. C is about as low level aspro gram ming gets with out dip ping into assem bly. It has very few if anyhigh-level con structs and does n’t abstract any thing away from the pro gram mer.

Intro duc tion

xviii

C++ is an inter est ing lan guage because it essen tially sits directly betweenthe func tion al ity of the other two lan guages. C++ sup ports COM better than Cdoes (this will be more thor oughly dis cussed in Chapter 1). Also, class sys temsand oper a tor over load ing gen er ally make code eas ier to read (although ofcourse any good thing can and will be abused). Java, although very cool, is aninter preted lan guage. Every year this seems to be less impor tant: JIT com pi la -tion gets faster and more grunt work is handed off to the APIs. How ever, I feltC++ would be a better fit for the book. Java is still a very young lan guage andis still going through a lot of change.

That depends. Tech nically no, you can get by with out any accel er a tor at all, justusing Direct3D’s soft ware rasterizer. How ever, it’s extremely slow, far from realtime for any thing but triv i ally sim ple scenes. It’s almost impos si ble to buy acom puter these days with out some sort of 3-D accel er a tion, and an accel er a torcapa ble of han dling all the code in this book can be pur chased for under $100.

Some peo ple see C++ as a divine blade to smite the wicked. They take con trolof tem plate classes the likes of which you have never seen. They over load theiostream oper a tors for all of their classes. They see mul ti ple inher i tance as ahellspawn of satan him self. I see C++ as a tool. The more eso teric fea tures ofthe lan guage (such as the iostream library), I don’t use at all. Less eso teric fea -tures (like mul ti ple inher i tance), I use when it makes sense. To a large degreeI’m still a learner. My cod ing style still leaves a great deal to be desired, but Idid every thing in my power to make the code clean and easy to read. Hav ing acod ing style you stick to is invalu able. The code for this book was writ ten overan eleven-month period, but I can pick up the code I wrote at the begin ning and still grok it, because I com mented and used some good con ven tions. If I canunder stand it, hope fully you can too.

One of the great est books I’ve ever read on pro gram ming was Code Com plete(Microsoft Press). It’s a hand book on how to pro gram well, not just how to pro -gram. Nuances like the length of vari able names, design of sub rou tines, andlength of files are cov ered in detail in this book; I strongly encour age any onewho wants to become a great pro gram mer to pick it up. You may notice some of the con ven tions I use in this book are sim i lar to the con ven tions described in

Intro duc tion

xix

Code Com plete: some of them are bor rowed from the great game pro gram merslike John Carmack, and some of them are bor rowed from source in DirectX,MFC, and Win32.

I’ve tried really hard to make the code in this book acces si ble to every one. Icom ment any thing I think is unclear, I strive for good choice in vari able names,and I try to make my code look clean while still try ing to be fast. Of course, Iwon’t please every one. Assuredly, there are some C++ cod ing stan dards I’mprob a bly not fol low ing cor rectly. There are some pieces of code that would getmuch faster with a lit tle obfus ca tion.

If you’ve never used C++ before, or are new to pro gram ming, this book isgoing to be extremely hard to digest. A good dis cus sion on pro gram ming essen -tials and the C++ lan guage is C++ Primer (Lippman et al; Addi son-Wes leyPub lishing).

Class/structure namesMFC names its classes with a pre fixed C. As an exam ple, a class that rep re sentsthe func tion al ity of a but ton is called CButton. I like this fine, but due tonamespace clash ing, I instead pre fix my own classes with a low er case c forclasses, a low er case s for structs, a low er case i for inter faces, and a low er case efor enu mer a tions (cButton or sButton).

There is one nota ble excep tion. While most classes are intended to hidefunc tion al ity away and act as com po nents, there are a few classes/struc turesthat are intended to be instantiated as basic prim i tives. So for basic math e maticprim i tives like points and matri ces, I have no pre fix, and I post fix with thedimen sion of the prim i tive (2-D points are point2, 3-D points are point3, etc.).This is to allow them to have the same look and feel as their clos est con cep tualneigh bor, float. For the same rea son, all of the math e matic prim i tives have a lotof over loaded oper a tors to sim plify math-laden code.

Variable namesSemi-long vari able names are a good thing. They make your code self-com mentitself. One needs to be care ful though: make them too long, and they dis tractfrom both the code itself and the pro cess of writ ing it.

I use short vari ables very spo rad i cally; int i, j, k pop up a lot in my codefor loops and what not, but besides that I strive to give mean ing ful names to thevari ables I use. Usually, this means that they have more than one word in them.The sys tem I use spec i fies low er case for the first word and ini tial cap for eachword after that, with no under scores (an exam ple would be int numObjects). If the last let ter of a word is a cap i tal let ter, an under score is placed to sep a rate itfrom the next word (exam ple, class cD3D_App).

A pop u lar nomen cla ture for vari ables is Hun gar ian nota tion, which we’lltouch on in Chap ter 1. I’m not hard core about it, but gen er ally my floats are

xx

Intro duc tion

pre fixed with f, my ints with i, and my point ers with p (exam ples: floatfTimer; int iStringSize; char* pBuffer). Note that the pre fix counts as thefirst word, mak ing all words after it caps. (I find pBuffer much more read ablethan pbuffer.)

I also use pre fixes to define spe cial qual i ties of vari ables. Global vari ablesare pre ceded with a “g_” (an exam ple would be int g_hInstance); static vari -ables are preceeded with an “s_” (static float s_fTimer); mem ber vari ablesof classes are preceeded with an “m_” (int m_iNumElements).

Intro duc tion

xxi