Hidden Surface EliminationHidden Surface Elimination
Wen-Chieh (Steve) Lin
Institute of Multimedia Engineering
I-Chen Lin’ CG Slides, Rich Riesenfeld’s CG Slides,
Shirley, Fundamentals of Computer Graphics, Chap 8
ILE5014 Computer Graphics 10F 3
Hidden Surface Elimination (Removal)Hidden Surface Elimination (Removal)
• In a 3D wireframe displayer, the line segments between projected point pairs are drawn.
• To fill projected polygons, we have to remove “hidden surfaces”.
ILE5014 Computer Graphics 10F 4
Hidden Surface Elimination (Removal)Hidden Surface Elimination (Removal)
• Object (Model) Space Algorithms
– Work in the model data space
• Image Space Algorithms
– Work in the projected space
– Most common domain for hidden surface removal
ILE5014 Computer Graphics 10F 5
Hidden Surface RemovalHidden Surface Removal
• Object-space approach: use pairwise testing between polygons (objects)
• Worst case complexity O(n2) for n polygons
ILE5014 Computer Graphics 10F 6
Backface CullingBackface Culling
View
ILE5014 Computer Graphics 10F 7
Back Face Culling: Object SpaceBack Face Culling: Object Space
nn
n
n
v
v
v
v
z
ILE5014 Computer Graphics 10F 8
Back Face Culling: Object SpaceBack Face Culling: Object Space
zn n
v
v
z
0nvVisible:
ILE5014 Computer Graphics 10F 9
Back Face Culling TestBack Face Culling Test
• For Object Space look at sign of
• For Image Space look at sign ofz
n
nv
ILE5014 Computer Graphics 10F 10
Back Face Culling: Image SpaceBack Face Culling: Image Space
nn
n
n
z
ILE5014 Computer Graphics 10F 11
Back Face Culling: Image SpaceBack Face Culling: Image Space
n n
z
0z
nVisible:
ILE5014 Computer Graphics 10F 12
Painter’s AlgorithmPainter’s Algorithm
• Render polygons in a back to front order so that polygons behind others are simply painted over
• Depth sort method
ILE5014 Computer Graphics 10F 13
Depth Sort MethodDepth Sort Method
• Requires ordering of polygons first
– O(n log n) calculation for ordering
– Not every polygon is either in front or behind all other polygons
• Sort polygons and deal with easy cases first, harder later
ILE5014 Computer Graphics 10F 14
Easy CasesEasy Cases
• A polygon lies behind all other polygons
– Can render
• Polygons overlap in z but not in either x or y
– Can render independently
z
z
ILE5014 Computer Graphics 10F 15
Difficult CasesDifficult Cases
cyclic overlap penetration
ILE5014 Computer Graphics 10F 16
Space PartitioningSpace Partitioning
• Avoid rendering an object when it’s unnecessary
– In many real-time applications, we want to eliminate as many objects as possible within the application.
– Reduce burden on pipeline
– Reduce traffic on bus
• Octree
• BSP tree
ILE5014 Computer Graphics 10F 17
OctreeOctree
http://www.imagico.de/fast_iso/patch.html
ILE5014 Computer Graphics 10F 18
Why do we use BSP trees?Why do we use BSP trees?
• Hidden surface removal
– A back-to-front painter’s algorithm
• Partition space with Binary Spatial Partition (BSP) Tree
ILE5014 Computer Graphics 10F 19
A Simple ExampleA Simple Example
top view
The plane of A separates B and C from D, E and F
ILE5014 Computer Graphics 10F 20
Binary Space Partitioning TreeBinary Space Partitioning Tree
• Can continue recursively
– Plane of A separates C from D
– Plane of D separates E and F
• Can put this information in a BSP tree
– Use for visibility and occlusion testing
ILE5014 Computer Graphics 10F 21
Key Ideas of BSPKey Ideas of BSP
• Assume T2 not cross the plane of T1
• If e and T2 on the same side, T1 won’t block
• If e and T2 on different sides, T1 may block
ILE5014 Computer Graphics 10F 22
What if a triangle cross a plane of another triangle?What if a triangle cross a plane of another triangle?
• Break the triangle into three!
ILE5014 Computer Graphics 10F 23
Creating a BSP treeCreating a BSP tree
0
0
11
2
2
33
4 -> 4f,4b
4f
4b 4f 4b5f
5b
5f
5b
ILE5014 Computer Graphics 10F 24
Back-to-Front RenderBack-to-Front Render
Render(node, view){
if node is a leaf
draw this node to the screenelse
if the viewpoint is in back of the dividing line {
render(front subtree) draw node to screen render(back subtree) } else the viewpoint is in front of the dividing line {
render (back subtree) draw node to screen render (front subtree) }
ILE5014 Computer Graphics 10F 25
0
0
11
2
2
33
4f
4b 4f 4b5f
5b
5f
5b
5b 1 4f 3 5f 4b 0 2
Back-to-Front RenderBack-to-Front Render
ILE5014 Computer Graphics 10F 26
0
0
11
2
2
33
4f
4b 4f 4b5f
5b
5f
5b
2 0 5b 1 4f 3 5f 4b
Back-to-Front RenderBack-to-Front Render
ILE5014 Computer Graphics 10F 27
Optimizing BSP TreeOptimizing BSP Tree
• Efficiency of tree traversal is more important than tree construction
• Speed determined by the number of nodes
• Fewer nodes if less node splitting
• Order of adding triangles determines the number of nodes in a BSP
• Choose a better ordering from a random collection of permutations
2
1b1f
1
2
ILE5014 Computer Graphics 10F 28
BSP-based CullingBSP-based Culling
• Pervasively used in first person shooting games.
– Doom, quake….etc.
• Visibility test
• Skip objects that are “occluded”.a screen shot from Doom.
ILE5014 Computer Graphics 10F 29
Image Space ApproachImage Space Approach
ILE5014 Computer Graphics 10F 30
Image Space ApproachImage Space Approach
• Look at each projector (nm for an n m frame buffer) and find the k closest polygons
• Complexity O(nmk)
• z-buffer
• Ray casting
ILE5014 Computer Graphics 10F 31
z-Buffer Algorithmz-Buffer Algorithm
• The z or depth buffer
– stores the depth of the closest object at each pixel found so far
• As we render each polygon, compare the depth of each pixel to depth in z buffer
– If less, place the shade of pixel in the color buffer and update z buffer
ILE5014 Computer Graphics 10F 32
z-Buffer Algorithmz-Buffer Algorithm
Function setpixel(int i, int j, rgb c, real z)
If z > z-buffer(i, j) then
z-buffer(i, j) = z
screen(i,j) = c
ILE5014 Computer Graphics 10F 33
A
C
BA’
C’ B
’
focus
Projection plane
Interpolation of Z valuesInterpolation of Z values
ILE5014 Computer Graphics 10F 34
• To fill the polygon on the screen, we only fill the color and estimate the z value “pixel by pixel”.
• How to estimate z of in-between pixels ?
z1
z2
z3
za zbzt
Interpolation of Z valuesInterpolation of Z values
ILE5014 Computer Graphics 10F 35
• Interpolation in screen space
– P(m) = P1 + m(P2 – P1)
• Interpolation in 3D space
– V(n) = V1 + n(V2 – V1)
– Py(n) / f = Vy(n) / Vz(n) P1
P2
V1
V2
m
n 212
1
zzmz
mzn
n ≠ m
Screen Space vs. 3D SpaceScreen Space vs. 3D Space
f
ILE5014 Computer Graphics 10F 36
Suppose plane equation for P is
0 DzCyBxA
yBxADC
z 1
Solving for z,
Scan Conversion of Polygon PScan Conversion of Polygon P
ILE5014 Computer Graphics 10F 37
y
x
1z 2
z
Scan Conversion of Polygon P (2)Scan Conversion of Polygon P (2)
ILE5014 Computer Graphics 10F 38
1 111z D Ax ByC
is first z-value on scanline,1
z
Scan Conversion of Polygon P (3)Scan Conversion of Polygon P (3)
1 121 ( )z D A x x ByC
Consider , next z-value on scanline,2z
),(112
yxxz
ILE5014 Computer Graphics 10F 39
1 121 ( )z D A x x ByC
2 1A
Cz z x
In incremental terms,
1 111z D Ax ByC
Scan Conversion of Polygon P (4)Scan Conversion of Polygon P (4)
ILE5014 Computer Graphics 10F 40
Kzz 12
Assume ,1
x
And note ,KC
A
Therefore,
One add (subtract) generates the next iz
Scan Conversion of Polygon P (5)Scan Conversion of Polygon P (5)
ILE5014 Computer Graphics 10F 41
Incremental ApproachIncremental Approach
• Can efficiently generate z-values incrementally
• Can use Bresenham to grow the edges
efficiently after direct evaluation at vertices
ILE5014 Computer Graphics 10F 42
Integer Z-bufferInteger Z-buffer
• If B bins are used for depth range (n, f)
• Bin size △z = (n-f)/B
• Smaller △z is desirable
– Depth of two objects are indistinguishable if △z is larger than the difference between their real z-values
– Increase B
– Move n and f closer
ILE5014 Computer Graphics 10F 43
Bin size for z-buffer in world spaceBin size for z-buffer in world space
• Take perspective into account
• Differentiating both side
• Bin sizes vary in depth
• The bin size in world space is
wzfnfnz
ww
zz
fnz 2
zfnzz w
w
2
ILE5014 Computer Graphics 10F 45
Bin size for z-buffer in world spaceBin size for z-buffer in world space
• Bin size in the world space increases as depth increases
• Objects farther away is more likely to be indistinguishable in depth (same integer z-values)
• Maximize n and minimize f for small △zw
zfnzz w
w
2
nzfz
w max
zw=f