intro to computer graphics transformations...5 cs5620 intro to computer graphics copyright c....

11
1 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 1 Transformations Specifying Complex Scenes (x,y,z) (0,0,0) 2 Specifying Complex Scenes β€’ Absolute position is not very natural β€’ Need a way to describe β€œrelative” relationship: – β€œThe panel is on top of the roof, which is above the house” β€’ Work β€œlocally” for every object and then combine 3 Transformations β€’ Transforming an object = transforming all its points β€’ For a polygon = transforming its vertices 4 Scaling β€’ = – vector in plane β€’ Scaling operator with parameters ( , ): (0,0) (s x ,s y )=(0.7, 0.7) (s x ,s y )=(0.7, 1.0) (s x ,s y )=(3.0, 1.5) ( , ) = 5 Matrix Form β€’ Convenient math for representing linear transformations – Composition β†’ multiplication – Inverse transformation β†’ matrix inverse – More on that later… 6

Upload: others

Post on 10-Jul-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

1

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 1

Transformations

Specifying Complex Scenes

(x,y,z)

(0,0,0) 2

Specifying Complex Scenes

β€’ Absolute position is not very natural

β€’ Need a way to describe β€œrelative” relationship:

– β€œThe panel is on top of the roof, which is above the house”

β€’ Work β€œlocally” for every object and then combine

3

Transformations

β€’ Transforming an object = transforming all its points

β€’ For a polygon = transforming its vertices

4

Scaling

β€’ 𝑣 =𝑣π‘₯

𝑣𝑦 – vector in π‘‹π‘Œ plane

β€’ Scaling operator 𝑆 with parameters (𝑠π‘₯ , 𝑠𝑦):

(0,0) (sx,sy)=(0.7, 0.7) (sx,sy)=(0.7, 1.0) (sx,sy)=(3.0, 1.5)

𝑆(𝑠π‘₯,𝑠𝑦) 𝑣 =𝑣π‘₯𝑠π‘₯𝑣𝑦𝑠𝑦

5

Matrix Form

β€’ Convenient math for representing linear transformations

– Composition β†’ multiplication

– Inverse transformation β†’ matrix inverse

– More on that later…

6

Page 2: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

2

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 2

Scaling Matrix Form

β€’ Independent in π‘₯ and 𝑦

– Non-uniform scaling is allowed

𝑆𝑠π‘₯,𝑠𝑦 𝑣 =𝑠π‘₯ 00 𝑠𝑦

𝑣π‘₯

𝑣𝑦=

𝑣π‘₯𝑠π‘₯𝑣𝑦𝑠𝑦

7

Rotation

β€’ Polar form:

𝑣 =𝑣π‘₯

𝑣𝑦=

π‘Ÿ cos π›Όπ‘Ÿ sin 𝛼

β€’ Rotate v anti-clockwise by to w:

π‘…πœƒ(𝑣) =𝑀π‘₯

𝑀𝑦=

π‘Ÿ cos 𝛼 + πœƒπ‘Ÿ sin 𝛼 + πœƒ

=𝑣π‘₯ cos πœƒ βˆ’ 𝑣𝑦 sin πœƒ

𝑣π‘₯ sin πœƒ + 𝑣𝑦 cos πœƒ

(0,0)

w π’š

v

𝒙

r

(0,0)

vy

vx

= -45o = -135o

8

Rotation

β€’ Matrix form:

π‘…πœƒ 𝑣 =cos πœƒ βˆ’ sin πœƒsin πœƒ cos πœƒ

𝑣

β€’ Rotation operator 𝑅 with parameter πœƒ:

π‘…πœƒ =cos πœƒ βˆ’sin πœƒsin πœƒ cos πœƒ

9

Rotation Properties

β€’ π‘…πœƒ is orthogonal

π‘…πœƒ βˆ’1= π‘…πœƒ 𝑇

β€’ Rotation by βˆ’πœƒ is

π‘…βˆ’πœƒ =cos (βˆ’ πœƒ) βˆ’ sin βˆ’πœƒ

sin (βˆ’πœƒ) cos βˆ’πœƒ

=

cos πœƒ sin πœƒβˆ’sin πœƒ cos πœƒ

= π‘…πœƒ βˆ’1

π‘…πœƒ = cos πœƒ βˆ’ sin πœƒsin πœƒ cos πœƒ

10

Translation

β€’ Translation operator 𝑇 with parameters (𝑑π‘₯, 𝑑𝑦):

𝑇𝑑π‘₯,𝑑𝑦 𝑣 =𝑣π‘₯ + 𝑑π‘₯𝑣𝑦 + 𝑑𝑦

β€’ Can we express 𝑇 in a matrix form?

(tx,ty)=(0.0, 1.0) (tx,ty)=(1.0, 0.0) (tx,ty)=(1.5, 0.3)

11

Homogeneous Coordinates in 𝑅2

β€’ Points of the form

π‘£β„Ž =

𝑣π‘₯β„Ž

π‘£π‘¦β„Ž

π‘£π‘€β„Ž

β€’ Where we identify

𝑣π‘₯

β„Ž

π‘£π‘¦β„Ž

π‘£π‘€β„Ž

≑

𝑐𝑣π‘₯β„Ž

π‘π‘£π‘¦β„Ž

π‘π‘£π‘€β„Ž

for all nonzero 𝑐

12

Page 3: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

3

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 3

Conversion Formulae

β€’ From Euclidean to homogeneous

𝑣 =𝑣π‘₯

β„Ž

π‘£π‘¦β„Ž β†’ π‘£β„Ž =

𝑣π‘₯β„Ž

π‘£π‘¦β„Ž

1

β€’ From homogeneous to Euclidean

π‘£β„Ž =

𝑣π‘₯β„Ž

π‘£π‘¦β„Ž

π‘£π‘€β„Ž

β†’ 𝑣 =

𝑣π‘₯β„Ž

π‘£π‘€β„Ž

π‘£π‘¦β„Ž

π‘£π‘€β„Ž

13

Example

β€’ In homogeneous coordinates

221

=442

=110.5

14

Translation using Homogeneous Coordinates

𝑇𝑑π‘₯,𝑑𝑦 π‘£β„Ž =1 0 𝑑π‘₯0 1 𝑑𝑦0 0 1

𝑣π‘₯

𝑣𝑦

1=

=𝑣π‘₯ + 𝑑π‘₯𝑣𝑦 + 𝑑𝑦

1

15

Matrix Form

Why bother expressing transformations in matrix form?

16

Transformation Composition

What operation rotates by πœƒ around 𝑝 =𝑝π‘₯

𝑝𝑦?

– Translate 𝑝 to origin

– Rotate around origin by πœƒ

– Translate back

17

p p

17

Transformation Composition

β€’ 𝑀1 performs one transformation

β€’ 𝑀2 performs a second transformation

𝑀2 𝑀1𝑣 = 𝑀2𝑀1𝑣 = 𝑀2𝑀1 𝑣

β€’ 𝑀2𝑀1 performs the composed transformation

18

Page 4: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

4

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 4

Transformation Composition

𝑇 𝑝π‘₯,𝑝𝑦 π‘…πœƒπ‘‡ βˆ’π‘π‘₯,βˆ’π‘π‘¦ 𝑣 =

1 0 𝑝π‘₯

0 1 𝑝𝑦

0 0 1

cos πœƒ βˆ’ sin πœƒ 0sin πœƒ cos πœƒ 00 0 1

1 0 βˆ’π‘π‘₯

0 1 βˆ’π‘π‘¦

0 0 1

𝑣

19

Transformation Quiz

β€’ What do these Euclidean transformations do?

1 00 1

,1 00 βˆ’1

,βˆ’1 00 1

1 01 1

,1 π‘Ž0 1

20

Transformation Quiz

β€’ And these homogeneous ones?

1

1βˆ’1

1

10.5

21

Transformation Quiz

β€’ Can one rotate in the plane by reflection?

β€’ How can one reflect through an arbitrary line in the plane?

22

Arbitrary Reflection

β€’ Shift by 0,βˆ’π‘

β€’ Rotate by βˆ’π›Ό

β€’ Reflect through π‘₯

β€’ Rotate by 𝛼

β€’ Shift by (0, 𝑏)

𝑇 0,𝑏 𝑅𝛼𝑅𝑒𝑓π‘₯𝑅

βˆ’π›Όπ‘‡ 0,βˆ’π‘

23

𝑷

𝑷’ π’š = 𝒂𝒙 + 𝒃

𝜢 (𝟎, 𝒃)

𝒙

π’š

Rotate by Shear

β€’ Shear 1 01 1

,1 10 1

,1 π‘Ž0 1

,1 0π‘Ž 1

β€’ Rotation by 0 ≀ πœƒ β‰€πœ‹

2≑ composition of 3 shears

cos 𝜽 βˆ’sin 𝜽sin 𝜽 cos 𝜽

=1 𝒙0 1

1 0π’š 1

1 𝒛0 1

=

=1 + π‘₯𝑦 π‘₯

𝑦 11 𝒛0 1

=

=1 + π‘₯𝑦 𝑧 + π‘₯𝑦𝑧 + π‘₯

𝑦 𝑦𝑧 + 1

24

Page 5: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

5

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 5

Rotate by Shear

cos πœƒ βˆ’sin πœƒsin πœƒ cos πœƒ

=1 + π‘₯𝑦 𝑧 + π‘₯𝑦𝑧 + π‘₯

𝑦 𝑦𝑧 + 1

β€’ Solve for π‘₯, 𝑦, 𝑧:

cos πœƒ = 1 + π‘₯𝑦 = 𝑦𝑧 + 1 sin πœƒ = 𝑦 = βˆ’(𝑧 + π‘₯𝑦𝑧 + π‘₯)

β€’ Solution: 𝑦 = sin πœƒ , π‘₯ = 𝑧 = βˆ’ tan(πœƒ/2)

cos πœƒ βˆ’sin πœƒsin πœƒ cos πœƒ

=1 βˆ’tan πœƒ 2 0 1

1 0sin πœƒ 1

1 βˆ’tan πœƒ 2 0 1

β€’ When is this useful?

25

Rotate by Shear Image Rotation

26

http://www.datagenetics.com/blog/august32013/index.html

Rotate by Shear

β€’ Can we rotate with two (scaled) shears?

1 sin πœƒ0 cos πœƒ

sec πœƒ 0βˆ’tan πœƒ 1

=cos πœƒ βˆ’sin πœƒsin πœƒ cos πœƒ

β€’ What happens for πœƒ β†’πœ‹

2 ?

27

Rotation Approximation

β€’ For small angles πœƒ β†’ 0 we have: cos πœƒ β†’ 1 and sin πœƒ β†’ πœƒ (Taylor expansion of sin/cos)

β€’ Can approximate:

π‘₯ = π‘₯ cos πœƒ βˆ’ 𝑦 sin πœƒ β‰… π‘₯ βˆ’ π‘¦πœƒ 𝑦 = π‘₯ sin πœƒ + 𝑦 cos πœƒ β‰… π‘₯πœƒ + 𝑦

Examples (steps of πœƒ):

28

3D Transformations

β€’ All 2D transformations extend to 3D β€’ In homogeneous coordinates:

𝑆 𝑠π‘₯,𝑠𝑦,𝑠𝑧 =

𝑠π‘₯𝑠𝑦

𝑠𝑧

1

, 𝑇 𝑑π‘₯,𝑑𝑦,𝑑𝑧

11

1

𝑑π‘₯𝑑𝑦𝑑𝑧1

π‘…π‘§πœƒ =

cos πœƒ βˆ’ sin πœƒsin πœƒ cos πœƒ

1

1

β€’ What is 𝑅π‘₯πœƒ ? 𝑅𝑦

πœƒ ?

29

3D Transformations

β€’ Questions (commutativity):

– Scaling: Is S1S2 = S2S1?

– Translation: Is T1T2 = T2T1?

– Rotation: Is R1R2 = R2R1?

30

Page 6: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

6

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 6

Rotation Non-Commutativity

31

3D Coordinate Systems

OpenGL

swap any two axes

or reverse any one axis

Example: Arbitrary Rotation

Problem: Given two orthonormal coordinate systems

𝑋, π‘Œ, 𝑍 and [π‘ˆ, 𝑉,π‘Š], find a transformation from one to the other.

π‘Œ 𝑍

𝑋

π‘Š

𝑉

π‘ˆ

33

Arbitrary Rotation Answer:

Transformation matrix 𝑅 whose columns are π‘ˆ, 𝑉,π‘Š:

𝑅 =

𝑒π‘₯ 𝑣π‘₯ 𝑀π‘₯

𝑒𝑦 𝑣𝑦 𝑀𝑦

𝑒𝑧 𝑣𝑧 𝑀𝑧

Proof:

𝑅 𝑋 =

𝑒π‘₯ 𝑣π‘₯ 𝑀π‘₯

𝑒𝑦 𝑣𝑦 𝑀𝑦

𝑒𝑧 𝑣𝑧 𝑀𝑧

100

=𝑒π‘₯𝑒𝑦

𝑒𝑧

= π‘ˆ

Similarly 𝑅(π‘Œ) = 𝑉 and 𝑅(𝑍) = π‘Š

34

Arbitrary Rotation (cont.)

Inverse (=transpose) transformation, π‘…βˆ’1, maps [π‘ˆ, 𝑉,π‘Š] to 𝑋, π‘Œ, 𝑍 :

π‘…βˆ’1 π‘ˆ =

𝑒π‘₯ 𝑒𝑦 𝑒𝑧

𝑣π‘₯ 𝑣𝑦 𝑣𝑧

𝑀π‘₯ 𝑀𝑦 𝑀𝑧

𝑒π‘₯𝑒𝑦

𝑒𝑧

=𝑒 2

00

=100

= 𝑋

35

3D Graphics Coordinate Systems

36

β€’ How can we view (draw) 3D objects on a 2D screen?

3D World space

3D Camera space

2D View space

3D Object space

Page 7: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

7

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 7

Viewing Pipeline β€’ object - world positioning the objectβ€” modeling transformation glTranslate(tx,ty,tz), glScale(sx,sy,sz), glRotate(ang, xa,ya,za)

β€’ world - camera positioning the camera β€” viewing transformation gluLookAt(cx,cy,cz, ax,ay,az, ux,uy,uz)

β€’ camera – view taking a picture β€” projection transformation glFrustum(left, right, bottom, top, near,far)

β€’ view - screen displaying the image β€” viewport transformation glViewport(llx,lly, width,height)

37

3D World space

3D Camera space

2D View space

3D Object space

Viewing Transformations World β†’ Camera/Eye

38

World space

Camera/eye space

Viewing Transformations Camera β†’ View

39

π‘Œ

𝑍

𝑋

π‘Œ

𝑋

Camera space View space

Projection transformation

Projection Transformations

β€’ Canonical projection

– Ignore 𝑧 coordinate, use π‘₯, 𝑦 coordinates as view coordinates

40

1000

0000

0010

0001

π‘Œ

𝑍

𝑋

1 0 0 0

0 0 0 0

0 0 1 0

0 0 0 1

0 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Perspective Projector Lines

A line connecting a point in 3D with its 2D projection

41

object projection

plane

center of

projection

Pinhole Camera Model

42

Page 8: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

8

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 8

Comparison

43

Vanishing Points where 3D parallel lines meet in 2D

under perspective

44 Triple point perspective

Perspective Projection

45

𝑋

𝑍

Projection plane

𝑧 = 𝑑 Center of projection

π‘₯𝑝

𝑑=

π‘₯

𝑧 ,𝑦𝑝

𝑑=

𝑦

𝑧

π‘₯𝑝 =π‘₯

𝑧/𝑑 , 𝑦𝑝 =

𝑦

𝑧/𝑑

β€’ Viewing is from point at a finite distance

β€’ Without loss of generality: – Viewpoint at origin – Projection (viewing) plane is

𝑧 = 𝑑

Triangle similarity:

π‘₯𝑦𝑧

π‘₯𝑝

𝑦𝑝

𝑑

Perspective Projection β€’ Euclidean coordinates:

π‘₯𝑝

𝑦𝑝

𝑑=

π‘₯

𝑧 𝑑 𝑦

𝑧 𝑑 𝑧

𝑧 𝑑

β€’ Homogeneous coordinates:

π‘₯𝑝

𝑦𝑝

𝑑1

=

π‘₯

𝑧 𝑑 𝑦

𝑧 𝑑 𝑧

𝑧 𝑑 1

=

π‘₯𝑦𝑧

𝑧/𝑑

β€’ Matrix form:

𝑃

π‘₯𝑦𝑧1

=

1 0 0 00 1 0 00 0 1 0

0 01

𝑑0

π‘₯𝑦𝑧1

=

π‘₯𝑦𝑧

𝑧/𝑑

β€’ 𝑃 is not injective & singular: det (𝑃) = 0 46

𝑋

𝑍 𝑧 = 𝑑

π‘₯𝑦𝑧

π‘₯𝑝

𝑦𝑝

𝑑

π‘₯𝑝

𝑑=

π‘₯

𝑧 ,𝑦𝑝

𝑑=

𝑦

𝑧

47

www.pinterest.com/explore/forced-perspective/

Fun with Perspective

48

Page 9: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

9

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 9

Scale vs. Dolly What is the difference between:

β€’ Moving the projection plane (scale, zoom)

β€’ Moving the viewpoint (center of projection) (dolly)

Z

Z

49

Scale

y y

Zz z

y

z

Dolly

Scale Dolly

50

Original

The Dolly-Zoom Effect aka β€œHitchcock effect”

β€’ Dolly + scale s.t. subject remains same size

β€’ Zoom-in camera while moving away from subject (or the opposite)

51

vimeo.com/moogaloop.swf?clip_id=84548119

Perspective Warp

1 0 0 0

0 1 0 0

( )0 0

10 0 1/ 0 /

xx

yy

d d d zz

d d d

d z d

2

/

/

p

p

p

x

z dxy

yz d

zd z

z d

52

2 2( )( ) 1

( )

d z df z

z d d z

z

f(z)

monotonic increasing in z

preserves z ordering

z1 z2

53

2d

d

d

d

f(z2) f(z1)

View Frustrum

54

Page 10: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

10

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 10

55

1 0 0 0

0 1 0 0

0 0

0 0 1/ 0

d d

d d

d

20 0

20 0

( ) 20 0

0 0 1 0

n r l

r l r l

n t b

t b t b

f n fn

f n f n

Projection Matrix From View Frustrum

Important special case:

π‘Ÿ = βˆ’π‘™, 𝑑 = βˆ’π‘

perspective

warp normalized

device

coordinates

56

Viewport Matrix From NDC to Viewport

normalized

device

coordinates

(llx,lly) width

height

57

Fun with Perspective

3D Sidewalk Art by Julian Beever

58

59 60

Page 11: Intro to Computer Graphics Transformations...5 CS5620 Intro to Computer Graphics Copyright C. Gotsman, G. Elber, M. Ben-Chen Computer Science Dept., Technion Transformations Page 5

11

CS5620

Intro to Computer Graphics

Copyright

C. Gotsman, G. Elber, M. Ben-Chen

Computer Science Dept., Technion

Transformations

Page 11

61 62

63