computer graphics 3 - transformations · view transformation we want to convert a vertex vg from...

43
Computer Graphics 3 - Transformations Tom Thorne Slides courtesy of Taku Komura www.inf.ed.ac.uk/teaching/courses/cg

Upload: others

Post on 14-Jun-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Computer Graphics 3 - Transformations

Tom Thorne

Slides courtesy of Taku Komurawww.inf.ed.ac.uk/teaching/courses/cg

Page 2: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Overview

Homogeneous transformations

I 2D transformationsI 3D transformations

View transformation

Page 3: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Placing objects

I Having prepared objects, they need to be placed in theenvironment

I This involves knowing the vertex coordinates of the object inthe global coordinate system

I Object vertex coordinates are only defined locally

Page 4: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Transformations

The coordinates of the vertices of the object and translated, rotatedand scaled to place them in the global coordinate system.

Page 5: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Translation

x,y

x',y'

Ax

Õ

y

Õ

B

=

Ax

y

B

+

Ad

x

d

y

B

Page 6: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Scaling

x,y

x',y'

Ax

Õ

y

Õ

B

=

As

x

00 s

y

B Ax

y

B

Page 7: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Rotation

x,y

x',y'

Ax

Õ

y

Õ

B

=

Acos ◊ ≠ sin ◊sin ◊ cos ◊

B Ax

y

B

Page 8: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

RotationColumns of the matrix correspond to the new x and y axes aftertransformationA

x

Õ

y

Õ

B

=

Acos ◊ ≠ sin ◊sin ◊ cos ◊

B Ax

y

B

(1,0)cos θ

(0,1)

sin θ

-sin θ

cos θθ

θ

Page 9: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Operations

TranslationA

x

Õ

y

Õ

B

=

Ax

y

B

+

Ad

x

d

y

B

ScalingA

x

Õ

y

Õ

B

=

As

x

00 s

y

B Ax

y

B

RotationA

x

Õ

y

Õ

B

=

Acos ◊ ≠ sin ◊sin ◊ cos ◊

B Ax

y

B

Unfortunately these aren’t all the same operation!

Page 10: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Homogeneous coordinatesExtend the coordinate system with a mapping back to 2D:Q

cax

y

w

R

db =

Ax/w

y/w

B

Then:

Translation

Q

cax

Õ

y

Õ

1

R

db =

Q

ca1 0 d

x

0 1 d

y

0 0 1

R

db

Q

cax

y

1

R

db

Scaling

Q

cax

Õ

y

Õ

1

R

db =

Q

cas

x

0 00 s

y

00 0 1

R

db

Q

cax

y

1

R

db

Rotation

Q

cax

Õ

y

Õ

1

R

db =

Q

cacos ◊ ≠ sin ◊ 0sin ◊ cos ◊ 0

0 0 1

R

db

Q

cax

y

1

R

db

Page 11: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Combining transformations

• With a set of transformation matrices T, R, S, apply transformations with respect to global coordinate system: order them right to left

• E.g. Rotation R, then scaling S, then translation T, would be TSR

• Can combine these matrices into a single matrix by applying matrix multiplication.

• Note there is only one way to apply matrix multiplication, and it is not commutative (i.e. TSR!=RST)

Page 12: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Combining transformations

v1 = M0v0v2 = M1v1

»v2 = M1M0v0 v2

v0

Sequentially multiply the matrices

I Remember that matrix operations are not commutative

Page 13: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example 1

Page 14: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example 1

Page 15: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example 2

Page 16: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

A common matrix

Rotation followed by translation isoften used, this is worthremembering:

Q

cacos ◊ ≠ sin ◊ d

x

sin ◊ cos ◊ d

y

0 0 1

R

db v2

v0

Page 17: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Transforming between coordinate systems

• Positioning objects (defined in a local coordinate system) in a scene

• Finding coordinates of a point with respect to a specific origin and x/y axes (calculating camera coordinates of objects)

Page 18: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Transformation between coordinate systems

We can interpret the transformation matrix as converting thecoordinates of vertices between di�erent coordinate systems.

v

g

= Mv

l

v

l

= M

≠1v

g

vgv l

v l

Page 19: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example

I What is the position of X

G

in coordinate system L?I What is the position of X

L

in coordinate system G?

M =

Q

ca

1Ô2

1Ô2 2

≠ 1Ô2

1Ô2 2

0 0 1

R

db

M

≠1 =

Q

ca

1Ô2 ≠ 1Ô

2 01Ô2

1Ô2 ≠2

Ô2

0 0 1

R

db

G xLx (1,1)(1,2)

G

L

Page 20: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example

What is the position of X

G

in coordinate systemL?Q

ca

1Ô2 ≠ 1Ô

2 01Ô2

1Ô2 ≠2

Ô2

0 0 1

R

db

Q

ca121

R

db =

Q

ca≠ 1Ô

2≠ 1Ô

21

R

db

What is the position of X

L

in coordinate systemG?Q

ca

1Ô2

1Ô2 2

≠ 1Ô2

1Ô2 2

0 0 1

R

db

Q

ca111

R

db =

Q

ca2 +

Ô2

21

R

db

G xLx (1,1)(1,2)

G

L

Page 21: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Transformation between coordinate systems

We can generalize the idea to multiple coordinate systems ratherthan local and global:

v

i

= M

iΩj

v

j

v

j

= M

jΩi

v

i

= M

≠1iΩj

v

i

If we have multiple coordinate systems a, b, c and d , along withtransformations M

aΩb

, M

bΩc

, M

cΩd

. . .

Page 22: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Multiple coordinate systemsWhat is the position of V

d

with respect to coordinate system c?

a

b

c

d

vd

Page 23: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Multiple coordinate systemsWhat is the position of V

d

with respect to coordinate system c?

Iv

c

= M

cΩd

v

d

a

b

c

d

vdvc

Page 24: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Multiple coordinate systemsWhat is the position of V

d

with respect to coordinate system b?

Iv

c

= M

cΩd

v

d

Iv

b

= M

bΩc

v

c

= M

bΩc

M

cΩd

v

d

a

b

c

d

vd

vb

Page 25: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Multiple coordinate systemsWhat is the position of V

d

with respect to coordinate system a?

Iv

c

= M

cΩd

v

d

Iv

b

= M

bΩc

v

c

= M

bΩc

M

cΩd

v

d

Iv

a

= M

aΩb

v

b

= M

aΩb

M

bΩc

M

cΩd

v

d

a

b

c

d

vd

va

Page 26: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Combining transformations

• Less common: special case where we want to apply the transformations in our matrices with respect to the local coordinate system as it moves. Matrices are ordered from left to right.

• E.g. a car driving forwards, turning and then driving forwards again. The second transformation forwards should be along the local y axis of the car, not of the global coordinate system. Apply matrices as TRU

• This is equivalent to performing U then R then T in the global coordinate system - just an easier way of constructing it.

R

T

U

Page 27: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Order of multiplication

Page 28: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example - driving a carSitting in a car with the mirror 0.4m to your right, you

I drive 5m forwardsI turn 30 degrees rightI drive 2m forwards

Where is the side mirror relative to its original position?

Page 29: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example - driving a car

The transformation matrix of driving forwards 5m is:

T1 =

Q

ca1 0 00 1 50 0 1

R

db

The transformation matrix of turning 30 degrees to the right is:

R =

Q

ca≠

Ô3

212 0

≠12

Ô3

2 00 0 1

R

db

The transformation matrix of driving forwards 2m is:

T2 =

Q

ca1 0 00 1 20 0 1

R

db

Page 30: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Solution

x

Õ = T1RT2x =

Q

ca1 0 00 1 50 0 1

R

db

Q

ca≠

Ô3

212 0

≠12

Ô3

2 00 0 1

R

db

Q

ca1 0 00 1 20 0 1

R

db

Q

ca0.401

R

db

x

Õ =

Q

ca0.656.53

1

R

db

Page 31: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

3D translation

Very simple to extend 2D case to 3D:

Q

ccca

x

Õ

y

Õ

z

Õ

1

R

dddb =

Q

ccca

1 0 0 d

x

0 1 0 d

y

0 0 1 d

z

0 0 0 1

R

dddb

Q

ccca

x

y

z

1

R

dddb

Page 32: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

3D scaling

Very simple to extend 2D case to 3D:

Q

ccca

x

Õ

y

Õ

z

Õ

1

R

dddb =

Q

ccca

s

x

0 0 00 s

y

0 00 0 s

z

00 0 0 1

R

dddb

Q

ccca

x

y

z

1

R

dddb

Page 33: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

3D rotation - X axis

Q

ccca

x

Õ

y

Õ

z

Õ

1

R

dddb =

Q

ccca

1 0 0 00 cos ◊ ≠ sin ◊ 00 sin ◊ cos ◊ 00 0 0 1

R

dddb

Q

ccca

x

y

z

1

R

dddb

Page 34: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

3D rotation - arbitrary axis

u

Q

ccca

x

Õ

y

Õ

z

Õ

1

R

dddb =

Q

ccca

u

2x

+ c(1 ≠ u

2x

) u

x

u

y

(1 ≠ c) ≠ u

z

s u

z

u

x

(1 ≠ c) + u

y

s 0u

x

u

y

(1 ≠ c) + u

z

s u

2y

+ c(1 ≠ u

2y

) u

y

u

z

(1 ≠ c) ≠ u

x

s 0u

z

u

x

(1 ≠ c) ≠ u

y

s u

y

u

z

(1 ≠ c) + u

x

s u

2z

+ c(1 ≠ u

2z

) 00 0 0 1

R

dddb

Q

ccca

x

y

z

1

R

dddb

c = cos ◊, s = sin ◊

Page 35: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Transforming vertex normalsTransforming vertex normals, we need to take care to ensure theyremain perpendicular to the surface.Tangent vectors remain tangent to the surface after transformation.For tangent vector t and normal vector n transformed by scaling S:

n=(1,1)n'=(2,1)t=(-1,1) t'=(-2,1)

S =

Q

ca2 0 00 1 00 0 1

R

db n

Õ = Sn, t

Õ = St.

Page 36: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Transforming vertex normalsTransforming vertex normals, we need to take care to ensure theyremain perpendicular to the surface.Tangent vectors remain tangent to the surface after transformation.For any tangent vector t and normal vector n transformed by M:

n · t = 0n

T (M≠1M)t = 0

(nT

M

≠1)(Mt) = 0

Therefore n

T

M

≠1 is perpendicular to the transformed Mt for any t.So the desired normal vector is:n

Õ = (M≠1)T

n Shirley 6.2.2

Page 37: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

View transformation

I Transform to coordinatesystem of camera

I Use the orientation andtranslation of camera fromthe origin of the worldcoordinate system

vg

g

c

Mg<-c

Page 38: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

View transformation

We want to convert a vertex v

g

from the global coordinate systemto a vertex in the camera coordinate system v

c

. We can do thisusing the camera to world transformation matrix M

gΩc

.

v

g

= M

gΩc

v

c

v

c

= M

≠1gΩc

v

g

= M

cΩg

v

g

vg

g

c

Mg<-c

Page 39: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example

• Camera at (2,2), rotated 45 degrees clockwise

• We want to know the coordinates of P in the camera coordinate system i.e. starting from (2,2), what combination of the new x axis and y axis are needed?

2,2P

4,1

Page 40: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example

• Rotation then translation

R =

0

@1p2

1p2

0

� 1p2

1p2

0

0 0 1

1

A

T =

0

@1 0 20 1 20 0 1

1

A

TR =

0

@1 0 20 1 20 0 1

1

A

0

@1p2

1p2

0

� 1p2

1p2

0

0 0 1

1

A =

0

@1p2

1p2

2

� 1p2

1p2

2

0 0 1

1

A

2,2P

4,1R T

Page 41: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Example

• To find P with respect to the new x and y axes:

Ml!g = TR =

0

@1p2

1p2

2

� 1p2

1p2

2

0 0 1

1

A

Mg!l = M�1l!g = (TR)�1 =

0

B@

1p2

� 1p2

21p2

1p2

�2p2

0 0 1

1

CA

2,2P

P 0 = Mg!lP =

0

B@

1p2

� 1p2

21p2

1p2

�2p2

0 0 1

1

CA

0

@411

1

A =

0

@2 + 3p

21p21

1

A

Page 42: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

Summary

I Three kinds of transformation - translation, rotation and scalingI In homogeneous coordinates, transformations can be

represented by matrix multiplicationI After transforming objects to world coordinates, transform to

coordinate system of the camera for viewing

Page 43: Computer Graphics 3 - Transformations · View transformation We want to convert a vertex vg from the global coordinate system to a vertex in the camera coordinate system vc.Wecandothis

References

Recommended reading:

I Shirley, Chapter 6.1 to 6.1.5 inclusive, 6.2 to 6.5 inclusive

Extra reference materials:

I Foley, Chapter 5 (Geometrical Transformations)