Title: GAM666
1GAM666 Introduction To Game Programming
Basic 3D Using DirectX 9
- As of DirectX 8, DirectDraw (2D) and Direct3D
(3D) have been combined into DirectX Graphics
(still often called Direct3D, however) - DirectX Graphics includes a library of 3D math
helper functions, d3dx9math.h, the use of which
is entirely optional but has gained wide
acceptance
2GAM666 Introduction To Game Programming
Basic 3D Using DirectX 9
- DirectX 9 COM Object Pointers
- LPDIRECT3D9 main Direct3D control object used
to create others - LPDIRECT3DDEVICE9 device onto which 3D is
rendered - LPDIRECT3DVERTEXBUFFER9 list of vertices
describing a shape to be rendered - LP3DXFONT font for rendering text onto a 3D
scene
3GAM666 Introduction To Game Programming
Basic 3D Using DirectX 9
- Basic frame rendering logic
- Clear the display target's backbuffer using
Direct3DDevice Clear() - Call Direct3DDevice BeginScene()
- Render primitives shapes using Direct3DDevice
DrawPrimitive() and text using Direct3DXFont
DrawText() - Call Direct3DDevice EndScene()
- Flip backbuffer to screen with Direct3DDevice
Present()
4GAM666 Introduction To Game Programming
3D Setup
- Direct3DCreate9() to create Direct3D object
- Enumeration in DirectX Graphics is easier than in
DirectDraw7 (no enumeration callback function
needs to be supplied, rather call a query
function in your own loop) - Direct3D CreateDevice() to create Direct3DDevice
- Direct3DDevice CreateVertexBuffer() to allocate
vertex buffers - D3DXCreateFont() to make 3D font
5GAM666 Introduction To Game Programming
Critical d3dx9math.h data types
- D3DXVECTOR3 x, y and z coordinates for a 3D
point or vector (context determines which) - D3DXMATRIX 4x4 3D transformation matrix
- There are many D3DX functions to create or
manipulate these - Visit http//gpwiki.org/index.php/3DMatrix_Math
to review basics of 3D vectors and matrix
arithmetic
6GAM666 Introduction To Game Programming
Fixed Function Transformation Pipeline
- Direct3D has us specify 3 transformation matrices
(each vertex is transformed by all three, in
turn, before it is rendered) - World matrix moves a vertex from its specified
coordinates to somewhere else - View matrix moves a vertex so it will be as if
we were looking from a particular viewpoint (with
increasing Z coordinates in front of us) - Projection matrix moves a vertex so that if we
ignore the Z coordinate (2D projection),
reasonable perspective will be apparent
7GAM666 Introduction To Game Programming
Fixed Function Transformation Pipeline
- Use Direct3DDevice SetTransform() to change one
of the transformation matrices - The world matrix is changed whenever we have a
primitive to render that should not be moved to
where the current world matrix would move it
(typically, just before rendering each
relocatable primitive) - The view matrix is changed whenever we want the
camera or viewpoint to change, typically with
each frame as the user moves around - The projection matrix is changed whenever we want
to change the perspective or clipping, typically
only once at the beginning
8GAM666 Introduction To Game Programming
Flexible Vertex Formats (FVF)
- When a vertex buffer is created, there is some
flexibility in specifying exactly what data will
be supplied for each vertex of a primitive (e.g.
with a texture you need to supply texture
coordinates for each vertex, but with no texture
you might want to specify a colour for each
vertex) - The D3DFVF_ flags identify which pieces of data
you'll be using, e.g. - D3DFVF_XYZ means you want XYZ coordinates (almost
always want this) - D3DFVF_DIFFUSE means you want to specify a
diffuse colour (sometimes want)
9GAM666 Introduction To Game Programming
Flexible Vertex Formats (FVF)
- Although FVF flags are combined with bitwise or
() - so the order they are specified in doesn't
matter - there IS an order in which the matching
data will appear in the vertex buffer - Use Vertex Formats in Visual Studio help to see
the order the data will appear in the vertex
buffer when specific vertex flags are used - Use Direct3DDevice SetFVF() to specify what
vertex format you are about to use
10GAM666 Introduction To Game Programming
Vertex Buffer Primitive Types
- D3DPT_POINTLIST list of separate points
- D3DPT_LINELIST list of line segments, 2
vertices per segment - D3DPT_LINESTRIP list of joined line segments, 2
vertices for first, 1 for each thereafter - D3DPT_TRIANGLELIST list of triangles, 3
vertices per triangle - D3DPT_TRIANGLEFAN list of triangles sharing a
common starting point, 3 vertices for the first,
1 for each thereafter - D3DPT_TRIANGLESTRIP list of triangles, each
sharing two points with the next, 3 vertices for
the first, 1 for each thereafter
11GAM666 Introduction To Game Programming
Using Vertex Buffers
- Since vertex buffers may live on the video card
or elsewhere where shared access may be a
problem, you must lock a vertex buffer area
before you update it, and unlock it when you are
done - Don't update vertex buffers needlessly, as it is
potentially slow - Use Direct3DDevice SetStreamSource() to identify
which vertex buffer to use - Use Direct3DDevice DrawPrimitive() to render a
primitive from the current vertex buffer,
specifying the type of primitive and number of
primitives.
12GAM666 Introduction To Game Programming
Other Considerations
- The Direct3DDevice will probably need to be
Reset() if you regain focus after losing it, and
the vertex buffers (and anything else that might
have resided in video memory) may have to be
refilled or reallocated - Use Direct3DDevice SetRenderState() to turn
features (such as lighting and alpha blending) on
or off, or otherwise control them - Note that most of the characteristics of the
fixed function transformation pipeline are
obsoleted by newer, but not universally
available, shader technology double coverage is
required for the near future