You are on page 1of 38

School of Computing & Informatics

Department of Computer Science

Chapter 5

Geometrical Transformation

2/18/23 Computer Graphics MTU – CS


General Transformation
2

What are they?


 changing something to something else via rules
 transformation maps points to other points and/or
vectors to other vectors (x, y, z) -> (x’, y’, z’)
 Basic transformation: translate, rotate, scale,
shear, reflection, shear,…
Why are they important to graphics?
 moving objects on screen / in space
 specifying the camera’s view of a 3D scene
 mapping from model space to world space to camera
space to screen space . . . . .
2/18/23 Computer Graphics MTU – CS
3
2D Transformations
Review basics of matrices 2x2 – can be multiplied

For example;

# of columns of the 1st matrix = # rows of 2nd


Matrix multiplication is
- not commutative AB≠BA
- associative (AB)C = A(BC)
2/18/23 Computer Graphics MTU – CS
Translation
4

Shifts all points by the same amount from one


coordinate location to another, tx & ty
- original coordinate position (x, y) to move point to new
position (x’, y’)
x’ = x + tx, y’= y + ty

2/18/23 Computer Graphics MTU – CS


Rotations
5

rotates all points about a centre of rotation –


origin (0,0)
has a single parameter: angle of rotation θ
radius stays the same, angle increases by θ
x’ = rcos ()
- rotate a point P anti-clockwise by θ0
= rcoscos – rsinsin
= xcos – ysin

y’ = rsin ()
x = r cos
= rsincos + rcossin
y = r sin
= xsin + ycos
2/18/23 Computer Graphics MTU – CS
Contd…
6

2/18/23 Computer Graphics MTU – CS


Scaling
7

Changing the size of an object  scale


multiplies each coordinate of each point by a
scale factor
scale factor can be different for each
coordinate (e.g. for x & y coordinates)
 If all scale factors are equal we call it uniform
scaling, whereas Sx = Sy
 if they are different we call it non-uniform
scaling
2/18/23 Computer Graphics MTU – CS
8
Contd…
To scale a point P by scale factors Sx and Sy we apply the
scaling matrix S:

2/18/23 Computer Graphics MTU – CS


Inverse Transformation
9

Negation of one’s transformation


Inverse translation matrix
 Translate in the opposite direction

product of translation matrix & its inverse


produces identity matrix
Inverse rotation matrix
 Rotation in the clockwise direction
Inverse scaling matrix

2/18/23 Computer Graphics MTU – CS


Homogenous Coordinates
10

Many graphics applications involve


sequences of geometric transformations
 An animation might require an object to be
translated and rotated at each increment of the
motion
- e.g., in design and picture construction applications,
we perform translations, rotations, and scaling to fit
the picture components into their proper positions
 viewing transformations involve sequences of
translations and rotations to take us from the
original scene specification to the display on
2/18/23an output deviceComputer Graphics MTU – CS
11
Contd…
Matrix M1 is a 2x2 array containing
multiplicative factors, and M2 is a two-
element column matrix containing
translational terms
To produce a sequence of transformations
with these equations, such as scaling
followed by rotation then translation, we
could calculate the transformed coordinates
one step at a time.
First, coordinate positions are scaled, then
2/18/23 Computer Graphics MTU – CS
12
Contd…
Multiplicative and translational terms can be
combined into a single matrix if we expand
the representations to 3x3
(xh, yh, h)  homogeneous coordinates, where
the homogeneous parameter h is a nonzero
value such that

- E.g. homogenous coordinates (2,3,1), (4,6,2),


(6,9,3), (8,12,4), represent same point (2,3,1)
2/18/23 Computer Graphics MTU – CS
13
Contd…
Translation transformation using a single
matrix multiplication as:

2/18/23 Computer Graphics MTU – CS


14
Contd…
Rotations can also be expressed using
homogenous coordinates

2/18/23 Computer Graphics MTU – CS


15
Contd….
2D scaling with homogenous coordinate

2/18/23 Computer Graphics MTU – CS


16
Composite Transformation Matrix
Homogenous coordinates – used to allow us to compose
sequence of transformation into single matrix
Forming products transformation matrices 
composition of matrices
For any sequence of transformations – calculating the
matrix product of the individual transformations
We do premultiply the column matrix by the matrices
representing any transformation sequence  multiplying
two matrices together - concatenation
E.g., sequence of matrix transformations A, B, C in order
using premultiplying composite transformation  CBA
& postmultiply  ABC, so so C is applied first,
followed by B and then A
2/18/23 Computer Graphics MTU – CS
Contd…
17

 Translation
Two successive translation:

OR

T(tx2, ty2) . T(tx1, ty1) = T(tx1 + tx2, ty1 + ty2)


2/18/23 Computer Graphics MTU – CS
Contd…
18

 Rotation Two successive rotation:


The composition matrix

 Scaling – two successive scaling:

OR

2/18/23 Computer Graphics MTU – CS


Contd…
19

2/18/23 Computer Graphics MTU – CS


Contd…
20

2/18/23 Computer Graphics MTU – CS


Reflection
21

transformation that produces a mirror image of an


object
y
- by rotating the object 180’ about reflection angle
About the x axis:

Reflection of an object about the x axis

About the y axis: y

Reflection of an object about the y axis


2/18/23 Computer Graphics MTU – CS
22
Contd…
Relative to coordinate origin
y

Reflection of an object relative to an


With respect to the line y = x axis perpendicular to xy plane
Same as a rotation with 180º

y y=x

x
2/18/23 Computer Graphics MTU – CS
23
3D Transformation
Similar to the 2D transform: 3D transform
has 3 basic transformations
 Translation
 Rotation
 Scaling
 Reflection

2/18/23 Computer Graphics MTU – CS


Translation
24

(x, y, z) to homogeneous coordinates (x, y, z, 1)


P (x, y, z) is translated to P’ (x’, y’, z’) with matrix
operation

glTranslatef(, , )
2/18/23 Computer Graphics MTU – CS
Rotation
25

Rotation based on z–
axis

2/18/23 Computer Graphics MTU – CS


Contd…
26

x – axis y – axis
rotation rotation

2/18/23 Computer Graphics MTU – CS


Scaling
27

x
Matrix form
z

2/18/23 Computer Graphics MTU – CS


Reflection
28

 3D reflection can be performed relative to the


selected reflection axis
 Reflections with respect to the plane are
equivalent to 180 degree rotation in coordinate
axis (xy, yz, zx)
y
 E.g., reflection based on xy plane:
Reflection relative to xy plane
x

2/18/23 Computer Graphics MTU – CS


Transformations in OpenGL
29

OpenGL manages two 4 × 4 transformation matrices:


- Model-view matrix, and
- Projection matrix
Model-view matrix  used to represent geometric
transformations of objects from application or model
frame to the camera frame
- combines viewing matrix and modeling matrix into one
matrix
- gluLookAt() - particularly used to set viewing
transform.

4 columns of GL_MODELVIEW matrix

2/18/23 Computer Graphics MTU – CS


Contd….
30

The 3 matrix elements of the rightmost column (m12,


m13, m14) are for the translation transformation,
glTranslatef()
element m15 is the homogeneous coordinate
It is specially used for projective transformation
3 elements sets, (m0, m1, m2), (m4, m5, m6) and (m8, m9,
m10) are for rotation glRotatef() or scaling glScalef()
Note: these 3 sets are actually representing 3
orthogonal axes (x, y, z);
projection matrix  used to store camera
transformation (both carry out the desired projection and
also change the representation to clip coordinates)
2/18/23 Computer Graphics MTU – CS
31
Contd…
glMatrixMode – routine to select the matrix to
which the operations apply
To modify the current matrix,
- use glMatrixMode(GL_MODELVIEW) to modify
modelview matrix
- modelview matrix can then be initialized to the identity
glLoadIdentity()

OpenGL provides to multiply matrix by specific


transformation matrices (glRotate, glTranslate, and
glScale)
OpenGL provides stacks to assist with hierarchical
transformations
2/18/23 Computer Graphics MTU – CS
32
Contd…
OpenGL functions that affect the current matrix:
glTranslate*(tx,ty,tz): Postmultiply current matrix
by a translation matrix formed from the
translation parameters tx, ty and tz.
glRotate*(θ,vx,vy,vz): Postmultiply the current
matrix by a rotation matrix that rotates by θ o about
the axis defined by the direction of the vector
(vx,vy,vz)
glScale*(Sx,Sy,Sz): Postmultiply the current
matrix by a scaling matrix formed from the scale
factors Sx, Sy and Sz.
glLoadIdentity(elements16):
2/18/23 Computer Graphics Replaces
MTU – CS the current
Contd…
33

Basic OpenGL Geometric Transformations


 glTranslate* (tx, ty, tz);
 glTranslatef (25.0, -10.0, 0.0);
 glRotate* (theta, vx, vy, vz);
 glRotatef (90.0, 0.0, 0.0, 1.0);
 glScale* (sx, sy, sz);
 glScalef (2.0, -3.0, 1.0);
OpenGL Matrix Operations
 glMatrixMode (GL_MODELVIEW);
 glMatrixMode (GL_PROJECTION);

2/18/23 Computer Graphics MTU – CS


Order of Transformations
34

Each transform multiplies the object by a matrix that


does the corresponding transformation
The transform closest to the object gets multiplied
first.

Translation first
Rotation first

2/18/23 Computer Graphics MTU – CS


Contd…
35

Let Display(){

- glTranslate = Mat TransglMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- glRotate = Mat Rot glTranslatef(0.0, 0.0, -6.0);
glRotatef(45.0,0.0,1.0, 0.0);
- glScale = Mat Scale glScalef(2.0, 2.0, 2.0);
DrawCube();
- DrawCube = v …}
Modelview matrix:
- Identity -> Trans -> Trans*Rot -> Trans*Rot*Scale ->
Trans*Rot*Scale*v
- Or, Trans(Rot(Scale*v)))
- So Scale is applied first, then Rot, then Trans

2/18/23 Computer Graphics MTU – CS


Contd…
36

// Example 1 // Example 1
Display(){ Display(){
… …
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
glTranslatef(0.0, 0.0, -6.0); glRotatef(45.0, 0.0, 1.0, 0.0);
glRotatef(45.0, 0.0, 1.0, 0.0); glTranslatef(0.0, 0.0, -6.0);
glScalef(2.0, 2.0, 2.0); glScalef(2.0, 2.0, 2.0);
DrawCube(); DrawCube();
…} …}
= Trans * Rot * Scale * v = Trans * Rot * Scale * v

Generally, do not expect different orders of


transforms to produce the same result, because
matrix multiplication is not commutative.
2/18/23 Computer Graphics MTU – CS
Contd…
37

y
tipl
glLoadIdentity();

ul
tm
glTranslated(2.0, 2.0, 0.0);

os
sp
glRotated(90.0, 0.0, 0.0, 1.0);

ay
w
al
glTranslated(-2.0, -2.0, 0.0);

L
nG
pe
O
2/18/23 Computer Graphics MTU – CS
38

Question
s?
Thank You!
2/18/23 Computer Graphics MTU – CS

You might also like