Computer Graphics
COMP3421/9415 2021 Term 3 Lecture 6
What did we learn last lecture?
2D to 3D
¡ñ How our 2D skills relate to 3D
¡ñ Objects in 3D
¡ñ Coordinate Spaces and Transforms (they’re the same thing!)
¡ñ Making a Camera
What are we covering today?
Cameras and Scenes
¡ñ Converting coordinate spaces into visibility
¡ñ A more dynamic camera
Corrections from last lecture
I’d gotten confused by Matrix Maths
¡ñ Look, it can happen to all of us 😛
¡ñ The transform matrices as sets of axes
¡ñ I’d accidentally mixed up rows and
columns
¡ñ All the slides in lecture 5 have now
been updated!
The X axis
The Y axis
The Z axis
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
The origin
Corrected LookAt Matrix
Correction from last lecture
¡ñ These two matrices are “inverted” so that they move the world and not the camera
¡ñ Hence the horizontal vectors in the rotation and the negative values in the translation
X
Rx
Ry
Rz
0
Uz
0
Dx
Dy
Dz
0
0
0
0
1
1
0
0
-Px
0
1
0
-Py
0
0
1
-Pz
0
0
0
1
Model/View/Projection
Where are we up to with cameras?
We’ve started seeing cameras as a Transform Matrix
¡ñ The LookAt matrix (also known as the View Matrix)
¡ñ This allows us to transform the world’s vertices . . .
¡ñ . . . so that they’re now relative to the camera
Let’s look at different coordinate spaces
A vertex takes a journey through multiple coordinate systems
Image credit: learnopengl.com
A Vertex’s Journey
From creation through to visibility
¡ñ Local Space
¡ð Where an object is created
¡ñ Local to World (the Model Matrix)
¡ð Place an object in world coordinates
¡ð Uses things like Scale/Rotate/Translate to position the object in the scene
¡ñ World to View
¡ð Uses the camera’s transform (we’ve used LookAt to create this)
¡ð Coordinates are now in the camera’s viewpoint
A Vertex’s Journey (continued)
New transforms and coordinate spaces
¡ñ Projection to Clip Space
¡ð Uses the Projection Matrix
¡ð Figuring out the limits of what the camera can see
¡ð Uses Normalized Device Coordinates (-1.0 to 1.0)
¡ð Can also now use perspective transformation to mimic a single viewpoint
¡ñ Transform to Screen Space
¡ð The Viewport Transform
¡ð Changes our -1.0 to 1.0 into the actual pixels of the window/screen we’re rendering
¡ð Information then goes to the rasterizer to make fragments
Projection to Clip Space
Why are we doing another transform?
¡ñ The camera’s viewpoint, now known as the View Transform
¡ð Change the scale from World Coordinates to Normalized Device Coordinates (-1.0 to 1.0)
¡ñ Projection
¡ð Alter the world’s coordinates so that they’re a “projection”
¡ð We’ll use Perspective or Orthographic projections
¡ñ The next step is to “clip” the vertices that we can’t see
¡ð Any vertices outside of -1.0 to 1.0 are not visible to the camera
¡ð They will be discarded and will not be part of rendered fragments
The View Frustum
The Projection Matrix creates a “viewable area”
¡ñ Between -1.0 and 1.0 in three axes makes this a cube
¡ñ Forms the “viewable volume” for the camera
¡ñ This is known as the Frustum
¡ñ The Near Plane is like your screen
¡ñ The Far Plane is the maximum viewable
distance
¡ñ Anything outside this frustum will be clipped
Image credit: learnopengl.com
Types of Projection
Orthographic Projection
¡ñ We’ve actually already been using this!
¡ñ All our 2D projects have used -1.0 to 1.0 as our coordinates
¡ñ We’ve been ignoring the Model/View/Projection transforms
¡ñ . . . and just working in Clip Space
¡ñ This is the same as a camera that’s looking straight along the Z axis with
an orthographic projection
Orthographic Projection
Looking “Square on”
¡ñ Objects don’t change size based on distance
¡ñ The view frustum looks like a rectangular prism in world space
How do we see things?
Human Eyes, Real World Geometry
¡ñ We see the world from a single viewpoint
¡ñ As things get further away, they get smaller
¡ñ The idea of a “vanishing point” in the distance
¡ñ Appeared in art around the 1400s during the
Italian Renaissance
Image credit: www.CGPGrey.com
Masolino da Panicale: Healing of the Cripple and Raising of Tabitha (1424)
Perspective in Graphics
Showing 3D Graphics so that our eyes believe it
¡ñ We need to replicate the human viewpoint
¡ñ The frustum for this looks interesting in world
space
¡ñ It’s the idea of viewing the virtual screen (Near
Plane) as if from a single viewpoint
¡ñ Field of View (FOV) is the angle between the
top and bottom of the frustum
¡ñ Aspect Ratio is the width/height of the near
and far planes
Image credit: learnopengl.com
Transforming Coordinates in Perspective
If we go from the “pyramid” frustum to a cube
¡ñ Objects closer to us will end up being enlarged
¡ñ Objects further away will be smaller
¡ñ Mathematically, we’re using the w coordinate
¡ñ if we set w = -z (with some modifications) before applying the
perspective transform
¡ñ then divide x,y and z by w
¡ñ We end up with visible coordinates in the range of -1.0 to 1.0
¡ñ We’ve effectively normalized our coordinates based on their distance
from the camera
One Transform from Object to Screen
Multiple Matrices together can do a lot of work!
Local Vertices
The Model/View/Projection Transform Matrix
Model Matrix
World Space Vertices
View Matrix
Camera Space Vertices
Projection Matrix
Clip Space Vertices
In OpenGL
We won’t be building this transform matrix manually
¡ñ glm::perspective()
¡ñ This function will take: ¡ð FOV
¡ð Aspect Ratio
¡ð Distance to Near Plane
¡ð Distance to Far Plane
¡ñ It will create a projection matrix
Break Time
An Appreciation for Technology and Art
¡ñ Perspective Projection (Renaissance 15th Century)
¡ñ Cubism, the disruption of perspective (early 1900s)
¡ð Picasso
¡ñ Impressionism, brush strokes predating pixels
(19th Century) ¡ð Monet
¡ñ Colour Theory, mixing colours together (~300BC)
¡ð Aristotle and others along the way including
(1700s)
, 1910, Girl with a Mandolin ( )
, Impression, soleil levant (Impression, Sunrise), 1872
Dynamic Camera
Moving our Camera in a Scene
Cameras are the player’s view into a virtual world
¡ñ It’s important that we give players control in a game situation
¡ñ Letting the camera move in the scene is amazing for immersion
Image credit: id Software (edited by Marc)
What do we have so far?
Current Camera knowledge
¡ñ We can create a transform using: ¡ð Position
¡ð Look vector (also the Camera Direction vector)
¡ð Up vector
¡ñ We know we can recreate this transform very quickly with new information
The Render Loop
While(true) {render}
¡ñ You may have noticed a while loop in our code
¡ñ It runs for every “frame” that is displayed on your screen
¡ñ Each time it runs, it runs the entire OpenGL pipeline
¡ð Calculates vertex data
¡ð Passes it through to fragments
¡ð Renders the pixel colours
Player Input
We can detect things like keyboard and mouse input
¡ñ We have some nice tools in GLFW (Graphics Library Framework)
¡ñ These can pick up keys and mouse events each frame
¡ñ We can make changes in our camera based on these
¡ñ For Example: If ‘w’ is pressed, we could translate our camera towards its
target by a certain amount
How much time is there in between frames?
1/60th of a second? 1/144th of a second?
¡ñ Does this mean that a camera is going to move faster if our framerate is higher?
¡ñ Maybe we want to make sure our movement is NOT dependent on how many frames per second we are rendering
Delta Time
Make sure our render loop records time
¡ñ GLFW can give us the current time in our program
¡ñ We can record what the time was when we started rendering our last
frame
¡ñ Which means we can figure out how long it took in between frames!
¡ñ This is known as delta time
¡ñ Camera speed * delta time gives us smooth motion
Rotating a Camera
Using a mouse to control where a camera is aiming
¡ñ Euler Angles: Pitch, Yaw and Roll
¡ñ Pitch rotates around the camera’s x axis
¡ñ Yaw rotates around the camera’s y axis
¡ñ Roll rotates around the camera’s z axis
Image credit: learnopengl.com
Rotating a Camera (continued)
We’re not going to be using roll (we let our up vector always stay up)
¡ñ Mouse input delta
¡ð Where was the mouse last frame?
¡ð Where is it now?
¡ñ Mouse input delta is in two dimensions
¡ð x relates to yaw
¡ð y relates to pitch
¡ñ We can calculate a new Look Vector by rotating the previous Look Vector
based on the changes in the mouse input
Camera Control
Each frame . . .
¡ñ Detect the time between frames
¡ñ Detect user input
¡ñ Calculate how far the camera should move
¡ñ Calculate how much it should rotate
¡ñ Generate a new camera transform
¡ñ Pass this information to the renderer!
What did we learn today?
More details about 3D Graphics
¡ñ Model/View/Projection
¡ð One transform to go from local object to device coordinates
¡ñ Camera Control
¡ð Updating an object per frame based on player input