Computer Graphics Lecture 6-7
Computer Graphics Lecture 6-7
Computer Graphics Lecture 6-7
Lecture 6 & 7
Transformations
1
Transformations.
What is a transformation?
• P=T(P)
What does it do?
Transform the coordinates / normal vectors of objects
Why use them?
• Modelling
-Moving the objects to the desired location in the
environment
-Multiple instances of a prototype shape
-Kinematics of linkages/skeletons – character animation
• Viewing
– Virtual camera: parallel and perspective projections
2
Types of Transformations
– Geometric Transformations
• Translation
• Rotation
• scaling
• Linear (preserves parallel lines)
• Non-uniform scales, shears or skews
– Projection (preserves lines)
• Perspective projection
• Parallel projection
– Non-linear (lines become curves)
• Twists, bends, warps, morphs,
3
Geometric Transformation
– Once the models are prepared, we need to place them in
the environment
– Objects are defined in their own local coordinate
system
– We need to translate, rotate and scale them to put them
into the world coordinate system
15/01/20 Lecture 1 4
2D Translations.
Point P defined as P( x, y ),
translate to Point P( x, y) a distance d x parallel to x axis, d y parallel to y axis.
x x d x y y d y
Define the column vectors
x x d x
P , P , T
y y dy P’
Now P
P P T
5
2D Scaling from the origin.
Point P defined as P ( x, y ),
Perform a scale (stretch) to Point P( x, y) by a factor s x along the x axis,
and s y along the y axis.
x s x . x, y s y . y
Define the matrix P’
P
sx 0
S
0 s y
Now
x s x 0 x
P S P or y 0 .
s y y
6
2D Rotation about the origin.
P’(x’,y’)
P(x,y)
r
r
x
7
2D Rotation about the origin.
y
P’(x’,y’)
r
P(x,y) x r. cos
y r. sin
y
r
x
x
8
2D Rotation about the origin.
x r. cos( ) r. cos . cos r. sin . sin
y y r. sin( ) r. cos . sin r. sin . cos
P’(x’,y’)
P(x,y)
r x r. cos
y r. sin
y
r
x
x
9
2D Rotation about the origin.
x r. cos( ) r. cos . cos r. sin . sin
y r. sin( ) r. cos . sin r. sin . cos
Substituting for r :
x r. cos
y r. sin
Gives us :
x x. cos y. sin
y x. sin y. cos
10
2D Rotation about the origin.
x x. cos y. sin
y x. sin y. cos
Rewriting in matrix form gives us :
x cos sin x
y sin .
cos y
cos sin
Define the matrix R , P R P
sin cos
11
Transformations.
• Translation.
– P=T + P
• Scale
– P=S P
• Rotation x cos sin x
y sin cos . y
– P=R P
• We would like all transformations to be multiplications so
we can concatenate them
• express points in homogenous coordinates.
12
Example: 2D Geometric
Transformation
Modeling
Coordinates
World Coordinates
Example: 2D Scaling
Modeling
Coordinates
Scale(0.3, 0.3)
World Coordinates
Example: 2D Rotation
Modeling
Coordinates
Scale(0.3, 0.3)
Rotate(-90)
World Coordinates
Example: 2D Translation
Modeling
Coordinates
Scale(0.3, 0.3)
Rotate(-90)
Translate(5, 3)
World Coordinates
Example: 2D Geometric
Transformation
Modeling
Coordinates
Again?
World Coordinates
Example: 2D Geometric
Transformation
Modeling
Coordinates
Scale
Translate
Scale
Rotate
Translate
World Coordinates
Basic 2D Transformations
• Translation
– x x tx
– y y ty
• Scale
– x x sx
– y y sy
• Rotation
– x x cosθ - y sinθ
– y y sinθ y cosθ
• Shear
–
x x hx y
–
y y hy x
Basic 2D Transformations
• Translation
– x x tx
– y y ty
• Scale
– x x sx
– y y sy
• Rotation
– x x cosθ - y sinθ Transformations
– y y sinθ y cosθ can be combined
• Shear (with simple algebra)
–
x x hx y
–
y y hy x
Basic 2D Transformations
• Translation
– x x tx
– y y ty
• Scale
– x x sx
– y y sy
• Rotation
– x x cosθ - y sinθ
– y y sinθ y cosθ
• Shear x x sx
– y y sy
x x hx y
–
y y hy x
Basic 2D Transformations
• Translation
– x x tx
– y y ty
• Scale
– x x sx
– y y sy
• Rotation
– x x cosθ - y sinθ
– y y sinθ y cosθ
• Shear x ((x sx) cos (y sy) sin )
– y ((x sx) sin (y sy) cos )
x x hx y
–
y y hy x
Basic 2D Transformations
• Translation
– x x tx
– y y ty
• Scale
– x x sx
– y y sy
• Rotation
– x x cosθ - y sinθ
– y y sinθ y cosθ
• Shear x ((x sx) cos (y sy) sin ) tx
– y ((x sx) sin (y sy) cos ) ty
x x hx y
–
y y hy x
Basic 2D Transformations
• Translation
– x x tx
– y y ty
• Scale
– x x sx
– y y sy
• Rotation
– x x cosθ - y sinθ
– y y sinθ y cosθ
• Shear x ((x sx) cos (y sy) sin ) tx
– x x hx y y ((x sx) sin (y sy) cos ) ty
– y y hy x
Matrix Representation
• Represent a 2D Transformation by a Matrix
a b
c d
2D Scaling
x sx x x sx 0 x
y sy y y 0 sy y
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Rotation
x cos x sin y x cos sin x
y sin cos y
y sin x cos y
2D Shearing
x x shx y x 1 shx x
y shy x y y shy 1 y
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Mirror over Y axis
x x x 1 0 x
y y y 0 1 y
2D Mirror over (0,0)
x x x 1 0 x
y y y 0 1 y
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Translation
x x tx
y y ty NO!!
32
Translations in homogenised
coordinates
• Transformation matrices for 2D translation
are now 3x3.
x 1 0 d x x x x d x
y 0 1 d . y
y y y d y
1 0 0 1 1 11
33
Concatenation.
So we expect :
T (d x1 , d y1 ) T (d x 2 , d y 2 ) T (d x1 d x 2 , d y1 d y 2 )
34
Concatenation.
0 0 1 0 0 1
Matrix product is variously referred to as compounding,
concatenation, or composition
35
Concatenation.
The matrix product T (d x1 , d y1 ) T (d x 2 , d y 2 ) is :
1 0 d x1 1 0 d x 2 1 0 d x1 d x 2
0 1 d .0 1 d 0 1 d d
y1 y2 y1 y2
36
Properties of translations.
1. T (0,0) I
2. T ( s x , s y ) T (t x , t y ) T ( s x t x , s y t y )
3. T ( s x , s y ) T (t x , t y ) T (t x , t y ) T ( s x , s y )
4. T -1 ( s x , s y ) T ( s x , s y )
37
Homogeneous form of scale.
Recall the (x,y) form of Scale : sx 0
S (sx , s y )
0 s y
In homogeneous coordinates :
sx 0 0
S ( s x , s y ) 0 sy 0
0 0 1
38
Concatenation of scales.
39
Homogeneous form of rotation.
x cos sin 0 x
y sin cos 0 . y
1 0 0 1 1
40
Orthogonality of rotation
matrices.
cos sin 0 cos sin 0
R( ) sin cos 0 , RT ( ) sin cos 0
0 0 1 0 0 1
41
Other properties of rotation.
R(0) I
R( ) R( ) R( )
and
R( ) R( ) R( ) R ( )
But this is only because the axis of rotation
is the same
For 3D rotations, need to be more careful
42
How are transforms combined?
Scale then Translate
(5,3)
Scale(2,2) (2,2) Translate(3,1)
(1,1) (3,1)
(0,0) (0,0)
02/10/09 Lecture 4 44
Non-commutative Composition
Scale then Translate: p' = T ( S p ) = TS p
1 0 3 2 0 0 2 0 3
TS = 0 1 1 0 2 0 = 0 2 1
0 0 1 0 0 1 0 0 1
48
Transformations of coordinate
systems.
• Have been discussing transformations as
transforming points.
• Always need to think the transformation in the
world coordinate system
• Sometimes this might not be so convenient
– i.e. rotating objects at its location
49
Transformations of coordinate
systems - Example
• Concatenate local
transformation matrices
from left to right
• Can obtain the local –
world transformation
matrix
• p’,p’’,p’’’ are the world
coordinates of p after
each transformation
50
Transformations of coordinate
systems - example
52
Solution
• The side mirror position is locally
(0,4,0.3)
• The matrix of first driving
forward 5m is
1 0 0
T1 0 1 5
0 0 1
53
Solution
• The matrix to turn to the right 30
and 45 degrees (rotating -30 and
-45 degrees around the origin)
are
3 1 1 1
0 0
2 2 2 2
1 3 1 1
R1 0, R2 0, respectively
2 2 2 2
0 0 1 0 0 1
54
Solution
The local-to-global transformation matrix at
the last configuration of the car is
3 1 1 1
0 0
1 0 0 2 2 1 0 0 2 2
TR1TR2 1 3
0 1 1
0
0 1 5 2 2 0 1 5 2 2
0 0 1 0 0 1 0 0 1 0 0 1
55
This is convenient for character
animation / robotics
• In robotics / animation, we
often want to know what is
the current 3D location of
the end effectors (like the
hand)
• Can concatenate matrices
from the origin of the body
towards the end effecter
56
Transformations of coordinate
systems.
Define P ( i ) as a point in coordinate system i
Define M i j as the transform that converts a point in system j to a point in system i
P (i ) M i j P ( j )
and
P ( j ) M j k P ( k )
we obtain by substitution :
M i k M i j M j k
It can also be shown that :
M j i M i1 j
Lecture 4 57
3D Transformations.
• Use homogeneous coordinates, just as in 2D case.
• Transformations are now 4x4 matrices.
• We will use a right-handed (world) coordinate
system - ( z out of page ).
Lecture 4 58
Translation in 3D.
Simple extension to the 3D case:
1 0 0 dx
0 1 0 d y
T (d x , d y , d z )
0 0 1 dz
0 0 0 1
59
Scale in 3D.
Simple extension to the 3D case:
s x 0 0 0
0 sy 0 0
S (s x , s y , s z )
0 0 sz 0
0 0 0 1
60
Rotation in 3D
• Need to specify which axis the rotation is about.
• z-axis rotation is the same as the 2D case.
cos sin 0 0
sin cos 0 0
R z ( )
0 0 1 0
0 0 0 1
61
Rotating About the x-axis Rx()
x 1 0 0 0 x
y 0 cos θ sin θ 0 y
z 0 sin θ cos θ 0 z
1 0 0 0 1 1
62
Rotating About the y-axis
Ry()
x cos θ 0 sin θ 0 x
y 0 1 0 0 y
z sin θ
0 cos θ 0 z
1 0 0 0 1 1
63
Rotation About the z-axis
Rz()
x cos θ sin θ 0 0 x
y sin θ cos θ 0 0 y
z 0 0 1 0 z
1 0 0 0 1 1
64
Rotation about an y
Rotate(k, θ)
arbitrary axis θ
• About (ux, uy, uz), a unit
vector on an arbitrary axis u
x
z
Rx ( ) R y ( ) R y ( ) Rx ( )
Shearing
1 a 0 0 1 0 0 0
0 a 1 0 0
1 0 0
0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
67
Calculating the world
coordinates of all vertices
• For each object, there is a local-to-global
transformation matrix
• So we apply the transformations to all the
vertices of each object
• We now know the world coordinates of all
the points in the scene
68
Normal Vectors
• We also need to know the direction of the normal
vectors in the world coordinate system
• This is going to be used at the shading operation
• We only want to rotate the normal vector
• Do not want to translate it
69
Normal Vectors - (2)
• We need to set elements of the translation
part to zero
70
Viewing
• Now we have the world coordinates of all
the vertices
• Now we want to convert the scene so that it
appears in front of the camera
71
View Transformation
We want to know the positions in the camera coordinate system
We can compute the camera-to-world transformation matrix using
the orientation and translation of the camera from the origin of the
world coordinate system
Mw←c
Lecture 4 72
View Transformation
We want to know the positions in the camera coordinate system
vc = M -1
w←c vw
= Mc ←w vw
73
Summary.
• Transformations: translation, rotation and scaling
• Using homogeneous transformation, 2D (3D)
transformations can be represented by multiplication of a
3x3 (4x4) matrix.