Starting 3D Game Programming - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Starting 3D Game Programming

Description:

Device rendering settings - fill and backface culling modes, depth (z) buffering ... Back face culling is used to remove surfaces that should be facing away from the ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 42
Provided by: abdennour8
Category:

less

Transcript and Presenter's Notes

Title: Starting 3D Game Programming


1
Starting 3D Game Programming
2
3DStep1 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

3
Initialise 3D world
  • Any 3D world must have . . .
  • Some sort of camera and viewing system
  • Lighting and rendering settings
  • Objects and ways of transforming them

4
Initialise 3D world
  • To initialise the 3D world in D3D we have to
    specify three matrices

5
The 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.

6
Initialise 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)

7
D3D 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

8
So 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

9
Turn on depth buffering
  • Two lines of code are needed to turn z buffering
    on using the DXU library

10
Rendering modes
  • Can be set to solid, wire-frame or point

11
Back 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

12
Back face culling
  • None
  • Clockwise
  • Anticlockwise

13
Set Rendering (fill) and cull modes
  • One function call each . . .

14
Setting 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

15
Setting 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

16
Clipping planes
17
Effect of changing field of view
18
The world matrix
  • Transformations of the world matrix will
    transform all items in the world
  • Set to identity matrix - no transformation

19
Putting it all together - Initialise3DWorld
20
Camera 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

21
Lights
  • 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

22
Point lights
  • Point lights have colour and position in space
  • They give off light equally in all directions

23
Spotlights
  • 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.

24
Directional lights
  • Directional lights have only colour and
    direction, not position.

25
Ambient light
  • Background light, is modelled as a constant
  • With ambient light alone, all surfaces have equal
    brightness

26
Putting it together - the InitialiseLights
function
27
Dealing 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

28
Dealing with 3D objects - code
29
Transforming 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

30
D3DXMatrixRotationY() 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)

31
Putting it together - the UpdateWorld () function
32
3DStep1 - 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

33
3DStep1- LoadWorld()
  • This loads the car mesh, a font for writing, and
    initialises the 3D world and lighting

34
3DStep1- RenderWorld()
  • Draws the mesh and string

35
3DStep1- Release World
  • Release resources

36
3DStep1- 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)
39
An object in its starting position in D3D
40
Your object rotated 180 degrees round Y axis in
D3D
41
Summary
  • 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
Write a Comment
User Comments (0)
About PowerShow.com