3D Transformations

advertisement
Modeling
Transformations
2D Transformations
3D Transformations
OpenGL Transformation
1
2D-Transformations
Basic Transformations
Homogeneous coordinate system
Composition of transformations
2
Translation – 2D
Y
Y
(4,5)
(7,5)
(7,1)
Before Translation
x’ = x + dx
y’ = y + dy
 x
P 
 y
X
(10,1)
Translation by (3,-4)
d x 
 x 
P    T   
 y 
d y 
X
P  P  T
Homogeniou s Form
 x  1 0 d x   x 
 y    0 1 d  *  y 
y  
  
 1  0 0 1   1 
3
Scaling – 2D
Y
Y
(4,5)
Types of Scaling:
Differential ( sx != sy )
Uniform ( sx = sy )
(7,5)
Before Scaling
X
(2,5/4) (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 
*   

s y   y   y * s y 
0
sx
0
0  x 
0 *  y 
1  1 
4
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
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  sin  0  x 
 y    sin  cos 0 *  y 
  
  
 1   0
0
1  1 6
Mirror Reflection
Y
Y
(1,1)
(-1,1)
(1,1)
X
X
(1,-1)
Reflect ionabout X - axis
x  x y    y
Reflect ionabout Y - axis
x   x y   y
1 0 0 
M x  0  1 0
0 0 1
 1 0 0
M y   0 1 0
 0 0 1
7
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
8
Inverse 2D - Transformations
-1
(dx,dy)
T ranslait on : T
-1
(θ )
Rot at ion : R
Sclaing
: S
 R(-θ )
-1
(sx,sy)
MirrorRef : M
M
-1
x
-1
y
 T(-dx,-dy)
 S ( 1 sx, 1sy )
 Mx
 My
9
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
10
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
11
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
12
45 deg rotaton
Scale in X not in Y
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
B) Columns will be perpendicular to each other
(c11 , c12 ) . (c21 ,c22) = 0
e.g. Rotation matrix is orthogonal
 r11
r
 21
 0
cos
 sin 

 0
r12
r22
0
tx 
t y 
1 
 sin 
cos
0
• Orthogonal Transformation  Rigid-Body Transformation
• For any orthogonal matrix B  B-1 = BT
0
0
1
13
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
14
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.
15
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)
16
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)
Ssx,sy,P= T(h ,k) * S(sx,sy)* T(-h ,-k)
(4,3)
(1,1)
(1,1)
T(1,1)
(4,2)
(6,1)
(4,1)
S3/2,1/2,(1,1)
(0,0)
(4,0)
T(-1,-1)
(7,1)
(0,0)
(6,0)
S(3/2,1/2)
17
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)
ML = T(0 ,b) * R() * M x* R(-) * T(0 ,-b)
18
Problems to be solved:
Schaum’s outline series:
Problems:
 4.1
 4.2
 4.3, 4.4, 4.5
=> R,P
 4.6, 4.7, 4.8
=> Ssx,sy,,P
 4.9, 4.10, 4.11, 4.21 => ML
 4.12
=> Shearing
 Pg-281(1.24), Pg-320(5.19)
=> Circular view-port
19
3D Transformations
Basics of 3D geometry
Basic 3D Transformations
Composite Transformations
20
Orientation
Thumb points to +ve Z-axis
Fingers show +ve rotation from X to Y
axis
Y
Y
X
Z (larger z are
aw ay from view er)
X
Z (out of page)
Right-handed orentation
Left-handed orentation
21
Vectors in 3D
Have length and direction
V = [xv , yv , zv]
Length is given by the Euclidean Norm
||V|| = ( xv2 + yv2 + zv2 )
z
Dot Product
V • U = [xv, yv, zv]•[xu, yu, zu]
= xv*xu + yv*yu + zv*zu
= ||V|| || U|| cos ß
x
Cross Product
VU
= [yv*zu - zv yu , -xv*zu + zv*xu , xv*yu – yv*xu ]
=  ||V|| || U|| sin ß
V  U = - ( U x V)
(xv,yv,zv)
y
22
3D Equation of Curve & Line
 Parametric equations of Curve & Line
 Curve
 Line
x  f t 
C : y  g t  a  t  b
z  ht 
V  P0 P1  P1  P0
z
C
y
x
x  x0  x1  x0  t
L : y  y0   y1  y0  t 0  t  1
z  z0  z1  z0  t
L  P0  t ( P1  P0 )  P0  tV
V
P0(x0,y0,z0)
P1(x1,y1,z1)
23
3D Equation of Surface & Plane
 Parametric equations of Surface & Plane
 Surface
x  f s, t 
asb
S : y  g s, t 
ct d
z  hs, t 
 Plane : with Normal, N
N
Ax  By  Cz  D  0
N  Aiˆ  Bˆj  Ckˆ
P0
24
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 P1  P0 P2  Aiˆ  Bˆj  Ckˆ
P0
if P(x, y, z) is on the plane
N  P0 P  0
P1

P2
V

 ( Aiˆ  Bˆj  Ckˆ)  ( x  x0 )iˆ  ( y  y0 ) ˆj  ( z  z0 )kˆ  0
 Ax  By  Cz  D  0
where D  ( Ax0  By0  Cz 0 )
25
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 ,
is called a 3D affine transformation.
 x'   a xx
  
 y '   a yx
 z'    a
   zx
 w  0
  
a xy
a yy
a zy
0
a xz
a yz
a zz
0
bx  x 
 
b y  y 
bz  z 
 
1  1 
 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.
26
Translation – 3D
x  x  d x
y  y  d y
z  z  d z
0 dx   x  x  dx 
0 d y   y   y  d y 
*

1 dz   z   z  dz 
   

0 1  1   1 



T (d x , d y , d z ) * P 
P
1
0

0

0
0
1
0
0
27
Scaling – 3D
x  s x * x
y  s y * y
Original
scale Y axis
S (sx , s y , sz )

scale all axes
z  s z * z
sx
0

0

0
0
sy
0
0
* P 

0
0
sz
0
P

0  x   x * s x 
0  y   y * s y 
*

0  z   z * s z 
   

1  1   1 28
Rotation – 3D
For 3D-Rotation 2
parameters are needed
Rotation about z-axis:
 Angle of rotation
 Axis of rotation
cos
 sin 

 0

 0
R ,k
* P 
P



 sin 
cos
0
0
0
0
1
0
0  x   x * cos  y * sin  
0  y   x * sin   y * cos 
*


0  z  
z
   

1  1  
1

29
Rotation about Y-axis & X-axis
About y-axis
R , j
* P 
P



 cos
 0

 sin 

 0
About x-axis
0
1
0 cos

0 sin 

0
0
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

R , i
* P 
P



0
 sin 
cos
0
0  x  
x

0  y   y * cos  z * sin  
*

0  z   y * sin   z * cos 
   

1  1  
1

30
Shear along Z-axis
y
x
z
SH xy ( sh x , sh y ) * P 

1
0

0

0
0 sh x
1 sh y
0 1
0 0

P

0  x   x  z * sh x 





0  y   y  z * sh y 
*


0  z  
z
   

1  1  
1

31
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
32
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
33
AV : aligning vector V with k
Step 1 : Rotat eabout x - axis by 
b
sin   
λ
2
2
λ

b

c

c
cos  
λ
z
( 0, 0, )
b
( a, 0, )
( 0, b,c)
c


| V|

V = aI + bJ + cK
| V|
k
b
y
a
Av =
R,i
34
x
AV : aligning vector V with k
Step 1 : Rotat eabout x - axis by 
z
( 0, 0, |V|)
a 
| V | 
2
2
2
 | V | a  b  c
λ 
cos( ) 
| V | 
z
a
b
(( 0,
0, b,c)
b,c)

b
sin   
λ
2
2
λ

b

c

c
( a, 0,  )
cos  
λ
Step 2 : RotateV about y - axis by - 
c
|| V|
V|

P( a, b, c)
sin( ) 
Av = R-,j * R,i
k
b
y
a
35
x
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
36
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
P
Q
2. Align V with vector k
3. Rotate  about k
4. Reverse step 2
5. Reverse step 1
L
V

Q'
k
y
x
R,L = T-P-1 * AV-1 * R,k AV * T-P
37
MN,P : Mirror reflection
Let the plane be represented by plane normal N
and a point P in that plane
z
x
y
38
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
x
y
MN,P =
T-P
39
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
x
y
MN,P =
AN * T-P
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
2. Align N with vector k
3. Reflect w.r.t xy-plane
x
y
MN,P =
S1,1,-1 * AN * T-P
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
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
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
z
2. Align N with vector k
3. Reflect w.r.t xy-plane
x
4. Reverse step 2
5. Reverse step 1
MN,P = T-P-1 * AN-1 * S1,1,-1 * AN * T-P
y
43
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
P1
R
P2
P1
z
Fig. 1
P3
T
x
z
y
P3
P1
x
z
P2
Fig. 2
x
44
Finding R
Let R be
 r11
R  r21
 r31
r12
r22
r32
r13   Rx .x Rx . y
r23    R y .x R y . y
r33   Rz .x Rz . y
Rx . z 
R y .z 
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
45
Finding Rz
R aligns P1P2 along z - axis
 R
P1P2
P1P2
P3
 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
P1

x
z
R
R z . x  0 
P P 

Rz . y  0  1 2
 
Rz .z  1 P1P2
 Rz . x 
P1P2


  Rz . y  
 Rz
 
 Rz .z  P1P2
P2
y
P3
kˆ
z
P2
P1
x
46
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

y
P3
 iˆ
P1
P1P3  P1P2
x
z
R
Rz .x  1
P P   P P 

Rz . y  0  1 3 1 2
 
 
Rz .z  0 P1P3  P1P2
 Rx . x 
P1P3  P1P2


  Rx . y  
 Rx
 
 
 Rx .z  P1P3  P1P2
P2
Rx
P1P3  P1P2
R y .x
Ry . y
R y .z
Rz
y
P3
kˆ
z
P2
P1
iˆ
x
47
Finding Ry
y
R aligns R z  Rx along y - axis

P3

 R  Rz  Rx  ˆj
P1
 R  ˆj  R z  R x
 R x . x R y . x Rz . x   0 


  Rx . y Ry . y Rz . y  1  R z  R x
 Rx .z Ry .z Rz .z  0


 R y .x 


  Ry . y   Rz  Rx  Ry
 R y .z 


P2
Ry
x
Rx
z
1
Rz
R
y
P3
kˆ
z
P2
ˆj
P1
iˆ
x
48
Problems to be solved:
Schaum’s outline series:
Problems:
 6.1
 6.2, 6.5, 6.9, 6.10, 6.11, 6.12  Av
 6.3, 6.4
 R,L
 6.6, 6.7, 6.8
 MN,P
49
Transformations in OpenGL
OpenGL transformation commands
Transformation Order
Hierarchical Modeling
50
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
51
General Transformation Commands
 Specify current matrix (stack) :
– void glMatrixMode(GLenum mode)
C
B
A
glLoadMatrix(M)
I
B
A
• Mode : GL_MODELVIEW, GL_PROJECTION,
GL_TEXTURE
 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
M
B
A
Note: current matrix  Top most matrix of the current
matrix stack 52
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
m9
m6
m7
m10
m11
m8
m12
m13 
m14 
m15 

m16 
– Best use utility function glTranslate, glRotate, glScale
for common transformation tasks.
53
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.
54
Sample Instance Transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(...);
glRotatef(...);
glScalef(...);
gluCylinder(...);
55
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
56
Order of Transformation T•R
Global View
 Rotate Object
 Then Translate
glLoadIdentity();
Local View
glMultiMatrixf( T);
 Translate Object
glMultiMatrixf( R);
draw_ the_ object( v);  Then Rotate
v’ = ITRv
Effect is same, but perception is different
57
Order of Transformation R•T
Global View
 Translate Object
 Then Rotate
glLoadIdentity();
Local View
glMultiMatrixf( R);
 Rotate Object
glMultiMatrixf( T);
draw_ the_ object( v);  Then Translate
v’ = ITRv
Effect is same, but perception is different
58
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
59
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);
C
C
B
A
C
B
A
C
B
A
B
A
m
C
glGetFloatv
60
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
61
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;
62
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
63
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 !!
64
Viewing Transformation
65
Viewing Pipeline Revisited
Graphics
Primitives
Modeling
Transform
Po
Object
Coordinates
Pw
Viewing
Transform
Pe
World
Coordinate
s
Eye
Coordinate
s
yw
yo
po
pw
xo
xw
zo
zw
66
Viewing Transformation in
OpenGL
 To setup the modelview matrix, OpenGL provides the
following function:
gluLookAt( eyex, eyey, eyez,
centerx, centery, centerz,
upx, upy, upz )
y
center
(centerx, centery, centerz)
up
(upx, upy, upz)
eye
(eyex, eyey, eyez)
z
x
67
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  norm alizecenter  eye


C.ez  v

 
C.ex  norm alizev  up 



C.e y  C.ez  C.ex 
C.ey
up
(upx, upy, upz)
v
center
C.O
(eye)
C.e x
68
C.e z
Thank You
69
Download