0% found this document useful (0 votes)
8 views48 pages

slides6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views48 pages

slides6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Computer Graphics 6 - View Transformation and

Clipping

Tom Thorne

Slides courtesy of Taku Komura


www.inf.ed.ac.uk/teaching/courses/cg
Overview

View transformation

I Homogeneous coordinates recap


I Parallel projection
I Persepctive projection
I Canonical view volume

Clipping

I Line and polygon clipping


Outline of procedure

1. Transform from world to camera coordinates


2. Project into view volume or screen coordinates
3. Clip geometry that falls outside of the view volume
View projection

I Homogeneous transformation
I Parallel projection
I Perspective projection
I Canonical view volume
Homogeneous transformations

v = Mproj Mc←g Mg←l vl


Homogeneous coordinates

I Allows us to represent translation, rotation and scaling by


matrix multiplication
I Coordinates (x , y , z, w ) and (cx , cy , cz, cw ) represent the
same point
I Return to Cartesian coordinates using
(x 0 , y 0 , z 0 ) = (x /w , y /w , z/w )
Camera coordinate system

I As used by OpenGL
Parallel (orthographic) projection

I Specified by a direction of projection


I No change in size of objects
Parallel projection

Project coordinates onto plane at z = 0


x0 = x, y0 = y, z0 = 0
 
1 0 0 0
0 1 0 0
Morth = 
 
0 0 0 0

0 0 0 1
    
1 0 0 0 x x
0 1 0 0 y  y 
  =  
    
0 0 0 0 z   0 

0 0 0 1 1 1
Perspective projection

I Far away objects appear smaller


I Specified by a center of projection and focal distance
Perspective projection
Perspective projection - simple case
Perspective projection

 0 
−d xz
  
x x
 y 0  −d y   y 

=
  z=
 
−d   −d   z 
 

1 1 −z/d

Represented as a matrix multiplication:


 
1 0 0 0
0 1 0 0
Mproj =
 
0 0 1 0

0 0 − d1 0
Perspective projection

    
1 0 0 0 x x  x 
0 1 0 0 y   y   −z/d
    
y 
Vp = Mproj Vc =    =   =  −z/d 

0 0 1 0  z   z 
0 0 − d1 0 1 − dz −d
Alternative formulation
Alternative formulation

 
1 0 0 0
0 1 0 0
Mproj =
 
0 0 0 0

−1
0 0 d 1
Exercise

Where will the two points be projected?

 
1 0 0 0
0 1 0 0
Mproj =
 
0 0 0 0

0 0 − d1 1
Problems

I After projection depth information is lost (this is needed for


hidden surface removal)
I Objects behind the camera are projected infront of the
camera!
3D view volume
Define a volume in which objects are visible

I For parallel projection, a box


I For perspective projection, a frustum
I Surfaces outside this volume are clipped or removed and not
drawn
Canonical view volume

It can be computationally expensive to check if a point is inside a


frustum

I Instead transform the frustum into a normalised canonical


view volume
I Uses the same ideas a perspective projection
I Makes clipping and hidden surface calculation much easier
Transforming the view frustum
The frustum is defined by a set of parameters, l, r , b, t, n, f :

l Left x coordinate of near plane


r Right x coordinate of near plane
b Bottom y coordinate of near plane
t Top y coordinate of near plane
n Minus z coordinate of near plane
f Minus z coordinate of far plane

With 0 < n < f .


Transforming the view frustum

We can transform the perspective canonical view volume to the


parallel view volume using:

If z ∈ [−n, −f ] and 0 < n < f then

 2n r +l 
r −l 0 r −l 0
 0 2n t+b
t−b t−b 0 
Mcan = 
 
− ff +n − f2fn

 0 0 −n −n

0 0 −1 0
Final steps

I After transformation to canonical view volume, divide by w to


produce 3D Cartesian coordinates
I Perform clipping in the canonical view volume:

−1 ≤ x ≤ 1,−1 ≤ y ≤ 1,−1 ≤ z ≤ 1
Example

What does ABC look like after projection?

Mcan =
 2n r +l 
r −l 0 r −l 0
 0 2n t+b
 t−b t−b 0 

− ff +n − f2fn
 
 0 0 −n −n

0 0 −1 0
Example
M
 can =
2n r +l 
r −l 0 r −l 0
 0 2n t+b
t−b t−b 0 
=
 
− ff +n
− f2fn

 0 0 −n −n

0 0 −1 0
 
1 0 0 0
0 1 0 0
 
0 0 −2 −3
 

0 0 −1 0
n = 1,f = 3,r = 1,l = −1,t =
1,b = −1

3
      
1 0 0 0 3 3 2
0 1 0 0   0  0  0 
     
  =   =  
 
0 0 −2 −3 −2 1  12 

0 0 −1 0 1 2 1
Projection summary

I Parallel and perspective projection


I Projection matrices transform points to 2D coordinates on the
screen
I Canonical view volumes can be used for clipping
Overview

View transformation

I Homogeneous coordinates recap


I Parallel projection
I Persepctive projection
I Canonical view volume

Clipping

I Line and polygon clipping


Projection of polygons and lines

I Lines in 3D become lines in 2D


I Polygons in 3D become polygons in 2D
Clipping

They may intersect the canonical view volume, then we need to


perform clipping:

I Clipping lines (Cohen-Sutherland algorithm)


I Clipping polygons (Sutherland-Hodgman algorithm)
Cohen-Sutherland algorithm

Input Screen and 2D line segment


Output Clipped 2D line segment
Cohen-Sutherland algorithm

Outcodes:

I Space is split into nine regions


I The center corresponds to the area visible on the screen
I Each region is encoded by four bits
Cohen-Sutherland algorithm

Outcodes:

1st bit Above top of screen (y > ymax )


2nd bit Below bottom of screen (y < ymin )
3rd bit Right of right edge of screen (x > xmax )
4th bit Left of left edge of screen (x < xmin )
Cohen-Sutherland algorithm

Data: Line from coordinate A to B


Result: Clipped line
while true do
Calculate outcodes of endpoints;
if trivial accept or trivial reject then
return accepted line or reject;
else
Find which endpoint is outside clipping rectangle;
Clip outside endpoint to edge of first non-zero outcode bit
and update endpoint coordinates;
end
end
Bitwise AND and OR

0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
Trivial accept
All line endpoints are inside the screen

I Apply an OR to outcodes of the line endpoints, test for equal


to 0
Cohen-Sutherland algorithm

Data: Line from coordinate A to B


Result: Clipped line
while true do
Calculate outcodes of endpoints;
if trivial accept or trivial reject then
return accepted line or reject;
else
Find which endpoint is outside clipping rectangle;
Clip outside endpoint to edge of first non-zero outcode bit
and update endpoint coordinates;
end
end
Trivial reject

Both line endpoints are outside of the screen on the same side

I Apply AND operation to the two endpoints and reject if not 0


Cohen-Sutherland algorithm
Cohen-Sutherland algorithm
Cohen-Sutherland algorithm

Data: Line from coordinate A to B


Result: Clipped line
while true do
Calculate outcodes of endpoints;
if trivial accept or trivial reject then
return accepted line or reject;
else
Find which endpoint is outside clipping rectangle;
Clip outside endpoint to edge of first non-zero outcode bit
and update endpoint coordinates;
end
end
Line intersection
I Need to clip a line to the edges of the rectangle
I Find an endpoint which is outside the rectangle
I Select edge to clip based on the outcode, split the line and
feedback into algorithm

A
A'
A''

B
Cohen-Sutherland algorithm

Extension to 3D

I Clip lines to near and far planes of view volume


I Needs more bits for the outcode
Polygon clipping

Sutherland-Hodgman algorithm

Input 2D polygon
Output List of clipped vertices

Traverse the polygon and clip at each edge of the screen


Sutherland-Hodgman algorithm

Traverse edges and divide into four types:


Sutherland-Hodgman algorithm

For each edge of the clipping rectangle:

I For each polygon edge between vi and vi+1


Example
Summary

Projection

I Parallel and perspective


I Canonical view volumes

Clipping

I Cohen-Sutherland algorithm
I Sutherland-Hodgman algorithm
References

View transformation

I Shirley, Chapter 7
I Foley, Chapter 6

Clipping

I Foley Chapter 3.12, 3.14


I http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/
Haowei.Hsieh/mm.html

You might also like