3dcg08 05vsd
3dcg08 05vsd
Bing-Yu Chen
National Taiwan University
Visible-Surface Determination
Back-Face Culling
The Depth-Sort Algorithm
Binary Space-Partitioning Trees
The z-Buffer Algorithm
Scan-Line Algorithm
Visible-Surface Ray Tracing
(Ray Casting)
Warnock’s Algorithm
Visible-Surface Determination
= Hidden Surface Removal
Determining what to render at each pixel.
A point is visible if there exists a direct line-of-sight to
it, unobstructed by another other objects (visible
surface determination).
Moreover, some objects may be invisible because
there are behind the camera, outside of the field-of-
view, too far away (clipping) or back faced (back-
face culling).
Hidden Surfaces: why care?
Occlusion: Closer (opaque) objects
along same viewing ray obscure more
distant ones.
Reasons for removal
Efficiency: As with clipping, avoid
wasting work on invisible
objects.
Correctness: The image will look wrong if
we don’t model occlusion properly.
Back-Face Culling = Front Facing
x
B
C
D
E
F
H
z G
Back-Face Culling = Front Facing
use cross-product to get the normal
of the face (not the actual normal)
use inner-product to check the
facing
v1
N (v2 v1 ) (v3 v1 )
N
v2
V v3
Clipping (View Frustum Culling)
view frustum
occlusion
back
eye
face
view frustum
List-Priority Algorithms
The Painter’s Algorithm
The Depth-Sort Algorithm
Binary Space-Partitioning Trees
The Painter’s
Algorithm
Draw primitives
from back to front
need for depth
comparisons.
The Painter’s
Algorithm
for the planes with constant z
not for real 3D, just for 2½D
y y
x
R
Q P
P Q
P Q
z x x
Overlap Detection
Do the polygons’x not overlap?
Do the polygons’y not overlap?
Is P entirely on the opposite side of
Q’s plane from the viewpoint?
Is Q entirely on the same side of
P’s plane as the viewpoint?
Do the projections of the polygons
onto the (x,y) plane not overlap?
Binary Space-Partitioning Trees
An improved painter’s
algorithm f(p)>0
Key observation:
f(p)<0
T3
T1
T5
T2
Ax+By+Cz+D=0
T4 f(p): n(p-a)=0
Binary Space-Partitioning Trees
T1
- +
T2 T3
T1
T3
T2
Binary Space-Partitioning Trees
T1
- +
T2
- +
T1
T3
T3
T2
Splitting triangles
a a
A A
c c
B B
b b
BSP Tree Construction
BSPtree makeBSP(L: list of polygons) {
if (L is empty) {
return the empty tree;
}
Choose a polygon P from L to serve as root;
Split all polygons in L according to P
return new TreeNode (
P,
makeBSP(polygons on negative side of P),
makeBSP(polygons on positive side of P))
}
Splitting polygons is expensive! It helps to choose P wisely
at each step.
Example: choose five candidates, keep the one that splits
the fewest polygons.
BSP Tree Display
void showBSP(v: Viewer, T: BSPtree) {
if (T is empty) return;
P = root of T;
if (viewer is in front of P)
{ showBSP(back subtree of
T); draw P;
showBSP(front subtree of T);
} else {
showBSP(front subtree of T);
draw P;
showBSP(back subtree of T);
}
} 2D BSP demo
Binary Space-Partitioning Trees
P1 P1
A P2 front back
1 P2 P2
back
3 front front back
D B
2
D C A B
3,1, 3,2,1 1,2,3
C
2 2,1,3
9
7 10 8
1
11
2
4
3
BSP Tree
6 5 1
inside outside
9
ones ones
7 10 8
1
11
2
4
3
BSP Tree
6 5 1
2 5
9 3 6
4 7
7 10 8
8
1 9
10
11
11
2
4
3
BSP Tree
6 1
5
5
9 9b
6 8
7 10 11 7 9b
8
9a b
1 9a 11
10 b
11
11 11
2 a a
4
3
BSP Tree
6 5
1
9 9b 2 5
7 10 11 3 6 8
8
9a b
1
11 4 7 9b
11
2 a
4 9a 11
b
3 10
11a
BSP Tree
6 5
1
9 9b 2 5
7 10 11 3 6 8
8
9a b
1
11 4 7 9b
11
2 a
4 9a 11
point b
3 10
11
a
BSP Tree Traversal
6 5
1
9 9b 2 5
7 10 11 3 6 8
8
9a b
1
11 4 7 9b
11
2 a
4 9a 11
point b
3 10
11
a
BSP Tree Traversal
6 5
1
9 9b 2 5
7 10 11 3 6 8
8
9a b
1
11 4 7 9b
11
2 a
4 9a 11
point b
3 10
11
a
The z-Buffer Algorithm
Resolve depths at the pixel level
Idea: add Z to frame buffer,
when a pixel is drawn, check
whether it is closer than what’s
already in the frame buffer
The z-Buffer Algorithm
+ =
+ =
The z-Buffer Algorithm
void zBuffer() {
int pz;
for (each polygon) {
for (each pixel in polygon’s projection) {
pz=polygon’s z-value at (x,y);
if (pz>=ReadZ(x,y)) {
WriteZ(x,y,pz);
WritePixel(x,y,color);
}
}
}
}
The z-Buffer Algorithm
y
y y
z1
y1 za z1 (z1 z2 ) y11 y2s
za zp zb Scan line y1 y s
ys zb z1 (z1 z3 )
y2 y1 y
z2
3
xb x p
z p zb (zb z a )
y3 z3 xb
xa
z-Buffer: Example
B
γ+2
γ+1
γ D
C
β
F
α
A
x
Scan-Line Algorithm
AET contents
ET entry x Δx ID α AB AC
ymax
β ABAC FD FE
γ,γ+1 ABDE CB FE
ET = edge table
PT = polygon table
AET = active-edge table
General Scan-Line Algorithm
add surfaces to polygon table (PT);
initialize active-edge table (AET);
Center of
projection
Window
Ray Casting (Appel, 1968)
Ray Casting (Appel, 1968)
Ray Casting (Appel, 1968)
Ray Casting (Appel, 1968)
n
Li N
nls
k a Ia i I d k i s
k R V
i1
Ray Casting (Appel, 1968)
direct illumination
Spatial Partitioning
Spatial Partitioning
A B
Spatial Partitioning
B
3 2
Space Subdivision
Approaches
Quadrant Numbering
Quadtree Data Structure
Quadrant Numbering
Quadtree Data Structure
Quadrant Numbering
Quadtree Data Structure
Quadrant Numbering
From Quadtree to Octree
y
z
K-d Tree
A
A
K-d Tree
C A
C
B
A
K-d Tree
C A
D B
C
B
A
K-d Tree
C A
D B
C
B
D
A
K-d Tree
A
D B
B C C
D B
B C C
Surrounding
polygon
Intersecting
polygon
Surrounding
polygon
z z
Performance of Four Algorithms
for Visible-Surface Determination
Number of Polygons
Algorithm
100 2,500 60,000
z-buffer 54 54 54