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 Primitives
- Meshes
- D3D Data Structures
- Vertex Buffers (VB)
- Flexible Vertex Format (FVF)
- Loading and Editing
- Meshes
- Setting World and View
- Transforming
5Meshes
- 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.
6Meshes
- 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
7Types of Primitives
8Vertex 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 - 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
9Flexible 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)
103DStep1 Example
- The following example 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 - The Example use DXU library which provides high
level D3D functions See DirectX 9.c
documentation for information and examples in L
drive.
11Modelling and Loading
12Rendering modes
- Can be set to solid, wire-frame or point
13Initialise 3D world
- Any 3D world must have . . .
- Some sort of camera and viewing system
- Lighting and rendering settings
- Objects and ways of transforming them
14Initialise 3D world- The matrices
- To initialise the 3D world in D3D we have to
specify three 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.
15Initialise 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)
16D3D 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
17So 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
18Turn on depth buffering
- Two lines of code are needed to turn z buffering
on using the DXU library
19Back 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
20Back face culling
- None
- Clockwise
- Anticlockwise
21Set Rendering (fill) and cull modes
- One function call each . . .
22Setting 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
23Setting 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
24Effect of changing field of view
25The world matrix
- Transformations of the world matrix will
transform all items in the world - Set to identity matrix - no transformation
26Putting it all together - Initialise3DWorld
27Camera 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
28Lights
- 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
29Ambient light
- Background light, is modelled as a constant
- With ambient light alone, all surfaces have equal
brightness
30Putting it together - the InitialiseLights
function
31Dealing 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
32Dealing with 3D objects - code
33Transforming 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
34D3DXMatrixRotationY() 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)
35Putting it together - the UpdateWorld () function
363DStep1 - 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
373DStep1- LoadWorld()
- This loads the car mesh, a font for writing, and
initialises the 3D world and lighting
383DStep1- RenderWorld()
- Draws the mesh and string
393DStep1- Release World
403DStep1- WinMain()
- The Winmain() function does the D3D, windows, and
input initialisation - It calls LoadWorld() to initialise the world
- Game loop calls updateWorld and renderWorld
- Cleans up on exit()
41(No Transcript)
42GAME 3D - ENGINE
43Model Viewer
- The purpose of this example is to provide an
introduction to the engine functionalities. - This example does not use the full engine, only
the components needed to load and view a model in
3D (S3D format), but all the components to
develop a game will be provided. - This presentation will cover the code and
concepts needed to make this example.
44Key Topics
- The following areas will be investigated
- Controls
- Renderer
- Models
- GameBase
- Resources
- Input
- DirectoryManager
45Summary
- You should be able to take any of the examples
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