CSE 167: Introduction to Computer Graphics Lecture #2: Linear Algebra Primer Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016
Announcements Project 1 due next Friday at 2pm Grading window is 2-3:30pm Upload source code to TritonEd by 2pm 2 nd discussion of project 1 Monday at 4pm 2
Vectors Direction and length in 3D Vectors can describe Difference between two 3D points Speed of an object Surface normals (directions perpendicular to surfaces) Normal vector 3 Surface normals Surface
Vector arithmetic using coordinates a = ax ay az b = bx by bz a + b = ax + bx ay + by az + bz a b = ax bx ay by az bz ax a= ay az sax sa= say saz where s is a scalar 4
Vector Magnitude The magnitude (length) of a vector is: v 2 = v x 2 + v y 2 + v z 2 v = v x 2 + v y 2 + v z 2 A vector with length of 1.0 is called unit vector We can also normalize a vector to make it a unit vector Unit vectors are often used as surface normals v v 5
Dot Product a b = a i b i a b = a x b x + a y b y + a z b z a b = a b cosθ 6
Angle Between Two Vectors a b = a b cosθ cosθ = a b a b b θ = cos 1 a b a b a 7
Cross Product a b is a vector perpendicular to both a and b, in the direction defined by the right hand rule a b = a b sinθ a b = area of parallelogram ab a b = 0 if aand b are parallel (or one or both degenerate) 8
Cross product 9
Matrices Rectangular array of numbers Square matrix if m = n In graphics often m = n = 3; m = n = 4 10
Matrix Addition 11
Multiplication With Scalar 12
Matrix Multiplication 13
Matrix-Vector Multiplication 14
Identity Matrix 15
Matrix Inverse If a square matrix M is non-singular, there exists a unique inverse M -1 such that 16
Lecture Overview Affine Transformations Homogeneous Coordinates 17
Affine Transformations Most important for graphics: rotation, translation, scaling Wolfram MathWorld: An affine transformation is any transformation that preserves collinearity (i.e., all points lying on a line initially still lie on a line after transformation) and ratios of distances (e.g., the midpoint of a line segment remains the midpoint after transformation). Implemented using matrix multiplications 18
Uniform Scale Uniform scaling matrix in 2D Analogous in 3D 19
Non-Uniform Scale Nonuniform scaling matrix in 2D Analogous in 3D 20
Rotation in 2D Convention: positive angle rotates counterclockwise Rotation matrix 21
Rotation in 3D Rotation around coordinate axes 22
Rotation in 3D Concatenation of rotations around x, y, z axes are called Euler angles Result depends on matrix order! 23
Rotation about an Arbitrary Axis Complicated! Rotate point [x,y,z] about axis [u,v,w] by angle θ: 24
How to rotate around a Pivot Point? Rotation around origin: p = R p Rotation around pivot point: p =? 25
Rotating point p around a pivot point 1. Translation T 2. Rotation R 3. Translation T -1 p = T -1 R T p 26
Concatenating transformations Given a sequence of transformations M 3 M 2 M 1 Note: associativity applies: 27
Lecture Overview Affine Transformations Homogeneous Coordinates 28
Translation Translation in 2D t x t y Translation matrix? Analogous in 3D: 4x4 matrix 29
Homogeneous Coordinates Basic: a trick to unify/simplify computations. Deeper: projective geometry Interesting mathematical properties Good to know, but less immediately practical We will use some aspect of this when we do perspective projection 30
Homogeneous Coordinates Add an extra component. 1 for a point, 0 for a vector: p = Combine M and d into single 4x4 matrix: p x p y p z 1 And see what happens when we multiply r v = v x v y v z 0 m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 31
Homogeneous Point Transform Transform a point: p x p y p z 1 = m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 p x p y p z 1 = m xx p x + m xy p y + m xz p z + d x m yx p x + m yy p y + m yz p z + d y m zx p x + m zy p y + m zz p z + d z 0 + 0 + 0 + 1 Top three rows are the affine transform! Bottom row stays 1 M p x p y p z + r d 32
Homogeneous Vector Transform Transform a vector: v x v y v z 0 = m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 v x v y v z 0 = m xx v x + m xy v y + m xz v z + 0 m yx v x + m yy v y + m yz v z + 0 m zx v x + m zy v y + m zz v z + 0 0 + 0 + 0 + 0 M v x v y v z Top three rows are the linear transform Displacement d is properly ignored Bottom row stays 0 33
Homogeneous Arithmetic Legal operations always end in 0 or 1! M vector+vector: 0 + M M 0 0 M vector-vector: 0 M M 0 0 scalar*vector: s M M 0 0 M point+vector: 1 + M M 0 1 M point-point: 1 M M 1 0 M point+point: 1 + M M 1 2 scalar*point: s M M 1 s weighted average affine combination of points: 1 M 3 1 + 2 M 3 M 1 1 34
Homogeneous Transforms Rotation, Scale, and Translation of points and vectors unified in a single matrix transformation: p = M p Matrix has the form: Last row always 0,0,0,1 m xx m xy m xz d x m yx m yy m yz d y m zx m zy m zz d z 0 0 0 1 Transforms compose by matrix multiplication! Same caveat: order of operations is important Same note: Transforms operate right-to-left 35