# 12 - Clipping

Document Sample

```					Visibility culling – Clipping
The visibility problem

• What polygons are visible?
• There are few visible polygons.
– Avoid redundant processing
• Three classes of non visible objects
– Outside the volume (view frustum culling)
– Facing away (back-face culling)
– Occluded (occlusion culling)
Visibility culling
Visibility culling

View frustum
culling
Visibility culling

Occlusion
culling
View frustum
culling
Visibility culling

Occlusion
culling
View frustum
culling

Back-face
culling
Clipping
• 2D clipping
– Line / polygon clipping by the viewport
– 3D frustum culling
– Occlusion culling
– Lines are used for polygons
Convex
• Convex polygon – line between every two
points belong to the polygon.
• The intersection of two convex regions –
convex region (single).
• The intersection of convex and concave ?
• Clipping – intersection of a polygon with
rectangle.
Clipping endpoints
B
Ymax
A
Ymin
Xmin     Xmax

Ax >= Xmin, Ax <= Xmax
Ay >= Ymin, Ay <= Ymax
Clipping lines
• Both points inside – trivially accepted
• Brute force:
– Calculate the infinite line-edge intersection
– Check if the intersection is on the edge/line
Cohen-Sutherland algorithm
•   Trivial acceptance
•   Trivial rejection
•   Subdivision into two trivial parts
•   Especially effective in two common cases:
– Large viewports
– Small viewports
Cohen-Sutherland algorithm
• Region coding
• Assign code to endpoints
• Simple calculation
–   Sign for bit value
–   Zero for acceptance
–   And for rejection
–   Or for acceptance
bit         1 - out     0 - in

1         y  ymin     y  ymin
2         x  xmin     x  xmin
3         y  ymax     y  ymax
4         x  xmax     x  xmax
Cohen-Sutherland example
• Line AB                                            B
•   And(A,B) = 0, Or(A,B) ≠ 0
•   Test A with bottom edge                      E
•   Create AC (rejected) and CB
•   Code(C) = 0 → Choose B
•   Test B with the right/top edge           D
C

A
Cohen-Sutherland
C - S - Clip( P0  ( x 0 , y0 ), P1  ( x1 , y1 ), x min , x max , y min , y max )
C0  code ( P0 );                 C1  code ( P1 );
if ( ( C0 and C1 ) !  0 ) then return;
if ( ( C0 or C1 )  0 ) then draw( P0 , P1 );
else if (OutsideWindow( P0 ) ) then
begin
Edge  Window boundary of leftmost non - zero bit of C0 ;
P2  P0 , P1  Edge;
C - S - Clip( P1 , P2 , x min , x max , y min , y max );
end
else
Edge  Window boundary of leftmost non - zero bit of C1 ;
P2  P0 , P1  Edge;
C - S - Clip( P0 , P2 , x min , x max , y min , y max );
end
• “Random” edge choice
• Redundant edge-line cross calculations
Cyrus-Beck algorithm
• Put line in a parametric form
P(t) = P0 + (P1 – P0)t
• Calculate 4 line-edge intersections – only 1D
• Check if there is intersection

P1
C0
C1
P0
Cyrus-Beck algorithm
LiD
L(t1)PiD   PiD
P0
L(t3)PiD
L(t)

N iD                     P1

•   Denote L(t) = P0+(P1  P0)t, t[0,1] .
•   PiD is a point on the edge LiD with normal NiD.
•   For every vector V colinear with LiD, VNiD = 0.
•   Specifically, for V=L(t)  PiD,
0 = NiD(L(t)  PiD)
Cyrus-Beck algorithm
0 = NiD  (L(t)  PiD)
= NiD  (P0 + (P1  P0)t  PiD)
= NiD  (P0  PiD) + NiD  ((P1  P0)t)

Solving for t, where  = (P1  P0):

             N  P  P 
                      
N i  P0  Pi
D           D              D               D
t
i           0   i
                                
 N i  P  P0                Ni 
D                              D
1

Works especially fast when rectangle is upright
Cyrus-Beck algorithm
P1
PE                                           P1
•   If NiD = 0, L and LiD are parallel            PL
P1

•   The intersections of L and Li   D are computed
PL                            P
P0              PE                               PL L
•   If ti[0,1], there might be an intersection
P0
•   Based on the sign of NiD, each point is
P
classified as PE (potentially entering) or PL     PE E
(potentially leaving)                           P1
•   PE with the largest t and PL with the
               N  P  P 
                        
N i  P0  Pi
D             D              D               D
t
i           0   i
smallest t define the intersection                                                        
 N i  P  P0                  Ni 
D                                D
1
Cohen-Sutherland – polygons
• Create a list of vertices – {v1, …, vn}
• Clip against a single infinite edge
Cohen-Sutherland – polygons

Inside   Outside Inside       Outside Inside   Outside Inside       Outside
p                    s
i
s                                                      p
1st Output

2nd Output
s
i     p

p                 s
Output                   Output            No Output
Hierarchical clipping
• Build hierarchical scene representation
– The bounding box of root includes the children
• Test the root node
– If it is outside, stop and discard everything
– If it’s fully inside, render everything
– Otherwise, test recursively every child
Example

3                           1
1   2       4   5   6       2
3

5
6
4

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 5 posted: 10/21/2012 language: English pages: 23