Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Introduction to 3D Game Programming with DirectX.9.0 - F. D. Luna

.pdf
Скачиваний:
239
Добавлен:
24.05.2014
Размер:
6.94 Mб
Скачать

x Contents

12.2.1.3 Combining Both Parts . . . . . . . . . . . 204

12.2.2Rotation about an Arbitrary Axis . . . . . . . . . . 205

12.2.3Pitch, Yaw, and Roll. . . . . . . . . . . . . . . . . . 205

12.2.4 Walking, Strafing, and Flying . . . . . . . . . . . . 207

12.3Sample Application: Camera . . . . . . . . . . . . . . . . 209

12.4Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

Chapter 13 Basic Terrain Rendering. . . . . . . . . . . . . . . 212

13.1Heightmaps . . . . . . . . .Y. . . . . . . . . . . . . . . . 213

13.1.1Creating a Heightmap . . . . . . . . . . . . . . . . 214

13.1.2Loading a RAW FileL. . . . . . . . . . . . . . . . . 215

13.1.3Accessing andFModifying the Heightmap . . . . . . 215

13.2Generating the Terrain Geometry . . . . . . . . . . . . . 216

13.2.1ComputingMthe Vertices . . . . . . . . . . . . . . . 217

13.2.2Computing the Indices—Defining the Triangles . . 220

13.3Texturing.A. . . . . . . . . . . . . . . . . . . . . . . . . . 221

13.3.1A Procedural pproach. . . . . . . . . . . . . . . . 222

13.4Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

13.4.1Overview . . . . . . . . . . . . . . . . . . . . . . . 224

13.4.2Computing the Shade of a Quad . . . . . . . . . . . 225E

13.4.3 Shading the Terrain . . . . . . . . . . . . . . . . . 227

13.5“Walking” on the Terrain . . . . . . . . . . . . . . . . . . 228

13.6Sample Application: Terrain. . . . . . . . . . . . . . . . . 231T

13.7

Some Improvements . . . . . . . . . . . . . . . . . . . .

233

13.8

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

Chapter 14 Particle Systems . . . . . . . . . . . . . . . . . . . 235

14.1

Particles and Point Sprites . . . . . . . . . . . . . . . . .

235

14.1.1Structure Format . . . . . . . . . . . . . . . . . . . 236

14.1.2Point Sprite Render States. . . . . . . . . . . . . . 236

14.1.3Particles and Their Attributes . . . . . . . . . . . . 238

14.2Particle System Components . . . . . . . . . . . . . . . . 239

14.2.1Drawing a Particle System. . . . . . . . . . . . . . 244

14.2.2Randomness . . . . . . . . . . . . . . . . . . . . . 248

14.3Concrete Particle Systems: Snow, Firework,

Particle Gun . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

14.3.1Sample Application: Snow . . . . . . . . . . . . . . 250

14.3.2Sample Application: Firework . . . . . . . . . . . . 252

14.3.3Sample Application: Particle Gun . . . . . . . . . . 254

14.4Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

Chapter 15 Picking. . . . . . . . . . . . . . . . . . . . . . . . 257

15.1Screen to Projection Window Transform . . . . . . . . . . 259

15.2Computing the Picking Ray . . . . . . . . . . . . . . . . . 260

15.3 Transforming Rays . . . . . . . . . . . . . . . . . . . . . 261

Team-Fly®

Contents xi

15.4 Ray-Object Intersections . . . . . . . . . . . . . . . . . . 262 15.5 Picking Sample . . . . . . . . . . . . . . . . . . . . . . . 264 15.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

Part IV Shaders and Effects . . . . . . . . . . . . . . . . . 267

Chapter 16 Introduction to the High-Level Shading

Language . . . . . . . . . . . . . . . . . . . . . . 269

16.1 Writing an HLSL Shader . . . . . . . . . . . . . . . . . . 270

16.1.1Globals . . . . . . . . . . . . . . . . . . . . . . . . 272

16.1.2Input and Output Structures . . . . . . . . . . . . . 272

16.1.3Entry Point Function . . . . . . . . . . . . . . . . . 273

16.2Compiling an HLSL Shader . . . . . . . . . . . . . . . . . 275

16.2.1 The Constant Table . . . . . . . . . . . . . . . . . 275

16.2.1.1Getting a Handle to a Constant. . . . . . . 275

16.2.1.2Setting Constants . . . . . . . . . . . . . . 275

16.2.1.3Setting the Constant Default Values . . . . 278

16.2.2Compiling an HLSL Shader . . . . . . . . . . . . . 278

16.3Variable Types . . . . . . . . . . . . . . . . . . . . . . . . 280

16.3.1 Scalar Types . . . . . . . . . . . . . . . . . . . . . 280

16.3.2Vector Types . . . . . . . . . . . . . . . . . . . . . 281

16.3.3Matrix Types . . . . . . . . . . . . . . . . . . . . . 282

16.3.4Arrays. . . . . . . . . . . . . . . . . . . . . . . . . 283

16.3.5Structures . . . . . . . . . . . . . . . . . . . . . . 283

16.3.6The typedef Keyword . . . . . . . . . . . . . . . . 284

16.3.7Variable Prefixes . . . . . . . . . . . . . . . . . . . 284

16.4Keywords, Statements, and Casting . . . . . . . . . . . . 285

16.4.1Keywords . . . . . . . . . . . . . . . . . . . . . . . 285

16.4.2Basic Program Flow . . . . . . . . . . . . . . . . . 285

16.4.3Casting . . . . . . . . . . . . . . . . . . . . . . . . 286

16.5Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 287

16.6User-Defined Functions . . . . . . . . . . . . . . . . . . . 288

16.7Built-in Functions . . . . . . . . . . . . . . . . . . . . . . 290

16.8Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Chapter 17 Introduction to Vertex Shaders . . . . . . . . . . . 293

17.1Vertex Declarations . . . . . . . . . . . . . . . . . . . . . 294

17.1.1Describing a Vertex Declaration . . . . . . . . . . . 295

17.1.2Creating a Vertex Declaration . . . . . . . . . . . . 297

17.1.3Enabling a Vertex Declaration . . . . . . . . . . . . 297

17.2Vertex Data Usages . . . . . . . . . . . . . . . . . . . . . 298

17.3Steps to Using a Vertex Shader . . . . . . . . . . . . . . . 300

17.3.1Writing and Compiling a Vertex Shader . . . . . . . 300

17.3.2

Creating a Vertex Shader

.

.

.

.

.

.

. . . . .

.

.

.

300

17.3.3

Setting a Vertex Shader .

.

.

.

.

.

.

. . . . .

.

.

.

301

xii Contents

17.3.4Destroying a Vertex Shader . . . . . . . . . . . . . 301

17.4Sample Application: Diffuse Lighting . . . . . . . . . . . . 301

17.5Sample Application: Cartoon Rendering . . . . . . . . . . 307

17.5.1Cartoon Shading . . . . . . . . . . . . . . . . . . . 308

17.5.2The Cartoon Shading Vertex Shader Code . . . . . 309

17.5.3Silhouette Outlining . . . . . . . . . . . . . . . . . 311

17.5.3.1Edge Representation . . . . . . . . . . . . 312

17.5.3.2Testing for a Silhouette Edge. . . . . . . . 313

17.5.3.3Edge Generation . . . . . . . . . . . . . . 314

17.5.4The Silhouette Outlining Vertex Shader Code . . . 315

17.6Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

Chapter 18 Introduction to Pixel Shaders . . . . . . . . . . . . 318

18.1Multitexturing Overview . . . . . . . . . . . . . . . . . . 319

18.1.1Enabling Multiple Textures . . . . . . . . . . . . . 320

18.1.2Multiple Texture Coordinates . . . . . . . . . . . . 321

18.2Pixel Shader Inputs and Outputs . . . . . . . . . . . . . . 322

18.3Steps to Using a Pixel Shader. . . . . . . . . . . . . . . . 323

18.3.1Writing and Compiling a Pixel Shader . . . . . . . . 323

18.3.2Creating a Pixel Shader . . . . . . . . . . . . . . . 324

18.3.3Setting a Pixel Shader . . . . . . . . . . . . . . . . 324

18.3.4Destroying a Pixel Shader . . . . . . . . . . . . . . 325

18.4HLSL Sampler Objects . . . . . . . . . . . . . . . . . . . 325

18.5Sample Application: Multitexturing in a Pixel Shader . . . 326

18.6Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

Chapter 19 The Effects Framework . . . . . . . . . . . . . . . 335

19.1Techniques and Passes . . . . . . . . . . . . . . . . . . . 336

19.2More HLSL Intrinsic Objects . . . . . . . . . . . . . . . . 337

19.2.1 Texture Objects . . . . . . . . . . .

. . . . . . . . 337

19.2.2 Sampler Objects and Sampler States

. . . . . . . .

337

19.2.3 Vertex and Pixel Shader Objects . .

. . . . . . . .

338

19.2.4Strings . . . . . . . . . . . . . . . . . . . . . . . . 339

19.2.5Annotations . . . . . . . . . . . . . . . . . . . . . . 339

19.3Device States in an Effect File . . . . . . . . . . . . . . . 340

19.4Creating an Effect . . . . . . . . . . . . . . . . . . . . . . 341

19.5Setting Constants . . . . . . . . . . . . . . . . . . . . . . 342

19.6 Using an Effect . . . . . . . . . . . . .

. . . . . . . . . . 344

19.6.1 Obtaining a Handle to an Effect .

. . . . . . . . . . 345

19.6.2 Activating an Effect . . . . . . .

. . . . . . . . . .

345

19.6.3 Beginning an Effect . . . . . . .

. . . . . . . . . .

345

19.6.4Setting the Current Rendering Pass. . . . . . . . . 346

19.6.5Ending an Effect . . . . . . . . . . . . . . . . . . . 346

19.6.6Example . . . . . . . . . . . . . . . . . . . . . . . 346

Contents xiii

19.7 Sample Application: Lighting and Texturing in

an Effect File . . . . . . . . . . . . . . . . . . . . . . . . . . . 347

19.8Sample Application: Fog Effect . . . . . . . . . . . . . . . 352

19.9Sample Application: Cartoon Effect. . . . . . . . . . . . . 355

19.10EffectEdit . . . . . . . . . . . . . . . . . . . . . . . . . . 356

19.11Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 356

Appendix An Introduction to Windows Programming . . . . . 359

Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Events, the Message Queue, Messages, and the

Message Loop . . . . . . . . . . . . . . . . . . . . . . . . 360

GUI . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . 362

Hello World Windows Application

. . . . . . . . . . . . . . . . 363

Explaining Hello World . . . . . . . . . . . . . . . . . . . . . . 366

Includes, Global Variables, and Prototypes . . . . . . . . .

366

WinMain . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

WNDCLASS and Registration . . . . . . . . . . . . . . .

368

Creating and Displaying the Window . . . . . . . . . . . . 370

The Message Loop . . . . . . . . . . . . . . . . . . . . .

372

The Window Procedure . . . . . . . . . . . . . . . . . . . 373

The MessageBox Function . . . . . . . . . . . . . . . . . 375

A Better Message Loop . . . . . . . . . . . . . . . . . . . . .

375

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

376

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

This page intentionally left blank.

Acknowledgments

I would like to thank my technical editor Rod Lopez for putting in the time to review this book for both accuracy and improvements. I would also like to thank Jim Leiterman (author of Vector Game Math Processors from Wordware Publishing) and Hanley Leung (programmer for Kush Games), who both reviewed portions of this book. Next, I want thank Adam Hault and Gary Simmons, who both teach the BSP/PVS course at www.gameinstitute.com, for their assistance. In addition, I want thank William Chin who helped me out many years ago. Lastly, I want to thank the staff at Wordware Publishing, in particular, Jim Hill, Wes Beckwith, Beth Kohler, Heather Hill, Denise McEvoy, and Alan McCuller.

xv

This page intentionally left blank.

Introduction

This book is an introduction to programming interactive 3D computer graphics using DirectX 9.0, with an emphasis on game development. It teaches you the fundamentals of Direct3D, after which you will be able to go on to learn and apply more advanced techniques. Assumingly, since you have this book in your hands, you have a rough idea of what DirectX is about. From a developer’s perspective, DirectX is a set of APIs (application programming interfaces) for developing multimedia applications on the Windows platform. In this book we are concerned with a particular DirectX subset, namely Direct3D. As the name implies, Direct3D is the API used for developing 3D applications.

This book is divided into four main parts. Part I explains the mathematical tools that will be used throughout this book. Part II covers elementary 3D techniques, such as lighting, texturing, alpha blending, and stenciling. Part III is largely about using Direct3D to implement a variety of interesting techniques and applications, such as picking, terrain rendering, particle systems, a flexible virtual camera, and loading and rendering 3D models (XFiles). The theme of Part IV is vertex and pixel shaders, including the effects framework and the new (to DirectX 9.0) High-Level Shading Language. The present and future of 3D game programming is the use of shaders, and by dedicating an entire part of the book to shaders, we have an up-to-date and relevant book on modern graphics programming.

For the beginner, this book is best read front to back. The chapters have been organized so that the difficulty increases progressively with each chapter. In this way, there are no sudden jumps in complexity, leaving the reader lost. In general, for a particular chapter we will use the techniques and concepts previously developed. Therefore, it is important that you have mastered the material of a chapter before continuing. Experienced readers can pick the chapters of interest.

Finally, you may wonder what kinds of games you can develop after reading this book. The answer to that question is best obtained by skimming through this book and seeing the types of applications that are developed. From that you should be able to visualize the types of games that can be developed based on the techniques taught in this book and some of your own ingenuity.

xvii

xviii Introduction

Prerequisites

This book is designed to be an introductory level textbook. However, that does not imply that it is easy for people with no programming experience. Readers are expected to be comfortable with algebra, trigonometry, their development environment (e.g., Visual Studio), C++, and fundamental data structures such as arrays and lists. Being familiar with Windows programming is also helpful but not imperative; refer to Appendix A for an introduction to Windows programming.

Required Development Tools

This book uses C++ as its programming language for the sample programs. To quote the DirectX documentation, “DirectX 9.0 supports only Microsoft Visual C++ 6.0 and later.” Therefore, as of publication, in order to write C++ applications using DirectX 9.0, you need either Visual C++ (VC++) 6.0 or VC++ 7.0 (.NET).

Note: The sample code for this book was compiled and built using VC++ 7.0. For the most part, it should compile and build on VC++ 6.0 also, but be aware of the following difference. In VC++ 7.0 the following will compile and is legal because the variable cnt is considered to be local to the for loop.

int main()

{

for(int cnt = 0; cnt < 10; cnt++)

{

std::cout << "hello" << std::endl;

}

for(int cnt = 0; cnt < 10; cnt++)

{

std::cout << "hello" << std::endl;

}

return 0;

}

However, in VC++ 6.0 this will not compile. It gives the error message error C2374: 'cnt' : redefinition; multiple initialization because in VC++ 6.0 the variable cnt is not treated as being local to the for loop.

Therefore, when porting to VC++ 6.0, you may need to make some minor changes to get it to compile due to this difference.

Introduction xix

Recommended Hardware

The following hardware recommendations are if you wish to be able to run the sample programs at an acceptable frame rate; all the samples can be run using the REF device, which emulates Direct3D functionality in software. Because things are being emulated in software, they run very slow. We discuss the REF more in Chapter 1.

The sample programs in Part II of this book are fairly basic and should run on low-end cards, such as the Riva TNT or an equivalent graphics card. The sample programs in Part III push more geometry and use some newer features, such as point sprites. For these samples we recommend a graphics card at the level of a GeForce2. The sample programs in Part IV use vertex and pixel shaders; therefore, to run these programs in real time, you will need a graphics card that supports shaders such as the GeForce3.

Intended Audience

This book was designed with the following three audiences in mind:

Intermediate level C++ programmers who would like an introduction to 3D programming using the latest iteration of Direct3D— Direct3D 9.0

3D programmers experienced with an API other than DirectX (e.g., OpenGL) who would like an introduction to Direct3D 9.0

Experienced Direct3D programmers who would like an up-to-date book covering the latest version of Direct3D, including vertex and pixel shaders, the High-Level Shading Language, and the effects framework

Installing DirectX 9.0

To write and execute DirectX 9.0 programs, you need both the DirectX 9.0 runtime and the DirectX 9.0 SDK (Software Development Kit) installed on your computer. Note that the runtime will be installed when you install the SDK. The DirectX SDK can be obtained at http://msdn.microsoft.com/library/default.asp?url=/downloads/list/ directx.asp. The installation is straightforward; however, there is one important point. When you get to the dialog box, as shown in Figure I.1, make sure that you select the debug option.