Title: Starting 3D Game Programming
1Starting 3D Game Programming
23DStep1 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
3Initialise 3D world
- Any 3D world must have . . .
- Some sort of camera and viewing system
- Lighting and rendering settings
- Objects and ways of transforming them
4Initialise 3D world
- To initialise the 3D world in D3D we have to
specify three matrices
5The 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.
6Initialise 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)
7D3D 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
8So 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
9Turn on depth buffering
- Two lines of code are needed to turn z buffering
on using the DXU library
10Rendering modes
- Can be set to solid, wire-frame or point
11Back 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
12Back face culling
- None
- Clockwise
- Anticlockwise
13Set Rendering (fill) and cull modes
- One function call each . . .
14Setting 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
15Setting 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
16Clipping planes
17Effect of changing field of view
18The world matrix
- Transformations of the world matrix will
transform all items in the world - Set to identity matrix - no transformation
19Putting it all together - Initialise3DWorld
20Camera 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
21Lights
- 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
22Point lights
- Point lights have colour and position in space
- They give off light equally in all directions
23Spotlights
- 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.
24Directional lights
- Directional lights have only colour and
direction, not position.
25Ambient light
- Background light, is modelled as a constant
- With ambient light alone, all surfaces have equal
brightness
26Putting it together - the InitialiseLights
function
27Dealing 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
28Dealing with 3D objects - code
29Transforming 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
30D3DXMatrixRotationY() 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)
31Putting it together - the UpdateWorld () function
323DStep1 - 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
333DStep1- LoadWorld()
- This loads the car mesh, a font for writing, and
initialises the 3D world and lighting
343DStep1- RenderWorld()
- Draws the mesh and string
353DStep1- Release World
363DStep1- 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()
37(No Transcript)
38(No Transcript)
39An object in its starting position in D3D
40Your object rotated 180 degrees round Y axis in
D3D
41Summary
- 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