Anda di halaman 1dari 57

2IV60 Computer Graphics

2D transformations
Jack van Wijk
TU/e

Overview
Why transformations?
Basic transformations:
translation, rotation, scaling

Combining transformations
homogenous coordinates, transform. Matrices

First 2D, next 3D

Transformations
instantiation
world
train

viewing
image

wheel
modelling

animation

Why transformation?
Model of objects
world coordinates: km, mm, etc.
Hierarchical models::
human = torso + arm + arm + head + leg + leg
arm = upperarm + lowerarm + hand

Viewing
zoom in, move drawing, etc.

Animation

Translation
Translate over vector (tx, ty)
x=x+ tx, y=y+ ty
or
P' P T, with

y
T

P+T

P
x

tx
x'
x
P ' , P and T
y'
y
ty
H&B 7-1:220-222

Translation polygon
Translate polygon:
Apply the same operation
on all points.
Works always, for all
transformations of
objects defined as a set
of points.

y
T
x

H&B 7-1:220-222

Rotation
Rotate over an angle :
x' x cos y sin

y
P

y ' x sin y cos

Or
P' RP, with
x'
cos
P ' , R
y'
sin

P
x

sin

x
and P
cos
y
H&B 7-1:222-223

Rotation around a point Q


Rotate around origin :
Px ' Px cos Py sin
Py ' Px sin Py cos

y
P
P
PQ
Q

Rotate around Q over an angle :


Px ' Qx ( Px Qx ) cos ( Py Q y ) sin

Py ' Q y ( Px Qx ) sin ( Py Q y ) cos


H&B 7-1:222-223

Scaling
Schale with factor sx and sy:

y
P

x= sx x, y= sy y
P

or

P' SP, with


sx
x'
P ' , S
0
y'

0
x
and P
s y
y
H&B 7-1:224-225

Scaling with respect to a point F


Scale with factors sx and sy:

Px= sx Px, Py= syPy


With respect to F:
Px Fx = sx (Px Fx),
Py Fy = sy (Py Fy)

P
PF
F

Q
Q
x

or
Px= Fx + sx (Px Fx),
Py= Fy + sy (Py Fy)
H&B 7-1:224-225

Transformations
Translate with V:
T = P+ V

Schale with factor sx = sy =s:


S = sP

Rotate over angle

S
T

R
P

Rx = cos Px sin Py
Ry = sin Px + cos Py
H&B 7-2:225-228

Transformations
Messy!
Transformations with respect to points:
even more messy!
How to combine transformations?

H&B 7-2:225-228

Homogeneous coordinates 1
Uniform representation of translation,
rotation, scaling
Uniforme representation of points and
vectors
Compact representation of sequence of
transformations
H&B 7-2:225-228

Homogeneous coordinaten 2
Add extra coordinate:
P = (px , py , ph) or
x = (x, y, h)
Cartesian coordinates: divide by h
x = (x/h, y/h)
Points: h = 1 (for the time being),
vectors: h = 0
H&B 7-2:225-228

Translation matrix
Translation :
x' 1 0 t x

y' 0 1 t y
1 0 0 1

or
P ' T(t x , t y )P

x

y
1

H&B 7-2:225-228

Rotation matrix
Rotation :
x' cos

y ' sin
1 0

or
P ' R ( )P

sin
cos
0

0
1

x

y
1

H&B 7-2:225-228

Scaling matrix
Scaling :
x' s x

y' 0
1 0

or

0
sy
0

0
1

x

y
1

P ' S( s x , s y ) P
H&B 7-2:225-228

Inverse transformations
Translation :
T -1 (t x , t y ) T(t x ,t y )
Rotation :
R -1 ( ) R ( )
Scaling :
1 1
S ( s x , s y ) S( , )
sx s y
1

H&B 7-3:228

Combining transformations 1
P' M1P

first transformation...

P '' M 2 P '

second transformation...

Combined :
''

P M 2 (M 1P)
M 2M 1P
MP with M M 2 M 1
H&B 7-4:228-229

Combining transformations 2
P ' T(t1x , t1 y )P

first translation

P '' T(t 2 x , t 2 y )P '

second translation

Combined :
P '' T(t 2 x , t 2 y )T(t1x , t1 y )P
1 0 t 2 x 1 0 t1x

0 1 t 2 y 0 1 t1 y P
0 0 1 0 0 1

T(t1x t 2 x , t1x t 2 y )P

1 0 t1x t 2 x

0 1 t1 y t 2 y P
0 0

H&B 7-4:229

Combining transformations 3
Composite translations :
T(t 2 x , t 2 y )T(t1x , t1 y ) T(t1x t 2 x , t1x t 2 y )
Composite rotations :
R ( 2 ) R (1 ) R (1 2 )
Composite scaling :
S( s2 x , s2 y )S( s1x , s1 y ) S( s1x s2 x , s1 y s2 y )
H&B 7-4:229

Rotation around a point 1


Rotate over angle around point R :
1) Translate such that R coincides with origin;
2) Rotate over angle around origin;
3) Translate back.
R

1)

2)

3)

H&B 7-4:229-230

Rotation around a point 2


Rotate over angle around point R :
1) P ' T( Rx , R y )P
''

2) P R( )P

'

3) P ''' T( Rx ,R y )P ''
R

1)

2)

3)

H&B 7-4:229-230

Rotation around point 3


1) P ' T( Rx , R y )P
2) P '' R( )P ' R( )T( Rx , R y )P
3) P ''' T( Rx ,R y )P ''
T( Rx ,R y )R( )P '
T( Rx ,R y )R( )T( Rx , R y )P

1)

2)

3)

H&B 7-4:229-230

Rotation around point 4


1 - 3) P ''' T( Rx ,R y )R( )T( Rx , R y )P
or
cos
'''
P sin
0

sin

Rx (1 cos ) R y sin

cos
0

R y (1 cos ) Rx sin P

1)

2)

3)

H&B 7-4:229-230

Scaling w.r.t. point 1


Scale with factors s x and s x w.r.t. point F :
1) Translate such that F coincides with origin;
2) Schale w.r.t. origin;
3) Translate back again.
F

1)

2)

3)

H&B 7-4:230-231

Scaling w.r.t.point 2
Schale w.r.t. point F :
1) P ' T( Fx , Fy )P
2) P '' S( s x , s y )P '
3) P ''' T( Fx ,Fy )P ''
F

1)

2)

3)

H&B 7-4:230-231

Scaling w.r.t.point 3
1 - 3) P ''' T( Fx ,Fy )S( s x , s y )T( Fx , Fy )P
or
sx
'''
P 0
0

0
sy
0

Fx (1 s x )

Fy (1 s y ) P

1)

2)

3)

H&B 7-4:230-231

Scale in other directions 1


Scale with factors s1 and s2 w.r.t. rotated frame :
1) Rotate such that frame coincides with standard xy - frame;
2) Scale w.r.t. origin;
3) Rotate back again.

1)

2)

3)

H&B 7-4:230-231

Scale in other directions 2


Scale in other direction :
1) P ' R( )P
2) P '' S( s1 , s2 )P '
3) P ''' R( )P ''

1)

2)

3)

H&B 7-4:230-231

Scale in other directions 3


1 - 3) P ''' R( )S( s1 , s2 )R( )P
or
s1 cos 2 s2 sin 2

'''
P ( s2 s1 ) cos sin

1)

2)

( s2 s1 ) cos sin
s1 sin 2 s2 cos 2
0

3)

0
0 P

H&B 7-4:230-231

'R
(30)T
(2,3x
x)
'T
(2,3)R
(30xx)
Order of transformations 1

Rotation, translation

Translation, rotation
y

Matrix multiplication does not commute.

The order of transformations makes a difference!

H&B 7-4:232

Order of transformations 2
Pre-multiplication:
P = M n M n-1M 2 M 1 P
Transformation M n in global coordinates

Post-multiplication:
P = M 1 M 2M n-1 M n P
Transformation M n in local coordinates: the
coordinate system after application of
M 1 M 2M n-1
H&B 7-4:232

Order of transformations 3
OpenGL: glRotate, glScale, etc.:
Post-multiplication of current
transformation matrix
Always transformation in local coordinates
Global coordinate version: read in reverse
order

H&B 7-4:232

x)
'R
(30)T
(2,3x

Order of transformations 4

Local transformations:

Global transformations:

interpretation

x
Local trafo

glTranslate();

Global trafo

glRotate();

interpretation
H&B 7-4:232

'x1'xy
P

,'r
M
rsx 0rxs
P
o

t
r
s
s
x

xyr
x
x
y

r
y
,
o
r
y
0
1
1

s
y

t
r
s
x
y
x
yy y
Matrices in general

rotation and scaling


translation

H&B 7-4:233-235

Direct construction of matrix


If you know the target frame:
Construct matrix directly.

Define shape in nice local


u,v coordinates, use matrix
transformation to put it
in x,y space.

T
x
H&B 7-4:233-235

Direct construction of matrix


If you know the target frame:
Construct matrix directly.
P ' Au Bv T , or
x
u


y A B T v
1
1


x Ax Bx Tx

y Ay B y T y
1 0

0
1

y
v

, or
u

v
1

T
x
H&B 7-4:233-235

Rigid body transformation


only rotation

P ' MP , of
x rxx

y ryx
1 0

rxx

ryx

rxy
ryy
0

trx

try
1

translation

u

v
1

rxy
: orthonormal submatrix
ryy

rxx
rxy
and B
, | A | 1, | B | 1, A B 0
A

ryx
ryy

y
u

B
T

A
x

H&B 7-4:233-235

Other 2D transformations
Reflection
Shear
Can also be combined

H&B 7-4:240

Reflection over axis


Reflext over x-axis:
x= x, y= y
or
1 0 0

P' 0 1 0 P
0 0 1

H&B 7-4:240-242

Reflect over origin


Reflect over origin:
x= x, y= y
or
1 0 0

P' 0 1 0 P
0 0 1

Same as P' R (180)P


H&B 7-4:240-242

Shear
Shear the y-as:
x=x+fy, y=y
or
1 f 0

P' 0 1 0 P
0 0 1

with f tan
H&B 7-4:242-243

Transformations coordinates
Given (x,y)-coordinates,
Find (x,y)-coordinates.

y
y

Reverse route as
object transformaties.

(x0, y0)

H&B 7-8:246-248

Transformations coordinates
Given (x,y)-coordinates,
Find (x,y)-coordinates.

y
y

Example: user points at


(x,y), whats the position
in local coordinates?

(x0, y0)

H&B 7-8:246-248

Transformations coordinates
Given X: (x,y)-coordinates,
Find X: (x,y)-coordinates.
Standard:
X=MX (object trafo:
from local to global)
Here:
X=M-1X (from global to local)

y
y

x
(x0, y0)

H&B 7-8:246-248

Transformations coordinates
Given X: (x,y)-coordinates,
y
Find X: (x,y)-coordinates.
y
Here:
X=M-1X (from global to local)
x

Approach 1:
(x0, y0)and invert
- Determine standard matrix M (from local to global coordinates)

H&B 7-8:246-248

Transformations coordinates
Given X: (x,y)-coordinates,
y
Find X: (x,y)-coordinates.
y
Here:
X=M-1X (from global to local)
x

Approach 2:
(x0, y0) of usual).
- construct transformation that maps local frame to global (reverse

H&B 7-8:246-248

Transformations coordinates
Given X: (x,y)-coordinates,
Find X: (x,y)-coordinates.
Here:
X=M-1X (from global to local)
Approach 2:
1.Translate (x0, y0) to origin;
2.Rotate x-axis to x-axis.

y
y

x
(x0, y0)

H&B 7-8:246-248

Transformations coordinates
Given X: (x,y)-coordinates,
Find X: (x,y)-coordinates.
Here:
X=M-1X (from global to local)
Approach 2:
M-1 = T(x0, y0) R()

y
y

x
(x0, y0)

H&B 7-8:246-248

OpenGL 2D transformations 1
Internally:
Coordinates are four-element row vectors
Transformations are 44 matrices
2D trafos: Ignore z-coordinates, set z = 0.

H&B 7-9:248-253

OpenGL 2D transformations 2
OpenGL maintains two matrices:
GL_PROJECTION
GL_MODELVIEW

Transformations are applied to the current matrix, to


be selected with:
glMatrixMode(GL_PROJECTION) or
glMatrixMode(GL_MODELVIEW)

H&B 7-9:248-253

OpenGL 2D transformations 3
Initializing the matrix to I:
glLoadIdentity();

Replace the matrix with M:


GLfloat M[16]; fill(M);
glLoadMatrix*(M);

Matrices are specified in


column-major order:

m[0]
m[1]
M
m[2]

m[3]

m[4] m[8] m[12]


m[5] m[9] m[13]
m[6] m[10] m[14]

m[7] m[11] m[15]

Multiply current matrix with M:


glMultMatrix*(M);

H&B 7-9:248-253

OpenGL 2D transformations 4
Basic transformation functions: generate matrix and postmultiply this with current matrix.
Translate over [tx, ty, tz]:
glTranslate*(tx, ty, tz);

Rotate over theta degrees (!) around axis [vx, vy, vz]:
glRotate*(theta, vx, vy, vz);

Scale axes with factors sx, sy, sz:


glScale*(sx, sy, sz);

H&B 7-9:248-253

OpenGL 2D Transformations 5
OpenGL maintains stacks of transformation matrices.
Two operations:
glPushMatrix():

Make copy of current matrix and put that on top of the stack;
glPopMatrix():

Remove top element of the stack.

Handy for dealing with hierarchical models


Handy for undoing transformations
H&B 9-8:324-327

OpenGL 2D Transformations 6
Standard:

Using the stack:

glRotate(10, 1, 2, 0);
glScale(2, 1, 0.5);
glTranslate(1, 2, 3);

glPushMatrix();
glRotate(10, 1, 2, 0);
glScale(2, 1, 0.5);
glTranslate(1, 2, 3);

glutWireCube(1);
glTranslate(1, 2,
3);
glScale(0.5, 1, 2);
glRotate(10, 1, 2, 0);

glutWireCube(1);
glPopMatrix();

Shorter, more robust

Undo transformation H&B 9-8:324-327

2D transformations summarized
-

Transformations: modeling, viewing, animation;


Several kinds of transformations;
Homogeneous coordinates;
Combine transformations using matrix multiplication.

Up to 3D!

Anda mungkin juga menyukai