Now put this directory at the top of the list by using the arrow buttons.After you have referenced the directories to the include files, choose the path to C:\dxsdk\lib to reference the
Trang 1After you have configured Visual C/C++ 6, make sure it is configured correctly by compiling one of the examples in the C:\DXSDK\Samples\C++\Direct3D directory You might see
something like the following message if you haven’t configured the include path properly:
d:\book\source\part 1\chapter6\animated objects\objects.cpp(68) : error C2146: syntax error : missing ‘;’ before ‘g_Keyboard_pDI’
Even if you provided the correct path to the header and library files, you might have to feed the names of these files to the linker of your development environment The proper path to these object/library modules should be listed in your Link dialog box To reach this dialog box, select Project, Settings and then choose the Link tab In the General category, there is
an entry field called Object/Library Modules It holds all of the library files, which should
be linked to the application you’re currently developing It should look like Figure I.15
In this entry field, you will need to name at least the following:
• d3dx9.lib
• 3dxof.lib
• d3d9.lib
• winmm.lib
• dxguid.lib
If you missed a file, an error message that might look like this will appear:
d3dapp.obj : error LNK2001: unresolved external symbol _Direct3DCreate9@4
Debug/SkinnedMesh.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
xxxi
Introduction
Figure I.15
Configuring the Linker Path
Trang 2Here, d3d9.lib is missing The unresolved external symbols are part of the COM
(Component Object Model ) interfaces of Direct3D (I will explain COM in detail later
in the book.)
You should also check another include path, the one that holds the path to the directories
of the common include files for the C/C++ compiler If you change your machine or you are working on another machine with a different DirectX directory, normally you have to add the common files using Project, Add Files to Project, Files
Now let’s compile our first project
1. Fire up your Visual C++ configured development environment
2. Click on Open Workspace
3. Choose basics.dsp
4. Check whether you’ve configured the paths to the directories as described previously
5. Choose the Build/basics.exe build
6. When everything works correctly, choose run basics.exe
That’s it If something went wrong, reread the sections on installing the DirectX SDK and Visual C++, and consult the provided documentation
Setting Up Visual C++ NET
Similar to Visual C/C++ 6, the DirectX 9 SDK will set all paths for you if you install it after you install Visual C++ NET Nevertheless, in case anything went wrong, this section tells you what you have to do if you need to configure DirectX 9 in the Visual C++ NET IDE manually You start by clicking on Tools, Options in the Visual C++ NET IDE, as shown in Figure I.16 There is a project section that holds all the paths to the relevant directories, as shown in Figure I.17
If you have seen the dialog box that is used by Visual C/C++ 6 Studio, you should feel com-fortable because this dialog box uses the same logic You can see that at the top-right of the dialog box, the Show Directories For drop-down menu offers the different kinds of files that you can reference (see Figure I.18)
Trang 3Introduction
Figure I.16
Choose the Options menu item
Figure I.17
Choose the Project, VC++ Directories section
Trang 4After you choose a type of file, the referenced directories will appear in the large area in the middle of the dialog box
The include file directories are C:\dxsdk\include and C:\DXSDK9\Samples\C++\Common\ Include You should point your Visual C++ NET IDE to these directories by clicking on the New button and then on the ellipsis button A default dialog box will open, allowing you to choose a directory (see Figure I.19)
Figure I.18
Choose the type of file to reference
Figure I.19
Choose a directory
Trang 5Now put this directory at the top of the list by using the arrow buttons.
After you have referenced the directories to the include files, choose the path to C:\dxsdk\lib
to reference the library files correctly Don’t forget to bring them to the top of the list using the arrow buttons
The proper path to the additional dependencies files should be listed in the Additional
Dependencies field at Project, Properties, Linker, Input In this entry field, you will need to name at least the following (see Figure I.20):
• d3dx9.lib
• d3dxof.lib
• d3d9.lib
• winmm.lib
• dxguid.lib
If any problem occurs, check out the end of the “Setting Up Visual C++ 6.0” section for
solutions to common problems The solutions are the same for Visual C++ NET
Now let’s compile our first project
1. Fire up your Visual C++ NET configured development environment
2. Click on Open Solution
3. Choose basics.sln
4. Check whether you’ve configured the paths to the directories as described previously
5. Choose Debug, Start Without Debugging
6. When everything works correctly, basics.exe will start
xxxv
Introduction
Figure I.20
Choose additional dependencies
Trang 6That’s it If something went wrong, reread the sections on installing the DirectX SDK and Visual C++ NET, and study the provided documentation
DirectX Shader Debugger
To debug vertex and pixel shaders, you need to install the DirectX shader debugger This debugger is only provided for Visual C++ NET You can find it in the dialog box of the DirectX installation routine, as shown in Figure I.21
By default, the shader debugger is not selected, so you have to choose it explicitly If the computer only hosts a Visual C/C++ 6 installation, you don’t get this choice at all The dialog box will look like Figure I.22
Please note that only Visual C++ NET users can use the shader debugger
Figure I.21
Choose the shader debugger by clicking on DirectX Extensions for Visual Studio 7
Figure I.22
Installable DirectX utilities under Visual C/C++ 6
Trang 7Other Tools
Two plug-and-play compilers (provided by Intel and Codeplay) can be switched into
the Visual C/C++ 6 or Visual C++ NET IDE They offer higher performance for game
programmers than the compilers provided with these IDEs However, I don’t recommend that beginners use these compilers because they overcomplicate some things You can
find more information at http://www.intel.com/software/products/compilers and at
http://www.codeplay.com While on Intel’s Web site, don’t forget to check out VTune, a
profiler that helps you to optimize your game These and the Visual C++ compilers from
Microsoft are the fundamental development tools that the majority of PC game program-mers use nowadays
Additional Resources
The CD-ROM provided with this book includes a directory called Movies These movies inter-actively show you how to configure the default paths of Visual C/C++ 6 and Visual C++ NET You should visit the Microsoft MSDN site for DirectX (http://msdn.microsoft.com/directx)
at regular intervals, and also the mailing list at http://discuss.microsoft.com/SCRIPTS/
WA-MSD.EXE?S1=DIRECTXDEV You can find daily news about the game developer com-munity at http://www.gamedev.net or http://www.flipcode.com I’ll also provide additional information on http://www.direct3d.info
xxxvii
Introduction
Trang 8PART ONE
DirectX
Graphics: Don’t
Hurt Me
Trang 91 The History of
Direct3D/DirectX Graphics
2 Overview of HAL and COM
3 Programming Conventions
4 3D Fundamentals, Gouraud Shading, and Texture-Mapping Basics
5 The Basics
6 First Steps to Animation
Trang 10CHAPTER 1
The History
of Direct3D/
DirectX Graphics
Trang 11This chapter covers the history of Direct3D and its functional development over time
It starts with DOS and PC history, and it discusses how three British students changed the world of 3D programming
Before Windows, DOS was the most popular operating system for the PC Games were programmed exclusively in DOS for many years Game developers resisted developing for Windows because of its unacceptable graphics and audio performance at the time
The direct access to hardware that DOS afforded came with its own complications, however DOS games had to support the full range of video and audio hardware This forced devel-opers to write complex code to support dozens of different configurations just to provide consistent graphics and audio across all PCs
In this chapter, you will learn about:
• The history and earlier versions of DirectX, leading up to the current version
• The introduction of point sprites and vertex and pixel shaders in DirectX 8
• The basics of 3D textures
DirectX 2.0
With the advent of DirectX in 1995, Microsoft provided within Windows the performance previously available only through DOS, without the complexity of supporting each vendor’s particular hardware Now every hardware vendor delivers its product with Windows drivers Direct3D, part of DirectX, appeared in 1996 in DirectX 2.0 Direct3D is designed to give access
to the advanced graphics capabilities of 3D hardware accelerators, while promoting device independence and hardware abstraction by providing a common interface to the programmer Code properly written for Direct3D will work on Direct3D devices now and in the future Let’s dive a little bit deeper into history In the early 1990s, many 3D engines for PCs were built
in Great Britain There was the well-known Renderware (http://www.renderware.com) and the BRender from Argonaut (http://www.argonaut.com), which was ported in 1994 to OS/2 and a small British company called RenderMorphics RenderMorphics was founded in 1993 by Servan Keondjian, Kate Seekings, and Doug Rabson, and they produced a product called Reality Lab Keondjian played piano in a band at night and programmed his 3D engine by day Seekings subsequently upped her credentials with a quick master’s degree in computer graph-ics at Middlesex University It’s interesting to note that her 3D rendering library, developed with input from Keondjian and Rabson, was submitted as a school project that she flunked for not following the assigned specs closely enough Microsoft spotted the team at the first trade show they attended (SIGGRAPH 94), and RenderMorphics was acquired in February 1995
4 1 The History of Direct3D/DirectX Graphics
Trang 12After the acquisition of RenderMorphics, Microsoft integrated Reality Lab into its DirectX
family of APIs (Application Programming Interfaces) The Immediate Mode component of
Reality Lab absorbed the standard 3D Windows API of the time, 3-D-DDI, which was created
by Michael Abrash, who later helped create the Quake 1 engine at id Software
DirectX 6/7
Until the advent of DirectX 8.0, Direct3D consisted of two distinct APIs: Retained Mode and Immediate Mode At the time, the Immediate Mode API was difficult to use, but it was a
flexible, low-level API that ran as efficiently as possible Retained Mode was built on top of Immediate Mode and provided additional services, such as frame hierarchy and animation Retained Mode was easier to learn and use than Immediate Mode, but programmers wanted the added performance and flexibility that Immediate Mode provided Development of the Retained Mode API has been frozen with the release of DirectX 6.0
The major changes between Direct3D Immediate Mode versions 6.0 and 7.0 affected the
support of hardware-accelerated transformation and lighting and the reorganization of the lights, materials, and viewport objects, which now are set directly by calling the methods of
IDirect3DDevice7 Direct3D 7 dropped the special interface used in Direct3D 6 to access tex-tures The IDirect3DDrawSurface7interface also provided an easier way to manage the textures
DirectX 8
The advent of the DirectX 8.0 SDK brought the biggest improvements in the history of
Direct3D Direct3D got a fundamentally new architecture with version 8.0 The
initializa-tion, allocainitializa-tion, and management of data were simplified by the integration of DirectDraw and Direct3D into one interface, called DirectX Graphics, which led to a smaller memory footprint and a simpler programming model It is safe to say that these changes made
DirectX 8 easier to use and more consistent than OpenGL for the first time
Some of the new features in DirectX 8 included
• Point sprites (hardware-supported sprite objects)
• 3D volumetric textures (textures with three dimensions)
• An improved Direct3DXlibrary (which provided many useful and highly
optimized routines)
• N-patches (which add an algorithm and vertices to a model to get a
higher-tessellated model)
• Vertex and pixel shaders (which interface to program the graphics
processor directly)
5
DirectX 8
Trang 13Point Sprites
A new feature of DirectX 8 was the ability to use hardware sprites for a particle system to
generate sparks, explosions, rain, snow, and so on So-called point sprites are supported by
their own programming interface to help you accomplish this
3D Textures
With 3D volumetric textures, you can accomplish exact per-pixel lighting effects, point- and spot-light effects, and atmospheric effects
Direct3DX Utility Library
DirectX 8 also enhanced the Direct3DXutility library, which was introduced in DirectX 7 This library provides helper functionality for:
• Enumerating device configurations
• Setting up a device
• Running full-screen or windowed mode uniformly
• Running resizing operations
• Calculating vector and matrix operations
• Simplifying image-file loading and texture creation
• Drawing simple shapes, sprites, and cube maps
With the release of DirectX 8, the utility library offered support for skinned meshes, multi-resolution level-of-detail (LOD) geometry, and higher-order surface data for X files The
D3DXimage file loader functions now support BMP, TGA, PNG, JPG, DIB, PPM, and DDS files DirectX 8.0 also introduced helper methods to port OpenGL applications to DirectX Graphics, as well as the only higher-order surfaces that are used in current games—
N-patches These are used to tessellate models higher with the help of the graphics
processor, which means you send in the data for a low-polygon model and it is tessellated (polygons are added) in the graphics card
Vertex and Pixel Shaders
The big bang in DirectX 8.0 that changed the overall development process and the appear-ance of modern games was the introduction of vertex and pixel shaders Whereas DirectX 8.0 supported the vertex and pixel shader standards vs_1_1 and ps_1_1, DirectX 8.1 added the pixel shader standards ps_1_2, ps_1_3, and ps_1_4 The last pixel shader standard, in particular, exposed the advanced capabilities of the ATI RADEON 8500–9200 cards, and ps_1_4 pointed the syntactical way for the upcoming pixel shader standards in DirectX 9.0, such as ps_2_0, ps_2_x, ps_2_a, and ps_3_0 (ps_2_0 pixel shaders are used in most exam-ples in Chapter 9, “Shader Programming with the High-Level Shader Language.”)
6 1 The History of Direct3D/DirectX Graphics
Trang 14DirectX 9
The overall API has not changed much from DirectX 8.1 to DirectX 9.0 The new version introduced new and very much improved vertex shader and pixel shader standards, includ-ing the vs_2_0, vs_2_x, and vs_3_0 vertex shader standards and the ps_2_0, ps_2_x, and
ps_3_0 pixel shader standards Although these three pixel shader standards correspond to three hardware designs, in the past the ps_1_1 standard that was initially designed for the NVIDIA GeForce 3 was adopted by all major graphic card manufacturers, for example
There are now more equivalent software implementations for the vertex and pixel shader 2_0 and 3_0 versions, called vs_2_sw, vs_3_sw, ps_2_sw, and ps_3_sw
To be able to write shaders in the most efficient way, an HLSL (High-Level Shader Language)
was introduced in DirectX 9 This HLSL helps you write shaders faster and easier in a C-like syntax instead of the old assembly syntax All shader examples in this book use HLSL
To keep up with OpenGL, a scissor test and line anti-aliasing were introduced with DirectX 9.0 The scissor test is performed after the fragment color is computed but before alpha
testing It determines whether the screen coordinates of a value are within the scissor rec-tangle defined by four values, and it discards fragments that are lying outside this recrec-tangle Additionally, there is a new render state that supports the OpenGL-style sphere map texgen
(Texgen is a nickname for automatic texture-coordinate generation.) Two-sided stencil support
was implemented to enable programmers to add shadow volumes to occlude geometry
(Please see the two-sided stencil example in Chapter 10, “More Advanced Shader Effects,” which shows the usage of shadow volumes.)
One feature that should soon make it into DirectX 9.0 games is the ability to render from one pixel shader to multiple render targets (This is called a G-buffer.) You can then blend
up to four render targets in another pass A feature that is comparable but less useful than the multiple render targets is the multi-element texture The pixel shader can store up to four textures in one multi-element texture, and it can read them from there Displacement maps were added as a new class of higher-order surfaces, but at the time of this writing, it looks like only the Matrox Parhelia and the ATI RADEON 9700 PRO are supporting them
in hardware
Two long-awaited features of DirectX 9.0 are the support of up to 24-bit color precision and the ability to change the gamma values depending on your monitor’s gamma support This gamma support uses very simple filters that are applied to data as it leaves a surface and
before it is rendered on the screen The so-called gamma controls don’t change the con-tents of the surface itself
The DirectX 9.0 SDK provides a programming framework that has been used in versions
since DirectX 6 and is used in all examples in this book Starting with DirectX 8.0, this
framework is called common files You will find these files in the
C:\DXSDK9\Samples\C++\Common directory
7
DirectX 9