CSE 167: Introduction to Computer Graphics Lecture #2: Coordinate Transformations Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2013
Announcements Homework #1 due Friday Oct 4 th at 1:30pm; presentation in CS basement lab 260 Tip: Don t save anything on the C: drive of the lab PCs in Windows. You will lose it when you log out! 2
Lecture Overview Vectors and Matrices Linear Transformations Homogeneous Coordinates Affine Transformations 3
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 4 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 5
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 6
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θ 7
Angle Between Two Vectors a b = a b cosθ cosθ = a b a b b θ = cos 1 a b a b a 8
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 a and b are parallel (or one or both degenerate) 9
Cross product 10
Sample Vector Class in C++ class Vector3 { public: float x,y,z; Vector3() Vector3(float x0,float y0,float z0) void set(float x0,float y0,float z0) void add(vector3 &a) void add(vector3 &a,vector3 &b) void subtract(vector3 &a) void subtract(vector3 &a,vector3 &b) void negate() void negate(vector3 &a) void scale(float s) void scale(float s,vector3 &a) float dot(vector3 &a) void cross(vector3 &a,vector3 &b) float magnitude() void normalize() }; {x=0.0; y=0.0; z=0.0;} {x=x0; y=y0; z=z0;} {x=x0; y=y0; z=z0;} {x+=a.x; y+=a.y; z+=a.z;} {x=a.x+b.x; y=a.y+b.y; z=a.z+b.z;} {x-=a.x; y-=a.y; z-=a.z;} {x=a.x-b.x; y=a.y-b.y; z=a.z-b.z;} {x=-x; y=-y; z=-z;} {x=-a.x; y=-a.y; z=-a.z;} {x*=s; y*=s; z*=s;} {x=s*a.x; y=s*a.y; z=s*a.z;} {return x*a.x+y*a.y+z*a.z;} {x=a.y*b.z-a.z*b.y; y=a.z*b.x-a.x*b.z; z=a.x*b.y-a.y*b.x;} {return sqrt(x*x+y*y+z*z);} {scale(1.0/magnitude());} 11
Matrices Rectangular array of numbers Square matrix if m = n In graphics often m = n = 3; m = n = 4 12
Matrix Addition 13
Multiplication With Scalar 14
Matrix Multiplication 15
Matrix-Vector Multiplication 16
Identity Matrix 17
Matrix Inverse If a square matrix M is non-singular, there exists a unique inverse M -1 such that 18
OpenGL Matrices Vectors are column vectors Column major ordering Matrix elements stored in array of floats float M[16]; Corresponding matrix elements: 19
Lecture Overview Vectors and Matrices Linear Transformations Homogeneous Coordinates Affine Transformations 20
Linear Transformations Scaling, shearing, rotation, reflection of vectors, and combinations thereof Implemented using matrix multiplications 21
Scaling Uniform scaling matrix in 2D Analogous in 3D 22
Scaling Nonuniform scaling matrix in 2D Analogous in 3D 23
Shearing Shearing along x-axis in 2D Analogous for y-axis, in 3D 24
Rotation in 2D Convention: positive angle rotates counterclockwise Rotation matrix 25
Rotation in 3D Rotation around coordinate axes 26
Rotation in 3D Concatenation of rotations around x, y, z axes are called Euler angles Result depends on matrix order! 27
Rotation in 3D Around arbitrary axis R(a,θ) = 1+ (1 cos(θ))(a 2 x 1) a z sin(θ) + (1 cos(θ))a x a y a y sin(θ) + (1 cos(θ))a x a z a z sin(θ) + (1 cos(θ))a y a x 1+ (1 cos(θ))(a 2 y 1) a x sin(θ) + (1 cos(θ))a y a z a y sin(θ) + (1 cos(θ))a z a x a x sin(θ) + (1 cos(θ))a z a y 1+ (1 cos(θ))(a 2 z 1) Rotation axis a a must be a unit vector: Right-hand rule applies for direction of rotation Counterclockwise rotation a = 1 28
Lecture Overview Vectors and Matrices Linear Transformations Homogeneous Coordinates Affine Transformations 29
Homogeneous Coordinates We add a 4 th coordinate to vectors and points. Usually for 3D points we choose. For 3D vectors. Benefit of homogeneous coordinates: vectors and points both represented by 4 coordinates 30
Homogeneous Coordinates Generalized homogeneous point: Obtain 3D coordinates of point: divide by homogeneous coordinate 31
Translation Example for homogeneous coordinates: 32
Translation Matrix notation 33 Translation matrix
Transformations Add 4 th row/column to 3 x 3 transformation matrices Example: rotation 34
Transformations Concatenation of transformations: Arbitrary transformations (scale, shear, rotation, translation) Build chains of transformations Result depends on order 35
Lecture Overview Vectors and Matrices Linear Transformations Homogeneous Coordinates Affine Transformations 36
Affine transformations Generalization of linear transformations Scale, shear, rotation, reflection (linear) Translation Preserve straight lines, parallel lines Implementation using 4x4 matrices and homogeneous coordinates 37
Translation 38
Translation Inverse translation 39
Scaling Origin does not change 40
Scaling Inverse of scale: 41
Shear Pure shear if only one parameter is non-zero 42
Rotation around coordinate axis Origin does not change 43
Rotation around arbitrary axis Origin does not change Angle, unit axis a 44
Rotation matrices Orthonormal Rows, columns are unit length and orthogonal Inverse of rotation matrix: Its transpose 45
Videos Linear Algebra - Affine Matrix Transformations With Shapes (0:00-5:00) Part of http://computersciencevideos.org/cplusplus-linear- Algebra/ http://www.youtube.com/watch?v=4inr_27dm4u Online Graphics Basic Math: Matrices http://www.youtube.com/watch?v=rkx2u16qyy8 46