Technische Universität München Fakultät für Informatik Computer Graphics SS 2014 Transformations Rüdiger Westermann Lehrstuhl für Computer Graphik und Visualisierung
Technische Universität München Computer Graphics Motivation In computer graphics we often have to transform objects –Define camera position and orientation - Viewing –Define object and light position, extend and orientation - Modelling –Get 3D objects onto 2D screen - Projection 2
Technische Universität München Computer Graphics Motivation Objects are typically modeled in a local object coordinate space –e.g. vertex coordinates are within the unit cube –Many objects are used to create the final scene 3
Technische Universität München Computer Graphics Motivation What we render is a portion of the global world coordinate space –Objects have to be placed in this space World coordinate system Object coordinate system 4
Technische Universität München Computer Graphics Motivation Objects are modeled once and they should then be rendered in multiple instances –Instances can be positioned at different locations, they can be scaled and rotated 5
Technische Universität München Computer Graphics Motivation Complex objects are often compose of multiple instances of base objects What we need is a general approach to arbitrarily transform objects in space 6
Technische Universität München Computer Graphics 3D Transformations 7
Technische Universität München Computer Graphics 3D Transformations Lemma: L M ℝ 3x3 : L(p)= Mp, p ℝ 3 L(p) = L(x e 1 + y e 2 + z e 3 ) = x L(e 1 ) + y L(e 2 ) + z L(e 3 ) = x f 1 + y f 2 + z f 3 L(p) = M [x,y,z] T with 8
Technische Universität München Computer Graphics 3D Transformations What kind of transformations can we imagine? –Move object: Translation –Change size of object: Scaling –Shear object: Shear 9
Technische Universität München Computer Graphics 3D Transformations What kind of transformations can we imagine? –Rotate object: Rotation Rotation is about a rotation axis Distance of points on the objects to rotation axis does not change 10
Technische Universität München Computer Graphics 3D Transformations Scaling, for instance about the x-axis –Multiplication of each coordinate with a scaling factor –Keeps direction of e i but changes their length f 1 = e 1 = [ 0 0] T f 2 = e 2 = [0 0] T f 3 = e 3 = [0 0 ] T yields 11
Technische Universität München Computer Graphics 3D Transformations Shear, for instance along the x-axis –Translation of x-coordinates about factor depending on y and/or z –Changes direction of some of the e i f 1 = e 1 = [1 0 0] T f 2 = s e 1 + e 2 = [s 1 0] T f 3 = e 3 = [0 0 1] T yields 12
Technische Universität München Computer Graphics 3D Transformations Rotations, for instance about the x-axis –Points on the x-axis remain fix –All other points rotate along a circle about the x-axis f 1 = e 1 = [1 0 0] T f 2 = [ 0, cos( ), -sin( ) ] T f 3 = [ 0, sin( ), cos( ) ] T yields Z Y f3f3 f2f2 13
Technische Universität München Computer Graphics 3D Transformations Rotations, for instance about the y-axis –Points on the y-axis remain fix –All other points rotate along a circle about the y-axis f 1 = [ cos( ), 0, sin( ) ] T f 2 = e 2 = [0 1 0] T f 3 = [ -sin( ), 0, cos( ) ] T yields Z Y X 14
Technische Universität München Computer Graphics 3D Transformations Rotations, for instance about the z-axis –Points on the z-axis remain fix –All other points rotate along a circle about the z-axis f 1 = [ cos( ), -sin( ), 0 ] T f 2 = [ sin( ), cos( ), 0 ] T f 3 = e 3 = [0 0 1] T yields Z Y X 15
Technische Universität München Computer Graphics Transformation classes Affine transforms –Preserve parallel lines Translation Rotation Rigid Linear Similarity Isotropic Scaling Scaling Shear Reflection Identity Affine 16
Technische Universität München Computer Graphics Combining transformations (0,0) (1,1) (2,2) (0,0) (5,3) (3,1) Scale(2,2)Translate(3,1) TS = = Scale then Translate Use matrix multiplication: p' = T ( S p ) = TS p Caution: matrix multiplication is NOT commutative!
Technische Universität München Computer Graphics Combining transformations Scale then Translate: p' = T ( S p ) = TS p Translate then Scale: p' = S ( T p ) = ST p (0,0) (1,1) (4,2) (3,1) (8,4) (6,2) (0,0) (1,1) (2,2) (0,0) (5,3) (3,1) Scale(2,2)Translate(3,1) Scale(2,2) 18
Technische Universität München Computer Graphics Combining transformations TS = ST = Scale then Translate: p' = T ( S p ) = TS p = = Translate then Scale: p' = S ( T p ) = ST p
Technische Universität München Computer Graphics Combining transformations 20
Technische Universität München Computer Graphics 3D Transformations What is the inverse of a rotation? –It is the rotation about the same axis but using the negative angle of rotation –In general, the inverse of a transformation is just the inverse transformation matrix –In a rotation matrix, { p 1, p 2, p 3 } form an orthonormal basis (unit vectors are orthogonal to each other) ⇒ the inverse of a rotation matrix is just its transpose [ p 1, p 2, p 3 ] -1 = [ p 1, p 2, p 3 ] T 21
Technische Universität München Computer Graphics 3D Transformations How does the mapping from one rotation onto another rotation look like? –{ p 1, p 2, p 3 } { q 1, q 2, q 3 } ??? –[ p 1, p 2, p 3 ] : { e 1, e 2, e 3 } { p 1, p 2, p 3 } –[ q 1, q 2, q 3 ] : { e 1, e 2, e 3 } { q 1, q 2, q 3 } –[ p 1, p 2, p 3 ] -1 : { p 1, p 2, p 3 } { e 1, e 2, e 3 } –[ q 1, q 2, q 3 ][ p 1, p 2, p 3 ] -1 : { p 1, p 2, p 3 } { q 1, q 2, q 3 } 22
Technische Universität München Computer Graphics 3D Transformations Arbitrary axis of rotation –How does the transformation look like if we want to rotate about an arbitrary axis of rotation? –Can be obtained by concatenation of transformations, i.e. by multiplying the respective matrices 23
Technische Universität München Computer Graphics 3D Transformations Arbitrary axis of rotation 1. Translate so that rotation axis passes through origin - Use coordinates of any point along the axis 2. Rotate so that the rotation axis is aligned with one of the principle coordinate axes - See next slide 3. Perform rotation of object about coordinate axis - Use standard rotation matrix 4. Perform inverse rotation of (2) 5. Perform inverse translation of (1) 24
Technische Universität München Computer Graphics 3D Transformations Arbitrary axis of rotation 1. Rotate so that the rotation axis r is aligned with one of the principle coordinate axes Determine an orthogonal basis {r, s, t} s = r x e 1 or r x e 2 if e 1 colinear to r t = r x s normalize r, s, t Change basis to r, s, t M = [r, s, t ] -1 25
Technische Universität München Computer Graphics 3D Transformations Translations –Moving every point a constant distance in a specified direction f 1 = [ 1 + t x, 0, 0 ] T f 2 = [ 0, 1 + t y, 0 ] T f 3 = [ 0, 0, 1 + t z ] T yields Wrong 26
Technische Universität München Computer Graphics 3D Transformations Translations –Translation is NOT linear; cannot be represented in a 3x3 matrix (image of origin: L(0) = 0) –Affine transformation = linear transformation + translation A : p L(p) + t –Matrix representation extended coordinates 27
Technische Universität München Computer Graphics 3D Transformations Translations –Expressing points and vectors in extended coordinates; What should happen to a point/vector under translation? A vector should remain unchanged Therefore: Point: [x y z 1] T Vector: [x y z 0] T 28
Technische Universität München Computer Graphics 3D Transformations Rendering (ray-tracing) transformed objects for (each model m) do build the model matrix M to position, orient, scale m for (each vertex v) do v´ = Mv// model matrix for vertices n´ = (M T ) -1 n// inverse transpose for normals endo// see board endo raytrace(scene) 29