530 likes | 543 Views
Unleash the power of matrices in computer graphics! Learn how to control, position, scale, and rotate 3D objects efficiently using matrix algebra. Dive into matrix operations, multiplication rules, and special matrices for advanced rotations in game development. Discover the magic of transposing, inverting matrices, and leveraging quaternions for smoother 3D rotations.
E N D
The Math Lecture (Part I - Matrices)
Introduction • For 2D games, we use a lot of trigonometry • For 3D games, we use a lot of linear algebra • Most of the time, we don’t have to use calculus • A matrix can: • Translate (move) a vertex • Rotate a vertex • Scale a vertex • Math libraries cover up these details • Learn it anyway!
Introduction • For 2D games, we use a lot of trigonometry • For 3D games, we use a lot of linear algebra • Most of the time, we don’t have to use calculus • A matrix can: • Translate (move) a vertex • Rotate a vertex • Scale a vertex • Math libraries cover up these details • Learn it anyway!
What is the matrix? • Control • Position, scaling, and rotationof all your 3D objects in OpenGL
What is a matrix? • m x n array of scalars M =
A Frame in Matrix Form • Where P0 is the origin of the frame M: v2 v1 P0 v3
Matrix Operations • Scalar multiplication, addition, matrix-matrix multiplication… • In this class, we really only care about: • matrix-matrix multiplications with square (m x m) matrices • matrix-vector or matrix-point multiplication
Matrix-Point (or vector) Multiplication In GLM you can use the overloaded operator * for this
Matrix-Matrix Multiplication Does A*B = B*A? NO What does the identity do? ? ? In GLM you can use the overloaded operator * for this
Examples ? ?
Activity: 4x4 Matrix Multiplication • (5 min) In CG, we mostly use 4x4 matrices • Multiply the following: • Use GLM!
Properties of Matrices • Associative • A(BC) = (AB)C • NOT commutative (usually) • AB = BA • However, • IA = AI = A In GLM the * operator adheres to these rules
Matrix Algebra • Assume AB=C • Assume A and C are known. • B is unknown • What is B? • Lets do some algebra!! • A-1AB=A-1C • A-1A = Identity • Then B=A-1C
MAtrices • The foundation of all geometric operations (translation, rotation, scaling, skewing…) • Have multiple rows and columns (usually 3x3 or 4x4) • Below is an identity matrix • We can multiply a matrix • with another matrix, and get a matrix • with a vector of “appropriate” dimension (later)
Transposing a Matrix • Have a transpose (denoted ) where • In other words, make the rows columns! • glm::transpose Transpose
Transposing a Matrix • Have a transpose (denoted ) where • In other words, make the rows columns! • Mirror image along the diagonal Transpose The Matrix has you…
Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) • glm::inverse
Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero)
Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero)
Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero)
Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) Follow the white rabbit…
Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element = Oldpoint Newpoint
Axis-Angle Rotations in GLM glm::rotate( angle, x, y, z); Where angle is the angle and (x,y,z) is a vector you want to rotate around (normalize it first!) Example: Rotate 30 deg around the vector (1,1,1) glm::vec3 v = normalize(vec3(1,1,1)); glm::rotate(30, v.x, v.y, v.z)
Euler Angles More intuitive: represent rotations by 3 angles, one for each axis glm::rotate(anglex,1,0,0) * glm::rotate(angley,0,1,0) * glm::rotate(anglez,0,0,1); Think: if we have a torus unstranformed at the origin, what will the torus look like if you have anglex=90, angley=90, and anglez=90 Transformed
Gimbal Lock Rotate around these Initial orientation (x=blue, y=green, z = red) How can we gain altitude here?
Quaternions • Quaternions represent 3D rotations in 4D using imaginary numbers – a 4-tuple Q = (w,x,y,z) • IF YOU EVER RUN INTO GIMBAL LOCK: • 1) Convert to Quaternion from euler, matrix, or angle-axis • 2) Do rotations in quaternion form • 3) Convert to angle-axis or matrix • Quaternions are easy to use in GLM • http://glm.g-truc.net/api-0.9.0/a00184.html
Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element = -1 0
Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element =
Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element =
Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element = What IS real?
Translation • It’s a piece of cake, because the 4th column is the translation!
Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =
Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =
Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =
Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =
Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: = Why, oh why, didn’t I take the blue pill?
Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =
SCALING • It’s a piece of cake too, because it’s the diagonal! • We can scale along just one axis, or more than one! • Imagine we want to scale the point by the values x, y and z. Then: = Trace program: running
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix There is no spoon…
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix ?
Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix =