Title: Advanced Game Technology CMPCD3026 CMPSEM044
1Advanced Game TechnologyCMPCD3026-CMPSEM044
Abdennour El Rhalibi Room 723 a.elrhalibi_at_livjm.ac
.uk
2Course Details (Attempt)
- 3D Game Engines Components
- DirectX D3D, 3D Modelling and Rendering
- Meshes, Level Loading and Editing
- Terrain Rendering and LOD
- Camera Setting and Animation
- Spatial Data structure BSP and PVS
- NPC Behaviour and 3D PathFinding A, Flocking,
Scripting - 3D Collision Detection and Response
- Shading languages
- Game networking Issues Architecture, Protocol,
Event Synchronisation - Introduction to Console Programming
3Meshes, Level Loading and Editing Lecture 3
4Overview
- 3D Rendering systems
- Basic Concepts
- 3D Primitives
- Meshes
- D3D Data Structures
- Vertex Buffers (VB)
- Flexible Vertex Format (FVF)
- Loading and Editing
- Meshes
- Setting World and View
- Transforming
53D Rendering systems
- The modeling-rendering paradigm
- Graphics pipeline
- At the head of the pipeline, all of a model's
vertices are declared relative to a local
coordinate system. - The first stage of the geometry pipeline
transforms a model's vertices from their local
coordinate system to a coordinate system that is
used by all the objects in a scene. - In the next stage, the vertices that describe
your 3-D world are oriented with respect to a
camera. - The next stage is the projection transformation.
In this part of the pipeline, objects are usually
scaled with relation to their distance from the
viewer in order to give the illusion of depth to
a scene. - In the final part of the pipeline, any vertices
that will not be visible on the screen are
removed. This process is called clipping. After
clipping, the remaining vertices are scaled
according to the viewport parameters and
converted into screen coordinates. The resulting
vertices - seen on the screen when the scene is
rasterized - exist in screen space.
6Basic Concepts
- DX uses Left Hand Coordinate System. OpenGL uses
Right Hand Coordinate System - Primitives are built using vertices, stored in
vertex buffers - Different types of primitives D3DPT_POINTLIST,
D3DPT_LINELIST, D3DPT_LINESTRIP,
D3DPT_TRIANGLELIST, D3DPT_TRIANGLESTRIP, D3DPT_T
RIANGLEFAN - Each face has a normal associated with it.
Normals are vectors perpendicular to the faces
surface and point the way the face is facing - Face normals are specified using order of
vertices, Clock-Wise or Counter-Clock-Wise - Faces with normal facing away from the camera are
usually backfaced culled
73D Primitives
- 3-dimensional shapes can be composed of 2D
(coplanar) primitives in space (usually
triangles). - Each triangle (or coplanar polygon) on a 3D shape
is called a face. - Smooth surfaces can be achieved by enough
primitives and correct use of shading.
8Different Primitives
- D3DPT_POINTLIST, D3DPT_LINELIST, D3DPT_LINESTRIP,
D3DPT_TRIANGLELIST, D3DPT_TRIANGLESTRIP, D3DPT_T
RIANGLEFAN
9Meshes
- A scene is a collection of objects or models. An
object is represented as a triangle mesh
approximation. - The triangles of the mesh are the building blocks
of the object that we are modeling. - We use the terms all interchangeably to refer to
the triangles of a mesh polygons, primitives and
mesh geometry. - Triangles are primitives, but Direct3D also
supports line and point primitives.
10Meshes
- Meshes are data structures that hold several
primitives to create complex shapes - You can associate vertices, faces, materials,
textures, and more to a mesh - Meshes can be created with a modeling software
(like 3D Studio) and then exported to X file
format and loaded
11Polygonal Meshes
- The major way to represent objects in games and
computer graphics. - A bunch of triangles that together form a surface
(mesh) - But how to store them?
- How to draw them?
- Answer.
12Types of Primitives
13Rendering Primitives
- Several calls to do thisHRESULT DrawPrimitive(
D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex,
UINT PrimitiveCount )HRESULT DrawPrimitiveUP(
D3DPRIMITIVETYPE PrimitiveType, UINT
PrimitiveCount, CONST void pVertexStreamZeroData,
UINT VertexStreamZeroStride ) - Etc, etc, etc (see SD for more..)
- UP User Pointer, i.e., store your primitive
in system memory. Works, but slow.
14Indexed Primitives
- Learn to Share Vertices
- No indexingN triangles x 3 vertices 3N
verticesIn a large mesh, 6 triangles store the
same vertex! (seperately!) - Indexing reduces to one instance of each vertex!
15Indexed Primitives
- Now we can refer to a primitive by a list of
indices (Index Buffer) which index into the list
of vertices (Vertex Buffer) - This saves memory, is faster, and cache coherent
on the video card (to an extent)
1 2 3 4 1 4 3 4 4 3 6 7 8 9 10
16Vertex Buffers
- Vertex buffers, represented by the
IDirect3DVertexBuffer9 interface, are memory
buffers that contain vertex data - Vertex buffers can contain any vertex type -
transformed or untransformed, lit or unlit - The flexibility of vertex buffers make them ideal
staging points for reusing transformed geometry - Ex Rendering models that use multiple textures
the geometry is transformed only once, and then
portions of it can be rendered as needed,
interleaved with the required texture changes - Vertex buffer is described in terms of its
capabilities if it can exist only in system
memory, if it is only used for write operations,
and the type and number of vertices it can
contain traits described by D3DVERTEXBUFFER_DESC
17Flexible Vertex Formats
- A flexible vertex format
- (FVF) code describes
- the contents of vertices
- stored interleaved in a
- single data stream
- struct BLENDVERTEX
-
- D3DXVECTOR3 v // Referenced as v0 in the vertex
shader - FLOAT blend1 // Referenced as v1.x in the vertex
shader - FLOAT blend2 // Referenced as v1.y in the vertex
shader - FLOAT blend3 // Referenced as v1.z in the vertex
shader - // v1.w 1.0 - (v1.x v1.y v1.z)
- D3DXVECTOR3 n // Referenced as v3 in the vertex
shader - FLOAT tu, tv // Referenced as v7 in the vertex
shader -
- define D3DFVF_BLENDVERTEX (D3DFVF_XYZB3D3DFVF_NO
RMALD3DFVF_TEX1)
183DStep1 project
- The following slides introduce 3D programming
with reference to a minimal example program
called 3dstep1 - which - loads a 3D object from disk
- displays the lit object on screen
- allows the user to rotate the object around the y
axis - Allows the user to extend the transform to rotate
the object around the x an z axis and to
translate in all the plans
19Modelling and Loading
20Rendering modes
- Can be set to solid, wire-frame or point
21Initialise 3D world
- Any 3D world must have . . .
- Some sort of camera and viewing system
- Lighting and rendering settings
- Objects and ways of transforming them
22Initialise 3D world
- To initialise the 3D world in D3D we have to
specify three matrices
23The matrices
- The world transformation matrix defines how to
translate, scale, and rotate the geometry in the
3-D model space. - The view transformation matrix defines the
position and rotation of the view. The view
matrix is the camera for the scene. - The projection transformation matrix defines how
geometry is transformed from 3-D view space to
2-D viewport space.
24Initialise 3D world
- A D3D 3D world must have . . .
- A camera - set in D3D by setting a camera matrix
and a perspective projection matrix - Lighting - in D3D could be directional, point,
spot, or ambient lighting - Device rendering settings - fill and backface
culling modes, depth (z) buffering - Objects - 3D objects, created on the fly, or
loaded from disk in the D3D .x file format (which
you may have modelled in 3DStudio MAX)
25D3D Z buffering
- D3D uses z (depth) buffering in order to work out
which polygons to display on screen - Considering what colour a particular pixel will
be a ray is cast into the scene - any number of polygons may be intersected by the
ray - the z buffer is used to determine which is
nearest and therefore visible
26So to Initialise the 3D world . . .
- Need to
- Turn on depth buffering
- Set rendering (fill) and backface culling modes
- create a camera matrix and set its parameters
- set the camera matrix to be the device camera
matrix - set the perspective projection matrix
- set the projection matrix to be the device
projection matrix
27Turn on depth buffering
- Two lines of code are needed to turn z buffering
on using the DXU library
28Back face culling
- Back face culling is used to remove surfaces that
should be facing away from the camera - Can be set to none, clockwise, or anticlockwise
- The orientation is calculated from the surface
normal vector - Whether the inside or outside surface is shown
depends on whether the polygon vertices are
specifies clockwise or anticlockwise
29Back face culling
- None
- Clockwise
- Anticlockwise
30Set Rendering (fill) and cull modes
- One function call each . . .
31Setting up the camera matrix
- To set up camera matrix we specify
- the cameras position in space
- where the camera is looking
- and which way is up
32Setting up the projection matrix
- Need to specify
- the field of view (how much of the scene we can
see) - view aspect ratio (image width height ratio)
- distance to front clipping plane
- distance to back clipping plane
33Clipping planes
34Effect of changing field of view
35The world matrix
- Transformations of the world matrix will
transform all items in the world - Set to identity matrix - no transformation
36Putting it all together - Initialise3DWorld
37Camera aspect ratio
- Aspect ratio is device width/device height
- Screen is usually 640 by 480 1.333
- Wrong aspect ratio leads to image distortion
- eg rotate car with screen 400x400 and a. ratio
1.33
38Lights
- There are three main types of lights
- Point lights - a point in space emitting light
- Spot lights, like real spot lights
- directional lights, light that comes from a
certain direction and exists everywhere in the
world - doesnt occur in real life but probably
the most useful type to use - Also ambient light - exists everywhere in scene -
by itself its like cartoon rendering - solid
colour with no shading
39Point lights
- Point lights have colour and position in space
- They give off light equally in all directions
40Spotlights
- Spotlights have colour, position, and direction
in which they emit light - Light emitted from a spotlight is made up of a
bright inner cone and a larger outer cone, with
the light intensity diminishing between the two. - Spotlights are affected by falloff, attenuation,
and range.
41Directional lights
- Directional lights have only colour and
direction, not position.
42Ambient light
- Background light, is modelled as a constant
- With ambient light alone, all surfaces have equal
brightness
43Putting it together - the InitialiseLights
function
44Dealing with 3D objects
- In D3D objects are stored in .x files
- To load an object into your program you need to
define a data structure to store it - The DXUMESH struct does this for you
- You can draw the mesh with the DXUDrawMesh()
function - When you are finished with the mesh you should
dispose of it with the DXUReleaseMesh() function
45Dealing with 3D objects - code
46Transforming an object
- We can transform an object by transforming the
WORLD matrix - This is easiest way to transform
- BUT it actually applies the transformation to the
whole world - Which is OK if we only have one object, but is
NOT OK if we need to transform objects
independently
47D3DXMatrixRotationY() function
- To set up a matrix for rotation we can use
D3DXMatrixRotationY() which we pass a pointer to
a matrix and an angle in radians - Then if we set the device World matrix to be a
matrix with rotation then the world will be
rotated - (in the following code the DegToRad() macro
converts from degrees to radians)
48Putting it together - the UpdateWorld () function
493DStep1 - functions
- Discussion so far has described in detail three
functions in the program - Initialise3DWorld()
- InitialiseLights()
- UpdateWorld()
- We just need a main loop and a functions to load
the world, render it, and release it - to
complete the program
503DStep1- LoadWorld()
- This loads the car mesh, a font for writing, and
initialises the 3D world and lighting
513DStep1- RenderWorld()
- Draws the mesh and string
523DStep1- Release World
533DStep1- WinMain()
- The Winmain() function does the D3D, windows, and
input initialisation we have already seen and
discussed - It calls LoadWorld() to initialise the world
- Game loop calls updateWorld and renderWorld
- Cleans up on exit()
54(No Transcript)
55Summary
- You should be able to take 3Dstep1 and do various
things like - load up a different object
- load up two objects
- play around with the lighting parameters
- create another light
- transform the object around the other axes
- Etc