Professional Documents
Culture Documents
2D and 3D
Transformations
Doug Bowman
Adapted from notes by Yong Cao
Virginia Tech
1
Transformations
What are they?
• changing something to something else via rules
• mathematics: mapping between values in a range set and
domain set (function/relation)
• geometric: translate, rotate, scale, shear,…
shear,…
Why are they important to graphics?
• moving objects on screen / in space
• specifying the camera’
camera’s view of a 3D scene
• mapping from model space to world space to camera space to
screen space
• specifying parent/child relationships
• …
2
Translation
ty
tx
3
Scaling
Changing the size of an object is called a scale. We scale an object by
scaling the x and y coordinates of each vertex in the object.
hnew
hold
wold wnew
sx=wnew/wold sy=hnew/hold
xnew = sxxold ynew = syyold
4
Rotation about the origin
Consider rotation about the origin by Θ
degrees
• radius stays the same, angle increases by Θ
x' = r cos (φ + θ)
y' = r sin (φ + θ)
x = r cos φ
y = r sin φ
5
Rotation about the origin (cont.)
From the double angle formulas: sin (A + B) = sinAcosB + cosAsinB
cos (A+B ) = cosAcosB - sinAsinB
Thus,
6
Transformations as matrices
Scale:
xnew = sxxold
ynew = syyold
Rotation:
x2 = x1cosθ - y1sin θ
y2 = x1sin θ + y1cos θ
Translation:
xnew = xold + tx
ynew = yold + ty
7
Homogeneous Coordinates
In order to represent a translation as a matrix multiplication
operation we use 3 x 3 matrices and pad our points to become 3
x 1 matrices. This coordinate system (using three values to
represent a 2D point) is called homogeneous coordinates.
8
Composite Transformations
Remember:
• Matrix multiplication is associative, not commutative!
• Transform matrices must be pre-multiplied
• The first transformation you want to perform will be at the far
right, just before the point
9
Composite Transformations -
Scaling
Given our three basic transformations we can create other
transformations.
Before After
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
10
Composite Transforms -
Scaling (cont.)
If we scale a line between (0,0) & (2,0) to twice its length, the
left-hand endpoint does not move.
Before After
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
11
Fixed Point Scaling
Scale by 2 with fixed point = (2,1)
Translate the point (2,1) to the origin
Scale by 2
Translate origin to point (2,1) Before
0 1 2 3 4 5 6 7 8 9 10
After
0 1 2 3 4 5 6 7 8 9 10
12
Example of 2D transformation
Rotate around an arbitrary point A:
P
y
13
Rotate around an arbitrary point
P
y
A -90
P’
14
Rotate around an arbitrary point
We know how to rotate around the origin
P
y
15
Rotate around an arbitrary point
…but that is not what we want to do!
P
y
P’
-90
P’
16
So what do we do?
P
y
17
Transform it to a known case
Translate(-Ax,-Ay)
P
y
P’
A’
18
Second step: Rotation
Translate(-Ax,-Ay)
Rotate(-90)
P
y
P’
A’
x
P’’
19
Final: Put everything back
Translate(-Ax,-Ay)
Rotate(-90)
P
y
Translate(Ax,Ay)
A
P’ P’’’
A’
x
P’’
20
Rotation about arbitrary point
IMPORTANT!: Order
M = T(Ax,Ay) R(-90) T(-Ax,-Ay)
P
y
P’ P’’’
A’
x
P’’
21
Rotation about arbitrary point
Rotate
Translate origin to (x,y)
22
Shears
23
Reflections
Reflection about the y-axis Reflection about the x-axis
24
More Reflections
25
Affine Transformations in 3D
General form
26
Elementary 3D Affine
Transformations
Translation
27
Scaling Around the Origin
28
Shear around the origin
Along x-axis
29
3D Rotation
Various representations
30
Reminder: 2D rotation (z-roll)
31
Three axes to rotate around
Z
32
Z-roll
33
X-roll
Cyclic indexing
34
Y-roll
35
Rigid body transformations
Translations and
rotations
Preserve lines, angles and
distances
36
Inversion of transformations
Translation: T-1(a,b,c) = T(-a,-b
T(-a,-b,,-c)
-c)
Rotation: R-1axis(b) = Raxis{-b)
-b)
Scaling: S-1(sx,
sx,sy,
sy,sz)
sz) = S(1/sx,1/sy,1/sz)
Shearing: Sh-1(a) = Sh(-a)
Sh(-a)
37
Rotation around an arbitrary axis
Euler’
Euler’s theorem: Any rotation or sequence of
rotations around a point is equivalent to a single
rotation around an axis that passes through the
point.
38
Rotation around an arbitrary axis
Axis: u
Point: P
Angle: β
Method:
1. Two rotations to align u
with x-axis
2. Do x-roll by β
3. Undo the alignment
39
Derivation
1. Rz(-φ)Ry(θ)
2. Rx(β)
3. Ry(-θ)Rz(φ)
Altogether:
Ry(-θ)Rz(φ) Rx(β) Rz(-φ)Ry (θ)
40
Properties of affine
transformations
Preservation of affine combinations of points.
Preservation of lines and planes.
Preservation of parallelism of lines and planes.
Relative ratios are preserved
Affine transformations are composed of
elementary ones.
41
General form
Translation
Rotation, Scaling,
Shear
42
Transformations as a change in
coordinate system
All transformations we have looked at
involve transforming points in a fixed
coordinate system (CS).
43
Transforming the CS - examples
Translate(4,4)
Rotate(180°)
44
Why transform the CS?
Objects often defined in a “natural”
natural” or
“convenient”
convenient” CS
(2,2)
45
Drawing in transformed CS
Tell system once how to draw the object,
then draw in a transformed CS to transform
the object
House drawn in a CS
that’s been translated,
rotated, and scaled
46
Mapping between systems
Given:
• The vertices of an object in CS2
• A transformation matrix M that transforms CS1 to CS2
What are the coordinates of the object’
object’s
vertices in CS1?
47
Mapping example
Translate(4,4)
P
48
Mapping rule
In general, if CS1 is transformed by a matrix
M to form CS2, a point P in CS2 is
represented by MP in CS1
49
Another example
Translate(4,4), then
P
Scale(0.5, 0.5)
50
General mapping rule
If CS1 is transformed consecutively by M1,
M2, …, Mn to form CSn+1, then a point P in
CSn+1 is represented by
M1 M2 … Mn P in CS1.
To form the composite transformation
between CSs,
CSs, you postmultiply each
successive transformation matrix.
51
OpenGL Transformations
52
OpenGL Matrices
In OpenGL matrices are part of the state
Multiple types
• Model-View (GL_MODELVIEW
(GL_MODELVIEW))
• Projection (GL_PROJECTION
(GL_PROJECTION))
• Texture (GL_TEXTURE
(GL_TEXTURE)) (ignore for now)
• Color(GL_COLOR
Color(GL_COLOR)) (ignore for now)
Single set of functions for manipulation
Select which to manipulated by
• glMatrixMode(GL_MODELVIEW);
• glMatrixMode(GL_PROJECTION);
53
Current Transformation Matrix
(CTM)
Conceptually there is a 4 x 4 homogeneous
coordinate matrix, the current transformation
matrix (CTM) that is part of the state and is applied
to all vertices that pass down the pipeline
The CTM is defined in the user program and loaded
into a transformation unit
C
p p'=Cp
vertices CTM vertices
54
CTM operations
The CTM can be altered either by loading a new
CTM or by postmutiplication
Load an identity matrix: C ← I
Load an arbitrary matrix: C ← M
55
Rotation about a Fixed Point
Start with identity matrix: C ← I
Move fixed point to origin: C ← CT
Rotate: C ← CR
Move fixed point back: C ← CT -1
Result:
Result: C = TR T –1 which is backwards.
backwards.
56
Reversing the Order
We want C = T –1 R T
so we must do the operations in the following order
C←I
C ← CT -1
C ← CR
C ← CT
Each operation corresponds to one function call in the
program.
Note that the last operation specified is the first executed in
the program
57
CTM in OpenGL
OpenGL has a model-view and a projection
matrix in the pipeline which are
concatenated together to form the CTM
Can manipulate each by first setting the
correct matrix mode
58
Rotation, Translation, Scaling
Load an identity matrix:
glLoadIdentity()
Multiply on right:
59
Example
Rotation about z axis by 30 degrees with a fixed
point of (1.0, 2.0, 3.0)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
glRotatef(30.0, 0.0, 0.0, 1.0);
glTranslatef(-1.0, -2.0, -3.0);
60
Arbitrary Matrices
Can load and multiply by matrices
defined in the application program
glLoadMatrixf(m)
glMultMatrixf(m)
61
Transformations in OpenGL
OpenGL makes it easy to do transformations
to the CS, not the object
Sequence of operations:
• Set up a routine to draw the object in its “base”
base” CS
• Call transformation routines to transform the CS
• Object drawn in transformed CS
62
OpenGL transformation example
drawHouse(){ drawTransformedHouse(){
drawTransformedHouse(){
glBegin(GL_LINE_LOOP); glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_MODELVIEW);
glVertex2i(0,0); glTranslatef(4.0, 4.0, 0.0);
glVertex2i(0,2); glScalef(0.5, 0.5, 1.0);
... drawHouse();
drawHouse();
glEnd();
}
}
63
Matrix Stacks
In many situations we want to save
transformation matrices for use later
• Traversing hierarchical data structures
• Avoiding state changes when executing display lists
glPushMatrix()
glPopMatrix()
64
OpenGL matrix stack
example
M0
room
glLoadMatrixf(m0); M1 M2
glPushMatrix();
glMultMatrixf(m1); table rug
glPushMatrix();
glMultMatrixf(m4); M3 M4
render chair2;
glPopMatrix(); chair1 chair2
glPushMatrix();
glMultMatrixf(m3);
render chair1;
glPopMatrix();
M0*M1*M4
render table;
glPopMatrix(); M0*M1
glPushMatrix();
glMultMatrixf(m2);
render rug; M0
glPopMatrix()
render room;
65