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
y
Translate over vector (tx, ty, tz ):
x’=x+ tx, y’=y+ ty , z’=z+ tz
or
T
P
P' P T, met
x'
x
tx
P' y ' , P y en T t y
z'
z
t
z
P+T
x
z
H&B 9-1:304-305
3D Translation 2
In 4D homogeneous coordinates:
y
P' MP , or
x' 1
y' 0
z' 0
1 0
0
1
0
0
T
0 t x x
0 t y y
1 t z z
0 1 1
P+T
P
x
z
H&B 9-1:304-305
3D Rotation 1
Rotate over angle a around z - as :
x' x cos a y sina
y ' x sina y cos a
z' z
Or
P' R z (a )P, with
cos a
sina
R z (a )
0
0
z
y
P’
a
P
sina
cos a
0
0
0 0
0 0
1 0
0 1
x
H&B 9-2:305-313
3D Rotation 2
z
z
y
x
z
y
x
y
x
Rotation around axis:
- Counterclockwise, viewed from rotation axis
H&B 9-2:305-313
3D Rotation 3
z
zx
x y
yz
y
x
z
x y
yz
zx
y
x
z
y
x
Rotation around axes:
Cyclic permutation coordinate axes
x yzx
H&B 9-2:305-313
3D Rotatie 4
Rotate over angle a around z - as :
x' x cos a y sina
zx
y ' x sina y cos a
x y
z' z
Rotate over angle a around x - as :
y ' y cos a z sina
z ' y sina z cos a
x' x
Or
P' R z (a )P, with
Or
P' R x (a )P, with
cos a
sina
R z (a )
0
0
yz
sina
cos a
0
0
0 0
0 0
1 0
0 1
0
1
0 cos a
R x (a )
0 sina
0
0
0
sina
cos a
0
0
0
0
1
H&B 9-2:305-313
3D Rotation around arbitrary axis 1
Rotation around axis, parallel to coordinate axis, through point Q.
For example. the z - as. Similaras 2D rotation :
1. T ranslate over -Q;
2. Rotate around z - axis;
3. T ranslate back over Q.
Or :
P' T(Q )Rz (a )T(Q )P
2
3
z
P’ y
a
1 Q
P
x
H&B 9-2:305-313
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.
y
P2
P1
z
x
H&B 9-2:305-313
3D Rotation around arbitrary axis 3
y
y
P2
P1
P1'
x
z
P
z
x
z
Initial
''
2
y
P2'
''
2
P
1. translate axis
y
y
P1'
P1'
x
3. rotate around
z-axis
z
x
2. rotate axis
y
P2'
x
4. rotate back
z
P1'
P2
P1
z
x
5. translate back
3D Rotation around arbitrary axis 3
y
y
P2
P1
z
P
x
z
Initial
y
Rz(a)
'
1
P
P
z
'
1
''
2
P
3. rotate around
z-axis
z
z
x
2. rotate axis
y
P2'
R1
x
4. rotate back
P2
P1
P
x
R
'
1
P
1. translate axis
y
''
2
T(P1)
'
1
x
y
P2'
z
T(P1)
x
5. translate back
3D Rotation around arbitrary axis 3
y
P2
M = T(P1) R1Rz(a) RT(P1)
y
y
'
P2
P1
P
x
z
x
z
Initial
y
Rz(a)
'
1
P
P
z
'
1
''
2
P
3. rotate around
z-axis
z
z
x
2. rotate axis
y
P2'
R1
x
4. rotate back
P2
P1
P
x
R
'
1
P
1. translate axis
y
''
2
T(P1)
'
1
z
T(P1)
x
5. translate back
3D Rotation around arbitrary axis 4
Difficult step:
y
P2'
R
y
2. rotate axis
'
1
P
z
x
''
2
P
z
P1'
x
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
H&B 9-2:305-313
3D Rotation around arbitrary axis 5
1. Construct orthonormal axis frame u, v, w;
2. Invent rotation matrix R, such that:
y
u is mapped to z-axis;
v is mapped to y-axis;
w is mapped to x-axis.
u
v
w
x
z
H&B 9-2:305-313
3D Rotation around arbitrary axis 6
Construct orthonormal axis frame u, v, w:
u = (P2P1) / |P2P1|
v = u (1,0,0) / | u (1,0,0) |
w=vu
(If u = (a, 0, 0), then use (0, 1, 0))
y
u
v
w
x
This frame is orthonormal:
z
Unit length axes: u.u = v.v = w.w = 1
All axes perpendicular: u.v = v.w = w.u = 0
H&B 9-2:305-313
3D Rotation around arbitrary axis 7
Generic rotation matrix :
r11 r12 r13
r21 r22 r23
R
r
r
r
31 32 33
0
0
0
Fillin R such that
0
0
0
1
y
u
v
w
x
z
ux 0
vx 0
wx 1
u y 0
vy 1
wy 0
R , R , and R
u
1
v
0
w
0
z
z
z
1 1
1 1
1 1
H&B 9-2:305-313
3D Rotation around arbitrary axis 8
Fill R, such that
ux 0
vx 0
wx 1
u y 0
vy 1
wy 0
R , R , en R
u
1
v
0
w
0
z
z
z
1 1
1 1 ind 1 1
Solution:
wx
vx
R
u
x
0
wy
wz
vy
vz
uy
uz
0
0
0
0
0
1
Done! But how to find R1 ?
y
u
v
w
x
z
H&B 9-2:305-313
Inverse of rotation matrix 1
Each rotation matrix is an orthonormal matrix M:
ux
M u v w u y
u
z
vx
vy
vz
wx
wy
wz
y
v
The frame u, v, w is orthonormal:
Unit length axes: u.u = v.v = w.w = 1 z
All axes perpendicular: u.v = v.w = w.u = 0.
Requested: M-1 such that M-1M = I
u
w
x
H&B 9-2:305-313
Inverse of rotation matrix 2
Requested: M-1 such that M-1M = I
Solution:
uT u x
1
T
T
M M v vx
T
w wx
uy
vy
wy
uz
vz
wz
In words:
The inverse of a rotation matrix is the transpose.
(For a rotation around the origin).
H&B 9-2:305-313
Inverse of rotation matrix 3
Requested: M-1 such that M-1M = I
Solution: M-1 = M-T
Check:
uT
u u u v u w 1 0 0
T
T
M M v u v w v u v v v w 0 1 0 I
T
w u w v w w 0 0 1
w
H&B 9-2:305-313
3D Rotation with quaternions
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)
H&B 9-2:313-317
3D scaling
Scale with factors sx, sy,sz :
x’= sx x, y’= sy y, z’= sz z
or
P' SP, or
x' s x
y' 0
z' 0
1 0
0
0
sy
0
0
0
sz
0
0 x
0 y
0 z
1 1
H&B 9-3:317-319
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
Affine transformations 1
Generic name for these transformations:
affine transformations
x' a xx x a xy y a xz z bx
y' a yx x a yy y a yz z by
z ' a zx x a zy y a zz z bz
H&B 9-7:324
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, reflection
4. Parallel lines remain parallel
5. Only translation, rotation reflection: angles and
lengths are maintained
H&B 9-7:324
Hierarchical modeling 1
ComplexObject::= Composition of Objects
Object::= SimpleObject or ComplexObject
puppet
head
upper arm
torso
arms
legs
left arm
right arm
lower arm
hand
H&B 11:383-391
Hierarchical modeling 2
Hierarchical model: tree structure …
puppet
head
upper arm
torso
arms
legs
left arm
right arm
lower arm
hand
H&B 11:383-391
Hierarchical modeling 3
Hierarchical model: tree structure or
directed, acyclic graph
puppet
head
upper arm
torso
arms
legs
left arm
right arm
lower arm
hand
H&B 11:383-391
Hierarchical modeling 4
Leaf: primitive object
geometric object, possibly parametrised
Composite node: instruction for composition
(usually union)
Leafs, nodes and/or edges:
transformations + other data
H&B 11:383-391
Hierarchical modeling 5
Local coordinates: coordinates of node
world coordinates
train coordinates
wheel coordinates
world
train
wheel
H&B 11:383-391
Hierarchical modeling 6
Implementation: Many variations possible.
DrawWorld
DrawTrain(P1, S1);
DrawTrain(P2, S2);
wereld
DrawTrain(P, S);
Translate(P); Scale(S);
DrawChimney(); …
DrawWheel(W1); DrawWheel(W2); DrawWheel(W3);
Scale(1/S); Translate(-P);
trein
wiel
DrawWheel(W);
Translate(W); DrawCircle(radius); Translate(-W);
H&B 11:383-391
Hierarchical modeling 7
Use 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
Hierarchical modeling 7
DrawTwoGrids
DrawGrid;
Translate(7,0);
DrawGrid;
How many lines of code you
need for this picture?
Mwah, 10-20 should do
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);
H&B 11:383-391
Next…
• We know how to transform objects
• Next step: Viewing objects