Docstoc

3D Transformations

Document Sample
3D Transformations Powered By Docstoc
					    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)      (10,1)
                Before Translation      X         Translation by (3,-4)    X



x’ = x + dx      x                   x   d x 
y’ = y + dy
              P               P    T                 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 
                              
                                                                               3
Scaling – 2D
Y                                 Y
                                                                       Types of Scaling:

                                                                       Differential ( sx != sy )
                                                                       Uniform ( sx = sy )
           (4,5)      (7,5)

                                      (2,5/4) (7/2,5/4)
          Before Scaling      X            Scaling by (1/2, 1/4)   X


x  s x * x
y  s y * y
      S            * P               P                           Homogeniou s Form
                                                                 x   s x   0    0  x 
sx         0   x   x * sx                                     y    0   sx   0 *  y 
0             *   y*s                                                           
           sy   y       y                                    1 0
                                                                               0    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
                                                                 Homogenious Form
        R            *P                P
                                                                  x   cos  sin  0  x 
cos  sin    x   x * cos  y * sin                      y    sin  cos 0 *  y 
 sin  cos  *  y    x * sin   y * cos                                       
                                                           1  0
                                                                                0   1  1 6
                                                                                          
Mirror Reflection
   Y                                 Y

          (1,1)
                            (-1,1)        (1,1)




                    X                             X



           (1,-1)



Reflection about X - axis   Reflection about Y - axis
x  x y    y            x   x y   y
      1 0 0                       1 0 0
M x  0  1 0             M y   0 1 0
                                          
              
      0 0 1                      0 0 1
                                          
                                                      7
Shearing Transformation


       1 a 0                    1 0 0                    1 a 0
SH x  0 1 0
                         SH y  b 1 0
                                                 SH xy    b 1 0
                                                                   
       0 0 1 
                                0 0 1 
                                                           0 0 1 
                                                                   




                Sheared in X        Sheared in Y    Sheared in both X
    unit cube
                  direction           direction      and Y direction



                                                                        8
Inverse 2D - Transformations

  Translaiton : T   -1
                   (dx,dy)    T(-dx,-dy)
  Rotation   : R    -1
                   (θ )    R(-θ )
  Sclaing    : S    -1
                   (sx,sy)    S ( 1sx , 1sy )
  Mirror Ref : M     -1
                     x     Mx
              M     -1
                    y      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)




                                                                   12
    unit cube          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
                                                              r11    r12      tx 
1. A) Each row are unit vector.                              r       r22      ty 
                                                              21                 
        sqrt(r11* r11 + r12* r12) = 1                        0                1
                                                                      0          
   B) Each column are unit vector.
        sqrt(c11* c11 + c12* c12) = 1
                                                          cos       sin     0
                                                           sin     cos       0
2. A) Rows will be perpendicular to each other                                  
                                                           0
                                                                      0        1
                                                                                 
         (r11 , r12 ) . ( r21 , r22) = 0
   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 = BT
                                                                                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                            Original
    Translate, Scale                                 Transitional
                                                             Final
    Rotate, Translate                                               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)
                                          P3(h,k)
R,P= T(h ,k) * R* T(-h ,-k)


         Q(x,y)

                         Q1(x’,y’)        Q2(x’,y’)
     P(h,k)

                    P1 (0,0)         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)
                                            (1,1)          (7,1)
Ssx,sy,P= T(h ,k) * S(sx,sy)* T(-h ,-k)
                                                  T(1,1)


           (4,3)
                             (4,2)
                                                       (6,1)
(1,1)      (4,1)
                    (0,0)     (4,0)       (0,0)        (6,0)
S3/2,1/2,(1,1)        T(-1,-1)                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
                                      (0,b)

                                              t

Step 4: Rotate  degrees
                                          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         Z (larger z are
                                     away from viewer)



                   X                    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
                                                      (xv,yv,zv)
   V • U = [xv, yv, zv]•[xu, yu, zu]
             = xv*xu + yv*yu + zv*zu                      y
             = ||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)                                                 22
3D Equation of Curve & Line
 Parametric equations of Curve & Line
 Curve
             x  f t                        z


         C : y  g t  a  t  b
                                                        C


             z  ht 
 Line                                                      y

                                          x
   V  P0 P1  P  P0
                1

      x  x0   x1  x0  t
 L : y  y0   y1  y0  t 0  t  1
     z  z0   z1  z0  t                      V
                                                        P1(x1,y1,z1)

   L  P0  t ( P  P0 )  P0  tV
                 1                       P0(x0,y0,z0)            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ˆ  Ck
         ˆ    j    ˆ                   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
Plane Passing through P0, P1, P2             N

                    ˆ         ˆ
N  P0 P  P0 P2  Ai  Bˆ  Ck
                         j
        1
                                                                P0        P2
if P(x, y, z) is on the plane                                              V
                                                                     P1
   N  P0 P  0
               ˆ                                           
                                                            ˆ
 ( Ai  Bˆ  Ck )  ( x  x0 )i  ( y  y0 ) ˆ  ( z  z0 )k  0
     ˆ                         ˆ
          j                                   j
 Ax  By  Cz  D  0
                    where D  ( Ax0  By0  Cz0 )
                                                                               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 ,          x'   a xx    a xy   a xz   bx  x 
                                                                                
     y’ = ayx x + ayy y + ayz z + by ,          y '   a yx   a yy   a yz   b y  y 
                                                z'    a     a zy   a zz   bz  z 
     z’ = azx x + azy y + azz z + bz ,            zx                            
                                                w  0                       1  1 
                                                              0      0          
    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.                                                                       26
Translation – 3D
                     x  x  d x
                     y  y  d y
                     z  z  d z



            1   0       0 dx   x  x  dx 
            0   1       0 d y   y  y  d y 
                                *         
            0   0      1 dz   z  z  dz 
                                           
            0   0       0 1  1   1 
                                         
            T (d x , d y , d z ) * P      P
                                                    27
Scaling – 3D
                                                   x  s x * x
                                                   y  s y * y
                                                   z  sz * z
    Original              scale Y axis



                      S (sx , s y , sz )     * P         P
                                                        
                    sx      0       0     0  x   x * s x 
                    0      sy       0     0  y   y * s y 
   scale all axes
                                           *            
                    0       0       sz    0  z   z * s z 
                                                         
                                                             28
                    0       0       0     1  1   1 
Rotation – 3D
For 3D-Rotation 2                      Rotation about z-axis:
  parameters are needed
 Angle of rotation
 Axis of rotation



                  R ,k         * P               P
                                                 
       cos      sin    0 0  x   x * cos  y * sin  
        sin    cos      0 0  y   x * sin   y * cos 
                             *                         
        0          0      1 0  z             z          
                                                        
        0          0      0 1  1             1             29
 Rotation about Y-axis & X-axis
About y-axis        R , j          * P                P
                                                      
          cos     0 sin         0  x   x * cos  z * sin  
          0        1        0     0  y             y           
                                   *                          
          sin    0 cos         0  z   x * sin   z * cos 
                                                               
          0        0  0           1  1             1           

About x-axis        R , i           * P                P
                                                       
         1   0              0     0  x             x          
         0 cos          sin    0  y   y * cos  z * sin  
                                   *                         
         0 sin          cos     0  z   y * sin   z * cos 
                                                              
         0   0             0      1  1             1              30
Shear along Z-axis             y

                                       x

                                z


SH xy ( sh x , sh y ) * P     P
                             
1   0 sh x    0  x   x  z * sh x 
0   1 sh y       y   y  z * sh 
               0   
                *                  y

0   0 1       0  z        z       
                                  
0   0 0       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 : Rotate about x - axis by 
       b                                       z
sin                                     ( 0, 0, )

       λ  λ  b2  c2
         
       c                                                                   b            ( 0, b,c)
cos                            ( a, 0, )
       λ                                                c
                                                                  
                                                                            
                                               |V|                              V = aI + bJ + cK

                                                                     |V|

                                                     k
                                                                            b                        y
                                                     a



Av =                 R,i
                                                                                                     34
                                      x
AV : aligning vector V with k

Step 1 : Rotate about x - axis by                     z
                                                               ( 0, 0, |V|)

       b                                        z
sin   
       λ  λ  b2  c2
                                               a
       c                                                                b              ( 0, b,c)
                                                                                          0, b,c)
cos                             ( a, 0, )
       λ
Step 2 : Rotate V about y - axis by - 




                                                           
                                                       c
                                                |V|
                                                | V|                         P( a, b, c)
           a 
sin( ) 
           |V| 
                 | V | a 2  b 2  c 2               k

             λ                                                        b
cos( ) 
                                                                                                    y

           | V |
                
                                                       a



Av = R-,j * R,i
                                                                                                    35
                                      x
AV : aligning vector V with k
 AV-1 = AVT
 AV,N = AN-1 * AV


              λ
               V
                   - ab
                   λV
                          - ac
                          λV
                                 0
                    c    b      
            0                    0
     AV   a        λ      λ
          V         b      c
                                 0
                                 
                    V      V

          0         0     0     1
                                      36
R,L : rotation about an axis L

Let the axis L be represented by vector V and
passing through point P           z
                                                      L

1. Translate P to the origin                     P
                                         Q
2. Align V with vector k             V
                                             
                                                 Q'
3. Rotate  about k             k

                                                          y
4. Reverse step 2
5. Reverse step 1           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                                y




MN,P =          AN-1 * S1,1,-1 * AN * T-P            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                                y

5. Reverse step 1
MN,P = T-P-1 * AN-1 * S1,1,-1 * AN * T-P             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                                         y
                 P3                              P3

                          P2
                               T                       P2       R   P3
          P1                                                                    P1     x
                      x
                                           P1               x
z
                                   z
                                                                     z
                                                                         P2                44
        Fig. 1                                                                  Fig. 2
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

                                                        45
 Finding Rz
R aligns P1P2 along z - axis
                                                             y
         P1P2                                                              P3
 R               ˆ
                  k
         P1P2                                                         Rz         P2
                                                                  P
     ˆ  P1P2           R                 
                                                                                           x
                                1
 R k                               R
     T                                    T                        1
                                                      z
         P1P2
                                                                                   R
   Rx . x    R y .x   Rz . x  0 
                                     P P 
                       Rz . y   0   1 2
                                                                 y
  Rx . y    Ry . y                    
   Rx . z
             R y .z   Rz .z  1 P1P2
                               
                                                      P3
  Rz . x                                                       P 
  R . y   P1P2  R                                                                x
                                                                  1
 z                                                 ˆ
                                                      k
                        z

  Rz .z  P1P2
                                                z       P2                                 46
Finding Rx
R aligns P1P3  P1P2 along x - axis                               y
                                                                                       P3
       P1P3  P1P2     ˆ
 R                      i
       P1P3  P1P2                                                            Rz         P2
                                                                      P
                                                                       1


     ˆ
     1       P1P3  P1P2                                                     Rx                 x
 R i                                                        z
              P1P3  P1P2
                                                                                             R
   Rx . x   R y .x      Rz .x  1
                                       P P   P P 
                         R z . y  0   1 3 1 2
                                                                          y
  Rx . y   Ry . y                              
   Rx . z
            R y .z      Rz .z  0 P1P3  P1P2
                                  
                                                               P3
  Rx . x                                                                P 
  R . y   P1P3  P1P2  R                                                                 x
                                                                           1
 x                                                          ˆ
                                                               k
                                 x                                            ˆ
                                                                              i
  Rx .z  P1P3  P1P2
                                                         z       P2                                  47
Finding Ry
                                                             y
R aligns R z  R x along y - axis                                          P3
          
 R  Rz  Rx  ˆ
                j                                                      Rz
                                                             P                  P2
                                                              1
                                                                          Ry             x
                                                      z              Rx
 R  ˆ  Rz  Rx
     1
      j
   Rx . x    R y .x   Rz . x  0                                              R
                              
  Rx . y    Ry . y   Rz . y  1  R z  R x
                                  
                                                                 y

   Rx . z             R z . z  0 
             R y .z                                          ˆ
                                                                 j
   R y .x                                           P3
                                                               P 
                                                                  1                  x
  Ry . y   Rz  Rx  Ry                           ˆ
                                                      k              ˆ
                                                                     i
   R y .z                                               P2                               48
                                                z
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                                        Output
                  M              N
Primitives                                      N•M•P
   (P)
              Model-View      Projection                 51
              Matrix Stack   Matrix Stack
                          General Transformation Commands

                           Specify current matrix (stack) :
                             – void glMatrixMode(GLenum mode)
    C                           • Mode : GL_MODELVIEW, GL_PROJECTION,
    B
    A                             GL_TEXTURE
                           Initialize current matrix.
        glLoadMatrix(M)




                             – void glLoadIdentity(void)
                                • Sets the current matrix to 4X4 identity matirx

I
                             – void glLoadMatrix{f|d}(cost TYPE *M)
B                               • Sets the current matrix to 4X4 matrix specified by M
A       M
        B
        A

                          Note: current matrix  Top most matrix of the current
                                                                         matrix 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   m5   m9    m13 
              m     m6   m10   m14 
               2                   
               m3   m7   m11   m15 
                                   
              m4    m8   m12   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              As a Local System
 Objects moves but              Objects moves and
  coordinates stay the same       coordinates move with it
 Think of transformation        Think of transformation
  in reverse order as they        in same order as they
  appear in code                  appear in code

                                                             56
Order of Transformation T•R
                    glLoadIdentity();
    Global View     glMultiMatrixf( T);    Local View
  Rotate Object    glMultiMatrixf( R);     Translate Object
 Then Translate    draw_ the_ object( v);  Then Rotate
                    v’ = ITRv




       Effect is same, but perception is different          57
 Order of Transformation R•T
                      glLoadIdentity();
      Global View     glMultiMatrixf( R);    Local View
 Translate Object    glMultiMatrixf( T);     Rotate Object
    Then Rotate      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                               m
                 A
  C                            C                  C
  B                            B   glGetFloatv
  A                            A

                 B                                    60
                 A
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;
     struct treenode *child;           Earth     Venus   Saturn
} 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                    Modeling          Viewing
     Primitives        Po                    Pw                    Pe
                                 Transform        Transform
               Object                      World                      Eye
             Coordinates                 Coordinate                Coordinate
                                             s                         s



           yw


           yo
           po
                                                              pw

                  xo        xw
     zo
                                                                                66
zw
Viewing Transformation in
OpenGL
 To setup the modelview matrix, OpenGL provides the
   following function:

gluLookAt( eyex, eyey, eyez,
           centerx, centery, centerz,
                                               y
           upx, upy, upz )                                   center
                                                   (centerx, centery, centerz)
                                  up
                            (upx, upy, upz)




                                     eye
                              (eyex, eyey, eyez)                          x




                                                                              67
                                 z
 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.                                C.ey

Clearly,
                                                         up
  C.O  eye                                        (upx, upy, upz)
                                                                              v
  
  v  normalizecenter  eye                                                     center

          
  C.ez  v                                                                                C.ex
                                                                    C.O
  C.ex  normalizev  up                                            (eye)

                  
  C.e y  C.ez  C.ex                                                                     68
                                                      C.ez
Thank You




            69

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:10
posted:8/22/2011
language:English
pages:69