4. Translation – 2D
Y
Y
(4,5)
(7,5)
(7,1)
Before Translation
x’ = x + dx
y’ = y + dy
X
Translation by (3,-4)
d x
x
x′
P = P′ = T =
y
y ′
d y
Homogeniou s Form
x ′ 1 0 d x x
y ′ = 0 1 d * y
y
1 0 0 1 1
(10,1)
X
P′ = P + T
4
5. Scaling – 2D
Y
Y
(4,5)
Types of Scaling:
Differential ( sx != sy )
Uniform ( sx = sy )
(7,5)
(2,5/4)
X
Before Scaling
(7/2,5/4)
Scaling by (1/2, 1/4)
X
x′ = s x * x
y′ = s y * y
S
* P =
P′
Homogeniou s Form
⇓
⇓
⇓
x′ s x
y ′ = 0
1 0
sx
0
0 x x * sx
* = y*s
sy y
y
0
sx
0
0 x
0 * y
1 1
5
6. te d
rota
Rotation – 2D
r cos φ
v=
r sin φ
r cos( φ + θ )
v′ =
r sin ( φ + θ )
x′ = r cos φ cosθ − r sinφ sinθ
expand (φ + θ ) ⇒
y ′ = r cos φ sinθ − r sinφ cosθ
x = r cos φ
x′ = x cos θ − y sinθ
but
⇒
y = r sinθ
y ′ = x sinθ + y cosθ
or
l
i na
ig
7. Rotation – 2D
Y
Y
Before Rotation
Rotation of 45 deg. w.r.t. origin
(4.9,7.8)
(2.1,4.9)
(5,2)
(9,2)
X
X
x * cosθ − y * sin θ = x′
x * sin θ + y * cosθ = y′
R
*P=
P′
cosθ − sin θ x x * cosθ − y * sin θ
sin θ cosθ * y = x * sin θ + y * cosθ
Homogenious Form
x ′ cosθ
y ′ = sin θ
1 0
− sin θ
cosθ
0
0 x
0 * y
1 1 7
9. Shearing Transformation
1 a 0
SH x = 0 1 0
0 0 1
unit cube
Sheared in X
direction
1 0 0
SH y = b 1 0
0 0 1
Sheared in Y
direction
SH xy
1 a 0
= b 1 0
0 0 1
Sheared in both X
and Y direction
9
10. Inverse 2D - Transformations
-1
(dx,dy)
Translaiton : T
Rotation
Sclaing
-1
(θ )
: R
: S
= R(-θ )
-1
(sx,sy)
Mirror Ref : M
M
-1
x
-1
y
= T(-dx,-dy)
= S ( 1 sx , 1 sy )
= Mx
= My
10
11. Homogeneous Co-ordinates
Translation, scaling and rotation are expressed
(non-homogeneously) as:
– translation: P′ = P + T
– Scale: P′ = S · P
– Rotate: P′ = R · P
Composition is difficult to express, since
translation not expressed as a matrix
multiplication
Homogeneous coordinates allow all three to be
expressed homogeneously, using multiplication
by 3 × 3 matrices
W is 1 for affine transformations in graphics
11
12. Homogeneous Co-ordinates
P2d is a projection of Ph onto the w = 1 plane
So an infinite number of points correspond to :
they constitute the whole line (tx, ty, tw)
w
Ph(x,y,w)
w=1
P2d(x,y,1)
y
x
12
13. Classification of Transformations
1. Rigid-body Transformation
Preserves parallelism of lines
Preserves angle and length
e.g. any sequence of R(θ) and T(dx,dy)
2. Affine Transformation
Preserves parallelism of lines
Doesn’t preserve angle and length
e.g. any sequence of R(θ), S(sx,sy) and T(dx,dy)
unit cube
45 deg rotaton
Scale in X not in Y
13
14. Properties of rigid-body transformation
The following Matrix is Orthogonal if the upper left 2X2 matrix has the
following properties
1. A) Each row are unit vector.
sqrt(r11* r11 + r12* r12) = 1
B) Each column are unit vector.
sqrt(c11* c11 + c12* c12) = 1
2. A) Rows will be perpendicular to each other
(r11 , r12 ) . ( r21 , r22) = 0
r11
r
21
0
cosθ
sin θ
0
r12
r22
0
− sin θ
cos θ
0
tx
ty
1
0
0
1
B) Columns will be perpendicular to each other
(c11 , c12 ) . (c21 ,c22) = 0
e.g. Rotation matrix is orthogonal
• Orthogonal Transformation ⇒ Rigid-Body
Transformation
• For any orthogonal matrix B ⇒ B -1 = B T
14
15. Commutativity of Transformation Matrices
• In general matrix multiplication is not commutative
• For the following special cases commutativity holds i.e.
M1.M2 = M2.M1
M1
M2
Translate
Translate
Scale
Scale
Rotate
Rotate
Uniform Scale
Rotate
• Some non-commutative
Compositions:
Non-uniform scale, Rotate
Translate, Scale
Rotate, Translate
Original
Transitional
Final
15
16. Associativity of Matirx Multiplication
Create new affine transformations by multiplying sequences of
the above basic transformations.
q = CBAp
q = ( (CB) A) p = (C (B A))p = C (B (Ap) ) etc.
matrix multiplication is associative.
To transform just a point, better to do
q = C(B(Ap))
But to transform many points, best to do
M = CBA
then do
q = Mp
for any point p to be rendered.
For geometric pipeline transformation, define M and set it up
with the model-view matrix and apply it to any vertex
subsequently defined to its setting.
16
17. Rotation of θ about P(h,k): Rθ,P
Step 1: Translate P(h,k) to origin
Step 2: Rotate θ w.r.t to origin
Q3(x’+h, y’ +k)
Step 3: Translate (0,0) to P(h,k0)
R θ,P = T(h ,k) * R θ * T(-h ,-k)
P3(h,k)
Q(x,y)
P(h,k)
Q1(x’,y’)
P1 (0,0)
Q2(x’,y’)
P2 (0,0)
17
18. Scaling w.r.t. P(h,k): Ssx,sy,p
Step 1: Translate P(h,k) to origin
Step 2: Scale S(sx,sy) w.r.t origin
(7,2)
Step 3: Translate (0,0) to P(h,k)
S sx,sy,P = T(h ,k) * S(s x ,s y )* T(-h ,-k)
(4,3)
(1,1)
(1,1)
T(1,1)
(4,2)
(6,1)
(4,1)
S 3/2,1/2,(1,1)
(0,0)
(4,0)
T(-1,-1)
(7,1)
(0,0)
(6,0)
S(3/2,1/2)
18
19. Reflection about line L, ML
Y
Step 1: Translate (0,b) to origin
Step 2: Rotate -θ degrees
Step 3: Mirror reflect about X-axis
Step 4: Rotate θ degrees
(0,b)
t
O
X
Step 5: Translate origin to (0,b)
M L = T(0 ,b) * R(θ) * M x * R(-θ) * T(0 ,-b)
19
22. Orientation
Thumb points to +ve Z-axis
Fingers show +ve rotation from X to Y
axis
Y
Y
Z (larger z are
away from viewer)
X
X
Z (out of page)
Right-handed orentation
Left-handed orentation
22
23. Vectors in 3D
Have length and direction
V = [xv , yv , zv]
Length is given by the Euclidean Norm
||V|| = √( xv2 + yv2 + zv2 )
Dot Product
V • U = [xv, yv, zv]•[xu, yu, zu]
= xv*xu + yv*yu + zv*zu
= ||V|| || U|| cos ß
Cross Product
V×U
= [yv*zu - zv yu , -xv*zu + zv*xu , xv*yu – yv*xu ]
= η ||V|| || U|| sin ß
V × U = - ( U x V)
z
K
J+c
b
aI+
V=
(xv,yv,zv)
y
x
23
24. 3D Equation of Curve & Line
Parametric equations of Curve & Line
Curve
Line
x = f (t)
C : y = g(t )
z = h( t )
z
C
a≤t ≤b
y
x
V = P0 P1 = P − P0
1
x = x0 + ( x1 − x0 ) × t
t =1
L : y = y0 + ( y1 − y0 ) × t 0 ≤ t ≤ 1
z = z0 + ( z1 − z0 ) × t
L = P0 + t ( P − P0 ) = P0 + tV
1
t
t=
<0
0
1
t<
0< V
P0(x0,y0,z0)
t>
1
P1(x1,y1,z1)
24
25. 3D Equation of Surface & Plane
Parametric equations of Surface & Plane
Surface
x = f ( s, t )
S : y = g ( s, t )
z = h( s , t )
a≤s≤b
c≤t ≤d
Plane : with Normal, N
Ax + By + Cz + D = 0
ˆ
ˆ
N = Ai + Bˆ + Ck
j
N
P0
25
26. 3D Plane
Ways of defining a plane
1. 3 points P0, P1, P2 on the plane
2. Plane Normal N & P0 on plane
3. Plane Normal N & a vector V on the plane
N
Plane Passing through P0, P1, P2
ˆ
ˆ
N = P0 P × P0 P2 = Ai + Bˆ + Ck
j
1
P0
if P(x, y, z) is on the plane
N • P0 P = 0
[
P1
P2
V
]
ˆ
ˆ
ˆ
ˆ
⇒ ( Ai + Bˆ + Ck ) • ( x − x0 )i + ( y − y0 ) ˆ + ( z − z0 )k = 0
j
j
⇒ Ax + By + Cz + D = 0
where D = −( Ax0 + By0 + Cz0 )
26
27. Affine Transformation
Transformation – is a function that takes a point (or vector) and
maps that point (or vector) into another point (or vector).
A coordinate transformation of the form:
x’ = axx x + axy y + axz z + bx ,
y’ = ayx x + ayy y + ayz z + by ,
z’ = azx x + azy y + azz z + bz ,
x' a xx
y ' a yx
z' = a
zx
w 0
a xy
a xz
a yy
a zy
a yz
a zz
0
0
bx x
b y y
bz z
1 1
is called a 3D affine transformation.
The 4th row for affine transformation is always [0 0 0 1].
Properties of affine transformation:
– translation, scaling, shearing, rotation (or any combination of them) are
examples affine transformations.
– Lines and planes are preserved.
– parallelism of lines and planes are also preserved, but not angles and
length.
27
28. Translation – 3D
x′ = x + d x
y′ = y + d y
z′ = z + d z
1
0
0
0
0 0 dx x x + dx
1 0 d y y y + d y
* =
0 1 dz z z + dz
0 0 1 1 1
⇓
⇓
T (d x , d y , d z ) * P =
⇓
P′
28
29. Scaling – 3D
x′ = s x * x
y′ = s y * y
Original
scale Y axis
S (sx , s y , sz )
⇓
scale all axes
z′ = sz * z
sx
0
0
0
* P =
⇓
0
0
sy
0
0
sz
0
0
P′
⇓
0 x x * s x
0 y y * s y
* =
0 z z * s z
29
1 1 1
30. Rotation – 3D
For 3D-Rotation 2
parameters are needed
Rotation about z-axis:
Angle of rotation
Axis of rotation
Rθ ,k
P′
⇓
cosθ
sin θ
0
0
* P =
⇓
⇓
− sin θ
cos θ
0
0
0 0 x x * cos θ − y * sin θ
0 0 y x * sin θ + y * cosθ
* =
1 0 z
z
0 1 1
1
30
31. Rotation about Y-axis & X-axis
About yaxis
Rθ , j
* P =
P′
⇓
⇓
⇓
cos θ
0
− sin θ
0
About x-axis
0 sin θ
1
0
0 cosθ
0
0
0 x x * cosθ + z * sin θ
0 y
y
* =
0 z − x * sin θ + z * cosθ
1 1
1
* P =
P′
⇓
0
1
0 cos θ
0 sin θ
0
0
Rθ , i
⇓
⇓
0
− sin θ
cosθ
0
0 x
x
0 y y * cosθ − z * sin θ
* =
0 z y * sin θ + z * cosθ
1 1
1
31
32. Shear along Z-axis
y
x
z
SH xy ( shx , sh y ) * P =
⇓
1
0
0
0
⇓
0 shx
1 sh y
0 1
0
0
P′
⇓
0 x x + z * shx
y y + z * sh
0
y
*
=
0 z
z
1 1
1
32
33. Object Transformation
Line: Can be transformed by transforming
the end points
Plane:(described by 3-points) Can be
transformed by transforming the 3-points
Plane:(described by a point and Normal)
Point is transformed as usual. Special
treatment is needed for transforming
Normal
33
34. Composite Transformations – 3D
Some of the composite transformations to
be studied are:
AV,N = aligning a vector V with a vector N
Rθ,L = rotation about an axis L( V, P )
Ssx,sy,P= scaling w.r.t. point P
34
35. AV : aligning vector V with k
Step 1 : Rotate about x - axis by θ
b
sin θ =
λ λ = b2 + c2
c
cos θ =
λ
z
( 0, 0,
λ)
( 0, b,c
b
( a, 0,
λ)
)
cλ
θ
λ
|V|
θ
V = aI + bJ + cK
|V|
k
b
y
a
Av =
Rθ,i
35
x
36. AV : aligning vector V with k
Step 1 : Rotate about x - axis by θ
b
sin θ =
λ λ = b2 + c2
c
cos θ =
λ
z
( 0, 0,
|V|)
z
a
( 0, b,c
b
( a, 0,
λ)
−a
|V|
2
2
2
| V |= a + b + c
λ
cos(−φ ) =
| V |
λ
Step 2 : Rotate V about y - axis by - φ
)
c
|V|
|V|
P( a, b, c)
ϕ
sin( −φ ) =
Av = R-ϕ,j * Rθ,i
k
b
y
a
36
x
37. AV : aligning vector V with k
AV-1 = AVT
AV,N = AN-1 * AV
0
AV = a
V
0
λ
V
- ab
λV
c
λ
b
V
- ac
λV
−b
λ
c
V
0
0
0
0
0
1
37
38. Rθ,L : rotation about an axis L
Let the axis L be represented by vector V and
z
passing through point P
1. Translate P to the origin
2. Align V with vector k
3. Rotate θ° about k
4. Reverse step 2
5. Reverse step 1
x
L
P
Q
V
k
Rθ,L = T-P-1 * AV-1 * Rθ,k * AV * T-P
θ
Q'
y
38
39. MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
z
N
P
x
y
39
40. MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
1. Translate P to the origin
z
N
P
x
y
MN,P =
T-P
40
41. MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
1. Translate P to the origin
z
N
2. Align N with vector k
P
x
y
MN,P =
AN * T-P
41
42. MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
1. Translate P to the origin
2. Align N with vector k
3. Reflect w.r.t xy-plane
z
N
P
x
y
MN,P =
S1,1,-1 * AN * T-P
42
43. MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
1. Translate P to the origin
z
2. Align N with vector k
3. Reflect w.r.t xy-plane
x
4. Reverse step 2
MN,P =
AN-1 * S1,1,-1 * AN * T-P
y
43
44. MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
1. Translate P to the origin
2. Align N with vector k
3. Reflect w.r.t xy-plane
x
4. Reverse step 2
5. Reverse step 1
z
N
P
MN,P = T-P-1 * AN-1 * S1,1,-1 * AN * T-P
y
44
45. Further Composition
Translate points in fig. 1 into points in fig 2 such that:
– P3 is moved to yz plane
– P2 is on z-axis
– P1 is at Origin
The Composite Transform must have
– Translation of P1 to Origin ⇒ T
– Some Combination of Rotations ⇒ R
y
y
P3
P2
P
1
x
z
Fig. 1
y
P3′
T
R
P2′
P′
1
z
P3′′
x
z
P2′′
P′′
1
Fig. 2
x
45
46. Finding R
Let R be
r11 r12 r13 Rx .x Rx . y Rx .z
R = r21 r22 r23 = R y .x R y . y R y .z
r31 r32 r33 Rz .x Rz . y Rz .z
R is Rigid - body Transform
i) Rx , R y , Rz are unit vectors
ii) Rx , R y , Rz are perpendicu lar
to each other
Note : Rx .x ⇒ x component of vextor Rx
46
47. Finding Rz
R aligns P1′P2′ along z - axis
⇒ R⋅
P1′P2′
P1′P2′
P′
3
ˆ
=k
ˆ = P1′P2′
⇒ R ⋅k
P1′P2′
T
Rx . x
⇒ Rx . y
Rx .z
y
R y .x
Ry . y
R y .z
Rz
[ R
−1
=R
T
P′
1
]
x
z
R
Rz . x 0
P ′P ′
R z . y 0 = 1 2
′ ′
Rz .z 1 P1P2
Rz . x
R . y = P1′P2′ = R
⇒ z
z
Rz .z P1′P2′
P2′
y
′
P′
3
ˆ
k
z
′
P2′
′
P′
1
x
47
48. Finding Rx
R aligns P1′P3′ × P1′P2′ along x - axis
⇒ R⋅
P1′P3′ × P1′P2′
P1′P3′ × P1′P2′
ˆ
⇒ R ⋅i =
−1
Rx . x
⇒ Rx . y
Rx .z
P′
3
ˆ
=i
P′
1
P1′P3′ × P1′P2′
P2′
x
Rx
R
R z . x 1
P ′P ′ × P ′P ′
R z . y 0 = 1 3 1 2
′ ′
′ ′
Rz .z 0 P1P3 × P1P2
Rx . x
R . y = P1′P3′ × P1′P2′ = R
⇒ x
x
Rx .z P1′P3′ × P1′P2′
Rz
z
P1′P3′ × P1′P2′
R y .x
Ry . y
R y .z
y
y
′
P′
3
ˆ
k
z
′
P2′
′
P′
1
ˆ
i
x
48
49. Finding Ry
y
R aligns R z × R x along y - axis
(
)
P′
3
⇒ R ⋅ Rz × Rx = ˆ
j
P′
1
⇒ R ⋅ ˆ = Rz × Rx
j
R y .x
Ry . y
R y .z
Ry
x
R
Rz . x 0
Rz . y 1 = R z × R x
R z . z 0
R y .x
⇒ Ry . y = Rz × Rx = Ry
R y .z
P2′
Rx
z
−1
Rx . x
⇒ Rx . y
Rx .z
Rz
y
′
P′
3
ˆ
k
z
′
P2′
ˆ
j
′
P′
1
ˆ
i
x
49
52. Transformations in OpenGL
OpenGL uses 3 stacks to maintain transformation
matrices:
– Model & View transformation matrix stack
– Projection matrix stack
– Texture matrix stack
You can load, push and pop the stack
The top most matrix from each stack is applied to
all graphics primitive until it is changed
Graphics
Primitives
(P)
M
Model-View
Matrix Stack
N
Projection
Matrix Stack
Output
N•M•P
52
53. General Transformation Commands
Specify current matrix (stack) :
– void glMatrixMode(GLenum mode)
I
B
A
• Mode : GL_MODELVIEW, GL_PROJECTION,
GL_TEXTURE
glLoadMatrix(M)
glLo
a
dIde
ntity
C
B
A
M
B
A
Initialize current matrix.
– void glLoadIdentity(void)
• Sets the current matrix to 4X4 identity matirx
– void glLoadMatrix{f|d}(cost TYPE *M)
• Sets the current matrix to 4X4 matrix specified by M
Note: current matrix ⇒ Top most matrix of the current
matrix stack
53
54. General Transformation Commands
Concatenate Current Matrix:
– void glMultMatrix(const TYPE *M)
• Multiplies current matrix C, by M. i.e. C = C*M
– Caveat: OpenGL matrices are stored in
column major order.
m1
m
2
m3
m4
m5
m6
m7
m8
m9
m10
m11
m12
m13
m14
m15
m16
– Best use utility function glTranslate, glRotate,
glScale for common transformation tasks.
54
55. Transformations and OpenGL®
Each time an OpenGL transformation M is called
the current MODELVIEW matrix C is altered:
v′ = Cv
v′ = CMv
glTranslatef(1.5, 0.0, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
v′ = CTRv
Note: v is any vertex placed in rendering pipeline v’ is the transformed
vertex from v.
55
57. Thinking About Transformations
There is a World Coordinate System where:
All objects are defined
Transformations are in World Coordinate space
Two Different Views
As a Global System
Objects moves but
coordinates stay the same
Think of transformation
in reverse order as they
appear in code
As a Local System
Objects moves and
coordinates move with it
Think of transformation
in same order as they
appear in code
57
58. Order of Transformation T•R
Global View
Rotate Object
Then Translate
glLoadIdentity();
Local View
glMultiMatrixf( T);
Translate Object
glMultiMatrixf( R);
Then Rotate
draw_ the_ object( v);
v’ = ITRv
Effect is same, but perception is different
58
59. Order of Transformation R•T
Global View
Translate Object
Then Rotate
glLoadIdentity();
Local View
glMultiMatrixf( R);
Rotate Object
glMultiMatrixf( T);
Then Translate
draw_ the_ object( v);
v’ = ITRv
Effect is same, but perception is different
59
60. Hierarchical Modeling
Many graphical objects are structured
Exploit structure for
– Efficient rendering
– Concise specification of model parameters
– Physical realism
Often we need several instances of an object
– Wheels of a car
– Arms or legs of a figure
– Chess pieces
Encapsulate basic object in a function
Object instances are created in “standard” form
Apply transformations to different instances
Typical order: scaling, rotation, translation
60
61. OpenGL & Hierarchical Model
Some of the OpenGL functions helpful for
hierarchical modeling are:
– void glPushMatrix(void);
– void glPoipMatrix(void);
– void glGetFloatv(GL_MODELVIEW_MATRIX, *m);
s
glPu
C
B
A
rix
Mat
h
glP
ush
Ma
trix
C
C
B
A
B
A
C
B
A
glGetFloatv
m
C
61
62. Scene Graph
A scene graph is a hierarchical representation of a scene
We will use trees for representing hierarchical objects
such that:
– Nodes represent parts of an object
– Topology is maintained using parent-child relationship
– Edges represent transformations that applies to a part and all the
subparts connected to that part
Scene
typedef struct treenode {
GLfloat m[16]; // Transformation
Sun
Star X
void (*f) ( );
// Draw function
struct treenode *sibling;
Earth
Venus
Saturn
struct treenode *child;
} treenode;
Moon
Ring
62
63. Example - Torso
Initializing transformation matrix for node
treenode torso, head, ...;
/* in init function */
glLoadIdentity();
glRotatef(...);
glGetFloatv(GL_MODELVIEW_MATRIX, torso.m);
Initializing pointers
torso.f = drawTorso;
torso.sibling = NULL;
torso.child = &head;
63
64. Generic Traversal
To render the hierarchy:
– Traverse the scene graph depth-first
– Going down an edge:
• push the top matrix onto the stack
• apply the edge's transformation(s)
– At each node, render with the top matrix
– Going up an edge:
• pop the top matrix off the stack
64
65. Generic Traversal : Torso
Recursive definition
void traverse (treenode *root) {
if (root == NULL) return;
glPushMatrix();
glMultMatrixf(root->m);
root->f();
if (root->child != NULL) traverse(root->child);
glPopMatrix();
if (root->sibling != NULL) traverse(root->sibling);
}
C is really not the right language for this !!
65
68. Viewing Transformation in
OpenGL
To setup the modelview matrix, OpenGL provides the
following function:
gluLookAt( eyex, eyey, eyez,
centerx, centery, centerz,
upx, upy, upz )
up
(upx, upy, upz)
eye
(eyex, eyey, eyez)
z
y
center
(centerx, centery, centerz)
x
68
69. Implementation
We want to construct an Orthogonal Frame such that,
(1) its origin is the point eye
(2) its -z basis vector points towards the point center
(3) the up vector projects to the up direction (+ve y-axis)
Let C (for camera) denote this frame.
Clearly,
C.O = eye
v = normalize( center − eye )
C.ez = −v
C.ex = normalize( v × up )
C.e y = ( C.ez × C.ex )
C.e
y
up
(upx, upy, upz)
v
center
C.O
(eye)
C.e
C.e
x
69
z