Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
Universidade Federal da Paraíba Centro de Informática Geometric Transformations Lecture 6 1107190 - Introdução à Computação Gráfica – Turma 01 Prof. Christian Azambuja Pagot CI / UFPB Universidade Federal da Paraíba Centro de Informática Transformation Types (Perception) ● Scale. ● Shear. ● Rotation. ● Translation. Universidade Federal da Paraíba Centro de Informática Transformation Classification ● Linear transformations. ● Affine transformations. ● Projective transformations. Universidade Federal da Paraíba Centro de Informática Transformation Classification ● Linear transformations. – Maps straight lines to straight lines. – Can be expressed in matrix form. – Examples: ● Scale, rotation and shear. ● Affine transformations. ● Projective transformations. Linear Universidade Federal da Paraíba Centro de Informática Transformation Classification ● Linear transformations. ● Affine transformations. – Include all linear transformations. – Preserves parallelism between lines. – Examples: ● Scale, rotation, shear and translation. ● Projective transformations. Affine Linear Universidade Federal da Paraíba Centro de Informática Transformation Classification ● Linear transformations. ● Affine transformations. ● Projective transformations. – Include all affine transformations. – Keep straight lines straight. – Do not preserve parallelism between lines. – Examples: ● Scale, roration, shear, translation and homogeneous projection. Projective Affine Linear Universidade Federal da Paraíba Centro de Informática Scale y x y x x '=x⋅sx y '= y⋅s y [ x 'y ' ]=[sx 00 s y ][ xy ] Matrix Form Universidade Federal da Paraíba Centro de Informática Types of Scaling ● Isotropic: ● Anisotropic: y x y x s x≠s yOriginal Scaled y x y x s x=s yOriginal Scaled Universidade Federal da Paraíba Centro de Informática Mirroring sx=1 s y=−1 sx=−1 s y=−1 y x Original y x y x Scaled Scaled Universidade Federal da Paraíba Centro de Informática Scaling off the Origin y x Original y x Scaled Implicit translation s x=2, s y=2 Universidade Federal da Paraíba Centro de Informática Shear ● In 2D: – Shear keeps coordinate U fixed while changes the coordinate V along its axis. The amount of change to V is linear with respect to U. y x y x Original Shear x '=x+mx y y '= y [ x 'y ' ]=[1 mx0 1 ] [ xy ] Matrix form Universidade Federal da Paraíba Centro de Informática Shear ● In 2D: – Can be applied in both directions. [ x 'y ' ]=[ 1 m xm y 1 ][ xy ] y x y x Original Shear ? Universidade Federal da Paraíba Centro de Informática Shear ● Most of the current graphics APIs do not implement shear. ● However, shear can be reproduced through the combination of a rotation, scaling, and another rotation. y x Original y x Scale y x Rotation y x Rotation Universidade Federal da Paraíba Centro de Informática Rotation y x r w α x=r cos(α) y=r sin (α) Universidade Federal da Paraíba Centro de Informática Rotation y x r α w x=r cos(α) y=r sin (α) w' θ y' x' x '=r cos(α+θ) y '=r sin (α+θ) Universidade Federal da Paraíba Centro de Informática Rotation x '=r cos(α)cos (θ)−r sin(α)sin(θ) y '=r sin (α)cos (θ)+r cos (α)sin(θ) x '=r cos(α+θ) y '=r sin (α+θ) x=r cos(α) y=r sin (α) x '=x cos (θ)− y sin(θ) y '= ycos (θ)+ x sin (θ) [ x 'y ' ]=[cos(θ) −sin (θ)sin(θ) cos (θ) ] [ xy ] Universidade Federal da Paraíba Centro de Informática Rotation “at” and “off” Origin y x Original y x Rotated y x Original y x Rotated Universidade Federal da Paraíba Centro de Informática Translation y x Original y x Translated x '=x+d x y '= y+d y Translation is an affine transformation and cannot be represented by a matrix! Universidade Federal da Paraíba Centro de Informática Homogeneous Coordinates (HC) ● In HC, n-dimensional vectors are represented with n+1 components: – Example: (x,y) → (xw, yw, w), where w is the homogeneous coordinate. ● To transform from HC to Euclidean space, divide coordinates by the homogeneous coordinate: – Example: (xw, yw, w) → (xw/w, yw/w) ● Allows the representation of translation by means of a matrix. ● The use of HC gives rise to a Projective Space. Universidade Federal da Paraíba Centro de Informática Translation in HC (x , y) Euclidean space (xw , yw , w) Homogeneous space w=1 (x , y ,1) Homogeneous space x '=x+dx y '= y+d y 1=1 [ x ' y ' 1 ]=[1 0 d x0 1 d y0 0 1 ] [ xy1 ] Universidade Federal da Paraíba Centro de Informática All Other Transformations can be Rewritten in Terms of HC [ x 'y '1 ]=[sx 0 00 s y 00 0 1 ][ xy1 ] [ x 'y '1 ]=[1 mx 00 1 00 0 1] [ xy1 ] [ x 'y '1 ]=[cos(θ) −sin (θ) 0sin(θ) cos (θ) 00 0 1] [ xy1 ] [ x 'y ' ]=[sx 00 s y ][ xy ] Scale [ x 'y ' ]=[1 mx0 1 ] [ xy ] Shear along X axis. [ x 'y ' ]=[cos(θ) −sin (θ)sin(θ) cos (θ) ] [ xy ] Rotation Universidade Federal da Paraíba Centro de Informática How about Transformations in 3D Space? ● 3D transformations can also be represented through matrices. ● As in the 2D case, 3D transformations are expressed with the help of HC to allow for the representation of translation in matrix form. ● 3D vectors in Euclidean space are represented through 4-element vectors in HC: – Example: (x, y, z) → (xw, yw, zw, w) Universidade Federal da Paraíba Centro de Informática 3D Scale ● The scale can be computed along x, y and z axis. x '=x⋅sx y '= y⋅s y z '= z⋅s z Matrix Form in HC [ x 'y 'z '1 ]=[ s x 0 0 0 0 s y 0 0 0 0 sz 0 0 0 0 1 ] [ xyz1 ] Universidade Federal da Paraíba Centro de Informática 3D Shear ● Extra possibilities due the z axis. ● The example below changes x and y by amounts proportional to z: x '=x+mx z y '= y+m y z z '= z [ x 'y 'z '1 ]=[ 1 0 mx 0 0 1 m y 0 0 0 1 0 0 0 0 1][ x y z 1 ] Matrix Form in HC Universidade Federal da Paraíba Centro de Informática 3D Rotation ● Rotation is always ... – … around the coordinate system origin. – … about an axis. ● There are three possibilities for a rotation in 3D space: – About X, Y, or Z axis. Universidade Federal da Paraíba Centro de Informática 3D Rotation about X x y z x '=x y '= y cos (θ)− zsin (θ) z '= y sin (θ)+ zcos (θ) [ x 'y 'z '1 ]=[ 1 0 0 0 0 cos (θ) −sin(θ) 0 0 sin (θ) cos(θ) 0 0 0 0 1 ] [ x y z 1 ] Matrix Form in HC Universidade Federal da Paraíba Centro de Informática 3D Rotation about Y x y z x '=x cos (θ)+ zsin (θ) y '= y z '=−x sin(θ)+z cos (θ) [ x 'y 'z '1 ]=[ cos (θ) 0 sin (θ) 0 0 1 0 0 −sin(θ) 0 cos (θ) 0 0 0 0 1 ] [ x y z 1 ] Matrix Form in HC Universidade Federal da Paraíba Centro de Informática 3D Rotation about Z x y z x '=x cos (θ)− y sin(θ) y '=x sin (θ)+ y cos(θ) z '= z [ x 'y 'z '1 ]=[ cos (θ) −sin(θ) 0 0 sin (θ) cos(θ) 0 0 0 0 1 0 0 0 0 1 ] [ x y z 1 ] Matrix Form in HC Universidade Federal da Paraíba Centro de Informática Gimbal Lock ● Loss of degree of freedom in rotations in 3D space due the parallelism between two of the three gimbal. 3 DOFs Images by MathsPoetry (Wikipedia) 2 DOFs due Gimbal lock (Green and blue gimbals aligned) Universidade Federal da Paraíba Centro de Informática Gimbal Lock ● Animated Gimbal lock Gimbal lock (video) Video by GuerrillaCG (YouTube) Universidade Federal da Paraíba Centro de Informática How to Solve Gimbal Lock? ● Two common approaches: – Change the precedence order of the rotations in advance. – Use quaternions, which is a Gimbal lock-free mathematical rotation representation. Universidade Federal da Paraíba Centro de Informática 3D Translation ● An additional dimension creates one additional DOF regarding translation: x '=x+dx y '= y+d y z '= z+dz [ x ' y ' z ' 1 ]=[ 1 0 0 dx 0 1 0 d y 0 0 1 dz 0 0 0 1 ][ xyz1 ] Matrix Form in HC Universidade Federal da Paraíba Centro de Informática Composing Transformations ● The representation of geometric transformations with matrices allows for the composition of transformations through matrix product! Universidade Federal da Paraíba Centro de Informática Composing Transformations ● Example: – Suppose that one wishes to apply a scale S and then a rotation R on a 3D vertex v 1 , generating, as a result, the transformed vertex v 2 : ● Approach 1: – vtmp = Sv1 – v2 = Rvtmp ● Approach 2: – Matrix product is associative: ● v 2 = R(Sv 1 ) → v 2 = (RS)v 1 – Thus, we can say that M = RS ● v 2 = Mv 1 Be aware that the order of the product matters! M will first apply the scaling S and, after that, the rotation R! Universidade Federal da Paraíba Centro de Informática Composing Transformations ● Example: – Supposing a translation matrix T and a rotation matrix R: ● M 1 = TR (applies rotation first, and then translation.) ● M 2 = RT (applies translation first, and then rotation.) M 1 M 2y x Original y x Original y x Transformed y x Transformed Universidade Federal da Paraíba Centro de Informática Transformations in OpenGL ● Transformation functions in OpenGL: – glRotatef(<degrees>, <x>, <y>, <z>) – glScalef(sx, sy, sz) – glTranslatef(dx, dy, dz) ● Those transformations will affect the current active matrix! ● OBS: The last transformation called is the first to be applied! Universidade Federal da Paraíba Centro de Informática Transformations in OpenGL ● Code example: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 1.0f, 2.0f); glRotatef(45.0f, 0, 1, 0); glScalef(0.2f, 4.0f, 3.0f); glutSolidSphere(1.0f, 20, 20); Set active matrix Reset the active matrix 3rd. transformation to be applied 2nd. transformation to be applied 1st. transformation to be applied Draws a sphere Slide 1 Slide 2 Slide 3 Slide 4 Slide 5 Slide 6 Slide 7 Slide 8 Slide 9 Slide 10 Slide 11 Slide 12 Slide 13 Slide 14 Slide 15 Slide 16 Slide 17 Slide 18 Slide 19 Slide 20 Slide 21 Slide 22 Slide 23 Slide 24 Slide 25 Slide 26 Slide 27 Slide 28 Slide 29 Slide 30 Slide 31 Slide 32 Slide 33 Slide 34 Slide 35 Slide 36 Slide 37