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 objs (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.

A tree

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 4×4 matrix to model our matrix, called the model matrix. Say, for our example, we send our treeVAO object, which is on the GPU. We send model matrices for each instance of our treeVAO:

Here each M1,...,M4 is a matrix that the GPU handles computing to transform each object separately.

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).

Drawing more triangles

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 θ variable that gets updated).

So in main you need to:

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 v,w via:

vx,vy,vz+wx,wy,wzvx+wx,vy+wy,vz+wz

We use tip-to-tail to draw vectors being added to each other:

We can also scale our vector by a magnitude α:

αvx,vy,vz=αvx,αvy,αvz

We also have the dot product:

vw= a number (scalar)

where:

vw=vxwx+vywy+vzwz

So like:

2,13,2=23+12=8

But let's think about this geometrically.

vw is the length of the projection of v onto w.

Matrices

We have matrices in 2×2 and 3×3 matrices like:

[a11a12a21a22],[a11a12a13a21a22a23a31a32a33],...

Let's review matrix multiplication:

[2002][12]=[24]

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:

[α00α]

scaled by a factor α. If we want rotation matrix by 45 degrees it's:

[0.7070.70700.7070.707]

The worksheet we did reviews a lot of this information:

IMG_3089.jpeg

IMG_3090.jpeg