際際滷s from when I was teaching CS4052 Computer Graphics at Trinity College Dublin in Ireland.
These slides aren't used any more so they may as well be available to the public!
There are some mistakes in the slides, I'll try to comment below these.
This is the third lecture - on using linear algebra for transformations.
7. Matrix * Vector
Q. What is the result in the vector?
i.e. what does this matrix do?
8. Row-Major vs. Column-Major
We will use column-major notation
Multiplication order for column-major is right-to-left
It is possible to use row-major instead (most DX apps do)
Reversed in row-major
Column
9. 3X3 Rotation Around Z-Axis
cos(theta) -sin(theta) 0.0
sin(theta) cos(theta) 0.0
0.0 0.0 1.0
Theta will be in radians in C
Right-hand rule for rotation direction
Q. Which way will my triangle turn on screen?
10. Rotation Demo
Define matrix as an array of floats (how many for a 3x3?)
Array memory is in column order for OpenGL
Update shader uniform for matrix inside main loop
glUniform...() family of functions
glUniformMatrix3fv() takes an array of 9 floats
12. 4x4 Homogenous Matrices
You can use 3d matrices, but we tend to use 4d matrices
in graphics, and 4d vectors/points.
These are not 4d hyper-geometry - it's a sneaky exploit.
Q. Any idea why we might like 4d matrices?
13. Matrix * Vector Rules
A 3x3 matrix (mat3) can only multiply with a 3d vector
(vec3)
A 4x4 matrix (mat4) can only multiply with a 4d (vec4)
Q. If we have a 4x4 matrix and a 3d point, how do
we make our 3d point into a 4d point?
14. 4d Vectors
XYZ and W
vec4 in GLSL
For POINTS set the 4th component to 1.0
For VECTORS set the 4th component to 0.0
Q. Any idea why?
vec4 (1.0, 5.0, -20.0, 0.0);
vec4 (0.0, -1.0, 0.0, 1.0);
This is a dirty trick!
15. Now, What Was the Point in Going 4d?
We can combine many matrices together by
multiplication
mat4 M = R * T * S;
vec4 result = M * vec4 (vp.xyz, 1.0);
Send fewer matrix uniforms updated over the bus
Create a transformation pipeline (more on that soon)
16. 4X4 Homogenous Matrix
Sx 0.0 0.0 Tx
0.0 Sy 0.0 Ty
0.0 0.0 Sz Tz
0.0 0.0 0.0 1.0
rotation and
scaling
translation x, y, z
Putting translation in the final column lets us do our sneaky
trick...
17. Matrix * Vector
Q. Can you figure it out?
What the 0 or 1 does at the end of a vec4?
18. Matrix * Vector
Q. Can you figure it out?
What the 0 or 1 does at the end of a vec4?
19. Matrix Multiplication
a b
c d
e f
g h
AB = =
(ae + bg) (af + bh)
(ce + dg) (cf + dh)
Each cell in result is =
Sum of:
A(our row, first col) * B(first row, our col) +
... ... ...
+ A(our row, last col) * B(last row, our col)
A B
21. Transpose Matrix
Swaps between column-major and row-major layout
Flip values over the main diagonal
Q. Can y'all compute the transpose of this?
1.0 0.5 0.0 0.0
0.0 2.0 0.0 0.0
-5.0 0.0 1.0 -1.0
0.0 0.0 0.0 1.0
22. Inverse Matrix
Reverses any matrix transformation
Or transform relative to another object
Quite complicated to compute
Work out determinant, then multiply with cofactors
1.0 0.0 0.0 2.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
Q. Guess?
23. Most Important Homework
1.Find out how to build & use the following for 4x4 matrices on paper:
Identity
Scaling
Translation
Rotation around X axis, Y axis, and Z axis
Matrix * Matrix
2.Spot the difference between row and column-major layouts
Hint: column-major has the translation in part in a column
3.Know which order multiplication goes in R-to-L or L-to-R?
24. Guidelines
Get a 3d maths library for C/C++ or make your own
Christophe Riccio's GLM http://glm.g-truc.net/
I made a simple one (Blackboard)
Make a cheat-sheet (or grab mine off Blackboard)
Know how the maths work for all of these operations
If unusure textbooks and online sources!
This stuff definitely comes up in job interview tests,
especially certain famous companies starting with 'H'
26. Notes
I deliberately skipped some things
Vector addition
Unit vectors and normalisation
Dot product of 2 vectors
Cross product of 2 vectors
I plan to introduce this where we actually use it (lighting)
...and because I ramble too much