Title: Cameras and Viewing
1Cameras and Viewing
- Perspective Camera Model
- Lookat
- Spaces Model, World, View
- Camera implementations
- Projection
- Homogenation
- Window space
Pages 43-66 in the textbook. Viewing
demonstration program is online.
2Cameras in Graphics Systems
3Camera Configuration
What does it take to describe a camera?
4Specifying a Camera
- Positioning
- Location
- Which direction it is pointing
- Any roll
- Configuration
- Field of view
- Aspect ratio
5Positioning
Up vector
Eye location
- Lookat parameters
- Eye location
- Center location
- Up vector
Center location
The eye location is the location of the camera in
space. The center is the point the camera is
looking at (center of window). The up vector is
up as far as the camera is concerned.
6XNA
Up vector
Eye location
Center location
Matrix view Matrix.CreateLookAt(new
Vector3(3000, 3000, 3000), // eye
new Vector3(0, 0, 0), //
center
new Vector3(0, 1, 0))
// up
7Configuration
Far clipping plane
Near clipping plane
Field of View (angle)
h
w
Nothing is rendered that is closer than the near
clipping plane or farther than the far clipping
plane. Aspect ratio w/h
8XNA
Matrix projection
Matrix.CreatePerspectiveFieldOfView(MathHelper.ToR
adians(35), // Field of view
graphics.GraphicsDevice.Viewport.AspectRatio,
// Aspect ratio 10,
// Near
clip distance 10000)
// Far clip distance
Dont make the clip range larger than necessary.
This range is quantized into 32 bit values that
tell the system how far from the camera something
is. If you make the range too large, it may not
be able to tell something is in front of
something else.
9Matrices
At any time you will have three important
matrices.
- World Matrix
- Transforms points on a model to world coordinates
- View Matrix
- Transforms points in world coordinates to camera
coordinates
Projection Matrix Transforms points in camera
coordinates to window coordinates
10Coordinate Systems
- Object Coordinates
- Coordinates as supplied to you
- The coordinate system the object was created in
- World Coordinates
- A coordinate system for your virtual world
Eye or Camera Coordinates A coordinate system for
the camera
Also called spaces, coordinate spaces, or
frames
11Coordinate Systems and Matrices
- World Matrix
- Converts object coordinates to world coordinates
- This will be specific to the object
- View Matrix
- Converts world coordinates to camera coordinates
- This will be specific to the camera
Projection Matrix Converts camera coordinates to
window coordinates This will be specific to the
system
12Projection
- Perspective Projection
- How big something is depends on how far it is
from the camera. - Something twice as far is twice as big.
Basic projection equation xx / -z, then some
scaling.
13But
- Aint no way
- Theres no division in matrix multiplication
14Projection Matrices
- After this we divide the terms by w
- Heres where we do the division
- This is called homogenizing
15Projection in XNA
- Parameters
- a Aspect ratio
- fov Field of view
- n Near clip plane distance
- f Far clip plane distance
Projected x and y values are in the range -1 to
1 Projected z value is in the range 0 to 1
TT
16Plot of the Z equation
17Window to Viewport
(1,1)
(-1,-1)
Viewport is a region of the screen to draw on (in
pixels). The Window to Viewport transformation
scales the image to the pixel dimensions
18Frustums
- The region we can see is called the frustum
(right,top,-znear)
(0,0,0)
-zfar
znear, zfar are positive
(left,bottom,-znear)
Matrix.CreatePerspectiveOffCenter directly
accepts the frustum values
19fov to near frustum
(x,y,-znear)
-z
TT
20Models
26,907 Vertices 52,896 Triangles
Models are made up of triangles. The corners of
the triangles are called vertices. Vertices are
shared among triangles.
21The Entire Viewing Process
- Convert object coordinates to world coordinates
- Multiply vertices by world matrix
- Convert world coordinates to eye coordinates
- Multiply by view matrix
- Apply perspective projection matrix
- Multiply by projection matrix
- Then theres some other stuff (clipping,
homogenize, etc.) - Viewport transformation
- Convert to actual screen pixels
22Within OpenGL
glBegin(GL_POLYGON) glVertex3dv(a)
glVertex3dv(b) glVertex3dv( c)glEnd()
ObjectCoordinates
ModelviewMatrix
Eye coordinates
ProjectionMatrix
Clip coordinates
Homogenize
Normalized device coordinates
Window toViewport
Viewportcoordinates
23Within XNA
ObjectCoordinates
World coordinates
WorldMatrix
Eye coordinates
Clip coordinates
Homogenize
Normalized device coordinates
Window toViewport
Viewportcoordinates
24What are we doing this to?
25All XNA knows is how to draw triangles
Each triangle has three vertices, the corners.
To draw a triangle, each vertex is multiplied by
the world, view, and projection matrices, then
transformed to screen coordinates. When the
graphics card has three of them, it draws the
triangle. Well spend plenty more time on this
process, since well write code to actually do
this.
26Camera Fun
What all might we need to do in a game with the
camera?
27Common Camera Types and Activities
- Target cameras vs. Free cameras
- Pan/Tilt
- Dolly (move six different ways)
- Roll
- Zoom
- Follow a character
- First person viewpoint
- Chase camera
28Camera types
- Target Camera
- Eye, up, center
- Camera points at some fixed location
- Free Camera
- Eye, camera orientation
- Camera direction is specified directly
29Some Linear Algebra we will need!
- Multiplying Matrices together
- Should be able to do this in your sleep
- XNA M1 M2
- Other Basics
- What a matrix inverse is
- XNA M2 Matrix.Invert(M1)
- What matrix transposition is
- XNA M2Matrix.Transpose(M1)
30Some other basics
p1
- Distance between two points
- XNA float d(p2-p1).Length()
p2
- Magnitude of a vector
- XNA float mv.Length()
Normalized vector XNA v.Normalize()
-or- Vector2 v2 Vector3.Normalize(v)
TT
31Critical Concepts
- Orthogonal
- At right angles to each other
cos-1(dot(v,w))
- Dot product
- Dot product of two normalized vectors is the
cosine of the angle between the vectors - Dot product of two orthogonal vectors is zero
XNA Vector3 d Vector3.Dot(v, w)
32Cross Product
- Cross Product of two vectors is a vector
orthogonal to the two - CVxW
- Stuff
- Cross product of normalized vectors is not
necessarily normalized - XNA Vector3 dVector3.Cross(v,w)
The cross product is one of the most powerful
tools in computer graphics!
TT
33Camera Tilt
- Tilt up is a rotate around the cameras X axis
- What do we need to do this?
34Camera Tilt
- Tilt up is a rotate around the cameras X axis
But, we dont know Y. Do we know Z
- We need to know the vector X in world coordinates
35Camera Tilt
- Tilt up is a rotate around the cameras X axis
- We need to know the vector X in world coordinates
Now rotate around the Z axis and the camera
center. But, what do we rotate?
TT
36What do we rotate?
- Three parameters define the camera
- Eye, Center, Up
Which of these do we change to tilt the camera?
37Implementation
Vector3 cameraZ eye - center //
Direction we are looking Vector3
cameraX Vector3.Cross(up, cameraZ)
float len cameraX.LengthSquared()
if (len gt 0) cameraX.Normalize()
else cameraX new
Vector3(1, 0, 0) // Rotate
center around the eye Matrix t1
Matrix.CreateTranslation(-eye)
Matrix r Matrix.CreateFromAxisAngle(cameraX,
angle) Matrix t2
Matrix.CreateTranslation(eye)
Matrix M t1 r t2 center
Vector3.Transform(center, M)
38Pitch is different
- Pitch rotates the camera around the center
- Effectively rotates the object.
- What changes, now?
39 public void Pitch(float angle)
// Need a vector in the camera X
direction Vector3 cameraZ eye -
center Vector3 cameraX
Vector3.Cross(up, cameraZ) float len
cameraX.LengthSquared() if (len gt
0) cameraX.Normalize()
else cameraX new Vector3(1,
0, 0) Matrix t1
Matrix.CreateTranslation(-center)
Matrix r Matrix.CreateFromAxisAngle(cameraX,
angle) Matrix t2
Matrix.CreateTranslation(center)
Matrix M t1 r t2 eye
Vector3.Transform(eye, M) if
(!gravity) up Vector3.Transform(u
p, M) Set()
40Other Camera Types?
- Follow Camera
- Camera is always pointing at our hero
- First Person Camera
- Camera is viewpoint of the character
How do you implement these?
TT
41Chase Camera
- Camera is over the shoulder
- But, not rigidly attached