ceng 477 introduction to computer...

Post on 09-Oct-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CENG 477Introduction to Computer Graphics

Modeling Transformations

Modeling Transformations• Model coordinates to World coordinates:

World coordinates:All shapes with theirabsolute coordinates and sizes.

zworld

xworld

yworldModel coordinates:All shapes with theirlocal coordinates and sizes.

CENG 477 – Computer Graphics 2

Basic Geometric Transformations• Used for modeling, animation as well as viewing• What to transform?

– We typically transform the vertices (points) and vectors describing the shape (such as the surface normal)

• This works due to the linearity of transformations• Some, but not all, transformations may preserve attributes like

sizes, angles, ratios of the shape

CENG 477 – Computer Graphics 3

Translation• Simply move the object to a relative position

TPP

PTP

+=

y'x'

=tt

=yx

=

t+y=y't+x=x'

y

x

yx

'

' ⎥⎦

⎤⎢⎣

⎡⎥⎦

⎤⎢⎣

⎡⎥⎦

⎤⎢⎣

P

P'

CENG 477 – Computer Graphics 4

Rotation• A rotation is defined by a rotation axis and a rotation angle• For 2D rotation, the parameters are rotation angle (θ) and the

rotation point (xr, yr)• We reposition the object in a circular path around the rotation

point (pivot point)

a

xr

yr

CENG 477 – Computer Graphics 5

Rotation• When (xr, yr)=(0,0) we have:

P

P'

θφθφθφ

θφθφθφ

cossinsincos)sin(sinsincoscos)cos(

rrryrrrx

+=+=ʹ

−=+=ʹ

φ

φ

sincosryrx

=

=The original coordinates are:

θθ

θθ

cossinsincosyxyyxx

+=ʹ

−=ʹSubstituting them in the first equation we get:

In the matrix form we have: PRP ⋅=ʹ

⎥⎦

⎤⎢⎣

⎡ −=

θθ

θθ

cossinsincos

R

CENG 477 – Computer Graphics 6

Rotation• Rotation around an arbitrary point (xr, yr)

• These equations can be written as matrix operations (we will see when we discuss homogeneous coordinates)

P

P'

θθ

θθ

cos)(sin)(sin)(cos)(

rrr

rrr

yyxxyyyyxxxx

−+−+=ʹ

−−−+=ʹ

(xr,yr)

CENG 477 – Computer Graphics 7

Scaling• Changes the size of an object • Input: scaling factors (sx, sy)

PSP

S

' ⋅

⎥⎦

⎤⎢⎣

=

ss

=

ys=y'xs=x'

y

x

yx

00

PP'

non-uniform vs.uniform scaling

CENG 477 – Computer Graphics 8

Homogenous Coordinates• Translation is additive, rotation and scaling is multiplicative

(and additive if you rotate around an arbitrary point or scale around a fixed point)

• Goal: Make all transformations as matrix operations• Solution: Add a third dimension

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

1yx

=hyhxh

=hyx

=Phy=y

hx=x h

hhh

CENG 477 – Computer Graphics 9

Homogenous Coordinates• In HC, each point now becomes a line• The entire line represents the same point• The original (non-homogeneous) point resides on the w=1

plane

x

y

w

w = 1p1

p2

CENG 477 – Computer Graphics 10

All points along this linein HC represent the samepoint, p1

Transformations in HC

• Translation:

• Rotation:

• Scaling:

( ) ( )⎥⎥⎥

⎢⎢⎢

1001001

where y

x

yxyx' t

t=t,tTPt,tT=P

( ) ( )⎥⎥⎥

⎢⎢⎢

⎡ −

1000cossin0sincos

where θθθθ

=θRPθR=P'

( ) ( )⎥⎥⎥

⎢⎢⎢

⋅ʹ

1000000

where y

x

yxyx ss

=s,sSPs,sS=P

CENG 477 – Computer Graphics 11

Transforming Vectors• Vectors can be rotated and scaled• But translating a vector does not change it! Why?

– A vector is a difference between two points– These two points translate the same way– So the vector remains the same

• Mathematically this can be achieved by setting the last coordinate of a vector to 0 (the last coordinate of points should be 1)

CENG 477 – Computer Graphics 12

2D point 𝑥𝑦 in HC is equal to

𝑥𝑦1

2D vector 𝑥𝑦 in HC is equal to

𝑥𝑦0

Composite Transformations• Often, objects are transformed multiple times• Such transformations can be combined into a single composite

transformation• E.g. Application of a sequence of transformations to a point:

PMPMMP 12

⋅=

⋅⋅=ʹ

CENG 477 – Computer Graphics 13

Composite Transformations• Composition of the same types of transformations is simple• E.g. translation:

( ) ( ) ( )yyxxyy

xx

y

x

y

x

yxyx t+t,t+tT=tttt

=tt

tt

=t,tTt,tT 212121

21

1

1

2

2

1122

1001001

1001001

1001001

⎥⎥⎥

⎢⎢⎢

+

+

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

PTTPTTP⋅⋅=

⋅⋅=ʹ

)},(),({

}),({),(

1122

1122

yxyx

yxyx

tttttttt

CENG 477 – Computer Graphics 14

( ) ( ) ( )yyxxyy

xx

y

x

y

x

yxyx ss,ss=ssss

=ss

ss

=s,ss,s 212121

21

1

1

2

2

1122

1000000

1000000

1000000

⋅⋅

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⋅ SSS

( ) ( )

( )φ+θ=φ+θφ+θφ+θφ+θ

=φθ+φθφθ+φθφθφθφθφθ

=θθθθ

=φθ

R

RR

⎥⎥⎥

⎢⎢⎢

⎡ −

⎥⎥⎥

⎢⎢⎢

−−−

⎥⎥⎥

⎢⎢⎢

⎡ −

⎥⎥⎥

⎢⎢⎢

⎡ −

1000)cos()sin(0)sin()cos(

1000coscossinsinsincoscossin0cossinsincossinsincoscos

1000cossin0sincos

1000cossin0sincos

ϕϕ

ϕϕ

Composite Transformations• Rotation and scaling are similar:

CENG 477 – Computer Graphics 15

Rotation Around a Pivot Point• Step 1: Translate the object so that the pivot point moves to

the origin

( )rr y,x −−T

( )rr y,xM −−= T1

CENG 477 – Computer Graphics 16

Rotation Around a Pivot Point• Step 2: Rotate around origin

( )θR

( )θR=2M

CENG 477 – Computer Graphics 17

Rotation Around a Pivot Point• Step 3: Translate the object so that the pivot point is back to its

original position

( )rr y,xT

( )rr y,xM T=3

CENG 477 – Computer Graphics 18

Rotation Around a Pivot Point• The composite transformation is equal to their successive

application:

( ) ( ) ( )rrrr y,xy,xMMMM −−== TRT θ123

CENG 477 – Computer Graphics 19

Scaling w.r.t. a Fixed Point• The idea is the same:

– Translate to origin– Scale– Translate back

( ) ( ) ( )

( )( )

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

−−⋅⋅

1001010

1001001

1000000

1001001

yfy

xfx

f

f

y

x

f

f

ffyxff

syssxs

=yx

ss

yx

=y,xs,sy,x TST

( )ff y,x −−T

( )yx s,sS

( )ff y,xT

CENG 477 – Computer Graphics 20

Order of matrix compositions• Matrix composition is not commutative. So, be careful when

applying a sequence of transformations.

Pivot

21

Rotation and translation

Pivot

12

Translation and rotation

CENG 477 – Computer Graphics 21

Other Transformations• Reflection: special case of scaling

⎥⎥⎥

⎢⎢⎢

100

010

001

⎥⎥⎥

⎢⎢⎢

⎡−

100

010

001

⎥⎥⎥

⎢⎢⎢

100

010

001

CENG 477 – Computer Graphics 22

Other Transformations• Shear: Deform the shape like shifted slices (or deck of cards).

Can be in x or y direction

(1,1)(3,1)

(2,1)

x ' x shx y y ' y

1 shx 00 1 00 0 1

(0,1)

CENG 477 – Computer Graphics 23

3D Transformations• Similar to 2D but with an extra z component• We assume a right handed coordinate system• With homogeneous coordinates we have 4 dimensions • Basic transformations: Translation, rotation, scaling

x

y

z

z

x

y

y

z

x

Equivalent ways of thinking about a right-handed CS

CENG 477 – Computer Graphics 24

Translation• Move the object by some offset:

z

y

x

z

y

x

⎥⎥⎥⎥

⎢⎢⎢⎢

⎥⎥⎥⎥

⎢⎢⎢⎢

=

⎥⎥⎥⎥

⎢⎢⎢⎢

ʹ

ʹ

ʹ

11000100010001

1zyx

ttt

zyx

z

y

x

PTP ⋅=ʹ

P

CENG 477 – Computer Graphics 25

Rotation• Rotation around the coordinate axes

• Positive angles represent counter-clockwise (CCW) rotation when looking along the positive half towards origin

z

x

y

z

x

y

z

x

y

z

x

y

z

x

y

z

x

y

x-axis y-axis z-axis

CENG 477 – Computer Graphics 26

Rotation Around Major Axes• Around x:

• Around y:

• Around z:

⎥⎥⎥⎥

⎢⎢⎢⎢

−=

10000cossin00sincos00001

)(θθ

θθθxR PRP ⋅=ʹ )(θx

⎥⎥⎥⎥

⎢⎢⎢⎢

⎡ −

=

1000010000cossin00sincos

)(θθ

θθ

θzR PRP ⋅=ʹ )(θz

⎥⎥⎥⎥

⎢⎢⎢⎢

−=

10000cos0sin00100sin0cos

)(θθ

θθ

θyR PRP ⋅=ʹ )(θy

CENG 477 – Computer Graphics 27

Rotation Around a Parallel Axis• Rotating an object around a line parallel to one of the axes:

Translate to a major axis, rotate, translate back• E.g. rotate around a line parallel to x-axis:

z

y

x

z

y

x

z

y

x

z

y

x

Translate Rotate Translate back

PTRTP ⋅−−⋅⋅=ʹ ),,0()(),,0( ppxpp zyzy θ

(yp, zp)

CENG 477 – Computer Graphics 28

Rotation Around an Arbitrary Axis• Step 1: Translate the object so that the rotation axis passes

though the origin• Step 2: Rotate the object so that the rotation axis is aligned

with one of the major axes• Step 3: Make the specified rotation• Step 4: Reverse the axis rotation• Step 5: Translate back

z

y

x

CENG 477 – Computer Graphics 29

Rotation Around an Arbitrary Axis

CENG 477 – Computer Graphics 30

Rotation Around an Arbitrary Axis• First determine the axis of rotation:

• u is the unit vector along v:

),,( 12121212 zzyyxx −−−=−= PPv

),,( cba==vvu

CENG 477 – Computer Graphics 31

Rotation Around an Arbitrary Axis• Next translate P1 to origin:

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1000100010001

1

1

1

zyx

T

CENG 477 – Computer Graphics 32

Rotation Around an Arbitrary Axis• Then align u with one of the major axis (x, y, or z)• This is a two-step process:

– Rotate around x to bring u onto xz plane (CCW)– Rotate around y to align the result with the z-axis (CW)

z

x

u

y

α

z

xu

y

β

We need cosine and sine of angles α and ß

CENG 477 – Computer Graphics 33

Rotation Around an Arbitrary Axis• We need cosine and sine of angles α and ß:

z

x

uu'

uz

y

αux

uyu = ux + uy + uz = ux + u’

22z whereucos cbddc

+==ʹ

=u

α

db

=uyusinα

⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢

−=

1000

00

000001

)(

dc

db

db

dc

x αR

CENG 477 – Computer Graphics 34

Rotation Around an Arbitrary Axis• We need cosine and sine of angles α and ß:

z

x

y

ux

222

2222

coscba

cbuu zy

++

+=

+=

22zy uu +

β u

222sin

cbaaux

++==

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

++

+

++−

++++

+

=

1000

00

0010

00

)(

222

22

222

222222

22

cbacb

cbaa

cbaa

cbacb

y βR

CENG 477 – Computer Graphics 35

Note that 𝑎& + 𝑏& + 𝑐& = 1

-

+

Rotation Around an Arbitrary Axis• Putting it all together:

),,()()()()()(),,()( 111111 zyxzyx xyzyx −−−⋅⋅⋅⋅−⋅−⋅= TRRRRRTR αβθβαθ

This is the actual desiredrotation. Other terms arefor alignment and undoingthe alignment

CENG 477 – Computer Graphics 36

-+

Alternative Method• Assume we want to rotate around the unit vector u:• We create an orthonormal basis (ONB) uvw:

CENG 477 – Computer Graphics 37

z

x

u

y

z

x

u

y

v

w

1) To find v, set the smallestcomponent of u (in anabsolute sense) to zero andswap the other two whilenegating one:

E.g. if u = (a, b, c) with c being the smallest absolutevalue then v = (-b, a, 0)

Note that we are just finding one of the infinitely many solutions

This corresponds to projectingthe vector to the nearest major planeand rotating it 90° along the axisperpendicular to that plane

2) w = u x v

3) Normalize v and w

Alternative Method• Now rotate uvw such that it aligns with xyz: call this

transform M• Rotate around x (u is now x)• Undo the initial rotation: call this M-1

• Finding M-1 (rotating xyz to uvw) is trivial:

CENG 477 – Computer Graphics 38

⎥⎥⎥⎥

⎢⎢⎢⎢

=−

1000000

1

zzz

yyy

xxx

wvuwvuwvu

M

Verify that this matrix transforms x to u, y to v, and z to w

• How to transform x = [1 0 0 0]T

such that it turns into [ux uy uz 0]T

• Similar for the y and z axis

Alternative Method• Finding M is also trivial as M-1 is an orthonormal matrix (all

rows and columns are orthogonal unit vectors)• For such matrices, inverse is equal to transpose:

CENG 477 – Computer Graphics 39

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1000000

zyx

zyx

zyx

wwwvvvuuu

M

Alternative Method• The final rotation transform is:

• We assumed that the origin of uvw is the same as the origin of xyz

• Otherwise, we should account for this difference:

CENG 477 – Computer Graphics 40

𝑀,-𝑅/(𝜃)𝑀

𝑇,-𝑀,-𝑅/(𝜃)𝑀𝑇

Translate the originof uvw to xyz

Undo thetranslation

Scaling• Change the coordinates of the object by scaling factors

x 'y 'z '1

sx 0 0 00 s y 0 00 0 sz 00 0 0 1

xyz1

P' S P

z

y

x

z

y

x

PSP ⋅=ʹ

P

CENG 477 – Computer Graphics 41

Scaling w.r.t. a Fixed Point• Translate to origin, scale, translate back

z

y

x

z

y x

z

y

x

z

y

x

Translate Scale Translate back

PTSTP ⋅−−−⋅⋅=ʹ ),,(),,( ffffff zyxzyx

CENG 477 – Computer Graphics 42

Reflection• Reflection over the major planes:

z

y

x

z

y

x

z

y

x

⎥⎥⎥⎥

⎢⎢⎢⎢

⎡−

1000010000100001

⎥⎥⎥⎥

⎢⎢⎢⎢

1000010000100001

⎥⎥⎥⎥

⎢⎢⎢⎢

1000010000100001

How about reflection over an arbitrary plane?

CENG 477 – Computer Graphics 43

Transforming Normals• When we transform an object, what happens to its normals?• Do they get transformed by the same matrix or does it require

a different one?

x

y

1

1 x

y

1

3

⎥⎥⎥

⎢⎢⎢

=

100010003

S

Scale by:

𝒏 =1/√21/√20

𝒏 =3/√21/√20

CENG 477 – Computer Graphics 44

Transforming Normals• After the transformation the normal is no longer perpendicular

to the object• Also it is not a unit vector anymore

x

y

1

1 x

y

1

3

⎥⎥⎥

⎢⎢⎢

=

100010003

S

Scale by:

𝒏 =1/√21/√20

𝒏 =3/√21/√20

CENG 477 – Computer Graphics 45

Transforming Normals• Rotation and translation has no problems• But, since all transformations are combined into a single

matrix M, we should consider the general case.

• We must have n.(b-a) = n.v = 0 and this relationship should be preserved after the transformation

n

a

bv

CENG 477 – Computer Graphics 46

Transforming Normals• That is n.v = 0 and n'.v' = 0 where v' = Mv and n' = Zn• Z is the matrix we are looking for• How to compute Z?

n

a

bv

CENG 477 – Computer Graphics 47

Transforming Normals• n.v = nTv = 0• n'.v' = n'Tv' = n'T Mv = nTZTMv = 0• If ZTM = I (identity) the relationship will be preserved• So Z = (M-1)T

• Note that this is equal to (MT)-1 as (M-1)T= (MT)-1 for a square (n by n) matrix M

n

a

bv

CENG 477 – Computer Graphics 48

A Word on Notation• Until now, we performed transformations by multiplying our

points from the right:

• Another notation is to multiply from the left:

CENG 477 – Computer Graphics 49

𝑥9𝑦9𝑧91

=

𝑎 𝑏 𝑐 𝑑𝑒 𝑓 𝑔 ℎ𝑖 𝑗 𝑘 𝑙0 0 0 1

𝑥𝑦𝑧1

𝑥9 𝑦9 𝑧9 1 = 𝑥 𝑦 𝑧 1

𝑎 𝑏 𝑐 𝑑𝑒 𝑓 𝑔 ℎ𝑖 𝑗 𝑘 𝑙0 0 0 1

D

Note that in this caseeverything is transposed

A Word on Terminology• Imagine a 2D rotation matrix such as:

• Transforming an object by this matrix will not change its shape• If we also add translation:

• The shape will remain intact

CENG 477 – Computer Graphics 50

cos(𝜃) −sin(𝜃) 0sin(𝜃) cos(𝜃) 00 0 1

cos(𝜃) −sin(𝜃) 𝑡/sin(𝜃) cos(𝜃) 𝑡M0 0 1

A Word on Terminology• In general, an arbitrary sequence of rotation and translation

matrices will have the following form:

• Such transformations are called rigid-body transformations• A shape may be rotated and translated by its form is not altered

in any way

CENG 477 – Computer Graphics 51

𝑟-- 𝑟-& 𝑡/𝑟&- 𝑟&& 𝑡M0 0 1

A Word on Terminology• Imagine also adding scaling• The matrix will now look like:

• Such transformations will not necessarily preserve lengths and angles, but parallel lines will remain parallel

CENG 477 – Computer Graphics 52

𝑎 𝑏 𝑡/𝑐 𝑑 𝑡M0 0 1

where a, b, c, d contain the effect ofrotation and scaling combined

Original Rotation Rotation and scaling

A Word on Terminology• Such transformations are called affine transformations• An arbitrary sequence of rotation, translation, scaling, and

shearing will produce an affine transformation• Note that we still have some degrees of freedom left in the last

row of our matrix:

• By using this we can create projective transformations in which parallel lines may no longer be parallel

CENG 477 – Computer Graphics 53

𝑎 𝑏 𝑡/𝑐 𝑑 𝑡M0 0 1

top related