Title: Part VI Line and Polygon Clipping
1Part VI Line and Polygon Clipping
2Culling Clipping
- So far, we do not care about the objects which
are outside or intersecting the boundary of the
view volume. - Polygons that are completely inside the view
volume are processed for display. - Polygons that are completely outside the view
volume are culled. - Polygons that intersect the boundary of the view
volume are clipped against the boundary, and then
processed for display. - View frustum culling is a very important topic
for real-time rendering, but wont be covered in
this class.
3Line Clipping
- First of all, lets consider line clipping
against a rectangle. - After clipping lines against a rectangle, we have
the line portions which are only inside the
rectangle. - Lines lying on the clipping rectangles border
are considered inside and hence are displayed. - Observe that clipping a line against a rectangle
results in a single line segment.
4Parametric Equations for Lines
- Parametric equation of a line connecting P0 and
P1 - Parametric equation through interpolation
- Why parametric equations?
- good for representing a line segment, not an
infinitely long line - good for representing a line with infinite slope
t1
example
t0
P(t) tP1 (1-t)P0 P0 (P1-P0)t
where 0t1
t0
t1
t
1-t
P0(x0,y0)
P1(x1,y1)
5Computing Intersection using Parametric Equations
- Intersection between two parametric equations
- Intersection of a line segment and an edge of the
clip rectangle
t1
x(t) x(s) y(t) y(s)
s0
P2(x2,y2)
P3(x3,y3)
Two unknowns and two equations! So, solvable!
s1
t0
x(t) 0 t(2-0) 2t y(t) -1
t(1-(-1)) 2t - 1
clip rectangle
y
(2,1)
y(t)2t-10 ? t 1/2
x
? (x(1/2),0)(1,0) intersection
(0,-1)
6 Brute-force Line Clipping Algorithm
- For a point p(x,y) to be inside a rectangle b, r, l, it should be guaranteed that l ? x ? r
and b ? y ? t. - For a line segment L connecting p0 and p1,
- ? If both p0 and p1 are inside the rectangle,
trivially accept L. - ? Otherwise, compute intersections with L and
(possibly) all 4 edges of the rectangle and then
get the clipped version.
?
?
7Cohen-Sutherland Clipping Algorithm
- The planar area is divided into 9 regions, and
4-bit code is assigned to each
region. -
-
- Each edge of the rectangle partitions the whole
2D plane into two half-planes. - Each bit of is for an edge of the
rectangle - 0 if the region is at the rectangle-side
half-plane - 1 otherwise.
- An end point of a line segment is assigned a code
of the region where it is.
t b r l
bit 1
top
left
right
bit 0
bottom
8Cohen-Sutherland Clipping Algorithm (contd)
- Case 1 If both p0 and p1 have 0000 codes,
trivially accept. - Case 2 If their bitwise-AND is not zero(0000),
trivially reject. -
-
-
- Case 3 Otherwise. See the next slide.
p0
p1
i.e. Both vertices are outside. e.g.
1001
t b r l
0101
9Cohen-Sutherland Clipping Algorithm (contd)
- Note that Case 3 can be reached only after
trivial-rejection fails. - Suppose we see a 1-bit on one end point. Then the
corresponding bit on the other end must be 0.
(Otherwise, they must have been caught at Case
2.) The line segment crosses the edge associated
with the bit. -
-
t b r l
The edge crosses top and left
Choose any vertex (which is outside of the
rectangle, i.e. which is not 0000) and scan the
code until 1 is met.
10Cohen-Sutherland Clipping Algorithm (contd)
Loop Test for trivially accepted/rejected. Find
an endpoint outside the rectangle. Scan the code
and find the edge that might be crossed. Compute
the intersection (as we did in the brute-force
algorithm). Clip off the line segment from the
outside endpoint to the intersection
point. Update the code of the new endpoint.
example2
example1
a
a
1000
0000
0000
1000
a
a
0010
a
b
0010
b
b
b
0000
0010
0010
trivially rejected
11Whos Ivan Sutherland?
ACM Association for Computing
Machinery SIGGRAPH Special Interest Group for
Computer Graphics
ACM Turing Award 1966 A. J. Perlis 1969
Marvin Minsky 1971 John McCarthy 1972 E.W.
Dijkstra 1975 Herbert A. Simon 1977 John
Backus 1980 C. Antony R. Hoare 1983 Ken Thompson
Dennis M. Ritchie 1984 Niklaus Wirth 1988
Ivan Sutherland 1998 James Gray 2001 Ole-Johan
Dahl Kristen Nygaard
ACM SIGGRAPH Steven A. Coons Award 1983 Ivan
Sutherland 1985 Pierre Bezier 1987 Donald
Greenberg 1989 David Evans 1991 Andries van
Dam 1993 Ed Catmull 1995 Jose Luis
Encarnacao 1997 James Foley 1999 James Blinn 2001
Lance Williams
Isnt that enough to explain who he is??
12 Polygon Clipping
If line clipping were used
Instead, we need
Pipeline of boundary clipping
bottom clipper
right clipper
top clipper
left clipper
13Sutherland-Hodgeman Algorithm
e.g. right clipping Input is v1 ? v2 ? v3 ?
v4 Output should be v11 ? v2 ? v3 ? v33
While moving from vi to vj, ? determine if we
have to save vj, and ? determine if we have to
add a new vertex. ? out ? in (v1 ? v2) add v11,
and save v2 ? in ? in (v2 ? v3) save v3, and add
none ? in ? out (v3 ? v4) save none, and add
v33 ? out ? out (v4 ? v1) save none, and add
none
add, and then save
save, and then add
14Sutherland-Hodgeman Algorithm - Example
top clipping
Input 1 ? 2 ? 3 ? 4
11 ? 2 ? 3 ? 4 ? 44
right clipping
2 ? 22 ? 33 ? 4 ? 44 ? 11
15A Problem in Sutherland-Hodgeman Algorithm
- A polygon is convex if, given any 2 points in its
interior, the line segment joining them is also
in the interior. - Concave polygons are not always correctly
clipped. Apply Sutherland-Hodgeman algorithm to
the example, see what happens. - Convex polygons are correctly clipped by
Sutherland-Hodgeman algorithm. Fortunately its
reasonable to restrict polygons to be clipped
only to convex ones. Well see.
163D Clipping against CVV
- Recall that we need polygon clipping against the
view volume. - Clipping is more efficiently performed against
the CVV than against the general view volume,
especially in the case of hardware
implementation. - Also note that both parallel projection and
perspective projection go through CVV. - First of all, lets tackle 3D line clipping
against CVV.
173D Clipping against CVV (contd)
- 3D line clipping against CVV can be done by
extending 2D Cohen-Sutherland clipping algorithm. - 3D space is divided into 2 halfspaces by a plane.
- CVV consists of 6 planes, and so 6-bit code b, r, l, n, f (instead of 4-bit code used in
2D) is needed. -
-
y
-1
-z
same as 2D clipping
1
x-1,1 y-1,1 z-1,1
-1
1
t b r l n f
x
-x
y1
y
x
x1
z1
z
-1
z
1
-y
183D Clipping against CVV (contd)
- 3D line clipping algorithm is basically the same
as 2D algorithm. - If 000000, trivially accepted.
- If bitwise-AND is not 000000, trivially rejected.
- Otherwise,
- Similarly, we can easily devise the 3D version of
Sutherland-Hodgeman algorithm for polygon
clipping against the CVV.
p1
- Compute t through xx0t(x1-x0)1. - Compute y
and z using t. - Keep going .
CVV
p0
x1 plane
19Clipping before Perspective Division
- Recall that perspective transformation
Mpers(MdistortSpers H?xy(p,q)) requires
perspective division x?x/w,y?y/w, and z?z/w. - Perspective division is an expensive operation.
(Its a division!!) - In general, a lot of triangles are
culled/clipped, and so its inefficient to do
perspective division against polygons to be
clipped. - So, lets do clipping before perspective
division. We can then avoid expensive perspective
division for the clipped polygons.
y
Q(0 3/2 3/2)
Q?(0 1 -1/3)
P?(0 1 1)
3/2 1 1/2
P(0 1 -1)
-z
-2
R(0 1/2 3/2)
R?(0 1/3 -1/3)
20Clipping before Perspective Division (contd)
- Note that, after perspective division, CVV has
the following range. - -1? x?(x/w)?1, -1? y?(y/w)?1, and
-1?z?(z/w)?1. - We can rewrite the above as follows
- If w0, -w?x?w, -w?y?w, and -w?z?w.
- If w
- Mdistort transforms (x y z 1) into (-x/z -y/z
AB/z 1) (x y -Az-B -z) and w-z is always a
positive value. So, we can take only the first
case. - After perspective projection and before
perspective division, its said that we are in
clip coordinates. Now, do clipping. - After clipping in the clip coordinates,
perspective division is done such that all
polygons are in a 2x2x2 cube centered at the
origin. The clipped models are now really in NDC.
t b r l n f
t b r l n f
y1
yw
y
x1
x
z1
z
y
xw
x
zw
z
21Perspective Projection Summary (Revisited)
- The modeling transformation moves from LC to WC.
- Viewing specifies the location/orientation of the
viewer or camera, and viewing transformation
moves from WC to EC. - The two transformations are combined into
Mmodelview. - The frustum is defined by l, r, b, t, n, f.
- The perspective projection transformation Mpers
transforms the perspective view volume into
(parallel) CVV. - Do clipping and perspective division.
- Finally, the viewport transformation Mviewport
transforms vertices in CVV into 3D points in WdC.
projection transformation
modeling viewing transformation
in EC
in LC
perspective division
viewport transformation
in WdC
in NDC