Chapter Four
Chapter Four
(CoSc3121)
1
CHAPTER FOUR
TRANSFORMATIONS
Computer Graphics Chapter Four -
Transformations
2
2
2-D Matrix
Transformation
s
Homogeneous
Coordinates
Matrix
Composition
3-D Matrix
Transformation
s
Right-Handed vs.
Left-Handed
Coordinate Systems
Defining
Transformations in
OpenGL
3
2-D Matrix
Transformations
2-D Matrix 2x2 matrices can be multiplied
Transformation according to the following equation:
s
Matrix
Multiplication
For example,
2-D Translation
2-D Scaling
Matrix multiplication is not
commutative. In other words, for two
2-D Rotation matrices A and B, AB≠BA.
We can see this from the following
example.
4
2-D Matrix
Transformations
2-D Matrix However, matrix
Transformation
s multiplication is associative.
Matrix
This means that if we have three
Multiplication matrices A, B and C, then (AB)C
= A(BC).
2-D Translation We can see this from the
following example.
2-D Scaling
2-D Rotation
5
2-D Translation
2-D Matrix The translation transformation shifts all
Transformation points by the same amount.
s Therefore, in 2-D, we must define two
Matrix translation parameters:
Multiplication
The x-translation tx
The y-translation ty
2-D Translation To translate a point P to P’ we add on a
vector T:
2-D Scaling
2-D Translation
2-D Scaling
2-D Rotation
2-D Translation
2-D Scaling
2-D Rotation
Figure: 2-D Scaling
9
2-D Rotation
2-D Matrix The
Transformation
rotation transformation
s rotates all points about a centre
Matrix of rotation.
Multiplication The rotation transformation has
a single parameter:
2-D Translation
The angle of rotation,θ.
To rotate a point P anti-clockwise by θ, we
2-D Scaling apply the rotation matrix R:
2-D Translation
2-D Scaling
2-D Rotation
2-D Translation
2-D Scaling
2-D Translation
2-D Scaling
2-D Translation
2-D Scaling
2-D Rotation
Therefore 2-D Rotation with Homogenous
Matrix coordinates, which is the same outcome as
Composition before.
15
Matrix Composition
2-D Matrix Matrix composition refers to
Transformation
s combining (i.e. multiplying
Homogeneous
together) a sequence of
Coordinates transformations to produce a
single transformation matrix.
2-D Translation For example, Using matrix
composition, we can achieve this
2-D Scaling using the following sequence of
transformations:
2-D Rotation
Translate from pivot point to
origin,
Matrix
Rotate about origin, θ=90°
Composition Translate from origin back to
pivot point.
16
Matrix Composition
2-D Matrix Here we perform a rotation about the pivot point
Transformation (2,2) by translating by (-2,-2) to the origin,
s rotating about the origin (θ=90°) and then
translating by (2,2) back to the pivot point.
Homogeneous Let us denote our transformations as follows:
Coordinates T1 is a matrix translation by (-2,-2)
2-D Rotation
For instance, if we were to apply the three
transformations to a point P the result would be:
Matrix
Composition
17
Matrix Composition
2-D Matrix For example,
Transformation
s Therefore because T1 is right next to the point
2-D Translation
2-D Scaling
2-D Rotation
Figure: Composing Transformations to Achieve Rotation about an
Arbitrary Pivot Point
Matrix
Composition
18
3-D Translation
3-D Scaling
3-D Rotation
Right-Handed vs.
Left-Handed
Coordinate Systems
Defining Therefore,
Transformations in
OpenGL
20
3-D Scaling with Homogeneous Coordinates
3-D Matrix Similarly, 3-D scaling are defined by three
Transformation scaling parameters, Sx, Sy and Sz.
s The matrix is:
3-D Translation
3-D Scaling
3-D Rotation
Right-Handed vs.
Left-Handed
Coordinate Systems
Defining Therefore,
Transformations in
OpenGL
21
3-D Rotation with Homogenous Coordinates
3-D Matrix For rotations in 3-D we have three
Transformation possible axes of rotation: the x, y and z
s axes.
For a rotation about the x-axis the matrix
3-D Translation is:
3-D Scaling
3-D Rotation
Defining
Transformations in
OpenGL
22
3-D Rotation with Homogenous Coordinates
3-D Matrix For a rotation about the y-axis we use:
Transformation
s
3-D Translation
3-D Scaling
Therefore,
3-D Rotation
Right-Handed vs.
Left-Handed
Coordinate Systems
Defining
Transformations in
OpenGL
23
3-D Rotation with Homogenous Coordinates
3-D Matrix And for a rotation about the z-axis we
Transformation have:
s
3-D Translation
3-D Scaling
Right-Handed vs.
Left-Handed
Coordinate Systems
Defining
Transformations in
OpenGL
24
Right-Handed vs. Left-Handed
3-D Matrix Coordinate Systems
Transformation Most graphics packages use a right-
s
handed coordinate system.
3-D Translation We can visualize the axes of a right-
handed coordinate system by extending
the thumb and first two fingers of the
3-D Scaling right hand so that they are
perpendicular to each other.
The first finger is the x-axis,
3-D Rotation
The second finger is the y-axis and
Defining
Transformations in
OpenGL
25
Defining Transformations in
3-D Matrix OpenGL
Transformation To define matrix transformations in OpenGL we
s must introduce the concepts of premultiplying
and postmultiplying.
3-D Translation If we premultiply matrix A by matrix B the
result is BA.
If we postmultiply A by B the result is AB.
3-D Scaling
Whenever we apply a transformation in OpenGL
it is applied to a current matrix.
3-D Rotation Almost all transformations in OpenGL
postmultiply by this current matrix.
Right-Handed vs. OpenGL always uses a right-handed
Left-Handed coordinate system.
Coordinate Systems
OpenGL always postmultiplies by the current
Defining matrix. This means that the sequence of
Transformations in transformations must be specified in the
OpenGL
reverse order to that which we want them to
be applied.
26
Defining Transformations in
3-D Matrix OpenGL
Transformation
Now let us look at OpenGL
s
functions for defining
3-D Translation
transformations.
In total, there are six functions
3-D Scaling that affect the current matrix.
The following routines can be used in
3-D Rotation OpenGL to define transformations:
glTranslate*(tx,ty,tz)
glLoadMatrix*(elements16)
Defining
Transformations in glMultMatrix*(elements16)
OpenGL
glLoadIdentity(elements16)
27
Defining Transformations in
3-D Matrix OpenGL
Transformation The following routines can be used in
s
OpenGL to define transformations:
3-D Translation glTranslate*(tx,ty,tz) – Postmultiply
the current matrix by a translation
matrix formed from the translation
3-D Scaling
parameters tx, ty and tz.
glRotate*(θ,vx,vy,vz) – Postmultiply
3-D Rotation the current matrix by a rotation matrix
that rotates by θ about the axis defined
Right-Handed vs. by the direction of the vector (vx,vy,vz).
Left-Handed
glScale*(Sx,Sy,Sz) – Postmultiply the
Coordinate Systems
current matrix by a scaling matrix
Defining
Transformations in formed from the scale factors Sx, Sy
OpenGL and Sz.
28
Defining Transformations in
3-D Matrix OpenGL
Transformation The following routines can be used in OpenGL
s to define transformations:
glLoadMatrix*(elements16) -Replaces the
3-D Translation
current matrix with the 16-element array
element16.
3-D Scaling The array should be defined in column-major
order (i.e. the first four elements represent
the first column; the next four represent the
3-D Rotation second column, etc.).
glMultMatrix*(elements16)Postmultiplies
3-D Rotation
Right-Handed vs.
Left-Handed
Coordinate Systems
Defining
Transformations in
OpenGL
Any Question?