The Math Lecture

advertisement
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
Download