Title: Advanced Game Technology CMPCD3026 CMPSEM044
1Advanced Game TechnologyCMPCD3026-CMPSEM044
Abdennour El Rhalibi
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
3Camera Setting and AnimationLecture 6
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., - view direction.
- position of camera
- 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) - tilt of view/film plane, focal distance
(blurring)
5View Volumes
- A view volume contains everything visible from
the point of view or direction - It is what does 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 - This is a little abstracteasier formulation?
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 - soon youll learn how to express orientation in
terms of Look and Up vectors - 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
17- Front and Back Clipping Planes (3/3)
- Have you ever played a video game and all of the
sudden some object pops up in the background
(e.g. a tree in a racing game)? Thats the object
coming inside the far clip plane. - The old hack to keep you from noticing the pop-up
is to add fog in the distance. A classic example
of this is from Turok Dinosaur Hunter - Now all you notice is fog and how little you can
actually see. This practically defeats the
purpose of an outdoor environment! And you can
still see pop-up from time to time. - Thanks to fast hardware and LOD algorithms, we
can push the far plane back now and fog is much
less prevalent - Putting the near clip plane as far away as
possible helps Z precision.
18Focal 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
19What This Camera Model Can And Cannot Do
- It can create the following view volumes
- perspective positive view angle
- parallel zero view angle
- Model cannot create oblique view volume
- Non-oblique vs. oblique view volumes
- For example, view cameras with bellows are used
to take pictures of (tall) buildings. The film
plane is parallel to the façade, while the camera
points up. This is an oblique view volume, with
the façade undistorted
Non-oblique view volume
Look vector is perpendicular to film plane
Oblique view volume
Look vector is at an angle to the film plane
20View 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
21Truncated 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
22Truncated 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
23Building a Flexible Camera Class
- We have used the D3DXMatrixLookAtLH function to
compute a view space transformation matrix. - This function is particularly useful for
positioning and aiming a camera in a fixed
position, but its user interface is not so useful
for a moving camera that reacts to user input. - This motivates us to develop a new solution.
- We will discuss how to implement a Camera class
that gives us better control of the camera than
the D3DXMatrixLookAtLH function and is
particularly suitable for flight simulators and
games played from the first person perspective,
and can be adapted to other view.
24Camera Design
- We define the position and orientation of the
camera relative to the world coordinate system
using four camera vectors a right vector, up
vector, look vector, and position vector, as
Figure illustrates. - These vectors essentially define a local
coordinate system for the camera described
relative to the world coordinate system. - Since the right, up, and look vectors define the
cameras orientation in the world, we sometimes
refer to all three as the orientation vectors.
The camera vectors defining the position and
orientation of the camera relative to the world
25Camera Design
- With these four vectors describing the camera, we
would like our camera to be able to perform the
following six operations - Rotate around the right vector (pitch)
- Rotate around the up vector (yaw)
- Rotate around the look vector (roll)
- Strafe along the right vector
- Fly along the up vector
- Move along the look vector
- Through these six operations, we are able to move
along three axes and rotate around three axes,
giving us a flexible six degrees of freedom.
26Camera Design
- The following Camera class definition reflects
our description of data and desired methods - class Camera
- public
- enum CameraType LANDOBJECT, AIRCRAFT
- Camera()
- Camera(CameraType cameraType)
- Camera()
- void strafe(float units) // left/right
- void fly(float units) // up/down
- void walk(float units) // forward/backward
- void pitch(float angle) // rotate on right
vector - void yaw(float angle) // rotate on up vector
- void roll(float angle) // rotate on look vector
- void getViewMatrix(D3DXMATRIX V)
- void setCameraType(CameraType cameraType)
- void getPosition(D3DXVECTOR3 pos)
- void setPosition(D3DXVECTOR3 pos)
- void getRight(D3DXVECTOR3 right)
- void getUp(D3DXVECTOR3 up)
27Computing the View Matrix
- We build this matrix in the CameragetViewMatrix
method - void CameragetViewMatrix(D3DXMATRIX V)
- // Keep camera's axes orthogonal to each other
- D3DXVec3Normalize(_look, _look)
- D3DXVec3Cross(_up, _look, _right)
- D3DXVec3Normalize(_up, _up)
- D3DXVec3Cross(_right, _up, _look)
- D3DXVec3Normalize(_right, _right)
- // Build the view matrix
- float x -D3DXVec3Dot(_right, _pos)
- float y -D3DXVec3Dot(_up, _pos)
- float z -D3DXVec3Dot(_look, _pos)
- (V)(0, 0) _right.x
- (V)(0, 1) _up.x
- (V)(0, 2) _look.x
- (V)(0, 3) 0.0f
- (V)(1, 0) _right.y
- (V)(1, 1) _up.y
- (V)(1, 2) _look.y
28Rotation about an Arbitrary Axis
- To implement our camera rotation methods, we need
to be able to - rotate around an arbitrary axis.
- The D3DX library provides the following function
for just that purpose - D3DXMATRIX D3DXMatrixRotationAxis(
- D3DXMATRIX pOut, // returns rotation matrix
- CONST D3DXVECTOR3 pV, // axis to rotate around
- FLOAT Angle // angle, in radians, to rotate
- )
29Pitch, Yaw, and Roll
- Because the orientation vectors describe the
orientation of the camera relative to the world
coordinate system, we must figure out how we
update them when we pitch, yaw, and roll. - This is actually easy!
- Consider Figures 12.4, 12.5, and 12.6, which show
the camera pitching, yawing, and rolling,
respectively. - We see that when we pitch, we need to rotate the
up and look vectors around the right vector by
the specified rotation angle. - Similarly, we see that when we yaw, we need to
rotate the look and right vectors around the up
vector by the specified rotation angle. - Finally, we see that when we roll, we need to
rotate the up and right vectors around the look
vector by the specified rotation angle.
30Pitch, Yaw, and Roll
- The code for the pitch, yaw, and roll methods is
implemented as - follows
- void Camerapitch(float angle)
-
- D3DXMATRIX T
- D3DXMatrixRotationAxis(T, _right, angle)
- // rotate _up and _look around _right vector
- D3DXVec3TransformCoord(_up,_up, T)
- D3DXVec3TransformCoord(_look,_look, T)
-
- void Camerayaw(float angle)
-
- D3DXMATRIX T
- // rotate around world y (0, 1, 0) always for
land object - if( _cameraType LANDOBJECT )
- D3DXMatrixRotationY(T, angle)
- // rotate around own up vector for aircraft
- if( _cameraType AIRCRAFT )
- D3DXMatrixRotationAxis(T, _up, angle)
- void Cameraroll(float angle)
-
- // only roll for aircraft type
- if( _cameraType AIRCRAFT )
-
- D3DXMATRIX T
- D3DXMatrixRotationAxis(T, _look, angle)
- // rotate _up and _right around _look vector
- D3DXVec3TransformCoord(_right,_right, T)
- D3DXVec3TransformCoord(_up,_up, T)
-
-
31Walking, Strafing, and Flying
- When we refer to walking, we mean moving in the
direction that we are looking (that is, along the
look vector). - Strafing is moving side to side from the
direction we are looking, which is of course
moving along the right vector. - Finally, we say that flying is moving along the
up vector. - To move along any of these axes, we simply add a
vector that points in the same direction as the
axis that we want to move along to our position
vector
32Walking, Strafing, and Flying
- The code that implements the walk, strafe, and
fly methods follows - void Camerawalk(float units)
-
- // move only on xz plane for land object
- if( _cameraType LANDOBJECT )
- _pos D3DXVECTOR3(_look.x, 0.0f, _look.z)
units - if( _cameraType AIRCRAFT )
- _pos _look units
-
- void Camerastrafe(float units)
-
- // move only on xz plane for land object
- if( _cameraType LANDOBJECT )
- _pos D3DXVECTOR3(_right.x, 0.0f, _right.z)
units - if( _cameraType AIRCRAFT )
- _pos _right units
-
33Sample Application Camera
- The sample program creates and renders the scene
shown - You are free to fly around this scene using input
from the keyboard. - The following keys are implemented
- W/SWalk forward/backward
- A/DStrafe left/right
- R/FFly up/down
- Up/Down arrow keysPitch
- Left/Right arrow keysYaw
- N/MRoll
34Sample Application Camera
- The implementation of the sample is trivial,
since all the work is inside - the Camera class. We handle user input in the
Display function accordingly. Notice that we move
the camera with respect to the time change
(timeDelta) this keeps us moving at a steady
speed independent of the frame rate. - bool Display(float timeDelta)
-
- if( Device )
-
- // Update Update the camera.
- //
- if( GetAsyncKeyState('W') 0x8000f )
- TheCamera.walk(4.0f timeDelta)
- if( GetAsyncKeyState('S') 0x8000f )
- TheCamera.walk(-4.0f timeDelta)
- if( GetAsyncKeyState('A') 0x8000f )
- TheCamera.strafe(-4.0f timeDelta)
- if( GetAsyncKeyState('D') 0x8000f )
- TheCamera.strafe(4.0f timeDelta)
- if( GetAsyncKeyState('R') 0x8000f )
- TheCamera.fly(4.0f timeDelta)
- if( GetAsyncKeyState(VK_LEFT) 0x8000f )
- TheCamera.yaw(-1.0f timeDelta)
- if( GetAsyncKeyState(VK_RIGHT) 0x8000f )
- TheCamera.yaw(1.0f timeDelta)
- if( GetAsyncKeyState('N') 0x8000f )
- TheCamera.roll(1.0f timeDelta)
- if( GetAsyncKeyState('M') 0x8000f )
- TheCamera.roll(-1.0f timeDelta)
- // Update the view matrix representing cameras
- // new position/orientation.
- D3DXMATRIX V
- TheCamera.getViewMatrix(V)
- Device-gtSetTransform(D3DTS_VIEW, V)
- //
- // Render
- Device-gtClear(0, 0,
- D3DCLEAR_TARGET D3DCLEAR_ZBUFFER,
- 0x00000000, 1.0f, 0)
- Device-gtBeginScene()
35Summary
- We describe the position and orientation of our
camera in the world coordinate system by
maintaining four vectors a right vector, an up
vector, a look vector, and a position vector. - With this description, we can easily implement a
camera with six degrees of freedom, giving a
flexible camera interface that works well for
flight simulators and games played from the
first-person perspective.