You are on page 1of 49

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  x 
T t x , t y         0 1 t y 
t
 y  t y  0 0 1 
 
1 0 0 t x 
   x 
x t 
T t x , t y , t z    y   t y   
    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
s y   y  
* sy
0  0 0 1
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
rotating about in 2D?
Rotations about the Z axis
What do you think the rotation matrix is for
rotations about the z axis?
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
Rotations about the X axis

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
Rotations about the Y axis

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.
 xh   6
 x  3 
 xh   h  4  6  2
 x   y h   4   3    14 
P      yh    y   Ex.   2, Ex.   14  7  
 y  h   h   2
1  7 
 2   2 
2
   h 
 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  camerax 
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 By , Az Bx  Ax Bz , Ax B y  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
 z w  1 0 0 y 
   p 
p

y' y p z  z' yp 
 w  p ;  y'  y p    z '  0 0 1 0  z 
  0  p 
z'  zp w  1
z' z p
  0 0  1 
 z '  z
z'
  w   z' 
z'  z' 
p
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 
 z p w   zp hz p 
z  z' yp   z' z' 
w  p ;  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   z '   
f  z' f  z'
p
 zp   
Perspective Divide

 z' z'   z' 


 x'  x p  xp   0 0 0 
 zp hz p   x '   hhoriztonal  xp 
 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 
y
 world _ coordinates   M  object _ coordinates 
 
 zworld
'
 Object W orld
 zobject _ coordinates 
 
_ coordinates
 
 1   1 
What happens to an object...

World Transformation - Modelview


World Coordinates World -> Eye/Camera

Viewport
Viewport Coordinates

 xview
'
_ coordinates   xworld
'
_ coordinates 
 '   ' 
y
 view _ coordinates   M y
 world _ coordinates 
 
 zview
'
 W orld View
 zworld
'

   
_ coordinates _ 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 
 '   ' 
y
 normalized_ screen _ coordinates   M y
 view _ coordinates 
View  Screen
 znormalized
'
  z '

   
_ screen _ coordinates view _ coordinates

 1   1 
M View Screen  M Parallel_ Projection
M View Screen  M Perspective _ Projection
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
Think about:
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
•Shading
•Blending (the elusive alpha)
•Textures

You might also like