CS 543 - Computer Graphics: Transformations The Snthetic Camera b Robert W. Lindeman gogo@wpi.edu (with help from Emmanuel Agu ;-)
Introduction to Transformations A transformation changes an objects Size (scaling) Position (translation) Orientation (rotation) Shape (shear) We will introduce first in 2D or (,), build intuition Later, talk about 3D and 4D? Transform object b appling sequence of matri multiplications to object vertices R.W. Lindeman - WPI Dept. of Computer Science 2
Wh Matrices? All transformations can be performed using matri/vector multiplication Allows pre-multiplication of all matrices Note: point (, ) needs to be represented as (,, ), also called homogeneous coordinates R.W. Lindeman - WPI Dept. of Computer Science 3
Point Representation We use a column matri (2 matri) to represent a 2D point General form of transformation of a point (, ) to (, ) can be written as: = a + b + c = d + e + f or a b c = d e f * R.W. Lindeman - WPI Dept. of Computer Science 4
Translation To reposition a point along a straight line Given point (, ) and translation distance (t, t ) The new point: (, ) (, ) = + t = + t (,) or = P T where = P + P P = T = t t R.W. Lindeman - WPI Dept. of Computer Science 5
R.W. Lindeman - WPI Dept. of Computer Science 6 33 2D Translation Matri t t = + use 3 vector t t = * Note: it becomes a matri-vector multiplication = + t = + t
Translation of Objects How to translate an object with multiple vertices? Translate individual vertices R.W. Lindeman - WPI Dept. of Computer Science 7
2D Scaling Scale: Alter object size b scaling factor (s, s). i.e., = * S = * S = S S (4,4) (2,2) S = 2, S = 2 (,) (2,2) R.W. Lindeman - WPI Dept. of Computer Science 8
R.W. Lindeman - WPI Dept. of Computer Science 9 33 2D Scaling Matri = S S = S S = * S = * S
Shearing Y coordinates are unaffected, but coordinates are translated linearl with That is = + *h = h is fraction of to be added to h = ( R.W. Lindeman - WPI Dept. of Computer Science
2D Rotation Default rotation center is origin (,) θ θ> : Rotate counter clockwise θ θ< : Rotate clockwise R.W. Lindeman - WPI Dept. of Computer Science
2D Rotation (cont.) (,) -> Rotate about the origin b θ (, ) (, ) θ r φ (,) How to compute (, )? = r*cos(φ) = r*sin(φ) = r*cos(φ +θ) = r*sin(φ +θ) R.W. Lindeman - WPI Dept. of Computer Science 2
2D Rotation (cont.) Using trig. identities cos( + ) = cos cos sin sin (, ) sin( + ) = sin cos + cos sin = cos(θ) sin(θ) = sin(θ) + cos(θ) θ r φ (,) Matri form? = cos( ( ) sin( ( ) sin( ( ) cos( ( ) R.W. Lindeman - WPI Dept. of Computer Science 3
R.W. Lindeman - WPI Dept. of Computer Science 4 33 2D Rotation Matri (,) (, ) θ φ r = ) cos( ) sin( ) sin( ) cos( ( ( ( ( = ) cos( ) sin( ) sin( ) cos( ( ( ( (
2D Rotation How to rotate an object with multiple vertices? Rotate individual Vertices θ R.W. Lindeman - WPI Dept. of Computer Science 5
R.W. Lindeman - WPI Dept. of Computer Science 6 Arbitrar Rotation Center To rotate about arbitrar point P = (P, P) b θ: Translate object b T(-P, -P) so that P coincides with origin Rotate the object b R(θ) Translate object back: T(P, P) In matri form T(P,P) R(θ) T(-P,-P) * P Similar for arbitrar scaling anchor = ) cos( ) sin( ) sin( ) cos( P P P P ( ( ( (
Composing Transformations Composing transformations Appling several transforms in succession to form one overall transformation Eample M X M2 X M3 X P where M, M2, M3 are transform matrices applied to P Be careful with the order For eample Translate b (5, ), then rotate 6 degrees is NOT same as Rotate b 6 degrees, then translate b (5, ) R.W. Lindeman - WPI Dept. of Computer Science 7
OpenGL Transformations Designed for 3D For 2D, simpl ignore z dimension Translation: gltranslated( t, t, tz ) gltranslated( t, t, ) => for 2D Rotation: glrotated( angle, V, V, Vz ) glrotated( angle,,, ) => for 2D Scaling: glscaled( s, s, sz ) glscaled( s, s, ) => for 2D R.W. Lindeman - WPI Dept. of Computer Science 8
3D Transformations Affine transformations Mappings of points to new points that retain certain relationships Lines remain lines Several transformations can be combined into a single matri Two was to think about transformations Object transformations All points of an object are transformed Coordinate transformations The coordinate sstem is transformed, and models remain defined relative to this R.W. Lindeman - WPI Dept. of Computer Science 9
3D Transformations (cont.) Scale glscaled( s, s, sz ): Scale object b (s, s, sz) Translate gltranslated( d, d, dz ): Translate object b (d, d, dz) Rotate glrotated( angle, u, u, uz ): Rotate b angle about an ais passing through origin and (u, u, uz) OpenGL Creates a matri for each transformation Multiplies matrices together to form a single, combined matri Transformation matri is called modelview matri R.W. Lindeman - WPI Dept. of Computer Science 2
Eample: Translation The vertices of an object are mapped to new points Similarl for scaling and rotation R.W. Lindeman - WPI Dept. of Computer Science 2
OpenGL Matrices Graphics pipeline takes all vertices through a series of operations R.W. Lindeman - WPI Dept. of Computer Science 22
OpenGL Matrices and the Pipeline OpenGL uses three matrices for geometr Modelview matri Projection matri Viewport matri Modelview matri Combination of modeling matri M and camera transforms V Other OpenGL matrices include teture and color matrices glmatrimode command selects matri mode glmatrimode parameters GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE, etc. Ma initialize matrices with glloadidentit( ) R.W. Lindeman - WPI Dept. of Computer Science 23
OpenGL Matrices and the Pipeline OpenGL matri operations are 44 matrices Graphics card Fast 44 multiplier -> tremendous speedup R.W. Lindeman - WPI Dept. of Computer Science 24
View Frustum Side walls determined b window borders Other walls are programmer defined Near clipping plane Far clipping plane Transform 3D models to 2D Project points/vertices inside view volume onto view window using parallel lines along z-ais R.W. Lindeman - WPI Dept. of Computer Science 25
Tpes of Projections Different tpes of projections? Different view volume shapes Different visual effects Eample projections Parallel (a.k.a. orthographic) Perspective Parallel is simple Will use this for intro, epand later R.W. Lindeman - WPI Dept. of Computer Science 26
OpenGL Matrices and the Pipeline Projection matri Scales and shifts each verte in a particular wa View volume lies inside cube of to Reverses sense of z increasing z = increasing depth Effectivel squishes view volume down to cube centered at Clipping in 3D then eliminates portions outside view frustum Viewport matri: Maps surviving portion of block (cube) into a 3D viewport Retains a measure of the depth of a point R.W. Lindeman - WPI Dept. of Computer Science 27
Lighting and Shading Light components: Diffuse, ambient, specular OpenGL gllightfv( ), gllightf( ) Materials OpenGL glmaterialfv( ), glmaterialf( ) R.W. Lindeman - WPI Dept. of Computer Science 28
A Snthetic Camera Define: Ee position LookAt point Up vector (if spinning: confusing) Programmer knows scene, chooses: ee lookat Up direction usuall set to (,, ) OpenGL: glulookat( ee.pos[x], ee.pos[y], ee.pos[z], look.pos[x], look.pos[y], look.pos[z], up.vec[x], up.vec[y], up.vec[z] ); R.W. Lindeman - WPI Dept. of Computer Science 29
A Snthetic Camera (cont.) R.W. Lindeman - WPI Dept. of Computer Science 3