This document discusses different algorithms for visible surface detection in 3D computer graphics. It describes two main types of algorithms - object space algorithms that determine which parts of objects are visible, and image space algorithms that determine visibility on a per-pixel basis. It then explains four specific algorithms in more detail: back-face elimination, depth buffering, depth sorting, and ray casting. For each algorithm it provides an overview of the approach, terminology, and considerations around performance and implementation. The document concludes by comparing the different algorithms and noting how factors like hardware availability and scene complexity should guide algorithm selection.
Overview of the presentation on hidden surfaces in computer graphics.
Explains visible-surface detection, terms, types of algorithms: object space and image space. Performance factors include coherence in objects and depth.
Discusses algorithms such as back-face elimination and depth-buffer, methods of depth sorting and ray-casting, and their performance metrics.
Visible-Surface Detection 3
Twomain types of algorithms:
Object space: Determine which part of the object
are visible
Image space: Determine per pixel which point of
an object is visible
H&B 16-1:504
Object space Image space
5.
Visible-Surface Detection 4
Visible-surfacedetection = sort for depth
• what and in what order varies
Performance: use coherence
• Objects
• Position in world space
• Position in image space
• Time
H&B 16-1:504
6.
Visible-Surface Detection 5
Fouralgorithms:
• Back-face elimination
• Depth-buffer
• Depth-sorting
• Ray-casting
But there are many other.
H&B 16
7.
Back-face elimination 1
Wecannot see the back-face of solid objects:
Hence, these can be ignored
H&B 16-2:504-505
face
back
:
0
N
V
V
N
8.
Back-face elimination 2
Wecannot see the back-face of solid objects:
Hence, these can be ignored
face
front
:
0
N
V
V
N
H&B 16-2:504-505
9.
Back-face elimination 3
•Object-space method
• Works fine for convex polyhedra: ±50% removed
• Concave or overlapping polyhedra: require additional
processing
• Interior of objects can not be viewed
Partially visible front faces
H&B 16-2:504-505
10.
Depth-Buffer Algorithm 1
•Image-space method
• Aka z-buffer algorithm
xv
yv
zv
Normalized view volume
View plane
pixel
front =
visible
Algorithm:
Draw polygons,
Remember the
color most in front.
H&B 16-3:505-508
11.
Depth-Buffer Algorithm 2
varzbuf: array[N,N] of real; { z-buffer: 0=near, 1=far }
fbuf: array[N,N] of color; { frame-buffer }
For all 1<= i, j <=N do
zbuf[i,j] := 1.0; col[i,j] := BackgroundColour;
For all polygons do { scan conversion }
For all covered pixels (i,j) do
Calculate depth z;
If z < zbuf[i,j] then { closer! }
zbuf[i,j] := z;
fbuf[i,j] := surfacecolor(i,j);
Sorting
H&B 16-3:505-508
12.
polygon
Depth-Buffer Algorithm 3
Fastcalculation z: use coherence.
C
A
y
x
z
y
x
z
C
D
By
x
A
y
x
z
C
D
By
Ax
y
x
z
D
Cz
By
Ax
)
,
(
)
,
1
(
:
Thus
)
1
(
)
,
1
(
:
Also
)
,
(
:
Hence
0
:
Plane
scan line
x
y
x+1
display
C
B
y
x
z
y
x
z
)
1
,
(
)
,
(
:
Also
H&B 16-3:505-508
13.
Depth-Buffer Algorithm 4
+Easy to implement
+ Hardware supported
+ Polygons can be processed in arbitrary order
+ Fast: ~ #polygons, #covered pixels
- Costs memory
- Color calculation sometimes done multiple times
- Transparancy is tricky
H&B 16-3:505-508
14.
Depth-Sorting Algorithm 1
•Image and Object space
• Aka Painter’s algorithm
1. Sort surfaces for depth
2. Draw them back to front
H&B 16-6:511-514
Depth-Sorting Algorithm 3
Apolygon S can be drawn if all remaining polygons S’
satisfy one of the following tests:
1. No overlap of bounding rectangles of S and S’
2. S is completely behind plane of S’
3. S’ is completely in front of plane of S
4. Projections S and S’ do not overlap
H&B 16-6:511-514
17.
Depth-Sorting Algorithm 4
1.No overlap of bounding rectangles of S and S’
xv
zv
display
xv
yv
S
S’
S
S’
H&B 16-6:511-514
18.
Depth-Sorting Algorithm 5
xv
zv
display
xv
yv
2.S is completely behind plane of S’
Substitute all vertices of S in plane equation S’, and
test if the result is always negative.
S
S’
S
S’
H&B 16-6:511-514
19.
Depth-Sorting Algorithm 6
xv
zv
display
xv
yv
3.S’ is completely in front of plane of S
Substitute all vertices of S’ in plane equation of S, and
test if the result is always positive
S
S’
S
S’
H&B 16-6:511-514
Depth-Sorting Algorithm 10
-Tricky to implement
- Polygons have to be known from the start
- Slow: ~ #polygons2
0
1
2
3
0
1
2
3
-1
-0.5
0
0.5
1
0
1
2
3
+ Fine for certain types of objects,
such as plots of z=f(x, y) or
non-intersecting spheres
+ Produces exact boundaries polygons
H&B 16-6:511-514
24.
Ray-casting Algorithm 1
•Image-space method
• Related to depth-buffer, order is different
xv
yv
zv
Normalized view volume
View plane
pixel
front =
visible
Algorithm:
Per pixel:
- Calculate
intersection points
- Determine front one
z
H&B 16-10:518-519
25.
Ray-casting Algorithm 2
Varfbuf: array[N,N] of colour; { frame-buffer }
n : integer; { #intersections }
z : array[MaxIntsec] of real; { intersections }
p : array[MaxIntsec] of object; { corresp. objects }
For all 1<= i, j <=N do { for alle pixels }
For all objects do
Calculate intersections and add these to z and p,
keeping z and p sorted;
if n > 1 then fbuf[i,j] := surfacecolor(p[1], z[1]);
H&B 16-10:518-519
Ray-casting algorithm 4
+Relatively easy to implement
+ For some objects very suitable (for instance
spheres and other quadratic surfaces)
+ Transparency can be dealt with easily
- Objects must be known in advance
- Sloooow: ~ #objects*pixels, little coherence
+ Special case of ray-tracing
H&B 16-10:518-519
28.
Comparison
• Hardware available?Use depth-buffer, possibly in
combination with back-face elimination or depth-sort for
part of scene.
• If not, choose dependent on complexity scene and type
of objects:
– Simple scene, few objects: depth-sort
– Quadratic surfaces: ray-casting
– Otherwise: depth-buffer
• Many additional methods to boost performance (kD-
trees, scene decomposition, etc.)
H&B 16-11:519
29.
glEnable(GL_CULL_FACE); // Turnculling on
glCullFace(mode); // Specify what to cull
mode = GL_FRONT or GL_BACK // GL_BACK is default
Orientation matters! If you want to change the default:
glFrontFace(vertexOrder); // Order of vertices
vertexOrder = GL_CW or // Clockwise
GL_CCW; // Counterclockwise (default)
OpenGL backface removal
H&B 16-11:523-525
1
2
3
4
CCW:
front
4
3
2
1
CW:
back
30.
glEnable(GL_DEPTH_TEST); // Turntesting on
glClear(GL_DEPTH_BUFFER_BIT); // Clear depth-buffer,
// typically once per frame
glDepthFunc(condition); // Change test used
condition: GL_LESS // Closer: visible (default)
GL_GREATER // Farther: visible
Note: range between znear and zfar is mapped to [0,1], using one or
two bytes precision. If zfar has an unnecessarily high value,
you loose precision, and artifacts can appear.
OpenGL depth-buffer functions
H&B 16-11:523-525
31.
Next…
• We knowhow to determine what is visible,
and we looked at illumination and shading.
• Next: Let’s consider more advanced shading.