hidden lines - computer science and...
TRANSCRIPT
Visibility A
lgorithms
Roger C
rawfis
CIS 781
This set of slides reference slides used at Ohio State for
instruction by Prof. Machiraju and Prof. H
an-Wei Shen.
Visibility D
etermination
•A
KA
, hidden surface elimination
Hidden Lines
Hidden Lines R
emoved
Hidden Surfaces R
emoved
BackfaceCulling
Hidden O
bject Rem
oval: Painters Algorithm
Z-buffer
Spanning Scanline
Warnock
Atherton-Weiler
List Priority, NN
A
BSP Tree
Taxonomy
Topics
Where A
re We ?
Canonical view volum
e (3D im
age space)
Clipping done
division by w
z > 0
x
y
znear
far
clipped line
1
11
0
x
y
zim
age planenearfar
clipped line
Back-face C
ulling
Problems ?
Conservative algorithm
Real job of visibility never solved
Back-face C
ulling•
If a surface’s normal is pointing in the sam
e general direction as our eye, then this is a back face
•The test is quite sim
ple: if N * V
> 0 then we reject the
surface
•If test is in eye-space, then if N
z > 0 reject.
Back-face C
ulling
•O
nly handles faces oriented aw
ay from the view
er:–
Closed objects
–N
ear clipping plane does not intersect the objects
•G
ives complete solution for a single convex
polyhedron.•
Still need to sort, but we have reduced the num
ber of prim
itives to sort.
Painters Algorithm
Sort objects in depth order
Draw
all from Back-to-Front (far-to-near)
Simply overw
rite the existing pixels.
Is it so simple?
at z = 22, at z = 18,
at z = 10,
1 2 3
X
Y
Point sorting vsPolygon Sorting
•W
hat does it mean to sorttw
o line segm
ents?–
Zmin?
–Zm
ax?–
Slope?–
Length?z
3D C
yclesH
ow do w
e deal with cycles?
How
do we deal w
ith intersections?
How
do we sort objects that overlap in Z?
Z
Form of the Input
Object types: w
hat kind of objects does it handle?
convex vs. non-convex
polygons vs. everything else -sm
ooth curves, non-continuous surfaces, volum
etric data
Object Space
Geom
etry in, geometry out
Independent of image
resolution
Followed by scan
conversion
Form of the output
Image Space
Geom
etry in, image out
Visibility only at pixel centers
Precision: image/object space?
Object Space A
lgorithms
Volume testing –
Weiler-Atherton, etc.
input: convex polygons + infinite eye pt
output: visible portions of wirefram
eedges
Image-space algorithm
sTraditional Scan Conversion and Z-buffering
Hierarchical Scan Conversion and Z-buffering
input: any plane-sweepable/plane-boundable
objects
preprocessing: none
output: a discrete image of the exact visible set
Conservative V
isibility Algorithm
sView
portclipping
Back-face culling
Warnock's screen-space subdivision
Z-buffer
Z-buffer is a 2D array that stores a depth value for each pixel.
InitScreen: for
i := 0 toN
dofor
j := 1 toN
doScreen[i][j] := BAC
KG
ROU
ND
_CO
LOR; Zbuffer[i][j] := ∞
;
DrawZpixel(x, y, z, color)if(z <= Zbuffer[x][y]) then
Screen[x][y] := color; Zbuffer[x][y] := z;
Z-buffer: Scanline
I.for
each polygondo
foreach pixel (x,y)in the polygon’s projection
doz := -(D
+A*x+B
*y)/C;
Draw
Zpixel(x, y, z, polygon’s color);
II.for
each scan-liney
dofor
each “in range” polygon projectiondo
foreach pair (x
1 , x2 ) of X
-intersections dofor
x := x1 to x
2 doz := -(D
+A*x+B
*y)/C;
Draw
Zpixel(x, y, z, polygon’s color);
If we know
zx,y at (x,y) then:
zx+1,y = z
x,y -A/C
Incremental Scanline
On a scan line Y =
j, a constant Thus depth of pixel at (x
1 =x+
∆x,j)
0
0
≠+
+−
=
=+
++
CC
DBy
Ax
z
DCz
By
Ax
, )(
xC A
zz
Cx
xA
zz
CD
Bj
Ax
CD
Bj
Ax
zz
∆−
=
−=
−
++
−+
++
−=
−
)(
)(
)(
)(
1
11
11
, since∆x
= 1,
C Az
z−
=1
Incremental Scanline
(contd.)All that w
as about increment for pixels on each scanline.
How
about across scanlinesfor a given pixel ?
Assumption: next scanline
is within polygon
yC B
zz
Cy
yA
zz
CD
By
Ax
CD
By
Ax
zz
∆−
=
−=
−
++
++
+−
=−
)(
)(
)(
)(
1
11
11
, since∆y
= 1,
C Bz
z−
=1
P1
P2
P3
P4
ys
za
zp
zb
Non-Planar Polygons
Bilinear Interpolation of Depth Values
)(
)()
(
)(
)()
(
)(
)()
(
ba
pa
ab
ap
sb
sa
xx
xx
zz
zz
yy
yy
zz
zz
yy
yy
zz
zz
− −−
+=
− −−
+=
− −−
+=
21 1
12
1
41 1
14
1
Z-buffer -Exam
ple∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
Z-buffer
Screen
[0,1,5]
[0,7,5][6,7,5]
55
55
55
55
55
55
55
55
55
5∞
55
∞∞
5∞
∞∞
∞∞
∞∞
55
5∞
55
∞∞
5∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
∞∞
55
55
55
55
55
55
55
55
55
55
55
55
55
55
[0,1,2]
[0,6,7]
[5,1,7]
23
45
34
56
45
67
56
76
77
67
7
55
55
55
55
55
55
55
55
45
57
34
56
23
45
∞∞
∞∞
55
5∞
55
∞∞
5∞
∞∞
∞∞
∞∞
∞∞
∞∞
7∞
∞∞
67
∞∞
∞∞
∞∞
Rectangle: P1(10,5,10), P2(10,25,10), P3(25,25,10),
P4(25,5,10)
Triangle: P5(15,15,15), P6(25,25,5), P7(30,10,5)
Frame Buffer: Background 0, R
ectangle 1, Triangle 2
Z-buffer: 32x32x4 bit planes
Non Trivial Exam
ple ?
Example
Z-Buffer A
dvantages
Simple and easy to im
plement
Amenable to scan-line algorithm
s
Can easily resolve visibility cycles
Handles intersecting polygons
Z-Buffer D
isadvantagesD
oes not do transparency easily
Aliasing occurs! Since not all depth questions can be resolved
Anti-aliasing solutions non-trivial
Shadows are not easy
Higher order illum
ination is hard in general
Spanning Scan-Line
Can w
e do better than scan-line Z-buffer ?
Scan-line z-buffer does not exploit
Scan-line coherency across multiple scan-lines
Or span-coherence !
Depth coherency
How
do you deal with this –
scan-conversion algorithm and a little
more data structure
Spanning S
can Line Algorithm
•U
se no z-buffer•
Each scan line is subdivided into
several "spans"•
Determ
ine which polygon the
current span belongs to•
Shade the span using the current
polygon’s color•
Exploit "span coherence" :
•For each span, only one visibility test needs to be done–
Assum
ing no intersecting polygons.
Spanning S
can Line Algorithm
•A
scan line is subdivided into a sequence of spans•
Each span can be "inside" or "outside" polygon
areas–
"outside“: no pixels need to be drawn (background color)
–"inside“: can be inside one or m
ultiple polygons•
If a span is inside one polygon, the pixels in the span w
ill be drawn w
ith the color of that polygon•
If a span is inside more than one polygon, then w
e need to com
pare the z values of those polygons at the scan line edge intersection point to determ
ine the color of the pixel
Spanning S
can Line Algorithm
Determ
ine a span is inside or outside (single polygon)
•W
hen a scan line intersects an edge of a polygon –
for a 1sttim
e, the span becomes "inside" of the
polygon from that intersection point on
–for a 2
ndtime, the span becom
es "outside“ of the polygon from
that point on•
Use a "in/out" flag for each polygon to keep
track of the current state•
Initially, the in/out flag is set to be "outside" (value = 0 for exam
ple). Invert the tag for “inside”.
When there are m
ultiple polygons
•E
ach polygon will have its ow
n in/out flag•
There can be more than one polygon having
the in/out flags to be "in" at a given instance•
We w
ant to keep track of how m
any polygons the scan line is currently in
•If there is m
ore than one polygon "in", we
need to perform z value com
parison to determ
ine the color of the scan line span
Z value comparison
•W
hen the scan line intersects an edge, leaving the top-m
ost polygon, we use the color of the rem
aining polygon if there is now
only 1 polygon "in". •
If there is still more than one polygon w
ith an "in" flag, w
e need to perform z com
parison, but only when the
scan line leaves a non-obscured polygon.
xy
max
∆x
poly-IDE
T
PT
poly-IDA
,B,C
,D color
in/out flag
Many Polygons !
Use a PT entry for each polygon
When polygon is considered, Flag is true
Multiple polygons can have their flags set to true
Use IPL as active In-Polygon List !
ST
a
b
c
1
2
3
X0
I III
II
IV
XN
BG
Think of ScanPlanesto understand !
Example
Spanning Scan-Line: E
xample
YAET
IPL
Ix
0 , ba, bc, x
NBG
, BG+
S, BG
IIx
0 , ba, bc, 32, 13, x
NBG
, BG+
S, BG, BG
+T, BG
IIIIII
x x0 0 , , ba ba
, 32, ca, 13, , 32, ca, 13, x x
N NBG
, BG+
S, BG+
S+T, BG
+T, BG
BG, BG
+S, BG
+S+
T, BG+
T, BG
IVx
0 , ba, ac, 12, 13, x
NBG
, BG+
S, BG, BG
+T, BG
ST
a
b
c
1
2
3
X0
I III
II
IV
XN
BG
Scan Line I: Polygon S is in and flag of S=true
ScanLineII: Both S and T are in and flags are
disjointlytrue
Scan Line III: Both S and T are in simultaneously
Scan Line IV: Same as Scan Line II
Some Facts !
Spanning Scan-Line
buildET, PT
--all polys+BG
polyA
ET := IPL := Nil;
fory := y
min to
ym
ax doe1 := first_item
( AET );IPL := B
G;
while
(e1.x <> MaxX
) doe2 := next_item
(AET);
poly := closest poly in IPL at [(e1.x+e2.x)/2, y]draw
_line(e1.x, e2.x, poly-color);update
IPL (flags); e1 := e2;end-w
hile;IPL
:= NIL
; updateA
ET;end-for;
Depth C
oherence•
Depth relationships m
ay not change betw
een polygons from one scan-line to
the next scan-line.•
These can be kept track using the (active edge table) AET and the (polgontable) PT.
•H
ow about penetrating polygons?
Penetrating Polygons
YAET
IPL
Ix
0 , ba, 23, ad, 13, x
NBG
, BG+
S, S+T, BG
+T,BG
I’x
0 , ba, 23, ec, ad, 13, x
NBG
, BG+
S, BG+
S+T,
BG
+S+
T, BG+
T, BG
S
I
T
a
2
BG
b
c
3
de
1
False edges and new polygons!
surround intersect
containeddisjoint
Area Subdivision 1
(Warnock’s A
lgorithm)
Divide and conquer: the relationship of a display area
and a polygon after projection is one of the four basic cases:
Warnock : O
ne Polygonif it surrounds
thendraw
_rectangle(poly-color);
else begin
if it intersectsthen
poly := intersect(poly, rectangle);
draw_rectangle(BACKG
RO
UN
D);
draw_poly(poly);
end else;
What about contained and disjoint ?
Warnock’s A
lgorithm•
Starting w
ith the entire display, we check the follow
ing four cases. If none hold, w
e subdivide the area and repeat, otherw
ise, we stop and perform
the action associated w
ith the case1.A
ll polygons are disjoint wrtthe area -> draw
the background color2.O
nly 1 intersecting or contained polygon -> draw background, and
then draw the contained portion of the polygon
3.There is a single surrounding polygon -> draw the entire area in
the polygon’s color4.There are m
ore than one intersecting, contained, or surrounding polygons, but there is a front surrounding polygon -> draw
the entire area in the polygon’s color
•The recursion stops at the pixel level
At A
Single Pixel Level
•W
hen the recursion stops and none of the four cases hold, w
e need to perform a depth
sort and draw the polygon w
ith the closest Z value
•The algorithm
is done at the object space level, except scan conversion and clipping are done at the im
age space level
0
00
0 00
0 1
M 1
11
1
MM M
Warnock : Z
ero/One Polygons
warn
ock01
(rectangle, poly)
new-poly :=
clip(rectangle, poly);
ifnew
-poly = N
ULL th
en
draw_rectangle(BACKG
RO
UN
D);
elsedraw
_rectangle(BACKGRO
UN
D);
draw_poly(new
-poly); return;
surround intersect
containeddisjoint
1-polygon0-polygon
Warnock(rectangle,poly-list)
new-list :=
clip(rectangle, poly-list);if length(new
-list) = 0 then
draw_rectangle(BACKG
RO
UN
D); return;
if length(new-list) =
1 thendraw
_rectangle(BACKGRO
UN
D);
draw_poly(poly); return;
if rectangle size = pixel size then
poly := closest polygon at rectangle center
draw_rectangle(poly color); return;
warnock(top-left quadrant, new
-list);w
arnock(top-right quadrant, new-list);
warnock(bottom
-left quadrant, new-list);
warnock(bottom
-right quadrant, new-list);
11
0 0
1M
M1
1 M
M M
0
11
1
1
11
0M
M M
MM
M M
M
M M
M M
M
M M
M
Area Subdivision 2
Weiler
-Atherton A
lgorithm
Object space
Like Warnock
Output –
polygons of arbitrary accuracy
Weiler-A
therton Clipping
•G
eneral polygon clipping algorithm•
Allow
s one to clip a concave polygon against another concave polygon.
Weiler-A
therton Clipping
•First, find all of the intersection points betw
een edges of the two polygons.
A
B
C
D
E
ab
cd
e
ST
1
2
3
4
56
S:A
,B,C
,D,E
T: a,b,c,d,e
Weiler-A
therton Clipping
•N
ow, rebuild the polygon’s such that they
include the intersection points in their clock-w
ise ordering.
A
B
C
D
E
ab
cd
e
ST
S:A
,1,4,B,2,6,C
,D,5,3,E
T: a,4,2,b,6,c,5,d,e,3,1
1
2
3
4
56
Weiler-A
therton Clipping
•Find an intersecting vertex of the polygon to be clipped that starts outside and goes inside the clipping region.
•Traverse the polygon until another intersection point is found.
A
B
C
D
E
ab
cd
e
ST
S:A
,1,4,B,2,6,C
,D,5,3,E
T: a,4,2,b,6,c,5,d,e,3,1C
lip: 6,c,5,…
1
2
3
4
56
Weiler-A
therton Clipping
•Sw
itch from w
alking around the polygon 1, to w
alking around polygon 2, when an intersection is
detected.•
Stop when w
e reached the initial point.
A
B
C
E
ab
cd
e
ST
S:A
,1,4,B,2,6,C
,D,5,3,E
T: a,4,2,b,6,c,5,d,e,3,1C
lip: 6,c,5,3,1,4,2,6
1
2
3
4
56
Weiler
-Atherton A
lgorithm•
Subdivide along polygon boundaries (unlike W
arnock’s rectangular boundaries in im
age space);•
Algorithm
: 1.
Sort the polygons based on their minim
um z distance
2.C
hoose the first polygon P in the sorted list 3.
Clip all polygons left against P, create tw
o lists:–
Inside list: polygon fragments inside P (including P)
–O
utside list: polygon fragments outside P
4.A
ll polygon fragments on the inside list that are behind P
are discarded. If there are polygons on the inside list that arein front of P, go back to step 3), use the ’offending’ polygons as P
5.D
isplay P and go back to step (2)
Weiler
-Atherton A
lgorithmW
A_display(polys: L
istOfPolygons)
sort_by_minZ(polys);
while
(polys<> NU
LL) doW
A_subdiv(polys->
first, polys)end;
WA
_subdiv(first: Polygon; polys: ListO
fPolygons)
inP, outP: L
istOfPolygons:= N
ULL;
foreach
Pin
polysdo Clip(P, first->
ancestor, inP, outP);
for each Pin
inPdo ifP
is behind (min z)firstthen
discard P;
for each Pin
inPdo
ifP is not part of firstthen W
A_subdiv(P, inP);
for each Pin
inPdo display_a_poly(P);
polys:= outP;
end;
tr
tr
13
2
0.2
0.5
0.30.8
List Priority Algorithm
s•
Find a valid order for rendering.•
Only consider cases w
here the sort matters.
List Priority Algorithm
s
If objects do not overlap in X or in Y
there is no need for hidden object rem
oval process.
If they do not overlap in the Z dimension they can be
sorted by Z and rendered in back (highest priority)-to-front (low
est priority) order (Painter’s Algorithm
).
It is easy then to implem
ent transparency.
How
do we sort ? –
different algorithms differ
z
x/y1
2
3
44
z
x/y1
2
?
New
ell, New
ell, SanchaA
lgorithm1. Sort by [m
inz ..m
axz ] of each polygon
2. For each group of unsorted polygons G
resolve_ambiguities(G
);3. R
ender polygons in a back-to-front order.resolve_am
biguities is basically a sorting algorithm that relieson
the procedure rearrange(P, Q):
resolve_ambiguities(G
)not-yet-done := TR
UE;
while (not-yet-done) do
not-yet-done := FALSE;
for each pair of polygons P, Q in G
do --- bubble sortL :=
rearrange(P, Q, not-yet-done);
insert L into G instead of P,Q
New
ell, New
ell, SanchaA
lgorithmrearrange(P, Q
, flag)if (P and Q
do not have overlapping x-extents, returnP, Q
if (P and Q do not have overlapping y-extents, return
P, Qifall Q
is on the oppositeside of P from
the eyereturn P, Q
ifall P is on the same
side of Q from
the eyereturn
P, Qifnotoverlap-projection(P, Q
) return P, Qflag := T
RU
E; // m
ore work is needed
ifall Q is on the sam
eside of P from
the eyereturn Q
, Pif all P is on the opposite
side of Q from
the eyereturn Q
, Psplit(P, Q
, p1, p2);--split P by Q
return(p1, p2, Q
);
New
ell-New
ell-SanchaSorting
•Q
is on the oppositeside of P.
•M
eans, all of Q’s vertices are behind the
half-plane defined by P.
PQ
PQ
TrueFalse
PQ
PQ
New
ell-New
ell-SanchaSorting
•P is on the sam
eside of Q
.•
Means, all of P’s vertices are in front of the
half-plane defined by Q.
FalseTrue
Taxonomy
Apel, Weiler-Atherton
List priority
Image Space
edge
Object space
volumeRoberts
New
ellW
arnockSpan-line Algorithms
A’prioriD
ynamic
AreaPoint
A characterization of 10 Hidden Surface Algorithm
:
Sutherland, Sproull, Schumaker
(1974)
Spatial Subdivision
•U
niform grid
•O
ctrees•
K-d Trees
•B
SP-trees•
Non-overlapping polyhedra
–A
xis-Aligned B
ounding Boxes (A
AB
B’s)
–O
riented Bounding B
oxes (OB
B’s)
–U
seful for non-static scenes
Back-to-front Traversals
•For the first four, you can develop either a front-to-back or back-to-front traversal order explicitly.
•Thereby, solving the visibility sort efficiently.
•For the polyhedra, use a N
ewell-N
ewell-
Sanchasort.
Sorting for Uniform
Grid
•Parallel Projection–
Can alw
ays proceed along the x-axis, then y-axis then z-axis or any com
bination.–
Simply need to decide w
hether to go forward or
backward on each axis.
•Look at the z-value of the transform
ed x-axis, …•
Positive, go forward for back-to-front sort.
–B
etter ordering would choose the axis m
ost parallel to the view
ing direction to traverse last.
Sorting for Uniform
Grid
•Perspective projection–
May need to proceed forw
ard for part of the grid and backw
ards for the other.
K-d Trees
X
• •A
lternate splits in
each direction
Altern
ate splits in each
direction
Split X axis
Split Y axis
K-d Trees
•Extend to any dim
ension d•
In 3D, the splits are done w
ith axis-aligned planes.–
Test is simple, is x-value (for nodes splitting
the x-axis) greater than the node value?
K-d Trees
•A
subset of BSP-trees.
•Sorting is the sam
e.•
More efficient storage representation.
Binary Space-Partitioning T
ree
Given a polygon p
Two lists of polygons:
those that are behind(p) :B
those that are in-front(p) :F
If eye is in-front(p), right display order is B, p, F
Otherw
ise it is F, p, B
B
pF
p
BF
backfront
Bf
Bb
p
F
Bb
Bf
B
Display a B
SP Tree
structbspnode{
p: Polygon; back, front : *bspnode;} B
SPTree;
BSP_display ( bspt)
BSPTree
*bspt;{
if(!bspt) return;if(EyeInfrontPoly( bspt->p )) {B
SP_display(bspt->back);Poly_display(bspt->p);B
SP_display(bspt->front);} else
{BSP_display(bspt->front); Poly_display(bspt->p);
BSP_display(bspt->back);
}}
Generating a B
SP Tree
if(polysis empty ) then return N
ULL;
rootp:= first polygon in polys;
for eachpolygon p
in the rest of polysdoifp
is infrontof rootpthen
add pto the front list
else ifpis in the back of rootp
thenadd p to the back list
else split p
into a back poly pband front poly pf
add pfto the front listadd pb
to the back listend_for;bspt->back := B
SP_gentree(backlist);
bspt->front := BSP_gentree(frontlist);
bspt->p = rootp;returnbspt;
3
4, 5b1 2
34
5a
b
1 5a3
1 234
5a
b
1 5a
3
1, 2, 5a 4, 5b
1 234
5a
b
22
5b
4