Modeling Transformations Michael Kazhdan (601.457/657) HB Ch. 5 FvDFH Ch. 5
Announcement Assignment 2 has been posted: Due: 10/24 ASAP: Download the code and make sure it compiles» On windows: just build the Assignment2 project» On Linu: make f Makefile2 (Feel free to tr building the other projects. Let me know if ou have trouble.)
Overview Ra-Tracing so far Modeling transformations
Ra Tracing Image RaTrace( Camera camera, Scene scene, int width, int height, int depth, float cutoff ) { Image image = new Image( width, height ); for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) { Ra ra = ConstructRaThroughPiel( camera, i, j ); image[i][j] = GetColor( scene, ra, 1, depth, Color( cutoff, cutoff, cutoff ) ); } return image; }
Ra Tracing For each sample Construct ra from ee position through view plane Compute color contribution of the ra Ras through view plane Ee position Samples on view plane
Ra Tracing Image RaTrace( Camera camera, Scene scene, int width, int height, int depth, float cutoff ) { Image image = new Image( width, height ); for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) { Ra ra = ConstructRaThroughPiel( camera, i, j ); image[i][j] = GetColor( scene, ra, 1, depth, Color( cutoff, cutoff, cutoff ) ); } return image; }
Constructing Ra Through a Piel 2D Eample: The ra passing through the i-th piel is defined b: Ra t = p 0 + t Ԧv p 0 : camera position Ԧv: direction to the i-th piel: Ԧv = p i p 0 p i p 0 p 0 θ p[i]: i-th piel location: Ԧv i + 0.5 p i = p 1 + 2 d tan θ up height p 1 and p 2 are the endpoints of the view plane: p 1 = p 0 + d towards d tan θ up p 2 = p 0 + d towards + d tan θ up up towards d p[i] p 2 p 1 2 d tan(θ)
Ra Tracing Image RaTrace( Camera camera, Scene scene, int width, int height, int depth, float cutoff ) { Image image = new Image( width, height ); for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) { Ra ra = ConstructRaThroughPiel( camera, i, j ); image[i][j] = GetColor( scene, ra, 1, depth, Color( cutoff, cutoff, cutoff ) ); Color GetColor( } Scene scene, Ra ra, float ir, int d, Color co ) { return image; HitInformation } hit; Color c(0,0,0); if( d 0 (co[0]>1 && co[1]>1 && co[2]>1) ) return c; } if( Intersect( scene, ra, hit ) 0 ) { c = GetEmissive( hit ); for( i=0 ; i<lightnum ; i++) c += light[i].getambientdiffuseandspecular( ra, hit ); Ra reflectedra = ReflectedRa( ra, hit.p, hit.n ); c += GetColor( scene, reflectedra, ir, d-1, co/hit.spec ) * hit.spec; Ra refractedra = RefractedRa( ra, hit.p, hit.n, hit.ir ); c += GetColor( scene, refractedra, hit.ir, d-1, co/hit.tran ) * hit.tran; } return c;
Ra Tracing Image RaTrace( Camera camera, Scene scene, int width, int height, int depth, float cutoff ) { Image image = new Image( width, height ); for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) { Ra ra = ConstructRaThroughPiel( camera, i, j ); image[i][j] = GetColor( scene, ra, 1, depth, Color( cutoff, cutoff, cutoff ) ); Color GetColor( } Scene scene, Ra ra, float ir, int d, Color co ) { return image; HitInformation } hit; Color c(0,0,0); if( d 0 (co[0]>1 && co[1]>1 && co[2]>1) ) return c; } if( Intersect( scene, ra, hit ) 0 ) { c = GetEmissive( hit ); for( i=0 ; i<lightnum ; i++) c += light[i].getambientdiffuseandspecular( ra, hit ); Ra reflectedra = ReflectedRa( ra, hit.p, hit.n ); c += GetColor( scene, reflectedra, ir, d-1, co/hit.spec ) * hit.spec; Ra refractedra = RefractedRa( ra, hit.p, hit.n, hit.ir ); c += GetColor( scene, refractedra, hit.ir, d-1, co/hit.tran ) * hit.tran; } return c;
Ra-Scene Intersection Intersections with geometric primitives Sphere Triangle Groups of primitives (scene) Acceleration techniques Bounding volume hierarchies Spatial partitions» Uniform grids» Octrees» BSP trees
Ra Tracing Image RaTrace( Camera camera, Scene scene, int width, int height, int depth, float cutoff ) { Image image = new Image( width, height ); for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) { Ra ra = ConstructRaThroughPiel( camera, i, j ); image[i][j] = GetColor( scene, ra, 1, depth, Color( cutoff, cutoff, cutoff ) ); Color GetColor( } Scene scene, Ra ra, float ir, int d, Color co ) { return image; HitInformation } hit; Color c(0,0,0); if( d 0 (co[0]>1 && co[1]>1 && co[2]>1) ) return c; } if( Intersect( scene, ra, hit ) 0 ) { c = GetEmissive( hit ); for( i=0 ; i<lightnum ; i++) c += light[i].getambientdiffuseandspecular( ra, hit ); Ra reflectedra = ReflectedRa( ra, hit.p, hit.n ); c += GetColor( scene, reflectedra, ir, d-1, co/hit.spec ) * hit.spec; Ra refractedra = RefractedRa( ra, hit.p, hit.n, hit.ir ); c += GetColor( scene, refractedra, hit.ir, d-1, co/hit.tran ) * hit.tran; } return c;
Surface Illumination Calculation Viewer L 1 L 2 N V I = I E + L K A I L A + K D N, L + K S V, R n I L S L
Ra Tracing (Recursive) Image RaTrace( Camera camera, Scene scene, int width, int height, int depth, float cutoff ) { Image image = new Image( width, height ); for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) { Ra ra = ConstructRaThroughPiel( camera, i, j ); image[i][j] = GetColor( scene, ra, 1, depth, Color( cutoff, cutoff, cutoff ) ); Color GetColor( } Scene scene, Ra ra, float ir, int d, Color co ) { return image; HitInformation } hit; Color c(0,0,0); if( d 0 (co[0]>1 && co[1]>1 && co[2]>1) ) return c; } if( Intersect( scene, ra, hit ) 0 ) { c = GetEmissive( hit ); for( i=0 ; i<lightnum ; i++) c += light[i].getambientdiffuseandspecular( ra, hit ); Ra reflectedra = ReflectedRa( ra, hit.p, hit.n ); c += GetColor( scene, reflectedra, ir, d-1, co/hit.spec ) * hit.spec; Ra refractedra = RefractedRa( ra, hit.p, hit.n, hit.ir ); c += GetColor( scene, refractedra, hit.ir, d-1, co/hit.tran ) * hit.tran; } return c;
Ra Tracing (Recursive) Consider the contribution of: Reflected Ras Refracted Ras L 1 L 0 I = I E + L K A I L A + K D N, L + K S V, R n I L S L + K s I R + K T I T
Overview Ratracing so far Modeling transformations
Modeling Transformations Specif transformations for objects Allows definitions of objects in own coordinate sstems Allows use of object definition multiple times in a scene H&B Figure 109
Overview 2D Transformations Basic 2D transformations Matri representation Matri composition 3D Transformations Basic 3D transformations Same as 2D
Simple 2D Transformations Translation p = p + t p p = p p + t t
Simple 2D Transformations Scale p = S p p p = s 0 0 s p p
Simple 2D Transformation Rotation p = R p p p = cos θ sin θ sin θ cos θ p p
2D Modeling Transformations Object Coordinates Scale Translate Scale Rotate Translate World Coordinates
2D Modeling Transformations Object Coordinates Let s look at this in detail World Coordinates
2D Modeling Transformations Object Coordinates
2D Modeling Transformations Object Coordinates Scale.3,.3 Rotate -90 Translate 5, 3
2D Modeling Transformations Object Coordinates Scale.3,.3 Rotate -90 Translate 5, 3
2D Modeling Transformations Object Coordinates Scale.3,.3 Rotate -90 Translate 3, 5 World Coordinates
Basic 2D Transformations Translation: = + t = + t Scale: = s = s Rotation: = cos θ sin θ = sin θ + cos θ Transformations can be combined (with simple algebra)
Basic 2D Transformations Translation: = + t = + t Scale: = s = s Rotation: = cos θ sin θ = sin θ + cos θ
Basic 2D Transformations Translation: = + t = + t Scale: = s = s (, ) (, ) Rotation: = cos θ sin θ = sin θ + cos θ = s = s
Basic 2D Transformations Translation: = + t = + t Scale: = s = s Rotation: = cos θ sin θ = sin θ + cos θ (, ) = s = s = s cos θ s sin θ = s sin θ + s cos θ
Basic 2D Transformations Translation: = + t = + t Scale: = s = s (, ) Rotation: = cos θ sin θ = sin θ + cos θ = s = s = s cos θ s sin θ = s sin θ + s cos θ = s cos θ s sin θ + t = s sin θ + s cos θ + t
Overview 2D Transformations Basic 2D transformations Matri representation Matri composition 3D Transformations Basic 3D transformations Same as 2D
Matri Representation Represent 2D transformation b a matri a c b d Multipl matri b column vector appl transformation to point = a b c d = a + b = c + d
Matri Representation Transformations combined b multiplication = a b c d e g f h i k j k Matrices are a convenient and efficient wa to represent a sequence of transformations!
22 Matrices What tpes of transformations can be represented with a 22 matri? 2D Scale around (0,0)? = s = s = s 0 0 s 2D Rotate around (0,0)? = cos θ sin θ = sin θ + cos θ = cos θ sin θ sin θ cos θ 2D Mirror over Y ais? = = = 1 0 0 1
22 Matrices What tpes of transformations can be represented with a 22 matri? 2D Scale around (0,0)? = s = s = s 0 0 s Like scale with negative scale values 2D Mirror over Y ais? = = = 1 0 0 1
22 Matrices What tpes of transformations can be represented with a 22 matri? 2D Translation?
22 Matrices What tpes of transformations can be represented with a 22 matri? 2D Translation? = + t = + t ' ' = NO! a c b d Onl linear 2D transformations can be represented with a 2 2 matri
Linear Transformations Linear transformations are combinations of Scale, and Rotation Properties of linear transformations: Satisfies: T s 1 p 1 + s 2 p 2 = s 1 T p 1 + s 2 T(p 2 ) Origin maps to origin Lines map to lines Parallel lines remain parallel Closed under composition = a b c d
Linear Transformations Linear transformations are combinations of Scale, and Rotation Properties of linear transformations: Satisfies: T s 1 p 1 + s 2 p 2 = s 1 T p 1 + s 2 T(p 2 ) Origin maps to origin = Lines map to lines Parallel lines remain parallel Closed under composition a b c d Translations do not map the origin to the origin
2D Translation Treat 2D positions as 3D positions b adding a third coordinate with a 1 :, (,, 1) Represent translations using a 33 matri: 1 = 1 0 t 0 1 t 0 0 1 1 = + t = + t
Homogeneous Coordinates Add a 3rd coordinate to ever 2D point (,, w) represents a point at location, w w (,, 0) represents a (directed) point at infinit (0, 0, 0) is not allowed 2 1 (2,1,1) or (4,2,2) or (6,3,3) 1 2 Convenient coordinate sstem to represent man useful transformations
Basic 2D Transformations Basic 2D transformations as 3 3 matrices 1 1 0 t = 0 1 t 0 0 1 Translate 1 1 = s 0 0 0 s 0 0 0 Scale 1 1 1 = cos θ sin θ 0 sin θ cos θ 0 0 0 1 Rotate 1
Affine Transformations Affine transformations are combinations of Linear transformations, and Translations w = a b c d e f 0 0 1 w Properties of affine transformations: Origin does not necessaril map to origin Lines map to lines Parallel lines remain parallel Closed under composition
Affine Transformations Affine transformations are combinations of Linear transformations, and Translations w = a b c d e f 0 0 1 w Note that under projective transformations Properties of,, affine 1 has transformations: to map to (,, 1) Origin does not necessaril map to origin Lines map to lines Parallel lines remain parallel Closed under composition
Projective Transformations Projective transformations Affine transformations, and Projective warps w = a b c d e f g h i Properties of projective transformations: Origin does not necessaril map to origin Lines map to lines Parallel lines do not necessaril remain parallel Closed under composition w
Projective Transformations Projective transformations Affine transformations, and Projective warps w = a b c d e f g h i Properties Note of that projective under projective transformations: Origin, does, 1 not does necessaril not have to map to to ( origin,, 1) Lines map to lines Parallel lines do not necessaril remain parallel Closed under composition w
Overview 2D Transformations Basic 2D transformations Matri representation Matri composition 3D Transformations Basic 3D transformations Same as 2D
Matri Composition Transformations can be combined b matri multiplication 1 0 t cos θ sin θ 0 s 0 0 = 0 1 t sin θ cos θ 0 0 s 0 w 0 0 1 0 0 1 0 0 1 w p = T t, t R θ S s, s p
Matri Composition Matrices are a convenient and efficient wa to represent a sequence of transformations General purpose representation Efficienc with pre-multiplication of matrices p = T R S p p = (T R S)(p)
Matri Composition Be aware: order of transformations matters» Matri multiplication is not commutative p = T R S p Global Local
Matri Composition Rotate b θ around arbitrar point (a, b) M = T a, b R θ T( a, b) The trick: First, translate (a, b) to the origin. Net, do the rotation about origin. Finall, translate back. (a, b) Scale b (s, s ) around arbitrar point (a, b) M = T a, b S s, s T( a, b) (Use the same trick.) (a, b)
Overview 2D Transformations Basic 2D transformations Matri representation Matri composition 3D Transformations Basic 3D transformations Same as 2D
3D Transformations Same idea as 2D transformations Homogeneous coordinates: (,, z, w) 4 4 transformation matrices» Affine z 1» Projective z w = = a b c d e f g h i j k l 0 0 0 1 a b c d e f g h i j k l m n o p z 1 z w
Basic 3D Transformations z w = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Identit z w z w = s 0 0 0 0 s 0 0 0 0 s z 0 0 0 0 1 Scale z w z w = 1 0 0 t 0 1 0 t 0 0 1 t z 0 0 0 1 z w Translation
Basic 3D Transformations Pitch-Roll-Yaw Convention: An rotation can be epressed as the combination of a rotation about the -, the -, and the z-ais. Rotate around z ais: z w = cos θ sin θ 0 0 sin θ cos θ 0 0 0 0 1 0 0 0 0 1 z w Rotate around ais: z w = cos θ 0 sin θ 0 0 1 0 0 sin θ 0 cos θ 0 0 0 0 1 z w Rotate around ais: z w = 1 0 0 0 0 cos θ sin θ 0 0 sin θ cos θ 0 0 0 0 1 z w
Basic 3D Transformations Pitch-Roll-Yaw Convention: An rotation can be epressed as the combination of a rotation about the -, the -, and the z-ais. Rotate around z ais: z w = cos θ sin θ 0 0 sin θ cos θ 0 0 0 0 1 0 0 0 0 1 z w How do ou rotate around an Rotate around ais: = arbitrar ais U b angle ψ? Rotate around ais: z w z w = cos θ 0 sin θ 0 0 1 0 0 sin θ 0 cos θ 0 0 0 0 1 1 0 0 0 0 cos θ sin θ 0 0 sin θ cos θ 0 0 0 0 1 z w z w
Rotation B ψ Around Arbitrar Ais U Align U (wlog) with the z-ais: T (a,b,c) : Translate U b (a, b, c) to pass through origin R φ R θ : Rotate around the - and -aes b θ and φ degrees to get U aligned with the z-ais. R z (ψ): Perform rotation b ψ around the z-ais. Do inverse of original transformation for alignment. U U z T (a,b,c) z R φ R θ z U
Rotation B ψ Around Arbitrar Ais U Align U (wlog) with the z-ais: T (a,b,c) : Translate U b (a, b, c) to pass through origin R φ R θ : Rotate around the - and -aes b θ and φ degrees to get U aligned with the z-ais. R z (ψ): Perform rotation b ψ around the z-ais. Do inverse of original transformation for alignment. p = R φ R θ T a,b,c 1 Rz ψ R φ R θ T a,b,c p Aligning Transformation