Transformations of Objects
CVG lab
Introduction
Affine transformations : Affine transformations are a fundamental cornerstone of computer graphics and are central to OpenGL as well as most other graphics systems.
Introduction to Transformations
The overall transformation is a combination of three elementary ones : scaling, rotation and translation.
Transformations are useful in a number of situations :
1. Composing a “scene” out of a number of objects.
2. Creating a complex object from a single “motif”.
3. A designer may to view an object from different vantage points and make a picture from each one.
4. In a computer animation, several objects must move to one another from frame to frame.
Introduction to Transformations
Introduction to Transformations
Where Are We Headed? Using Transformations with OpenGL
A number of graphics platforms, including OpenGL, provide a “graphics pipeline,” or a sequence of operations that are applied to all points that are “sent through”.
glBegin (GL_LINES)
glVertex3f( . . .);//send P1 through
the pipeline
glVertex3f( . . .);//send P2 through
the pipeline
glVertex3f( . . .);//send P2 through
the pipeline
glEnd();
Introduction to Transformations
Object Transformations versus Coordinate Transformations
-. There are two ways to view a transformation : as an object transformation or as a coordinate transformation.
. An object transformation alters the coordinates of each point on the object to some rule, leaving the underlying coordinate system fixed.
. A coordinate transformation defines a new coordinate system in terms of the old one and then represents all of the object’s points in this new system.
Introduction to Transformations
1. Transforming Points and Objects
A transformation alters each point P in space (2D or 3D) into a new poi
nt Q by means of a specific formula or algorithm.
P = (Px, Py, 1)
Q = (Qx, Qy, 1)
(Qx, Qy, 1) = T (Px, Py, 1)
Q = T(P).
Introduction to Transformations
2. The Affine Transformations -. Affine transformations are the most common transformations used in co
mputer graphics. -. Affine transformations have a simple form : The coordinates of Q are lin
ear combinations of those of P. That is,
(Qx, Qy, 1) = (m11Px+m12Py+m13, m11Px+m12Py+m13, 1)
For an affine transformation, the third row of the matrix is always (0, 0, 1)
11001
232221
131211
y
x
y
x
P
P
mmm
mmm
Q
Q
Introduction to Transformations
3. Geometric Effects of Elementary 2D Affine Transformations
-. Affine transformations produce combinations of four elementary transformations : translation, scaling, rotation, and shear.
a. Translation
b. Scaling
1100
10
01
1
y
x
y
x
P
P
dy
dx
Q
Q
1100
00
00
1
y
x
y
x
y
x
P
P
S
S
Q
Q
Introduction to Transformations
c. Rotation
d. Shearing
1100
0)cos()sin(
0)sin()cos(
1
y
x
y
x
P
P
aa
aa
Q
Q
1100
010
01
1
y
x
y
x
P
Ph
Q
Q
1100
01
001
1
y
x
y
x
P
P
gQ
Q
Introduction to Transformations
4. The Inverse of an Affine Transformation
Most affine transformations of interest are nonsingular, which means that the determinant of m
Det M = m11m22 – m12m21
Q = MP by the inverse of M, denoted M-1
P = M-1 Q The inverse of M
M-1 = 1/det M
1121
1222
mm
mm
Introduction to Transformations
a. Scaling
b. Rotation
1100
10
01
1
y
x
y
x
P
P
dy
dx
Q
Q
1100
0/10
00/1
1
y
x
y
x
y
x
P
P
S
S
Q
Q
Introduction to Transformations
c. Shearing
d. Translations
1100
0)cos()sin(
0)sin()cos(
1
y
x
y
x
P
P
aa
aa
Q
Q
1100
010
01
1
y
x
y
x
P
Ph
Q
Q
1100
01
001
1
y
x
y
x
P
P
gQ
Q
Introduction to Transformations
5. Composing Affine Transformations
-. The process of applying several transformations in succession to form one overall transformation is called composing(or concatenating) the transformations.
W = MP
M = M2M1
Introduction to Transformations
6. Example Composing 2D Transformations
Introduction to Transformations
7. Some Useful Properties of Affine Transformations a. Affine Transformations Preserve Affine Comvinations of Points
W = a1P1 + a2P2
T(a1P1 + a2P2) = a1T(P1) + a2T(P2)
MW = M (a1P1 + a2P2) = a1MP1 + a2MP2
b. Affine Transformations Preserve Lines and Planes L(t) = (1 – t)A + tB Q(t) = (1 - t)T(A) + tT(B) P(s, t) = sA + tB + (1 - s- t)C T(P(s, t)) = sT(A) + tT(B) + (1 – s – t)T(C)
Introduction to Transformations
c. Parallelism of Lines and Planes is Preserved
d. The Columns of the Matrix Reveal the Transformed Coordinate Frame
M = = (m1 | m2 | m3)
)1, 0, 0 = (ט , I = (1, 0, 0), j = (0, 1, 0)
m1 = Mi.
P = Pxi + Pyj + ט
Q = Pxm1 + Pym2 + m3
000
132221
131211
mmm
mmm
Introduction to Transformations
e. Relative Ratios Are Preserved
f. Effect of Transformations on the Areas of Figures
area after transformation / area before transformation = |det M|
Introduction to Transformations
f. Every Affine Transformation Is Composed of Elementary Operations
-. 2D affine Transformation :
M = (translation)(shear)(scaling)(rotation)
-. 3D affine transformation :
M = (translation)(scaling)(rotation)(shear1)(shear2)
3D AFFINE TRANSFORMATIONS
P = Pxi + Pyj +PZK + ט
P = (PX, Py, Pz, 1)
M =
= M
1000
34333231
24232221
14131211
mmmm
mmmm
mmmm
1
x
x
x
Q
Q
Q
1
x
x
x
P
P
P
3D AFFINE TRANSFORMATIONS
1. The Elementary 3D Transformations
a. Translation
b. Scaling
1000
100
010
001
34
24
14
m
m
m
1000
000
000
000
z
y
x
S
S
S
3D AFFINE TRANSFORMATIONS
c. Shearing
d. Rotations
-. Elementary rotations about a coordinate axis
Positive values of β cause a counterclockwise (CCW) rotation about an axis as one looks inward from a point on the positive axis toward the origin.
1000
0100
001
0001
f
3D AFFINE TRANSFORMATIONS
90° rotation :
. For a z-roll, the x-axis rotates to the y-axis.
. For a x-roll, the y-axis rotates to the x-axis.
. For a y-roll, the z-axis rotates to the x-axis.
3D AFFINE TRANSFORMATIONS
. An x-roll
Rx(β) =
. A y-roll
Ry(β) =
1000
00
00
0001
cs
sc
1000
00
0010
00
cs
sc
3D AFFINE TRANSFORMATIONS
. An x-roll
Rz(β) =
1000
0100
00
00
cs
sc
3D AFFINE TRANSFORMATIONS
2. Composing 3D Affine Transformations
M = M2M1
3D AFFINE TRANSFORMATIONS
3. Combining Rotations
M = Rz(β3)Ry(β2)Rx(β1)
Rotations about an Arbitrary Axis
EUSER’S THEOREM : Any rotation (or sequence of rotations) about a point is equivalent to a single rotation about some axis through that point.
-. The Classic Way : We decompose the required rotation into a sequence of known steps:
3D AFFINE TRANSFORMATIONS
1. Perform two rotations so that u becomes aligned with the x-axis.
2. Do a z-roll through the angle β.
3. Undo the two alignment rotations to restore u to its original direction.
Ru(β) = Ry(-θ)Rz(Φ)Rx(β)Rz(- Φ)Ry(θ)
The Constructive Way
3D AFFINE TRANSFORMATIONS
Ru(β) =
1000
0100
00
00
cs
sc
3D AFFINE TRANSFORMATIONS
Rotations about an Arbitrary Axis
Ru(β) =
ux =( m32 – m23 )/ (2sin (β))
uy =( m13 – m31 )/ (2sin (β))
uz =( m21 – m12 )/ (2sin (β))
1000
0
0
0
333231
232221
131211
mmm
mmm
mmm
CHANGING COORDINATE SYSTEMS
(Px, Py, 1)T =
= M
1
y
x
p
p
1
b
a
1
d
c
CHANGING COORDINATE SYSTEMS
Successive Changes in a Coordinate Frame
= M1 = M1M2
1
b
a
1
d
c
1
f
e
CHANGING COORDINATE SYSTEMS
-. Transforming Points
M = M3 * M2 * M1.
-. Transforming the Coordinate System
M = M1 * M2 * M3
USING AFFINE TRANSFORMATIONS IN A PROGRAM
glBegin (GL_LINES) glVertex2d(V[0].x, V[0].y); glVertex2d(V[1].x, V[1].y); glVertex2d(V[2].x, V[2].y); ….. // the remaining points glEnd ( );
In either case, cvs.setWindow ( . . .) ; cvs.setViewport ( . . .) ;
USING AFFINE TRANSFORMATIONS IN A PROGRAM
The Hard Way
Q = transform2D (M, P);
cvs.moveTo (transform2D (M,V[0]))
cvs.lineTo (transform2D (M,V[1]))
cvs.lineTo (transform2D (M,V[2]))
…
USING AFFINE TRANSFORMATIONS IN A PROGRAM
The Easy Way
OpenGL maintains a so-called modelview matrix, and every vertex that is passed down the graphics pipeline is multiplied by this matrix.
USING AFFINE TRANSFORMATIONS IN A PROGRAM
CT = CT*M
. glScaled (sx, sy, 1.0) ; Postmultiply CT by a matrix that performs a scaling by sx in x and by sy in y ; put the result back into CT. No scaling in z is done.
. glScaled (dx, dy, 1.0) ; Postmultiply CT by a matrix that performs a translation by dx in x and by dy in y ; put the result back into CT. No scaling in z is done.
. glScaled (angle, 0, 1.0) ; Postmultiply CT by a matrix that performs a rotation through angle degrees about the z-axis. Put the result back into CT.
USING AFFINE TRANSFORMATIONS IN A PROGRAM
USING AFFINE TRANSFORMATIONS IN A PROGRAM
for (int count = 0; count<5; count ++) { starMotif ( ); cvs.rotate2D(72.0);//concatenate another rotation } void drawFlake() { for(int count = 0;count<6;count++) { flakeMotif(); cvs.scale2D(1.0, -1.0); flakeMotif(); cvs.scale2D(1.0, -1.0); cvs.rotate2D(60.0);}}
USING AFFINE TRANSFORMATIONS IN A PROGRAM
1. Saving the CT for Later Use
USING AFFINE TRANSFORMATIONS IN A PROGRAM
DRAWING 3D SCENES WITH OPENGL
DRAWING 3D SCENES WITH OPENGL
Some OpenGL Tools for Modeling and Vewing -. Setting the Camera in OpenGL (for a Parallel Projection) glMatrixMode (GL_PROJEXTION); glLoadIdentity ( ); glOrtho (left, right, bottom, top, near, far); Positioning and Aiming the Camera glMatixMode (GL_PROJECTION); glLoadIdentity ( ); glOrhto (-3.2, 3.2, -2.4, 2.4, 1, 50); glMatrixMode (GL_MODELVIEW); glLoadIdentity ( ); gluLookAt (4, 4, 4, 0, 1, 0, 0, 1, 0);