Lecture 5 - Matrices in CG
Lab 3 is due today. Program 2 is due in 2 weeks. Lab 4 will be all about what we talk about today, which is Matrix transformation of 3D scenes for rasterization.
The final project for this class should reflect your interests. You'll want to find some obj
s (more than one) to make a cool scene that really reflects your interests.
As an aside, for Program 2 the key task is to setup and move geometry (via our matrix transformations).
Matrices
Recall from Lecture 4 - Finishing Rasterizer, Moving to OpenGL. We need to have the GPU use the parallelism and matrix multiplication specialization of it to compute our graphics.
Think of it like the following diagram:
To minimize data transfer, we send the GPU a geometric data array, like so:
Really, we give the GPU all the data beforehand, then make GL calls to tell the GPU "rasterize this object, light up this object, ...". We just only have to then send one command during runtime.
Consider some treeVAO
(tree vertex array object). This object would be on the GPU after we've loaded in the geometric data array.
But what if the tree is suddenly far away? Scaled up? Etc.?
That's where geometric transforms come into play.
Geometric Transforms and the Model Matrix
We use a treeVAO
object, which is on the GPU. We send model matrices for each instance of our treeVAO
:
Here each
glsl
glsl
files are loaded on the GPU ahead of time, which the GPU uses to determine coloring and texturing and ... of objects in our Scene.
For example, consider:
// simple_vert.glsl
uniform mat4 M; //model
uniform mat4 P; //perspective (ignore for now)
uniform mat4 V; //view (ignore for now)
void main()
{
gl_Position = P * V * M * vec4(vertPos, 1.0);
// we'll focus on M * vec4(vertPos, 1.0); for L4 first!!!
fragCol = vec3(0.1, 0.3, 0.1);
}
initGeom
The code call initGeom
passes the data from CPU to GPU.
Then, the code that actually draws the function is in render/draw
call (glDrawArrays
more specifically).
So to draw more than one triangle, instead of the call glDrawArrays(GL_TRIANGLES, 0,3)
should instead be glDrawArrays(GL_TRIANGLES, 0, 3 * num_tri)
or something similar
So for animation, there's a variable like i
that you send to the GPU for updating, and then have it update that and have the transform matrices depend on that variable (for example, rotation, you can send some
So in main
you need to:
- Create our
VAO
- Send the data to the GPU. On there, each object individually gets a compute module to get worked on.
But okay how do we do the Geometric Transform? What are these mysterious matrices?
The Matrices
Recall:
Vectors are 3-components, with no position. They are a change in position.
We can add vectors
We use tip-to-tail to draw vectors being added to each other:
We can also scale our vector by a magnitude
We also have the dot product:
where:
So like:
But let's think about this geometrically.
Matrices
We have matrices in
Let's review matrix multiplication:
Notice that the input vector just gets scaled by 2, so if we want to encode scaling by a factor, we can make the scale matrix:
scaled by a factor
The worksheet we did reviews a lot of this information: