GAM 666 Introduction To Game Programming Basic 3
GAM 666 – Introduction To Game Programming Basic 3 D Using Direct. X 9 As of Direct. X 8, Direct. Draw (2 D) and Direct 3 D (3 D) have been combined into Direct. X Graphics (still often called Direct 3 D, however) ●Direct. X Graphics includes a library of 3 D math helper functions, d 3 dx 9 math. h, the use of which is entirely optional but has gained wide acceptance ●
GAM 666 – Introduction To Game Programming Basic 3 D Using Direct. X 9 COM Object Pointers: ●LPDIRECT 3 D 9 – main Direct 3 D control object used to create others ●LPDIRECT 3 DDEVICE 9 – device onto which 3 D is rendered ●LPDIRECT 3 DVERTEXBUFFER 9 – list of vertices describing a shape to be rendered ●LP 3 DXFONT – font for rendering text onto a 3 D scene
GAM 666 – Introduction To Game Programming Basic 3 D Using Direct. X 9 Basic frame rendering logic: ●Clear the display target's backbuffer using Direct 3 DDevice Clear() ●Call Direct 3 DDevice Begin. Scene() ●Render primitives [shapes] using Direct 3 DDevice Draw. Primitive() and text using Direct 3 DXFont Draw. Text() ●Call Direct 3 DDevice End. Scene() ●Flip backbuffer to screen with
GAM 666 – Introduction To Game Programming 3 D Setup Direct 3 DCreate 9() to create Direct 3 D object ●Enumeration in Direct. X Graphics is easier than in Direct. Draw 7 (no enumeration callback function needs to be supplied, rather call a query function in your own loop) ●Direct 3 D Create. Device() to create Direct 3 DDevice ●Direct 3 DDevice Create. Vertex. Buffer() to allocate vertex buffers ●D 3 DXCreate. Font() to make 3 D font ●
GAM 666 – Introduction To Game Programming Critical d 3 dx 9 math. h data types D 3 DXVECTOR 3 – x, y and z coordinates for a 3 D point or vector (context determines which) ●D 3 DXMATRIX – 4 x 4 3 D transformation matrix ●There are many D 3 DX* functions to create or manipulate these ●Visit ● http: //gpwiki. org/index. php/3 D: Matrix_Math to review basics of 3 D vectors and matrix arithmetic
GAM 666 – Introduction To Game Programming Fixed Function Transformation Pipeline Direct 3 D 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 (2 D projection), reasonable perspective will be apparent
GAM 666 – Introduction To Game Programming Fixed Function Transformation Pipeline Use Direct 3 DDevice Set. Transform() 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,
GAM 666 – 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 D 3 DFVF_ flags identify which pieces of data you'll be using, e. g. ●D 3 DFVF_XYZ means you want XYZ coordinates (almost always want this) ●D 3 DFVF_DIFFUSE means you want to specify a [diffuse] colour (sometimes ●
GAM 666 – 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 Direct 3 DDevice Set. FVF() to specify what vertex format you are about to use ●
GAM 666 – Introduction To Game Programming Vertex Buffer Primitive Types D 3 DPT_POINTLIST – list of separate points ●D 3 DPT_LINELIST – list of line segments, 2 vertices per segment ●D 3 DPT_LINESTRIP – list of joined line segments, 2 vertices for first, 1 for each thereafter ●D 3 DPT_TRIANGLELIST – list of triangles, 3 vertices per triangle ●D 3 DPT_TRIANGLEFAN – list of triangles sharing a common starting point, 3 vertices for the first, 1 for each thereafter ●D 3 DPT_TRIANGLESTRIP – list of triangles, each sharing two points with the next, 3 vertices for the first, 1 for each thereafter ●
GAM 666 – Introduction To Game Programming Using Vertex Buffers ●Since vertex buffers may live on the video card or elsewhere 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 Direct 3 DDevice Set. Stream. Source() to identify which vertex buffer to use ●Use Direct 3 DDevice Draw. Primitive() to render a primitive from the current vertex buffer, specifying the type of primitive and number of primitives.
GAM 666 – Introduction To Game Programming Other Considerations The Direct 3 DDevice 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 Direct 3 DDevice Set. Render. State() 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 ●
- Slides: 12