Title: Advanced Game Technology CMPCD3026 CMPSEM044
1Advanced Game TechnologyCMPCD3026-CMPSEM044
Abdennour El Rhalibi Room 723 a.elrhalibi_at_ljmu.ac.
uk
2Course Details (Attempt)
- 3D Game Engines Components
- DirectX D3D, 3D Modelling and Rendering
- Meshes, Level Loading and Editing
- Camera Setting and Animation
- Terrain Rendering and LOD
- 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
3Camera Setting and AnimationLecture 4
43D Viewing the Synthetic Camera
- Programmers reference model for specifying 3D
view projection parameters to the computer - General synthetic camera each API has its own
but they are all (nearly) equivalent. Many ways
to specify camera parameters, e.g., - position of camera
- view direction.
- orientation
- field of view (wide angle, normal)
- depth of field (near distance, far distance)
- focal distance
- tilt of view/film plane (if not normal to view
direction, produces oblique projections) - perspective or parallel projection? (camera near
objects or an infinite distance away)
5View Volumes
- A view volume contains everything visible from
the point of view or direction - It is what the camera see.
- Conical view volumes
- approximates what eye sees
- expensive math (simultaneous quadratics) when
clipping objects against cones surface - Can approximate with rectangular cone instead
(called a frustum) - works well with a rectangular viewing window
- simultaneous linear equations for easy clipping
of objects against sides
eye
synthetic camera
6Conceptual Model of 3D Viewing Process (for
wireframe)
- Viewport is rectangular area of the screen where
a scene is rendered - this may or may not fill Window Managers window
- note window in computer graphics often means a
2D clip rectangle on a 2D world coordinate
drawing, and viewport is the 2D integer
coordinate region of screen space to which the
clipped window contents are mapped.
Window/viewport terminology considerably predates
Window Manager terminology - Viewport and film plane may have different aspect
ratios - viewport mapping specifies what to do if aspect
ratios differ
7View Volume (1/2)
- We need to know six things about our synthetic
camera model in order to take a picture - Position of the camera (from where its looking)
- The Look vector specifies in what direction the
camera is pointing - The cameras Orientation is determined by the
Look vector and the angle through which the
camera is rotated about that vector, i.e., the
direction of the Up vector
8View Volume (2/2)
- Aspect ratio of the electronic film ratio of
width to height - Height angle determines how much of the scene we
will fit into our view volume - larger height angles fit more of the scene into
the view volume (width angle determined by height
angle and aspect ratio) - the greater the angle, the greater the amount of
perspective distortion - Front and back clipping planes limit extent of
cameras view by rendering (parts of) objects
lying between them and throwing away everything
outside of them - Optional parameter Focal length often used for
photorealistic rendering objects at distance
Focal length from camera rendered in sharp
detail, objects closer or farther away get
blurred reduction in visibility is continuous - DX camera dont be implementing focal length
blurring
9Position
- Determining the Position is analogous to a
photographer deciding the vantage point from
which to shoot a photo - Three degrees of freedom x, y, and z coordinates
in 3-space - This x, y, z coordinate system is right-handed or
left-handed
10Orientation
- Orientation is specified by a point in 3D space
to look at (or a direction to look in) and an
angle of rotation about this direction - Default (canonical) orientation is looking down
the negative z-axis and up direction pointing
straight up the y-axis - In general the camera is located at the origin
and is looking at an arbitrary point with an
arbitrary up direction
y
x
Up vector
-z
point to look at
(x, y, z)
Look vector
camera Position
z
11Look and Up Vectors
- More concrete way to say the same thing as
orientation - Look Vector
- the direction the camera is pointing
- three degrees of freedom can be any vector in
3D-space - Up Vector
- determines how the camera is rotated around the
Look vector - for example, whether youre holding the camera
horizontally or vertically (or in between) - projection of Up vector must be in the plane
perpendicular to the look vector (this allows Up
vector to be specified at an arbitrary angle to
its Look vector)
Up vector
projection of Up vector
12Aspect Ratio
- Analogous to the size of film used in a camera
- Determines proportion of width to height of image
displayed on screen - Square viewing window has aspect ratio of 11
- Movie theater letterbox format has aspect ratio
of 21 - NTSC television has an aspect ratio of 43, and
HDTV is 169
13View Angle (1/2)
- Determines amount of perspective distortion in
picture, from none (parallel projection) to a lot
(wide-angle lens) - In a frustum, two viewing angles
- width and height angles.
- We specify Height angle, and get the Width angle
from (Width Aspect ratio Height) - Choosing View angle analogous to photographer
choosing a specific type of lens (e.g., a
wide-angle or telephoto lens)
14View Angle (2/2)
- Lenses made for distance shots often have a
nearly parallel viewing angle and cause little
perspective distortion, though they foreshorten
depth - Wide-angle lenses cause a lot of perspective
distortion
Resulting pictures
15Front and Back Clipping Planes (1/3)
- Volume of space between Front and Back clipping
planes defines what camera can see - Position of planes defined by distance along Look
vector - Objects appearing outside of view volume dont
get drawn - Objects intersecting view volume get clipped
16Front and Back Clipping Planes (2/3)
- Reasons for Front (near) clipping plane
- Dont want to draw things too close to the camera
- would block view of rest of scene
- objects would be prone to distortion
- Dont want to draw things behind camera
- wouldnt expect to see things behind the camera
- in the case of the perspective camera, if we
decided to draw things behind the camera, they
would appear upside-down and inside-out because
of perspective transformation - Reasons for Back (far) clipping plane
- Dont want to draw objects too far away from
camera - distant objects may appear too small to be
visually significant, but still take long time to
render - by discarding them we lose a small amount of
detail but reclaim a lot of rendering time - the scene may be filled with many significant
objects for visual clarity, we may wish to
declutter the scene by rendering those nearest
the camera and discarding the rest
17Focal Length
- Some camera models take a Focal length
- Focal Length is a measure of ideal focusing
range approximates behavior of real camera
lens - Objects at distance of Focal length from camera
are rendered in focus objects closer or farther
away than Focal length get blurred - Focal length used in conjunction with clipping
planes - Only objects within view volume are rendered,
whether blurred or not. Objects outside of view
volume still get discarded
18View Volume Specification
- From Position, Look vector, Up vector, Aspect
ratio, Height angle, Clipping planes, and
(optionally) Focal length together specify a
truncated view volume - Truncated view volume is a specification of
bounded space that camera can see - 2D view of 3D scene can be computed from
truncated view volume and projected onto film
plane - Truncated view volumes come in two flavors
parallel and perspective
19Truncated View Volume forOrthographic Parallel
Projection
- Limiting view volume useful for eliminating
extraneous objects - Orthographic parallel projection has width and
height view angles of zero
Width
Far distance
Height
Look vector
Near distance
Up vector
Position
20Truncated View Volume (Frustum) for Perspective
Projection
- Removes objects too far from Position, which
otherwise would merge into blobs - Removes objects too close to Position (would be
excessively distorted)
Width angle
Height angle Aspect ratio
Up vector
Height angle
Position
Near distance
Far distance
21Game Engine 3DLecture 5
22GameEngine 3D
- Demo 0 has two functions
- Getting you started on coding with GameEngine
- Providing a tool that artists and programmers can
use to check S3D models for compatibility and
correctness
23Introduction
- Heres what well cover in this lecture
- Introduction to the GameEngine foundation code
- Using the foundation code to develop a model
viewer application
24High Level Overview
- The book 3D Math Primer for Graphics and Game
Development provides a code utility library - Common files are in a folder called common
- Utilities are provided for
- the standard math tools
- code for managing
- Windows
- DirectX
- includes a dialog box for displaying error
messages on exit
25High Level Overview 2
- More utilities
- a renderer
- primitive input handlers for mouse keyboard
- a model class, including importer for the S3D
model file format
26(No Transcript)
27(No Transcript)
28Files in Common
- AABB3.cpp, AABB3.h Axially aligned bounding
boxes (Section 12.4) - Bitmap.cpp, Bitmap.h Simple class to hold a
bitmap image - Camera.cpp, Camera.h Camera class
- CommonStuff.h, CommonStuff.cpp Common stuff that
doesnt belong elsewhere - EditTriMesh.cpp, EditTriMesh.h Editable triangle
mesh class (Section 14.5)
29More Files in Common
- EulerAngles.cpp, EulerAngles.h Euler angle class
(Section 10.3, 11.2) - FontCacheEntry.h, FontCacheEntry.cpp
- Font class
- MathUtil.cpp, MathUtil.h Basic math utilities
- Matrix4x3.cpp, Matrix4x3.h Homogenous
transformation matrix code (Section 11.5)
30Even More Files in Common
- Model.cpp, Model.h Simple class for a 3D model
- Quaternion.cpp, Quaternion.h Quaternion class
(Section 10.4, 11.3) - Random.cpp, Random.h, Random2.cpp, Random2.h
Random Number Generator classes - Renderer.cpp, Renderer.h Code for the rendering
engine - RotationMatrix.cpp, RotationMatrix.h Rotation
matrix class (Section 11.4)
31More Files in Common
- TextureCacheEntry.cpp, TextureCacheEntry.hTexture
management classes - TriMesh.cpp, TriMesh.h Triangle mesh class
(Chapter 14) - Vector2.h, vector3.h 2D and 3D vector classes
(Chapter 6)
32Your Main Program
- You need to provide function mainProgram()
- This function should consist of
- initialization
- while (!gQuitFlag) // frame loop
- move objects
- render objects
- process input
-
- shutdown
33Initialization
- Create the application window
- createAppWindow(My Game")
- Select the video mode to mode
- Initialize the renderer (second parameter is
whether windowed) - gRenderer.init(mode, true)
34Initialization 2
- Your initialization, eg. Reading in models
- Model model
- model.importS3d(Model.s3d)
- model.cache()
35Move Objects
- In vector parlance add to the location vector
the velocity vector times elapsed time - Similar for orientation
- Use the frame rate timer provided by the
renderer - float gRenderer.getTimeStep()
36Render Objects
- Set up the camera
- gRenderer.setCamera(cameraLoc, cameraOrient)
- gRenderer.setZoom(
- fovToZoom(degToRad(dFov)))
- Prepare the renderer
- gRenderer.beginScene()
- gRenderer.clear()
- Set the scene (e.g. lighting)
37Render Objects 2
- Render the objects using an instance stack
- gRenderer.instance(kZeroVector, modelOrient)
- model.render()
- gRenderer.instancePop()
- Show the result
- gRenderer.endScene()
- gRenderer.flipPages()
38Process Input
- Process input at the end of each frame.
- Support provided in Demo 0 is limited to
wrappering the Windows API input.
39Shutdown
- Shut down the renderer
- gRenderer.shutdown()
- Destroy the application window
- destroyAppWindow()
40Making it Into a Game Engine
- GameEngine will add code for some of the features
needed to make a game, for example - Terrain generation
- Object manager
- DirectInput
- Particle engine
- Game shell
- Sound manager
- Other things that might be added
- Artificial intelligence
- Physics
- Networking
41Key Topics
- Renderer
- Camera
- Reference Frames
- Textures
- Render States
- Models
- GameBase
- Resources
- Input
- DirectoryManager
42Renderer Overview
- The render code has the following
responsibilities - Initializing the scene
- Drawing everything to the screen
- Maintaining game data
- Validating devices
- Cleaning up memory
- Maintaining the camera
- Restoring a scene
- Managing the window
43Renderer Functions
- init() Initializes the renderer
- shutdown() Cleans up the renderer before
shutdown. - beginScene() Called at the start of a frame
- Checks to make sure we still have the device
- endScene() Called at the end of a frame
- Checks again to make sure we still have the
device
44Function Overview Cont.
- flipPages() Updates the screen
- validateDevice() Make sure you can still draw
- restoreRenderStates() Restores render states to
their default values
45Rendererinit()
- Initializes the renderer with the following
parameters - VideoMode The video mode attributes
- xRes The horizontal resolution of the screen
- yRes The vertical resoulution of the screen
- bitsPerPixel 16, 24, or 32-bit color
- shaderDebug True if you want shader debugging
turning on - windowed True if you want the game to run in
windowed
46RendererflipPages()
- Check to make sure we still have the device
- Update the screen if we have the device
- Update the timing
if (pD3DDevice ! NULL) HRESULT result
pD3DDevice-gtPresent(NULL,NULL,NULL,NULL)
47RenderervalidateDevice()
- validateDevice() performs the following functions
- Test if device is valid
- Checks if device is lost, and if so loops until
it returns.
HRESULT hr pD3DDevice-gtTestCooperativeLevel()
if (hr D3DERR_DEVICELOST) while (1)
// let windows process
gWindowsWrapper.idle() // check
device to see if it is ready hr
pD3DDevice-gtTestCooperativeLevel() if (hr
D3DERR_DEVICENOTRESET) break
48RenderervalidateDevice() Cont.
- Restores non-managed resources
// device is ready! restore all non managed
resources gResourceManager.releaseAll() //
release pointer to buffers b/c these are not in
the resource manager if (pOriginalBackBuffer)
pOriginalBackBuffer-gtRelease() if
(pOriginalDepthStencil) pOriginalDepthStencil-
gtRelease() pD3DDevice-gtReset(presentParms)
// get pointers to depth buffer and back
buffer pD3DDevice-gtGetRenderTarget(0,
pOriginalBackBuffer) pD3DDevice-gtGetDepthStenc
ilSurface(pOriginalDepthStencil)
gResourceManager.restoreAll()
restoreRenderStates()
49RendererrestoreRenderStates()
- Resets render states to default values
setD3DRenderState(D3DRS_ZENABLE, zEnable)
setD3DRenderState(D3DRS_ZWRITEENABLE, TRUE)
setD3DRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL)
setD3DRenderState(D3DRS_ALPHABLENDENABLE,
blendEnable) setD3DRenderState(D3DRS_SRCBLEND,
D3DBLEND_SRCALPHA) setD3DRenderState(D3DRS_DEST
BLEND, D3DBLEND_INVSRCALPHA)
setD3DRenderState(D3DRS_AMBIENT,
ambientLightColor) setD3DRenderState(D3DRS_FOGE
NABLE, fogEnable) setD3DRenderState(D3DRS_FOGCO
LOR, fogColor) setD3DRenderState(D3DRS_FOGTABLE
MODE, D3DFOG_LINEAR) setD3DRenderState(D3DRS_RA
NGEFOGENABLE, TRUE) setD3DRenderState(D3DRS_FOG
START, (DWORD)(fogNear)) setD3DRenderState(D
3DRS_FOGEND, (DWORD)(fogFar))
setBackfaceMode(backfaceMode)
setWireframe(wireframeOn)
50RendererrestoreRenderStates() Cont.
- Reset sample states to their default values
- Reset Material properties to their default values
- Reset Lights to their default values
setD3DSamplerState(D3DSAMP_MINFILTER,
D3DTEXF_ANISOTROPIC) setD3DSamplerState(D3DSAMP_
MAGFILTER, D3DTEXF_LINEAR) setD3DSamplerState(D3
DSAMP_MIPFILTER, D3DTEXF_LINEAR) // Set texture
wrapping mode setD3DSamplerState(D3DSAMP_ADDRESSU
, D3DTADDRESS_WRAP ) setD3DSamplerState(D3DSAMP
_ADDRESSV , D3DTADDRESS_WRAP )
51Camera
- The camera provides your view of the scene
- The renderer handles camera management, and gives
you access to the camera - Camera Attributes
- Position
- Orientation
- Zoom
- Clipping Planes
52Clipping Planes
- Creates boundaries for visible objects
- Near Clipping Plane
- Objects closer to the camera then the near
clipping plane are culled - Far Clipping Plane
- Object farther away from the camera then the far
clipping plane are culled
53Reference Frames
- How the camera views a scene
- Local Space
- The coordinate space in which an object is first
defined - World Space
- The space where all objects in a scene are
displayed - Object Space
- The coordinate space in which an object is defined
54Reference Frame Implementation
- Matrix representation
- Using a stack for reference frames
- For the model viewer we only use the reference
frame stack to push the current reference frame
onto before changing it so that we can pop it off
when we are done and leave it unchanged.
55Textures
- The Renderer manages textures using a texture
cache - This loads the texture into memory
// get a handle to the texture int txtHndl
gRenderer.cacheTexture("myImage.tga", true) //
... processing code // set the texture
myImage.tga to be the current texture gRenderer.se
lectTexture(txtHndl) // draw model
56Render States Overview
- The Fixed-Function Pipeline
- Depth Buffer
- Fog
- Wireframe
- Lighting
- Back-Face Culling
57Depth Buffer
- Z-Culling
- Culls objects behind other objects based on their
z-axis position - Transparency
- Typically you have to disable Z-Culling when
drawing transparent objects, and then draw them
in z-order from back to front
58Fog
- Obscuring the far clipping plane
- Using fog gradually decreases visibility of
objects as they get farther away. This keeps
them from just disappearing once they reach the
far clipping plane. - Fog near plane
- Objects closer to the camera the near fog plane
dont get obscured - Linear Interpolation
- Fog in demo is calculated using linear
interpolation. This means the thickness of the
fog is directly proportional to the distance from
the camera.
59Wireframe Mode
setWireFrame(true)
setWireFrame(false)
60Lighting
- Ambient Light
- General level of light that affects everything in
the scene the same - Directional Light
- Light with a location and direction.
- Spot Light
- Surface Normals
- Materials
61Back-Face Culling
- Winding
- CullMode
- Clockwise
- Polygons defined in a counter-clockwise order are
not drawn - Counter-Clockwise
- Polygons define in a clockwise order are not
drawn - None
- All polygons are drawn regardless of their order.
62Render Functions
- DrawPrimitiveUP
- Renders data as a sequence of geometric
primitives without using vertex buffers - DrawPrimitive
- Renders a sequence of non-indexed, geometric
primitives from a vertex buffer - DrawIndexedPrimitive
- Renders an indexed geometric primitive using a
vertex buffer and an index buffer
63Models Overview
- Initialization
- Importing Models
- Rendering Models
- Bounding Boxes
64Initialization
- BufferUsage Enumeration
- NoBuffers
- Dont buffer the models geometry
- StaticIndexBuffer
- Buffer the models geometry and use an index
buffer - StaticBuffers(default)
- Buffer the models geometry without an index buffer
65Importing Models Modelimports3d()
- Loads the s3d model from a file in three steps
- 1. Load the model data from a file
- 2. Optimize the mesh for rendering
- 3. Convert it into a renderable format
EditTriMesh editMesh editMesh.importS3d(s3dFilena
me, text, defaultDirectory)
editMesh.optimizeForRendering()
fromEditMesh(editMesh)
66Rendering Models
- render()
- Goes through every part of the model and calls
renderPart() - renderPart()
- Selects the texture for this part of the model
and renders its geometry using that texture
gRenderer.selectTexture(m_partTextureListindex)
gRenderer.render( m_vertexBuffer, 0,
m_partMeshListindex.getVertexCount(),
m_indexBuffer, m_indexOffsetsindex,
m_partMeshListindex.getTriCount())
67Bounding Box
- AABB
- Axially-Aligning Bounding Box
- Used in collision detection
- getBoundingBox(Matrix)
- Retrieves the AABB for a model
68GameBase
- Base class for game
- The game will inherit this class and implement
the methods it needs - Functions
- initiate()
- Initialize the game
- main()
- The main loop in the game.
- Called be window wrapper each frame
- shutdown()
- Clean up memory and exit program
69Resources
- Resources are allocated blocks of memory
- To maintain un-managed resources that need to be
restored if the game loses focus - Dynamic Resources
- Resources that change frequently
- These are usually the resources that you want to
manage yourself - Static Resources
- Resources that seldom change
- These resources are typically set up to be
managed by DirectX
70Input
- InputManager
- The input manager handles the input from devices
such as your keyboard, mouse, or joystick - Uses DirectInput to maintain input
- Basic functionality will tell you
- If a key is down
- InputManagerkeyDown(keyCode, override)
- If a key has just gone down (It was up last
frame) - InputManagerkeyJustDown(keyCode, override)
- If a key as just gone up (It was down last frame)
- InputManagerkeyJustUp(keyCode, override)
- The override is a boolean that is true if you
want to ignore the m_keyBoardOn flag
71DirectoryManager
- Default Directories
- You can add default directories to the directory
manager for use by your program - Adding a directory
- Add a line to the directories.xml
- Add an enumerated value to the EDirectory type in
the DirectoryManager.h file. - Add a condition statement to the function
DirectoryManagergetResourceIndex() that tests
for the xml tag name you used in step
72Add a line to the directories.xml
lt?xml version"1.0" encoding"utf-8"?gt ltdirectori
esgt ltmodels path"\Models"/gt lttextures
path"\Textures"/gt ltxmls path"\XML"/gt
ltotherstuff path\Stuff /gt lt/directoriesgt
73Add an enumerated value to the EDirectory type
enum EDirectory eDirectoryTextures, ///lt Any
texture for the game eDirectoryModels, ///lt
Any model for the game eDirectoryXML, ///lt Any
XML for the game eDirectoryStuff, ///lt Any
thing I want to be here eDirectoryMax // make
sure this is last
74Add a statement to the function
DirectoryManagergetResourceIndex()
EDirectory DirectoryManagergetResourceIndex(
stdstring resourceName) int index
-1 if (resourceName "textures") index
eDirectoryTextures else if (resourceName
"models") index eDirectoryModels else if
(resourceName "xmls") index
eDirectoryXML else if (resourceName
otherstuff) index eDirectoryStuff return
(EDirectory)index