2iv60 computer graphics set 4:3d transformations and hierarchical modeling

35
2IV60 Computer graphics set 4:3D transformations and hierarchical modeling Jack van Wijk TU/e

Upload: kanan

Post on 19-Mar-2016

42 views

Category:

Documents


1 download

DESCRIPTION

2IV60 Computer graphics set 4:3D transformations and hierarchical modeling. Jack van Wijk TU/e. From 2D to 3D. Much +/- the same: Translation, scaling Homogeneous vectors: one extra coordinate Matrices: 4x4 Rotation more complex. 3D Translation 1. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

2IV60 Computer graphicsset 4:3D transformations and

hierarchical modelingJack van Wijk

TU/e

Page 2: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

From 2D to 3D

• Much +/- the same:– Translation, scaling– Homogeneous vectors: one extra coordinate– Matrices: 4x4

• Rotation more complex

Page 3: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Translation 1

Translate over vector (tx, ty, tz ):

x’=x+ tx, y’=y+ ty , z’=z+ tz

orx

y

P

P+T

H&B 9-1:304-305

T

z

z

y

x

ttt

zyx

zyx

TPP

TPP'

en , '''

'

met ,

Page 4: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Translation 2

In 4D homogeneous coordinates:

x

y

P

P+T

H&B 9-1:304-305

T

z

11000100010001

1'''

or ,

zyx

ttt

zyx

z

y

x

MPP'

Page 5: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation 1

z

P

H&B 9-2:305-313

x

yP’

1000010000cossin00sincos

)(

with,)(Or

'cossin'sincos'

:as- around angleover Rotate

z

z

zzyxyyxx

z

R

PRP'

Page 6: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation 2

z

x

y

Rotation around axis:- Counterclockwise, viewed from rotation axis

z

x

y z

x

y

H&B 9-2:305-313

Page 7: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation 3

z

x

y z

x

y z

x

y

zyyxxz

xzzyyx

Rotation around axes:Cyclic permutation coordinate axes

xzyx H&B 9-2:305-313

Page 8: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotatie 4

zyyxxz

H&B 9-2:305-313

1000010000cossin00sincos

)(

with,)(Or'

cossin'sincos'

:as- around angleover Rotate

z

z

zzyxyyxx

z

R

PRP'

10000cossin00sincos00001

)(

with,)(Or

'cossin'sincos'

:as- around angleover Rotate

x

x

xxzyzzyy

x

R

PRP'

Page 9: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

1

3D Rotation around arbitrary axis 1

z

P

x

yP’

Q

23

H&B 9-2:305-313

Q)P)T((T(Q)RP'

Q

Q

Q

z :Or

.over back Translate 3.axis;- around Rotate 2.;over Translate 1.

:rotation 2D asSimilar as.- theexample.For .point through axis, coordinate toparallel axis, aroundRotation

z-z

Page 10: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

z x

y

1P

2P

3D Rotation around arbitrary axis 2 Rotation around axis through two points P1 and P1 .More complex:1.Translate such that axis goes through origin;2.Rotate…3.Translate back again.

H&B 9-2:305-313

Page 11: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 3

z x

y

1P

2P

Initialz x

y

'1P

1. translate axis

'P2

z x

y

2. rotate axis

'1P

''P2

z x

y

3. rotate aroundz-axis

'1P

''P2

4. rotate backz x

y

'1P

'P2

z x

y

1P

2P

5. translate back

Page 12: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 3

z x

y

1P

2P

Initialz x

y

'1P

1. translate axis

'P2

z x

y

2. rotate axis

'1P

''P2

z x

y

3. rotate aroundz-axis

'1P

''P2

4. rotate backz x

y

'1P

'P2

z x

y

1P

2P

5. translate back

T(P1) R

Rz()R1 T(P1)

Page 13: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 3

z x

y

1P

2P

Initialz x

y

'1P

1. translate axis

'P2

z x

y

2. rotate axis

'1P

''P2

z x

y

3. rotate aroundz-axis

'1P

''P2

4. rotate backz x

y

'1P

'P2

z x

y

1P

2P

5. translate back

T(P1) R

Rz()R1 T(P1)

M = T(P1) R1Rz() RT(P1)

Page 14: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 4

z x

y

'1P

'P2

z x

y2. rotate axis

'1P

''P2

Difficult step:

Find rotation such that rotation axis aligns with z-axis.Two options:1. Step by step by step (see H&B, 309-312)2. Direct derivation of matrix

R

H&B 9-2:305-313

Page 15: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 5

zx

yu

vw

1. Construct orthonormal axis frame u, v, w;

2. Invent rotation matrix R, such that:

u is mapped to z-axis; v is mapped to y-axis; w is mapped to x-axis.

H&B 9-2:305-313

Page 16: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 6

zx

yu

vw

Construct orthonormal axis frame u, v, w:

u = (P2P1) / |P2P1|v = u (1,0,0) / | u (1,0,0) |w = v u(If u = (a, 0, 0), then use (0, 1, 0))

This frame is orthonormal:Unit length axes: u.u = v.v = w.w = 1All axes perpendicular: u.v = v.w = w.u = 0

H&B 9-2:305-313

Page 17: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 7

zx

yu

vw

H&B 9-2:305-313

1001

1

and ,

1010

1

,

1100

1

such that in Fill1000000

:matrixrotation Generic

333231

232221

131211

z

y

x

z

y

x

z

y

x

www

vvv

uuu

rrrrrrrrr

RRR

R

R

Page 18: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation around arbitrary axis 8

zx

yu

vw

Done! But how to find R1 ? H&B 9-2:305-313

1000000

:Solution1001

1

and ,

1010

1

,

1100

1

such that , Fill

zyx

zyx

zyx

z

y

x

z

y

x

z

y

x

uuuvvvwww

www

vvv

uuu

R

RRR

R

Page 19: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

zzz

yyy

xxx

wvuwvuwvu

wvuM

Each rotation matrix is an orthonormal matrix M:

The frame u, v, w is orthonormal:Unit length axes: u.u = v.v = w.w = 1All axes perpendicular: u.v = v.w = w.u = 0.

Requested: M-1 such that M-1M = I

Inverse of rotation matrix 1

zx

yu

vw

H&B 9-2:305-313

Page 20: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Requested: M-1 such that M-1M = ISolution:

In words:The inverse of a rotation matrix is the transpose.(For a rotation around the origin).

zyx

zyx

zyx

T

T

T

T

wwwvvvuuu

1

wvu

MM

Inverse of rotation matrix 2

H&B 9-2:305-313

Page 21: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Iwwvwuwwvvvuvwuvuuu

wvuwvu

MM

100010001

T

T

T

T

Requested: M-1 such that M-1M = ISolution: M-1 = M-T

Check:

Inverse of rotation matrix 3

H&B 9-2:305-313

Page 22: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

3D Rotation with quaternions

H&B 9-2:313-317

Quaternions:• Extension of complex numbers• Four components• Scalar value + 3D vector: q=(s,v)• Special calculation rules• Compact description of rotations• To be used if many complex rotations have to be done (esp. animation)

Page 23: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

11000000000000

1'''

or ,

zyx

ss

s

zyx

z

y

x

SPP'

3D scaling

Scale with factors sx, sy,sz :

x’= sx x, y’= sy y, z’= sz z

or

H&B 9-3:317-319

Page 24: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

More 3D transformations+/- same as in 2D:

• Matrix concatenation by multiplication• Reflection• Shearing• Transformations between coordinate systems

H&B 9-4, 9-5, 9-6:319-324

Page 25: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

zzzzyzx

yyzyyyx

xxzxyxx

bzayaxaz

bzayaxay

bzayaxax

'

'

'

Affine transformations 1

Generic name for these transformations:affine transformations

H&B 9-7:324

Page 26: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Affine transformations 2

Properties:1. Transformed coordinates x’,y’ and z’ are linearly

dependent on original coordinates x, y en z.2. Parameters aij en bk are constant and determine type of

transformation;3. Examples: translation, rotation, scaling, reflection4. Parallel lines remain parallel5. Only translation, rotation reflection: angles and lengths

are maintained

H&B 9-7:324

Page 27: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 1

ComplexObject::= Composition of ObjectsObject::= SimpleObject or ComplexObject

H&B 11:383-391

puppet

head torso arms legs

left arm right arm

lower armupper arm hand

Page 28: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 2

Hierarchical model: tree structure …

puppet

head torso arms legs

left arm right arm

lower armupper arm handH&B 11:383-391

Page 29: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 3

Hierarchical model: tree structure or directed, acyclic graph

puppet

head torso arms legs

left arm right arm

lower armupper arm handH&B 11:383-391

Page 30: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 4

Scene graph:Leaf: primitive object geometric object, possibly parametrisedComposite node: instruction for composition (usually union)Leafs, nodes and/or edges: transformations + other data

Implementation: data (read model, generic engine) or code (translate into statements & calls).

H&B 11:383-391

Page 31: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 5

Local coordinates: coordinates of node

train

world

wheel

world coordinatestrain coordinateswheel coordinates

H&B 11:383-391

Page 32: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 6

Implementation: Many variations possible.

trein

wereld

wiel

DrawWorld DrawTrain(P1, S1); DrawTrain(P2, S2);

DrawTrain(P, S); Translate(P); Scale(S); DrawChimney(); … DrawWheel(W1); DrawWheel(W2); DrawWheel(W3); Scale(1/S); Translate(-P);

DrawWheel(W); Translate(W); DrawCircle(radius); Translate(-W); H&B 11:383-391

Page 33: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 7Use structure of model to structure implementation:- Hierarchy (use classes, procedures and functions);- Regularity (use loops);- Variation (use conditions).

How many lines of code you need for this picture?

H&B 11:383-391

Page 34: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Hierarchical modeling 7DrawTwoGrids DrawGrid; Translate(7,0); DrawGrid; DrawGrid for i := 1 to 5 do for j := 1 to 5 do DrawCell(i, j, (i+j) mod 2 = 0);

DrawCell(x, y, use_red) SetColor(dark_grey); DrawRect(x+0.2,y, 0.7, 0.7); if use_red then SetColor(red) else SetColor(light_grey); DrawRect(x, y+0.2, 0.7, 0.7);

How many lines of code you need for this picture?

Mwah, 10-20 should do

Aim for as clean as possible, not as compact as possible H&B 11:383-391

Page 35: 2IV60 Computer graphics set 4:3D transformations and hierarchical modeling

Next…

• We know how to transform objects• Next step: Viewing objects