290 likes | 528 Views
Transformations Chapter 3. Overview. Review 2-D matrix transformations Homogeneous coordinates Review 3-D matrix transformations 2-D/3-D transformations in OpenGL. Geometric Transformations. Geometric transformations Translation rotation Scaling
E N D
Overview • Review 2-D matrix transformations • Homogeneous coordinates • Review 3-D matrix transformations • 2-D/3-D transformations inOpenGL
Geometric Transformations • Geometric transformations • Translation • rotation • Scaling • Orthogonal projection, perspective projection • Why are geometric transformations necessary? • for positioning geometric objects in 2D and 3D. • for modeling geometric objects in 2D and 3D • For viewing geometric objects in 2D and 3D.
2-D Geometric Transformations • 2-D translation • Translating a point (x, y) means to move it by (tx,ty). tx=2 ty=1
Y 5,4 ¢ é ù é ù é ù tx x x ¢ = = = T P P 2,3 ê ú ê ú ê ú ¢ ty y y ë û ë û ë û 6,2 4,2 3,1 1,1 ¢ = + x x tx X ¢ = + y y ty translation (3,1) 2D Translation • Matrix Representation - x’ is not a linear combination of x and y - y’ is not a linear combination of x and y
Rotation 2D • Rotating a point P=(x,y) through an angle θ about the origin O(0,0) counterclockwise means to determine another point Q=(x´,y´) on the circle centered at O such that θ=∠POQ.
Rotation 2D • Rotation 2D: equations Expanding the expressions of x´ and y´, we have: x ' = r cosφ cosθ − r sinφ sinθ y' = r cosφ sinθ + r sinφ cosθ Replacing rcosφ and rsinφ by x and y in the previous equations, we get:
Y 2,3 3,1 1,1 X Θ é ù x' ¢ = P ê ú ¢ y ë û 2-D Rotation • 2-D rotation matrix representation Although sin(θ) and cos(θ) are not linear functions of θ, x’ is a linear combination of x and y y’ is a linear combination of x and y Rotation, Θ degrees
2-D scaling Scaling an object consists of multiplying each of its point component x and y by a scalar sx and sy, respectively Uniform Scaling Sx=2 Sy=2 Non-Uniform Scaling Sx=2 Sy=0.5
4,6 Y 2,3 2,2 6,2 3,1 1,1 X • 2-D scaling Matrix representation Scaling by factor (sx, sy)
Shearing • Shearing an object consists of linearly deforming it along either x-axis or y-axis or both. X’ = x + y tanƟ Y’ = y + x tanØ Ɵ Ø 11
Fundamental problem of geometrictransformations • Problem: • Rotation/scalingare performed with a matrixmultiplication • Translationis performed with a matrixaddition • Consequence: we are not allowed to effect a sequence of transformations (translations and rotations) through a product of matrices 2x2. • But, we can always produce k rotations by computing the product of k rotation matrices. SOLUTION: homogeneous coordinates!
Homogeneous Coordinates • Processing a sequence of geometrictransformations would be moreefficientif alltransformations could be expressed asmultiplications • Optimise hardwarefor matrix multiplication • Simplecompositionof many transformations
é ù é ù x x / h ê ú ê ú y / h y ê ú ê ú ê ú ê ú 1 h ë û ë û Homogeneous Coordinates • Homogeneous Coordinates • Treattranslationas amatrix multiplication • Requires an extra coordinate: Homogeneous point Cartesian point Usually we use h=1, so the homogenous point = Cartesian point
¢ é ù é ù é ù 1 0 tx x x ê ú ê ú ê ú Y ¢ ¢ = = = 5,4 T 0 1 ty P y P y ê ú ê ú ê ú ê ú ê ú ê ú 0 0 1 1 1 ë û ë û ë û 2,3 ¢ = + x x tx 6,2 4,2 ¢ = + y y ty 3,1 1,1 X translation (3,1) 2-D Geometric Transformations with Homogeneous Coordinates • 2-D translation with homogeneous coordinates
Y ù ¢ 2,3 é ù é ù é x x 0 cos θ -sin θ ú ê ú ê ú ê ¢ ¢ = = = 0 P y P y cos θ R sin θ ú ê ú ê ú ê ú ê ú ê ú ê 1 0 1 1 0 û ë û ë û ë 3,1 1,1 X Θ 2-D Geometric Transformations with Homogeneous Coordinates • 2-D rotation with homogeneous coordinates Rotation, Θ degrees
4,6 ¢ é ù é ù x x Y ê ú ê ú ¢ ¢ = = P y P y ê ú ê ú 2,3 ê ú ê ú 1 1 ë û ë û 2,2 6,2 3,1 1,1 X 2-D Geometric Transformations with Homogeneous Coordinates • 2-D scaling with homogeneous coordinates Scaling by factor (sx, sy)
How do we rotate about this point? Y a,b X Matrix Composition • Series of transformations can beconcatenatedinto acomposite matrix • E.g. how do we rotate about a point other than the origin?
Y Y Y a,b a,b X X Matrix Composition • Apply a series of transformations: • Translate to the origin,T1 • Rotate,R • Translate back,T2
Y Y Y a,b a,b X X X Matrix Composition • Composite transformation:C = T2 R T1 • P’ = C P translate(-a,-b) rotate(90) translate(a,b)
Premultiplying vs. Postmultiplying • When concatenatinga sequence of matrices, agraphics package maypremultiplyor postmultiplythem: • A ispremultipliedby B:B A • A ispostmultipliedby B:A B • E.g. we specify the sequenceA, B, C: • Usingpremultiplying, thecomposite matrixisC (B A) • Usingpostmultiplying, thecomposite matrixis (A B) C
OpenGL Geometric Transformation Functions • OpenGL allows us to specify transformations using: 1. Pre-defined transformations : • glTranslate* • glRotate* • glScale* 2. Arbitary transformations by direct specification of matrices(Matrix coefficients): • glLoadMatrix* • glMultMatrix* These transformations are effected by the modelview matrix (will be discussed latter)
OpenGL Geometric Transformation Functions • glTranslate{f,d} (tx, ty, tz) Causes subsequently defined coordinate positions to be translated by the vector (tx,ty,tz), where tx, ty, tz are either floating-point or double precision numbers. E.g. glTranslated(320.0, 260.0, 0.0); • glRotate{f,d} (theta, vx, vy, vz) 4 arguments: • 1st argument: rotation angle in degree • 2nd/3rd/4th arguments are a vector that defines the axis of rotation E.g. glRotated(45.0, 0.0, 0.0, 1.0); // 45 degrees about z-axis • glScale{f,d} (sx, sy, sz) • Causes subsequently defined coordinate positions to be scaled by factors sx,sy,sz along x, y, and z, respectively. • NOTE: setting any of these factors to zero can cause a processing error. E.g.glScalef(2.0, -3.0, 1.0)
OpenGL Geometric Transformation Functions • glLoadMatrix* (elements16) • Argument is a one-dimensional array of 16 matrix coefficients • 4x4 matrix for homogeneous coordinates • Column-majororder (i.e. coefficients of first column,then coefficients of second column, etc.) • glMultMatrix* (elements16) • Same asglLoadMatrix*,except it multiplies thespecified matrix by thecurrent matrix …
OpenGL Geometric Transformation Functions • OpenGLpostmultiplies matrices with thecurrent matrix • Remember, usingpostmultiplying,if we specify thesequenceA, B, C,the composite matrixis(A B) C(the last transform to be specified is the first one tobe applied) • The following functionspostmultiplywith thecurrent matrix: • glTranslate*, glRotate*, glScale*, glMultMatrix* • To set thecurrent matrixto theidentity matrix: • glLoadIdentity
Y Y Y 320,260 320,260 X X X OpenGL Geometric Transformation Functions glLoadIdentity();glTranslated(320.0, 260.0, 0.0); glRotated(90.0, 0.0, 0.0, 1.0); glTranslated(-320.0, -260.0, 0.0); Translate by (-320,-260,0) Rotate by 90o about z-axis Translate by (320,260)
void draw(){ • // Make background colour yellow • glClearColor( 100, 100, 0, 0 ); • glClear ( GL_COLOR_BUFFER_BIT ); • // modelview matrix for modeling transformations • glMatrixMode(GL_MODELVIEW); • // x-axis • glColor3f(0,0,0); • glBegin(GL_LINES); • glVertex2f(0.0,0.0); • glVertex2f(0.5,0.0); • glEnd(); • // y-axis • glColor3f(0,0,0); • glBegin(GL_LINES); • glVertex2f(0.0,0.0); • glVertex2f(0.0,0.5); • glEnd(); • // RED rectangle • glColor3f( 1, 0, 0 ); • glRectf(0.1,0.2,0.4,0.3); • // Translate GREEN rectangle • glColor3f( 0, 1, 0 ); • glTranslatef(-0.4, -0.1, 0.0); • glRectf(0.1,0.2,0.4,0.3); • // Rotate BLUE rectangle • glColor3f( 0, 0, 1 ); • glLoadIdentity(); // reset the modelview matrix • glRotatef(90, 0.0, 0.0,1.0); • glRectf(0.1,0.2,0.4,0.3); • // Scale MAGENTA rectangle • glColor3f( 1, 0, 1 ); • glLoadIdentity(); // reset the modelview matrix • glScalef(-0.5, 1.0, 1.0); • glRectf(0.1,0.2,0.4,0.3); • // display rectangles • glutSwapBuffers(); } Examples in OpenGL Output