Geometric Transformations in OpenGL
Geometric Transformations in OpenGL
Geometric Transformations in OpenGL
3: Geometric Transformations
Summary
Motivation. !Euclidean transformations: translation and rotation. !Euclidean geometry. !Homogeneous coordinates. !Affine transformations: translation, rotation, scaling, and shearing. !Matrix representation of affine transformations. !Composition of geometric transformations in 2D and 3D. !Affine transformations in OpenGL. !OpenGL matrix operations and arbitrary geometric transformations. !Examples in OpenGL.
!
Motivation
!
Geometric transformations
Translation, rotation, reflection " Scaling, shearing " Orthogonal projection, perspective projection
"
They allow us to define an object through its local coordinate system (modeling coordinates) They allow us to define an object several times in a scene with a global coordinate system (world coordinates)
y
Scaling Translation Global Coordinates (world coordinates)
Global Coordinates
x
Positioning
Scaling
y x
Global Coordinates
x
Rotation
y x
Translation
y x
7
Translation 2D
x=x+"x y=y+"y Translating a point (x, y) means to move it by ("x,"y).
"x=2 "y=1
8
http://encyclopedia.laborlawtalk.com/Linear_combination
" "
Rotation 2D
(x, y) # (x, y) 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 centred at O such that #=$POQ.
10
Expanding the expressions of x (x, y) and y, we have: $ x ' = r cos ! cos" # r sin ! sin " % & y ' = r cos ! sin " + r sin ! cos" % Replacing r cos(f) and r sin(f) by x and y in the previous equations, we get:
12
Translation is not a linear transformation of x and y. Consequence: we are not allowed to effect a sequence of transformations (tranlations 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!
13
http://www.geom.uiuc.edu/docs/reference/CRC-formulas/node6.html
Homogeneous coordinates
!
A triple of real numbers (x,y,t), with t&0, is a set of homogeneous coordinates for the point P with cartesian coordinates (x/t,y/t). Thus, the same point has many sets of homogeneous coordinates. So, (x,y,t) e (x,y,t) represent the same point if and only if there is some real scalar ' such that x= 'x, y= 'y and t= 't. So, if P has cartesian coordinates (x,y), one set of homogeneous coordinates for P is (x,y,1), being this set the most used in computer graphics.
P
Q
t =1
O
x
14
Translation
Rotation
! x ' $ ! cos' # y ' & = # sin ' # & # # "1 & % # " 0
15
http://planetmath.org/encyclopedia/Geometry.html
Set of geometric transformations: translations and rotations (also called isometries). By using homogeneous coordinates, these transformations can be represented through matrices 3x3. This enables the use of product operator for matrices to evaluate a sequence of translations and rotations The set of isometries I(n) in Rn and the concatenation operator form a group GI(n)=(I(n),). Fundamental metric invariant:
"
" "
"
Associativity Axiom. For all c1 ,c2 ,c3 (C, c1 (c2 c3) = (c1 c2) c3
17
http://planetmath.org/encyclopedia/Geometry.html
Geometria afim
! ! !
It generalizes the Euclidean geometry. Set of affine transformations (or affinities): translation, rotation, scaling and shearing. The set A(n) of affinities in Rn and the concatenation operator form a group GA(n)=(A(n),). Fundamental invariant:
"
parallelism.
Other invariants:
" "
distance ratios for any three point along a straight line co-linearity
Examples:
" "
a square can be transformed into a rectangle a circle can be transformed into an ellipsis
18
Scaling 2D
" x ' = !x x # $ y ' = !y y
Scaling an object consists of multiplying each of its point component x and y by a scalar )x and )y, respectively.
)x = 2 )y = 2
19
Non-uniform scaling
" x ' = !x x # $ y ' = !y y
com
!x " !y
Non-uniform scaling an object consists of multiplying each of its point component x and y by a scalar )x and )y, respectively, with )x&)y.
)x = 2 )y = 0.5
20
Shearing
"x ' = x + ! x y # $y ' = y + ! y x
Shearing an object consists of linearly deforming it along either x-axis or y-axis or both.
*x = 1 *y = 0
21
Translation
! x ' $ ! cos' # y ' & = # sin ' # & # # "1 & % # " 0 ( sin ' cos' 0
Scaling
Rotation
Shearing
22
The composition operator is the product of matrices. It is a consequence of the Associativity Axiom of the affine geometry and the dimension 3x3 of the matrices associated to 2D affine transformations. REMARK:
" " "
The order of the composition matters. The matrix product is not commutative. The affine geometry does not satisfy the Commutativity Axiom.
Example:
! x ' $ + ! 1 0 'x $ ! cos( # y ' & = - # 0 1 'y & # sin ( # & -# &# - 0 0 1 &# 0 # "1 & % ,# " %"
) sin ( cos( 0
*y 0
23
P
Wrong Rot(30)
P
Right Tr(-2) Rot(30) Tr(2)
24
P
! x ' $ ) ! 1 0 '2 $ ! cos( # y ' & = + # 0 1 0 & # sin ( # & +# &# + 0 0 1 &# 0 # "1 & % *# " %"
P
' sin ( cos( 0
P
0 $ ! 1 0 2 $, ! x $ # 0 1 0 &. # y & 0& &# &. # & . 1& 1& %# "0 0 1 & %- # " %
25
3D affine transformations
! x '$ !1 # y ' & #0 # &=# # z ' & #0 # & # "1 % " 0 0 1 0 0 0 0 1 0 0$ !x$ 0& #y& &# & 0& #z & &# & 1 % "1 % ! x ' $ ! 'x # y '& # 0 # &=# #z ' & # 0 # & # "1 % " 0 0 'y 0 0 0 0 'z 0 0$ !x$ 0& #y& &# & 0& #z & &# & 1 % "1 %
Identity
Scaling
0 1 0 0
0 'x $ ! x $ 0 'y & # y & &# & 1 'z & # z & &# & 0 1 % "1 %
0 1 0 0
0 0 1 0
0$ !x$ 0& #y& &# & 0& #z & &# & 1 % "1 %
26
Translation
Reflection across YZ
! x ' $ ! cos' # y '& # 0 # &=# # z ' & # ( sin ' # & # "1 % " 0 0 ! x '$ !1 # y ' & # 0 cos' # &=# # z ' & # 0 sin ' # & # 0 "1 % " 0
0$ !x$ 0& #y& &# & 0& #z & &# & 1 % "1 % 0$ !x$ 0& #y& &# & 0& #z & &# & 1 % "1 %
27
Pre-defined transformations: glTranslate, glRotate and glScale. Arbitary transformations by direct specification of matrices: glLoadMatrix,
glMultMatrix
! !
These transformations are effected by the modelview matrix. For that, we have to say that it is the current matrix. This is done through the statement glMatrixMode(GL_MODELVIEW). Let us say that the OpenGL has even a stack for each sort of matrix. It has 4 matrix sorts: modelview, projection, texture, and colour matrices. The modelview stack is initialized with the identity matrix.
28
Causes subsequently defined coordinate positions to be translated by the vector (dx,dy,dz), where dx, dy, dz are either floating-point or double
precision numbers.
Causes subsequently defined coordinate positions to be rotated by angle degrees about the axis (vx,vy,vz) through the origin.
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.
29
glLoadIdentity()
"
glLoadMatrix{f,d}(<array>)
"
Sets the current matrix to be given by the 16 elements of argument 1dimensional array. The entries must be given in column major order.
"
glMultMatrix{f,d}(<array>)
"
Post-multiplies the current matrix M with the matrix N that is specified by the elements in the argument 1-dimensional array: M=M.N
30
31
Examples in OpenGL
Cumulative affine transformations Non-cumulative affine transformations Stack-controlled cumulative affine transformations
32
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();
33
34
36
37
38
40
41
// Scale and translate MAGENTA rectangle glColor3f( 1, 0, 1 ); glScalef(-0.5, 1.0, 1.0); glRectf(0.1,0.2,0.4,0.3); // display rectangles glutSwapBuffers(); // end of draw()
42
44
FIM