# COMP136 Introduction to Computer Graphics

Document Sample

```					       CAP4730: Computational
Structures in Computer Graphics

3D Transformations
Outline

•3D World
•What we are trying to do
•Translate
•Scale
•Rotate
Transformations in 3D!

• Remembering 2D transformations -> 3x3
matrices, take a wild guess what happens to
3D transformations.                 T=(t , t , t )   x   y   z

1 0 t x 
 x     tx  
T t x , t y         0 1 t y       
 y  t y  0 0 1 
        
1 0 0 t x 
 x  t x              
T t x , t y , t z      y 
 y   t   0 1 0 t y 
0 0 1 t z 
 z  t z  
    0 0 0 1
          
Scale, 3D Style
sx       0   0
sx     0   x                                   S=(sx, sy, sz)
S s x , s y                    0            0
sy   y 
*              sy    
0            0             1
         0    
sx    0    0    0
sx        0   x 
0
0
S s x , s y , s z    0        0  *  y  
0    sy   0     
     sy         0         0    sz   0
0
     0    sz   z  
                         
0     0    0    1
Rotations, in 3D no less!
What does a rotation in 3D mean?   R=(rx, ry, rz, )
Q: How do we specify a rotation?

A: We give a vector to
rotate about, and a theta            
that describes how much
we rotate.

Q: Since 2D is sort of like a
special case of 3D, what is
the vector we’ve been
What do you think the rotation matrix is for
R=(0,0,1,)
cos     sin    0
cos   sin   
R                    sin      cos     0
 sin  cos   
  0

             0    1

cos   sin  0     0
 sin  cos  0      0
R (0,0,1, )                       

 0       0     1    0
                     
 0       0     0    1

Let’s look at the other axis rotations

R=(1,0,0,)


1   0        0      0
0 cos     sin    0
R(1,0,0, )                       
0 sin    cos      0
                     
0   0       0       1

R=(0,1,0,)

 cos     0 sin    0
 0        1   0     0
R(0,1,0, )                       
 sin    0 cos    0
                     
 0        0   0     1
Rotations for an arbitrary axis
1     0         0        0
0 cos        sin      0
R(1,0,0, )                            
0 sin       cos        0
                          
0     0            0     1
 cos  0       sin      0
 0      1        0       0
R(0,1,0, )                            
 sin  0      cos      0
                          
 0      0          0     1
cos      sin        0 0
 sin    cos          0 0
R(0,0,1, )                            
 0          0          1 0
                          
 0         0           0 1
Rotations for an arbitrary axis

R   Rx 1    Ry 1    Rz    Ry    Rx  
           

u

Steps:

                                 1. Normalize vector u
2. Compute 
                                     3. Compute 
4. Create rotation matrix
Vector Normalization

• Given a vector v, we want to create a unit
vector that has a magnitude of 1 and has the
same direction as v. Let’s do an example.
V
Normalized _ V 
V
Computing the Rotation Matrix

•   1. Normalize u                u  (a, b, c)
•   2. Compute Rx()              d  b2  c2
•   3. Compute Ry()              u z  (0,0,1)
u 'u z   c
•   4. Generate Rotation Matrix   cos           
u'  uz d
b
sin  
d
cos   d
sin   a
Rotation Matrix

R   Rx1    R y 1    Rz    R y    Rx  


1    0     0  0
     c     b  
0             0
Rx          d
b
d
c  
0             0
     d     d   
0
     0     0  1

d     0    a 0
0    1     0 0
R y                   
a    0     d 0
               
0     0    0 1
Applying 3D Transformations

P’=TRTP
Let’s compute M

P'  T (t x , t y , t z ) R( )T (t x ,t y ,t z ) P
P'  MP
 x'  a         b     c     d  x
 y '  e        f     g     h  y
                             
 z'  i         j     k     l  z 
                              
 1  m          n     o     p 1 
Homogenous Coordinates

• We need to do something to the vertices
• By increasing the dimensionality of the
problem we can transform the addition
component of Translation into
multiplication.
    x                                    6
 x h                                3 
 xh        h
  4    4          6         2

 x              yh          2, Ex.3  14  7  14 
P      yh    y   Ex.         7   
 y               h   2              2           2
h  h 
                        1 
               2 
 h 
                                     2 
       
Homogenous Coordinates
• Homogenous Coordinates – embed 3D transforms
into 4D
• All transformations can be expressed as matrix
multiplications.
• Inverses and combination easier
• Equivalence of vectors (4 2 1 1)=(8 4 2 2)
• What this means programatically
The Question

• Given a 3D point, an eye position, a camera
position, and a display plane, what is the
resulting pixel position?
• Now extend this for a group of three points
• Then apply what you know about scan
conversion.
Different Phases:
Model Definition
Different Phases:
Transformations
Different Phases:
Projection
Different Phases:
Projection
Different Phases:
Rasterization
Different Phases:
Scan Conversion
What are the steps needed?
Let’s Examine the Camera

• If I gave you a world, and said I want to
“render” it from another viewpoint, what
information do I have to give you?
–   Position
–   Which way we are looking
–   Which way is “up”
–   Aspect Ratio
–   Field of View
–   Near and Far
Camera
View Right
View Up

View Normal
View Direction
Camera
View Up

ViewRight
AspectRatio 
ViewUp

View Right

What are the vectors?
Graphics Pipeline So Far
Object                     Transformation
Object Coordinates         Object -> World

World                      Projection Xform
World Coordinates          World -> Projection

Camera                     Normalize Xform & Clipping
Projection Coordinates     Projection -> Normalized

Viewport                   Viewport Transform
Normalized Coordinates     Normalized -> Device

Screen
Device Coordinates
Transformation World->Camera
View Right
View Up

View Normal
View Direction
Transformation World->Camera
View Right = u
1      0 0  camera x 
0      1 0  camera y 
View Up = V
T                       
0      0 1  cameraz 
                      
0      0 0     1      
N
n         (n1 , n2 , n3 )
N
V N
u         (u1 , u 2 , u3 )
V N
v  n  u  (v1 , v2 , v3 )    View Direction = -N
u1    u2     u3    0
v     v2     v3    0
R 1                     
n1    n2     n3    0
                    
0      0      0    1
M W CVC    R T
Cross Products

Given two vectors, the cross product returns a vector that is
perpendicular to the plane of the two vectors and with
magnitude equal to the area of the parallelogram formed by
the two vectors.

A  B  u A B sin 
A  B  ( Ay Bz  Az B y , Az Bx  Ax Bz , Ax By  Ay Bx )   u
ux     uy    ux 
                                                       
A  B   Ax    Ay    Az 
 Bx    By    Bz 
                
Parallel Projections (known aliases):
Orthographic or Isometric Projection
VP'  PParallelMV
1   0 0 0
0   1 0 0
PParallel(Orthographic)            
0   0 0 0
         
0   0 0 1
Parallel Projection
Parallel Projections (known aliases):
Oblique Projection


VP'  PParallelMVVC                               L

L1  tan 1 
1    0 L1 cos  0
                  
0   1 L1 sin  0
PParallel( Oblique)   
0    0     0     0
                  
0    0     0     1
Projections

foreshortening - the
farther an object is from
the camera , the smaller it
appears in the final image
Perspective Projection Side View
P=(xp,yp,zp) t=0                            P’=(x’,y’,z’) t=?

C=(xc,yc,zc) t=1
xp
x’
z’
zp

x' x p
                                   z'      xp          x '  1   0   0    0  x 
 x'  x p            
 y '  0              
p
z' z p
          zp       w                       1   0    0 y
y' y p                 zp             z'      yp                             p 
                 w   ;  y'  y p                     z '  0   0   1    0  z 
  0                  p 
z'            zp       w                           1
z' z p
                                           0        0  1 
     z'  z p
z'
         w           z'    
z'  z'                                  zp      
Perspective Projection Side View
P=(xp,yp,zp) t=0                      P’=(x’,y’,z’) t=?

C=(xc,yc,zc) t=1
xp                                          h
x’
z’
zp

           z'      xp                                    z'        z'    
 x'  x p                                     x'  x p      xp         
          zp       w                                    zp       hz p    
zp             z'      yp                                    z'        z'    
w   ;  y'  y p                            w  z p  y'  y p      yp         
z'            zp       w     Scale by h                      zp       hz p   
              z'                                       fz p     z' f     
     z'  z p                                  z'                      
              zp                                    f  z'     f  z'    
Perspective Divide

           z'        z'                 z'                                                  
 x'  x p      xp                                       0            0              0      
          zp       hz p         x '   hhoriztonal                                          x p 
           z'        z'         y '                    z'                                  y p 
w  z p  y'  y p      yp               0                 hvertical
0             0
 
           zp       hz p        z'                                                          z 
   0                                 far       near * far   p 
         fz p                   w
0                                  
 z'          
z' f
                                       far  near    far  near   1 
      f  z'     f  z'                 0
                  0             1            0       


Foreshortening - look at the x,y, and w values, and how they depend on how far
away the object is.
Modelview Matrix - describes how to move the world->camera coordinate
system
Perspective Matrix - describes the camera you are viewing the world with.
Let’s closely examine
 z'                                                 
h                0            0             0       
 horiztonal                                         
 0               z'                                 
0             0
PPerspective _ Matrix                hvertical                             
                              far       near * far 
 0               0                                  
                          far  near    far  near 
 0
                 0             1            0       

z'               2 * z'

hhoriztonal xmax  xmin
z'            2 * z'

hvertical     ymax  ymin
What the Perspective Matrix
means
Note: Normalized Device Coordinates are a
LEFT-HANDED Coordinate system
Graphics Pipeline So Far
Object                     Transformation
Object Coordinates         Object -> World

World                      Projection Xform
World Coordinates          World -> Projection

Camera                     Normalize Xform & Clipping
Projection Coordinates     Projection -> Normalized

Viewport                   Viewport Transform
Normalized Coordinates     Normalized -> Device

Screen
Device Coordinates
What happens to an object...

Object                                                          Transformation
Object Coordinates                                              Object -> World

World
World Coordinates

 xworld _ coordinates 
'
 xobject _ coordinates 
 '                                         y                      
 yworld _ coordinates 
 M Object W orld  object _ coordinates 
 zworld _ coordinates 
'
 zobject _ coordinates 
                                                                  
          1                                           1           
What happens to an object...

World                                        Transformation - Modelview
World Coordinates                            World -> Eye/Camera

Viewport
Viewport Coordinates

 xview _ coordinates 
'
 xworld _ coordinates 
'

 '                                      '                    
 yview _ coordinates                     yworld _ coordinates 
 M W orldView  '
 zview _ coordinates 
'
 zworld _ coordinates 
                                                             
          1                                      1           
M W orldView  M Modelview
What happens to an object...
Transformation - Projection
Viewport
(Includes Perspective Divide)
Viewport Coordinates
Eye/Camera ->View Plane

Rasterization
Scan Converting Triangles

 xnormalized_ screen _ coordinates 
'
 xview _ coordinates 
'

 '                                                     '                   
 ynormalized_ screen _ coordinates                      yview _ coordinates 
 M View Screen  '
 znormalized_ screen _ coordinates 
'
 zview _ coordinates 
                                                                           
               1                                                1          
M View Screen  M Parallel_ Projection
M View Screen  M Perspective _ Pr ojection
Normalized Screen Coordinates
Normalized Screen Coordinates
Let’s label all the vectors

znsc=0    znsc=1
View Volume (View Frustum)
Usually:                            Far Plane
View Plane = Near Plane                               -1,1,1

Mperspective_Matrix
1,-1,-1
Clipping 3D Triangles
View Frustum Culling
Comparison with a camera
Let’s verbalize what’s going on

Review:
•Pipeline
•Series of steps
•What we’ll do next:
•Hidden Surface Removal
•Depth Buffers
•Lighting