0% found this document useful (0 votes)
10 views

Lecture13d-2D_ViewingVisibleSurfaceDetection-Extra

The document discusses visible surface detection methods, focusing on clipping algorithms used to eliminate portions of a scene outside a specified region for display. It outlines the graphics pipeline steps, including modeling, geometry processing, rasterization, and fragment processing, emphasizing the importance of efficient clipping to optimize performance. Various clipping techniques for different primitives such as points, lines, and polygons are also introduced, with a detailed example of line clipping using the Cohen-Sutherland algorithm.

Uploaded by

Iqra Suhana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture13d-2D_ViewingVisibleSurfaceDetection-Extra

The document discusses visible surface detection methods, focusing on clipping algorithms used to eliminate portions of a scene outside a specified region for display. It outlines the graphics pipeline steps, including modeling, geometry processing, rasterization, and fragment processing, emphasizing the importance of efficient clipping to optimize performance. Various clipping techniques for different primitives such as points, lines, and polygons are also introduced, with a detailed example of line clipping using the Cohen-Sutherland algorithm.

Uploaded by

Iqra Suhana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

7 Visible surface detection methods

Chapter 7

Visible surface detection methods

Department of Computer Science and Engineering 7-1


7 Visible surface detection methods

7.1 Overview
Generally, any procedure that eliminates those portions
of a picture that are either inside or outside of a specified
region of space is referred to as a clipping algorithm or
simply clipping. Usually a clipping region is a rectangle,
although we could use any shape for a clipping
application.
The most common application of clipping is in the viewing
pipeline, where clipping is applied to extract a designated
portion of a scene (either two-dimensional or three-
dimensional) for display on an output device. Clipping
methods are also used to anti-alias object boundaries, to
construct objects using solid-modeling methods, to
manage multi-window environments, etc.

Department of Computer Science and Engineering 7-2


7 Visible surface detection methods

7.1 Overview
Clipping algorithms are applied in two-dimensional
viewing procedures to identify those parts of a picture that
are within the clipping window (i.e. viewport). Everything
outside the clipping window is then eliminated from the
scene description that is transferred to the output device
for display. An efficient implementation of clipping in the
viewing pipeline is to apply the algorithms to the
normalized boundaries of the clipping window. This
reduces calculations, because all geometric and viewing
transformation matrices can be concatenated and applied
to a scene description before clipping is carried out. The
clipped scene can then be transferred to screen
coordinates for final processing.

Department of Computer Science and Engineering 7-3


7 Visible surface detection methods

7.1 Overview
Pipelines
Graphics hardware uses a pipelined approach to process
vertices and convert primitives into the final image. The
pipeline basically involves the following steps:
• Modeling
• Geometry Processing
• Rasterization
• Fragment Processing

Department of Computer Science and Engineering 7-4


7 Visible surface detection methods

7.1 Overview
Modeling
The conversion of analog (real world) objects into
discrete data
i.e. creating vertices and connectivity via range scanning
The design of a complex structure from simpler primitives
i.e. architecture and engineering designs
Done Offline
We will ignore this step for now

Department of Computer Science and Engineering 7-5


7 Visible surface detection methods

7.1 Overview
Application programmer pipes modeling output into…
Geometry Processing
– Animate objects
– Move objects into camera space
– Project objects into device coordinates
– Clip objects external to viewing window

Department of Computer Science and Engineering 7-6


7 Visible surface detection methods

7.1 Overview
Rasterization
– Conversion of geometry in device coordinates into
fragments (or pixels) in screen coordinates
– After this step there is no notion of a “polygon”, just
fragments

Department of Computer Science and Engineering 7-7


7 Visible surface detection methods

7.1 Overview
Fragment Processing
– Texture lookups
– Coloring
– Programmable GPU steps

Department of Computer Science and Engineering 7-8


7 Visible surface detection methods

7.1 Overview
These last 3 steps need to be FAST
• Developed 20-40 years ago… but little has changed
• Efficient memory use speeds things up
– Cache, cache, cache
• Integers and bit ops over floating point
• Fewer bits usually faster
– float over double, half over float
• Parallel processing

Department of Computer Science and Engineering 7-9


7 Visible surface detection methods

7.1 Overview
Rasterization is very expensive
– More or less linear w/ number of fragments created
– Consists of adds, rounding and logic branches per pixel
– Only rasterize objects that are in viewable region
A few operations now needed to remove invisible onjects
saves many later.

Department of Computer Science and Engineering 7-10


7 Visible surface detection methods

7.1 Overview
Geometry Processing
• Apply modelview and projection matrix.
• Not all primitives map to inside window
– Cull those that are completely outside
– Clip those that are partially inside
• 2D vs. 3D
– Projection plane v. projection cube
– Clipping can occur in either space
– Choice of visible surface algorithm used forces one or the
other

Department of Computer Science and Engineering 7-11


7 Visible surface detection methods

7.1 Overview
Clipping algorithms are available for basic primitives used
in computer graphics, such as
– Point clipping
– Line clipping (straight-line segments)
– Fill-area clipping (polygons)
– Curve clipping
– Text clipping
In the following, we will assume that the clipping region is
a rectangular window with boundary edges at xmin, xmax,
ymin, and ymax.

Department of Computer Science and Engineering 7-12


7 Visible surface detection methods

7.2 Point clipping


Since the projection of a point P results in coordinates (x,
y), we can easily identify the necessary equations for a
clipping algorithm:
xmin < x < xmax, ymin < y < ymax
Point clipping can be useful for particle systems, such as
smoke simulation or cloud modeling.

Department of Computer Science and Engineering 7-13


7 Visible surface detection methods

7.3 Line clipping


Line clipping against rectangles

(x1, y1)

ymax
(x0, y0)

ymin
xmin xmax

The problem: Given a set of 2D lines or polygons and a window, clip the lines or
polygons to their regions that are inside the window.

Department of Computer Science and Engineering 7-14


7 Visible surface detection methods

7.3 Line clipping


Direct approach
(x1, y1)
Clip a line against 1 edge of
a square
Similar Triangles A
(x’, y’)
– A/B = C/D C
– Which do we know?
– B = (y1 – y2) B
– D = (x1 – x2)
– A = (y1 – ymax)
D
– C = AD/B
(x2, y2)
– (x’, y’) = (x1+C, ymax)

Department of Computer Science and Engineering 7-15


7 Visible surface detection methods

7.3 Line clipping


• Similarly handled for the other cases
• Extends easily to 3D
• EXPENSIVE! (below for 2D)
– 4 floating point additions/subtractions
– 2 floating point multiplications
– 1 floating point div
– 4 times (for each edge!)
• We need to save ourselves some operations

Department of Computer Science and Engineering 7-16


7 Visible surface detection methods

7.3 Line clipping


Possible Configurations
• Both endpoints are inside the J
D
region (line AB)
– No clipping necessary I
• One endpoint in, one out (line F A
C
CD) H
– Clip at intersection point B

• Both endpoints outside the


region: E
G
 No intersection (lines EF, GH)
 Line intersects the region (line IJ)
• Clip line at both intersection points

Department of Computer Science and Engineering 7-17


7 Visible surface detection methods

7.3 Line clipping


Cohen-Sutherland
Basic algorithm: J
D
 Accept (and draw) lines that
have both endpoints inside
the region I Clip and
F A retest
 Reject (and don’t draw) lines C
that have both endpoints less H
than xmin or ymin or greater Trivially accept B
than xmax or ymax
 Clip the remaining lines at a E
region boundary and repeat G
steps 1 and 2 on the clipped Trivially reject
line segments

Department of Computer Science and Engineering 7-18


7 Visible surface detection methods

7.3 Line clipping


Assign 4-bit code to each endpoint corresponding to its
position relative to region:
First bit (1000): if y > ymax 1001 1000 1010
Second bit (0100): if y < ymin
Third bit (0010): if x > xmax
Fourth bit (0001): if x < xmin 0001 0000 0010
Test:
if code0 OR code1 = 0000
accept (draw)
else if code0 AND code1  0000 0101 0100 0110
reject (don’t draw)
else clip and retest

Department of Computer Science and Engineering 7-19


7 Visible surface detection methods

7.3 Line clipping


Intersection algorithm:
if code0  0000 then code = code0
else code = code1
dx = x1 – x0; dy = y1 – y0
if code AND 1000 then begin // ymax (x1, y1)
x = x0 + dx * (ymax – y0) / dy; y = ymax (x, y)
end
else if code AND 0100 then begin // ymin ymax
x = x0 + dx * (ymin – y0) / dy; y = ymin dy
end
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax (x0, y0) dx
end
else begin // xmin
y = y0 + dy * (xmin – x0) / dx; x = xmin ymin
end
xmin xmax
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end

Department of Computer Science and Engineering 7-20


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm:
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-21


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm:
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-22


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm: 1010
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-23


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm: 1010 250 150
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-24


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm: 1010 250 150
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-25


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm: 1010 250 150 233 200
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-26


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm: 1010 250 150 233 200
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-27


7 Visible surface detection methods

7.3 Line clipping


Code dx dy x y
Intersection algorithm: 1010 250 150 233 200
if code0  0000 then code = code0
else code = code1
(x1, y1)
dx = x1 – x0; dy = y1 – y0
(400, 300)
if code AND 1000 then begin // ymax
Code (1010)
x = x0 + dx * (ymax – y0) / dy; y = ymax
end
else if code AND 0100 then begin // ymin
x = x0 + dx * (ymin – y0) / dy; y = ymin
end
ymax=200
else if code AND 0010 then begin // xmax
y = y0 + dy * (xmax – x0) / dx; x = xmax
end
else begin // xmin (x0, y0)
y = y0 + dy * (xmin – x0) / dx; x = xmin (150, 150)
end Code (0000) ymin=100
if code = code0 then begin x0 = x; y0 = y; end
else begin x1 = x; y1 = y; end Xmin = 100 xmax = 300

Department of Computer Science and Engineering 7-28


7 Visible surface detection methods

7.3 Line clipping


Cohen-Sutherland algorithm: summary
• Choose an endpoint outside the clipping region
• Using a consistent ordering (top to bottom, left to right)
find a clipping border the line intersects
• Discard the portion of the line from the endpoint to the
intersection point
• Set the new line to have as endpoints the new
intersection point and the other original endpoint
• You may need to run this several times on a single line
(e.g., a line that crosses multiple clip boundaries)

Department of Computer Science and Engineering 7-29


7 Visible surface detection methods

7.3 Line clipping


H
1001 1000 1010

F
G D

0001 0000 C 0010 J


A
E

0101 I
0100 0110
B

A 0001 C 0000 E 0000 G 0000 I 0110


B 0100 D 0010 F 0000 H 1010 J 0010
OR 0101 OR 0010 OR 0000 OR 1010 OR 0110
AND 0000 AND 0000 AND 0000 AND 0000 AND 0010
subdivide subdivide accept subdivide reject

Department of Computer Science and Engineering 7-30


7 Visible surface detection methods

7.3 Line clipping


H
1001 1000 1010
G’

G D

0001 0000 C C’ 0010


A
A’

0101 0100
B 0110

A 0001 A’ 0001 C 0000 C’ 0000 G 0000 G’ 0000


A’ 0001 B 0100 C’ 0000 D 1010 G’ 0000 H 1010
OR 0101 OR 0000 OR 0000
remove AND 0000 AND 0000 remove AND 0000 remove
subdivide accept accept

Department of Computer Science and Engineering 7-31


7 Visible surface detection methods

7.3 Line clipping


1001 1000 1010

0001 0000 0010


A’

B’
0101 0100
B 0110

A’ 0001 B’ 0100
B’ 0100 B 0100
OR 0100
remove AND 0100
reject

Department of Computer Science and Engineering 7-32


7 Visible surface detection methods

7.3 Line clipping


Liang-Barsky line clipping
To achieve faster clipping, we should do a little more
testing before we actually compute the intersection.
Parametric definition of a line:
• x = x1 + uΔx
• y = y1 + uΔy
• Δx = (x2-x1), Δy = (y2-y1), 0 ≤ u ≤ 1
Goal: find range of u for which x and y both inside the
viewing window

Department of Computer Science and Engineering 7-33


7 Visible surface detection methods

7.3 Line clipping


Liang-Barsky line clipping (continued)
Mathematically, we need to find values for u that fulfill the
following inequalities:
xmin ≤ x1 + uΔx ≤ xmax
ymin ≤ y1 + uΔy ≤ ymax
This can be rearranged to:
1: u · (-Δx) ≤ (x1 – xmin)
2: u · (Δx) ≤ (xmax – x1)
3: u · (-Δy) ≤ (y1 – ymin)
4: u · (Δy) ≤ (ymax – y1)

Or in general: u · (pk) ≤ (qk)

Department of Computer Science and Engineering 7-34


7 Visible surface detection methods

7.3 Line clipping


Liang-Barsky line clipping (continued)
Rules:
pk = 0: the line is parallel to boundaries
If for that same k, qk < 0, it’s outside
Otherwise it’s inside
pk < 0: the line starts outside this boundary
rk = qk/pk
u1 = max(0, rk, u1)
pk > 0: the line starts inside the boundary
rk = qk/pk
u2 = min(1, rk, u2)
If u1 > u2, the line is completely outside

Department of Computer Science and Engineering 7-35


7 Visible surface detection methods

7.3 Line clipping


Liang-Barsky line clipping (continued)
The algorithm also extends to 3D
– Add z = z1 + uΔz to the parametric description of a line
– Add 2 more p’s and q’s
– Still only 2 u’s (since the line is still a 2-D primitive)

Department of Computer Science and Engineering 7-36


7 Visible surface detection methods

7.3 Line clipping


Liang-Barsky v. Cohen-Sutherland

– Generally, Liang-Barsky is more efficient


• Requires only one division
• Find intersection values for (x,y) only at end
– This depends, however, on the application
– Cohen-Sutherland may be easier to implement

Department of Computer Science and Engineering 7-37


7 Visible surface detection methods

7.3 Line clipping


Nicholl-Lee-Nicholl line clipping
• This test is most complicated
• Also the fastest
• Only works well for 2D
• Quick overview here

Department of Computer Science and Engineering 7-38


7 Visible surface detection methods

7.3 Line clipping


Nicholl-Lee-Nicholl line clipping T
Divide the region based on the
location of the first point p1
L R
– Case 1: p1 inside
– Case 2: p1 across edge
– Case 3: p1 across corner B
LT
L
L LR
T L
TR LB
L T
LB TB

Department of Computer Science and Engineering 7-39


7 Visible surface detection methods

7.3 Line clipping


Nicholl-Lee-Nicholl Line Clipping
• Symmetry handles other cases
• Find slopes of the line and 4 region bounding lines
• Find which region P2 is in
– If not in any labeled, the line is discarded
• Subtractions, multiplies and divisions can be carefully
used to minimum

Department of Computer Science and Engineering 7-40


7 Visible surface detection methods

7.3 Line clipping


A note on redundancy
Why present multiple forms of clipping?
– Why do you learn multiple sorts?
– Not always easy to do the fastest
– The fastest for the general case isn’t always the fastest for
every specific case
• Mostly sorted list  bubble sort
– History repeats itself
• You may need something similar in a different area. Grab the
one that maps best.

Department of Computer Science and Engineering 7-41


7 Visible surface detection methods

7.4 Polygon clipping


Clipping polygons is more complex than clipping the
individual lines
Input: polygon
Output: original polygon, new polygon, or nothing
Since polygons are bounded by line segments, can we
just use line clipping?

Department of Computer Science and Engineering 7-42


7 Visible surface detection methods

7.4 Polygon clipping


Why can’t we just clip the lines of a polygon?

Department of Computer Science and Engineering 7-43


7 Visible surface detection methods

7.4 Polygon clipping


Why Is Clipping Hard?
What happens to a triangle during clipping?
Possible outcomes:

triangle  triangle triangle  quad triangle  5-gon


How many edges can a clipped triangle have?

Department of Computer Science and Engineering 7-44


7 Visible surface detection methods

7.4 Polygon clipping


How many edges?
Seven…

Department of Computer Science and Engineering 7-45


7 Visible surface detection methods

7.4 Polygon clipping


Why Is Clipping Hard?
A really tough case:

Department of Computer Science and Engineering 7-46


7 Visible surface detection methods

7.4 Polygon clipping


Why Is Clipping Hard?
A really tough case:

concave polygon  multiple polygons

Department of Computer Science and Engineering 7-47


7 Visible surface detection methods

7.4 Polygon clipping


Sutherland-Hodgeman algorithm (A divide-and-conquer
strategy)
– Polygons can be clipped against each edge of the
window one at a time. Edge intersections, if any, are
easy to find since the x or y coordinates are already
known.
– Vertices which are kept after clipping against one
window edge are saved for clipping against the
remaining edges.
– Note that the number of vertices usually changes and
will often increases.

Department of Computer Science and Engineering 7-48


7 Visible surface detection methods

7.4 Polygon clipping


Clipping A Polygon Step by Step:
Right Clip
Boundary

Bottom Clip Boundary

Top Clip Boundary Left Clip


Boundary

Department of Computer Science and Engineering 7-49


7 Visible surface detection methods

7.4 Polygon clipping


Sutherland-Hodgeman Algorithm
Note the difference between this strategy and the Cohen-Sutherland
algorithm for clipping a line: the polygon clipper clips against each
window edge in succession, whereas the line clipper is a recursive
algorithm.
Given a polygon with n vertices, v1, v2,…, vn, the algorithm clips the
polygon against a single, infinite clip edge and outputs another series
of vertices defining the clipped polygon. In the next pass, the partially
clipped polygon is then clipped against the second clip edge, and so
on. Let us consider the polygon edge from vertex vi to vertex vi+1.
Assuming that the start point vi has been dealt with in the previous
iteration, four cases will appear.

Department of Computer Science and Engineering 7-50


7 Visible surface detection methods

7.4 Polygon clipping


Case 1: Inside Outside Case 2: Inside Outside

vi
Polygon is Polygon is
clipped vi+1
clipped
Clip vi
Boundary i: output
vi+1: output

Case 3: Case 4: i: first


(no output) output
Inside Outside
vi+1 vi
vi+1: second
output
Polygon is vi
clipped
Inside Outside

Department of Computer Science and Engineering 7-51


7 Visible surface detection methods

7.4 Polygon clipping


Sutherland-Hodgeman Clipping
Four cases:
– s inside plane and p inside plane
• Add p to output
• Note: s has already been added
– s inside plane and p outside plane
• Find intersection point i
• Add i to output
– s outside plane and p outside plane
• Add nothing
– s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed by p

Department of Computer Science and Engineering 7-52


7 Visible surface detection methods

Point-to-Plane test
Point-to-Plane test
A very general test to determine if a point p is “inside” a plane P,
defined by q and n:
(p - q) • n < 0: p inside P
(p - q) • n = 0: p on P
(p - q) • n > 0: p outside P
Remember: p • n = |p| |n| cos (q)
q = angle between p and n

q q q
n n n
p p p
P P P

Department of Computer Science and Engineering 7-53


7 Visible surface detection methods

Finding Line-Plane Intersections


Edge intersects plane P where E(t) is on P
q is a point on P
n is normal to P

L0
(L(t) - q) • n = 0
L1
q
t = [(q - L0) • n] / [(L1 - L0) • n] n

The intersection point i = L(t) for this value of t.


P

Department of Computer Science and Engineering 7-54


7 Visible surface detection methods

7.4 Polygon clipping


An example for the polygon clipping

v5

v4
v1

v2 v3

Department of Computer Science and Engineering 7-55


7 Visible surface detection methods

7.4 Polygon clipping


As we said, the Sutherland-Hodgeman algorithm clips the polygon against
one clipping edge at a time. We start with the right edge of the clip rectangle.
In order to clip the polygon against the line, each edge of the polygon have to
be considered. Starting with the edge, represented by a pair of vertices, v5v1:

v5 v5

v4
v1
v1 v1

v2 v3

Clipping edge Clipping edge Clipping edge

Department of Computer Science and Engineering 7-56


7 Visible surface detection methods

7.4 Polygon clipping


Now v1v2:

v5

v4
v1 v1 v1

v2 v3 v2 v2

Clipping edge Clipping edge Clipping edge

Department of Computer Science and Engineering 7-57


7 Visible surface detection methods

7.4 Polygon clipping


Now v2v3:

v5

v4
v1
v1

v2 v3 v2 v3 v2 v3

Clipping edge Clipping edge Clipping edge

Department of Computer Science and Engineering 7-58


7 Visible surface detection methods

7.4 Polygon clipping


Now v3v4:

v5

v4 v4
v1
v1
i1

v2 v3 v3 v2 v3

Clipping edge Clipping edge Clipping edge

Department of Computer Science and Engineering 7-59


7 Visible surface detection methods

7.4 Polygon clipping


Now v4v5:

v5 v5 v5

v4 v4 i2
v1
v1
i1

v2 v3 v2 v3

Clipping edge Clipping edge Clipping edge

After these, we have to clip the polygon against the other three edges of the
window in a similar way.

Department of Computer Science and Engineering 7-60


7 Visible surface detection methods

7.4 Polygon clipping


Problem with Sutherland-Hodgeman
Concavities can end up linked:

Weiler-Atherton creates separate polygons in cases like


this.

Department of Computer Science and Engineering 7-61


7 Visible surface detection methods

7.4 Polygon clipping


Weiler-Atherton Polygon Clipping
To find the edges for a clipped polygon, we follow a path
(either clockwise or counterclockwise) around the fill area
that detours along a clipping-window boundary whenever
a polygon edge crosses to the outside of that boundary.
The direction of a detour at a clipping-window border is
the same as the processing direction for the polygon
edges.
For a counterclockwise traversal of the polygon vertices,
we apply the following Weiler-Atherton procedures:

Department of Computer Science and Engineering 7-62


7 Visible surface detection methods

7.4 Polygon clipping


Weiler-Atherton Polygon Clipping (continued)
1. Process the edges of the polygon in a
counterclockwise order until an inside-outside pair of
vertices is encountered for one of the clipping
boundaries; that is, the first vertex of the polygon edge
is inside the clip region and the second vertex is
outside the clip region.
2. Follow the window boundaries in a counterclockwise
direction from the exit-intersection point to another
intersection point with the polygon. If this is a
previously processed point, proceed to the next step.
If this is a new intersection point, continue processing
polygon edges in a counterclockwise order until a
previously processed vertex is encountered.
Department of Computer Science and Engineering 7-63
7 Visible surface detection methods

7.4 Polygon clipping


Weiler-Atherton Polygon Clipping (continued)
3. Form the vertex list for this section of the clipped
polygon.
4. Return to the exit-intersection point and continue
processing the polygon edges in a counterclockwise
order.
Note: this may generate more than one polygon!

Department of Computer Science and Engineering 7-64


7 Visible surface detection methods

7.4 Polygon clipping


Weiler-Atherton Polygon Clipping (continued)
Example:

add clip pt. add end pt. add clip pt. follow clip edge until
and end pt. cache old dir. a) new crossing found
b) reach pt. already
added

Department of Computer Science and Engineering 7-65


7 Visible surface detection methods

7.4 Polygon clipping


Weiler-Atherton Polygon Clipping (continued)
Example (continued)

continue from add clip pt. add clip pt. follow clip edge until
cached location and end pt. cache dir. a) new crossing found
b) reach pt. already
added

Department of Computer Science and Engineering 7-66


7 Visible surface detection methods

7.4 Polygon clipping


Weiler-Atherton Polygon Clipping (continued)
Example (continued)

continue from nothing added Final result:


cached location finished Two unconnected
polygons

Department of Computer Science and Engineering 7-67


7 Visible surface detection methods

7.4 Polygon clipping


Difficulties with Weiler-Atherton polygon clipping
What if the polygon re-crosses edge?

How many “cached” crossings?

Your geometry step must be able to create new polygons instead


of 1-in-1-out

Department of Computer Science and Engineering 7-68


7 Visible surface detection methods

7.5 Curve clipping


Areas with curved boundaries can
be clipped with methods similar to
those discussed in the previous
sections. If the objects are
approximated with straight-line
segments, we use a polygon-
Before Clipping
clipping method. Otherwise, the
clipping procedures involve
nonlinear equations, and this
requires more processing than for
objects with linear boundaries.
After Clipping

Department of Computer Science and Engineering 7-69


7 Visible surface detection methods

7.5 Curve clipping


For a simple accept/reject test, the
bounding box can be used. This box
(in the 2-D case just a square)
describes the maximal extent of the
curved object parallel to the
coordinate axes. If the bounding box
Before Clipping
does not intersect with the clipping
region, no part of the object is
inside. Otherwise, if the bounding
box is completely contained by the
clipping region, the entire object is
going to be inside. After Clipping

Department of Computer Science and Engineering 7-70


7 Visible surface detection methods

7.5 Curve clipping


If the bounding box is partly inside
the clipping area we have to do
further testing. Similar to polygon
clipping, the intersections with the
boundaries of the clipping region
need to be computed. An
Before Clipping
intersection calculation involves
substituting a clipping-boundary
position (xmin, xmax, ymin, and ymax) in
the nonlinear equation for the object
boundary and solving for the other
coordinate value. After Clipping

Department of Computer Science and Engineering 7-71


7 Visible surface detection methods

7.6 Text Clipping


There are several techniques that can be used to provide
text clipping. The simplest method for processing
character strings relative to the clipping window is to use
the all-or-none string clipping strategy. This procedure
is implemented by examining the coordinate extent of the
text string (bounding box). If the coordinate limits of this
bounding box are not entirely within the clipping window,
the string is rejected.
Sometimes, only the lower left corner is used for clipping:
only if this point is within the clipping region the string is
drawn. This, for example, is how OpenGL clips the
Bitmap Characters (based on the current raster position).

Department of Computer Science and Engineering 7-72


7 Visible surface detection methods

7.6 Text Clipping


An alternative is to use the all-or-none character
clipping strategy. Here we eliminate only those
characters that are not completely inside the clipping
region. In this case, the coordinate extents of individual
characters are compared to the clipping boundaries. Any
character that is not completely within the clipping-
window boundary is eliminated.

Department of Computer Science and Engineering 7-73


7 Visible surface detection methods

7.6 Text Clipping


A third approach to text clipping is to clip the components
of individual characters. This provides the most accurate
display of clipped character strings, but it requires the
most processing. If an individual character overlaps a
clipping boundary, we clip off only the parts of the
character that are outside the clipping region. Outline
character fonts defined with line segments are processed
in this way using polygon-clipping algorithms. Characters
defined with bitmaps are clipped by comparing the
relative position of the individual pixels in the character
grid patterns to the borders of the clipping region.

Department of Computer Science and Engineering 7-74


7 Visible surface detection methods

7.6 Text Clipping


All or none STRING1

text clipping
STRING2 STRING2

Before Clipping After Clipping

STRING1 ING1
All or none
character clipping STRING3 TRING3
STRING4 STRING4

Before Clipping After Clipping

STRING1 STRING1
Clipping individual
character

Before Clipping After Clipping

Department of Computer Science and Engineering 7-75


7 Visible surface detection methods

7.7 3-D Clipping


• For orthographic projection, view volume is a box.
• For perspective projection, view volume is a frustrum.

left
Far clipping plane.

Near clipping plane


Need to calculate intersection
With 6 planes.
right

Department of Computer Science and Engineering 7-76


7 Visible surface detection methods

7.7 3D Clipping
We extend the Cohen-Sutherland algorithm.
– Now 6-bit code instead of 4 bits.
– Trivial acceptance where both endpoint codes are all zero.
– Perform logical AND, reject if non-zero.
– Find intersect with a bounding plane and add the two new
lines to the line queue.
– Line-primitive algorithm.

Department of Computer Science and Engineering 7-77


7 Visible surface detection methods

7.7 3D Clipping
Sutherland-Hodgman Algorithm

Four cases of polygon clipping :

Inside Outside Inside Outside Inside Outside Inside Outside

Output Output Second First


Vertex Intersection Output Output
Case 3
Case 1 Case 2 No Case 4
output

Department of Computer Science and Engineering 7-78


7 Visible surface detection methods

7.7 3D Clipping
• Sutherland-Hodgman extends easily to 3D
• Call ‘CLIP’ procedure 6 times rather than 4
• Polygon-primitive algorithm

Department of Computer Science and Engineering 7-79


7 Visible surface detection methods

7.8 Hidden Surface Removal


Visibility
• Given a set of polygons, which is visible at each pixel?
(in front, etc.). Also called hidden surface removal
• Very large number of different algorithms known. Two
main classes:
– Object precision: computations that operate on primitives
– Image precision: computations at the pixel level
• All the spaces in the viewing pipeline maintain depth, so
we can work in any space
– World, View and Canonical Screen spaces might be used
– Depth can be updated on a per-pixel basis as we scan convert
polygons or lines

Department of Computer Science and Engineering 7-80


7 Visible surface detection methods

7.8 Hidden Surface Removal


Visibility Issues
• Efficiency – it is slow to overwrite pixels, or scan convert
things that cannot be seen
• Accuracy - answer should be right, and behave well
when the viewpoint moves
• Must have technology that handles large, complex
rendering databases
• In many complex worlds, few things are visible
– How much of the real world can you see at any moment?
• Complexity - object precision visibility may generate
many small pieces of polygon

Department of Computer Science and Engineering 7-81


7 Visible surface detection methods

7.8 Hidden Surface Removal


Painters Algorithm (Image Precision)
•Algorithm:
– Choose an order for the
polygons based on some choice
(e.g. depth to a point on the Fails
polygon)
– Render the polygons in that
order, deepest one first
•This renders nearer polygons over
further
•Difficulty: zs Which
– works for some important point for
geometries (2.5D - e.g. VLSI)
choosing
– doesn’t work in this form for
most geometries - need at least ordering?
better ways of determining
ordering
xs

Department of Computer Science and Engineering 7-82


7 Visible surface detection methods

7.8 Hidden Surface Removal


Depth Sorting (Object Precision, in view space)
• An example of a list-priority algorithm
• Sort polygons on depth of some point
• Render from back to front (modifying order on the fly)
• Rendering: For surface S with greatest depth
– If no overlap in depth with other polygons, scan convert
– Else, for overlaps in depth, test for overlaps in the image plane
• If none, scan convert and go to next polygon
– If S, S’ overlap in depth, swap order and try again
– If S, S’ have been swapped already, split and reinsert

Department of Computer Science and Engineering 7-83


7 Visible surface detection methods

7.8 Hidden Surface Removal


Depth Sorting (continued)
Testing for overlaps: Start drawing when first condition is
met: S
S
x-extents or y-extents do not overlap S’ or
S’
z
S
S is behind the plane of S’ S’
x
z S
S’
S’ is in front of the plane of S x

S’
S and S’ do not intersect in the image plane S

Department of Computer Science and Engineering 7-84


7 Visible surface detection methods

7.8 Hidden Surface Removal


Depth Sorting (continued)
Advantages:
– Filter anti-aliasing works fine
– Composite in back to front order
– No depth quantization error
– Depth comparisons carried out in high-precision view space
Disadvantages:
– Over-rendering
– Potentially very large number of splits -
(n2) fragments from n polygons

Department of Computer Science and Engineering 7-85


7 Visible surface detection methods

7.8 Hidden Surface Removal


Area Subdivision
• Exploits area coherence: Small areas of an image are
likely to be covered by only one polygon
• Three easy cases for determining what’s in front in a
given region:
– a polygon is completely in front of everything else in that
region
– no surfaces project to the region
– only one surface is completely inside the region, overlaps the
region, or surrounds the region

Department of Computer Science and Engineering 7-86


7 Visible surface detection methods

7.8 Hidden Surface Removal


Warnock’s Area Subdivision (Image Precision)
• Start with whole image
• If one of the easy cases is satisfied (previous slide), draw what’s
in front
• Otherwise, subdivide the region and recurse
• If region is single pixel, choose surface with smallest depth
• Advantages:
– No over-rendering
– Anti-aliases well - just recurse deeper to get sub-pixel information
• Disadvantage:
– Tests are quite complex and slow

Department of Computer Science and Engineering 7-87


7 Visible surface detection methods

7.8 Hidden Surface Removal


Warnock’s Algorithm
• Regions labeled with case 2 3 2 2
used to classify them:
1) One polygon in front
2) Empty 3 3 3 2
3
3) One polygon inside, 3
surrounding or intersecting 3 1 3
1
• Small regions not labeled 1 1 1 3
3
• Note it’s a rendering 3
3 3
algorithm and a HSR
algorithm at the same time 2 3 3 3
– Assuming you can draw
squares
2 2 2 2

Department of Computer Science and Engineering 7-88


7 Visible surface detection methods

7.8 Hidden Surface Removal


BSP-Trees (Object Precision)
Construct a binary space partition tree
– Tree gives a rendering order
– A list-priority algorithm
Tree splits 3D world with planes
– The world is broken into convex cells
– Each cell is the intersection of all the half-spaces of splitting planes
on tree path to the cell
Also used to model the shape of objects, and in other visibility
algorithms
– BSP visibility in games does not necessarily refer to this algorithm

Department of Computer Science and Engineering 7-89


7 Visible surface detection methods

7.8 Hidden Surface Removal


BSP-Trees (continued)
Example:

Department of Computer Science and Engineering 7-90


7 Visible surface detection methods

7.8 Hidden Surface Removal


BSP-Trees (continued)
If a cutting plane intersects an object the object needs to
be split.
To render the scene, we process that part of the tree
which is further away from the view point with respect to
the cutting plane. This way, the objects are drawn in a
back to front order. Thus, the foreground objects are
painted over the background objects.
Note: if the viewpoint changes we can still use the same
BSP tree (assuming the objects did not change); only
the front and back side with respect to the cutting planes
may switch.

Department of Computer Science and Engineering 7-91


7 Visible surface detection methods

7.8 Back-face Culling


Depending on the number and layout of objects within the
scenes the removal of hidden surfaces can be quite
costly. Therefore, a simple test which helps to reduce
the complexity would be beneficial before using the
visibility algorithm.

Such a simple but effective approach is back-face culling.

Depending on the position of the viewer the backsides of


opaque objects are removed since these are occluded
by the object itself, thus invisible.

Department of Computer Science and Engineering 7-92


7 Visible surface detection methods

7.8 Back-face Culling


Classification of backsides
• First, the normal vectors Ni of all surfaces are
computed
• For back-facing surfaces, one component of the normal
vector points in the view direction, i.e. the scalar
product of the viewing direction and the normal is
positive: pNi > 0
N3 N4
pN1 < 0 pN4 > 0
p pN2 < 0 pN5 > 0
N2 N5
pN3 < 0 pN6 > 0
N1 N6
Department of Computer Science and Engineering 7-93
7 Visible surface detection methods

7.8 Back-face Culling


Properties
– The number of polygons that are required for rendering the
scene is approximately cut in half by removing the back-facing
surfaces.
– The computational effort for computing the scalar product is
minimal.
– If the scene is composed of a single convex polyhedron back-
face culling already solves the visibility problem.
With scenes consisting of concave polyhedra or more
than one convex polyhedron the objects can occlude
themselves or each other which requires more
complex algorithms.

Department of Computer Science and Engineering 7-94

You might also like