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 rotation of 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 π 1.0 0.0 0.0 0.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 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 π −1 ) where ππ −1 = πΌ • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) • glm::inverse πππ‘ π11 π21 π31 π π π12 π22 π32 π π = π π π = ππ − ππ π π13 π π23 = π11 22 π32 π33 π23 π21 − π 12 π π33 31 π23 π21 + π 13 π π33 31 π22 π32 MATRICES • There’s also an inverse matrix (denoted π −1 ) where ππ −1 = πΌ • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) πππ‘ π11 π21 π31 π π π12 π22 π32 π π = π π π = ππ − ππ π π13 π π23 = π11 22 π32 π33 π23 π21 − π 12 π π33 31 π23 π21 + π 13 π π33 31 π22 π32 MATRICES • There’s also an inverse matrix (denoted π −1 ) where ππ −1 = πΌ • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) πππ‘ π11 π21 π31 π π π12 π22 π32 π π = π π π = ππ − ππ π π13 π π23 = π11 22 π32 π33 π23 π21 − π 12 π π33 31 π23 π21 + π 13 π π33 31 π22 π32 MATRICES • There’s also an inverse matrix (denoted π −1 ) where ππ −1 = πΌ • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) πππ‘ π11 π21 π31 π π π12 π22 π32 π π = π π π = ππ − ππ π π13 π π23 = π11 22 π32 π33 π23 π21 − π 12 π π33 31 π23 π21 + π 13 π π33 31 π22 π32 MATRICES • There’s also an inverse matrix (denoted π −1 ) where ππ −1 = πΌ • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) πππ‘ π11 π21 π31 π π π12 π22 π32 π π = π π π = ππ − ππ π π13 π π23 = π11 22 π32 π33 π23 π21 − π 12 π π33 31 Follow the white rabbit… π23 π21 + π 13 π π33 31 π22 π32 A FEW SPECIAL MATRICES (USED FOR ROTATION) 1 0 0 0 cos(πΌ) −sin(πΌ) π π₯ πΌ = 0 sin(πΌ) cos(πΌ) 0 0 0 cos(π½) 0 sin(π½) 0 1 0 π π¦ π½ = −sin(π½) 0 cos(π½) 0 0 0 0 0 0 1 0 0 0 1 cos(πΎ) −sin(πΎ) 0 sin(πΎ) cos(πΎ) 0 π π§ πΎ = 0 0 1 0 0 0 0 0 0 1 MATRIX MULTIPLICATION • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees • Points are always stored with a 1 for the 4 th element cos(πΎ) sin(πΎ) 0 0 −sin(πΎ) cos(πΎ) 0 0 0 0 1 0 0 0 0 1 1 ? 0 ? = 0 ? 1 ? Old New point point 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 [1 0 0] around the z axis by 90 degrees • Points are always stored with a 1 for the 4 th element cos(90) −sin(90) 0 0 sin(90) cos(90) 0 0 0 0 1 0 0 0 0 1 1 0 0 ? = 0 ? 1 ? = (1 ∗ cos 90 ) + (0 ∗ − sin 90 ) + 0 ∗ 0 + 0 ∗ 1 = 0 0 -1 MATRIX MULTIPLICATION • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees • Points are always stored with a 1 for the 4 th element cos(90) −sin(90) 0 0 sin(90) cos(90) 0 0 0 0 1 0 0 0 0 1 1 0 0 1 = 0 ? 1 ? = (1 ∗ sin 90 ) + (0 ∗ cos 90 ) + 0 ∗ 0 + 0 ∗ 1 = 1 MATRIX MULTIPLICATION • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees • Points are always stored with a 1 for the 4 th element cos(90) −sin(90) 0 0 sin(90) cos(90) 0 0 0 0 1 0 0 0 0 1 = (1 ∗ 0) + (0 ∗ 0) + 0 ∗ 1 + 0 ∗ 1 = 0 1 0 0 1 = 0 0 1 ? MATRIX MULTIPLICATION • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees • Points are always stored with a 1 for the 4 th element cos(90) −sin(90) 0 0 sin(90) cos(90) 0 0 0 0 1 0 0 0 0 1 = (1 ∗ 0) + (0 ∗ 0) + 0 ∗ 0 + 1 ∗ 1 = 1 What IS real? 1 0 0 1 = 0 0 1 1 TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! 1 0 0 0 0 1 0 0 0 π₯ 0 π¦ 1 π§ 0 1 TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! • Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then: 1 0 0 0 0 1 0 0 0 2 0 1 1 −3 0 1 10 ? 5 ? = −7 ? 1 ? TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! • Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then: 1 0 0 0 0 1 0 0 0 2 0 1 1 −3 0 1 10 10 + 2 5 ? = −7 ? 1 ? TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! • Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then: 1 0 0 0 0 1 0 0 0 2 0 1 1 −3 0 1 10 10 + 2 5 5+1 = −7 ? 1 ? TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! • Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then: 1 0 0 0 0 1 0 0 0 2 0 1 1 −3 0 1 10 10 + 2 5 5+1 = −7 −7 − 3 1 ? TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! • Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then: 1 0 0 0 0 1 0 0 0 2 0 1 1 −3 0 1 10 10 + 2 5 5+1 = −7 −7 − 3 1 1 Why, oh why, didn’t I take the blue pill? TRANSLATION • It’s a piece of cake, because the 4 th column is the translation! • Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then: 1 0 0 0 0 1 0 0 0 2 0 1 1 −3 0 1 10 10 + 2 12 5 5+1 6 = = −7 −7 − 3 −10 1 1 1 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 2 9 4 by the values x, y and z. Then: π₯ 0 0 0 0 π¦ 0 0 0 0 π§ 0 0 0 0 1 2π₯ 2 9π¦ 9 = 4 4π§ 1 1 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 cos(π½) 0 −sin(π½) 0 0 sin(π½) 1 0 0 cos(π½) 0 0 0 0 0 1 1 0 0 0 0 π₯ 1 π¦ 0 π§ 0 0 0 ? ? 0 ? ? = ? ? 0 ? ? 1 ? ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 sin(π½) 1 0 0 cos(π½) 0 0 0 0 0 1 1 0 0 0 0 π₯ 1 π¦ 0 π§ 0 0 0 cos(π½) ? 0 ? ? = 0 ? ? 1 ? ? ? ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 sin(π½) 1 0 0 cos(π½) 0 0 0 0 0 1 1 0 0 0 0 π₯ 1 π¦ 0 π§ 0 0 0 cos(π½) 0 ? 0 ? ? ? = 0 ? ? ? 1 ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 1 0 0 sin(π½) 0 cos(π½) 0 0 0 0 1 1 0 0 0 0 1 0 0 π₯ π¦ π§ 0 0 cos(π½) 0 ? = 0 ? 1 ? 0 π₯cos π½ + π§sin(π½) ? ? ? ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 1 0 0 sin(π½) 0 cos(π½) 0 0 0 0 1 1 0 0 0 0 1 0 0 π₯ π¦ π§ 0 0 cos(π½) 0 ? = 0 ? 1 ? 0 π₯cos π½ + π§sin(π½) 1 ? ? ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 1 0 0 sin(π½) 0 cos(π½) 0 0 0 0 1 1 0 0 0 0 1 0 0 π₯ π¦ π§ 0 0 cos(π½) 0 0 = 0 ? 1 ? 0 π₯cos π½ + π§sin(π½) 1 ? ? ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 1 0 0 sin(π½) 0 cos(π½) 0 0 0 0 1 1 0 0 0 0 1 0 0 π₯ π¦ π§ 0 0 cos(π½) 0 0 = 0 ? 1 ? 0 π₯cos π½ + π§sin(π½) 1 1 ? ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 1 0 0 sin(π½) 0 cos(π½) 0 0 0 0 1 1 0 0 0 0 1 0 0 π₯ π¦ π§ 0 0 cos(π½) 0 0 = 0 ? 1 ? 0 π₯cos π½ + π§sin(π½) 1 1 π¦ ? ? ? ? ? ? ? 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 cos(π½) 0 −sin(π½) 0 0 sin(π½) 1 0 0 cos(π½) 0 0 0 0 0 1 1 0 0 0 0 π₯ 1 π¦ 0 π§ 0 0 cos(π½) 0 0 0 = 0 −sin(π½) 1 0 There is no spoon… 0 1 0 0 π₯cos π½ + π§sin(π½) π¦ −π₯sin π½ + π§cos(π½) 0 1 0 0 1 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 cos(π½) 0 −sin(π½) 0 0 sin(π½) 1 0 0 cos(π½) 0 0 ? 0 0 0 1 1 0 0 0 0 π₯ 1 π¦ 0 π§ 0 0 cos(π½) 0 0 0 = 0 −sin(π½) 1 0 0 1 0 0 π₯cos π½ + π§sin(π½) π¦ −π₯sin π½ + π§cos(π½) 0 1 0 0 1 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 cos(π½) 0 −sin(π½) 0 0 π₯cos π½ 1 0 −π₯sin π½ 0 + π§sin(π½) π¦ + π§cos(π½) 0 1 0 0 1 πππ€π ππππ πππ€π ππππ = πππ€π ππππ 1 1 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 cos(π½) 0 −sin(π½) 0 0 π₯cos π½ 1 0 −π₯sin π½ 0 + π§sin(π½) π¦ + π§cos(π½) 0 1 0 0 1 πππ€π ππππ πππ€π ππππ = πππ€π ππππ 1 1 Old point New point WORKING WITH MATRICES IN GLM • glm::mat4 M = glm::mat4(); // identity matrix • Changing/accessing elements: • Change: M[column][row] = myfloat; • Access: myfloat = : M[column][row] ; REVIEW: WHAT YOU LEARNED ABOUT TODAY • Using matrices in computer graphics • Frames • Multiplication etc (i.e., transformations) • Using math libraries in OpenGL • GLM • There is no spoon