Professional Documents
Culture Documents
One way to achieve this is to first sort all polygons in order of depth away
from the viewpoint. The polygons are then rendered (displayed) in order of
decreasing depth. Thus, the polygons closest to the viewpoint will be
drawn later and therefore obscure more distant polygons. This is known as
the painters algorithm.
Engineering 4892:
Data Structures
Faculty of Engineering & Applied Science
Memorial University of Newfoundland
Here the distant mountaints are painted first, then closer features of the
landscape, and finally the nearby trees.
ENGI 4892 (MUN)
Unit 6, Part 3
1 / 13
Ordering
Note: It is also possible to render in closest first order. This can be done if
we render on a pixel-by-pixel basis, as opposed to an object-by-object basis.
Another big issue is the cost of the sort. No general-purpose sorting
algorithm can do better than O(n lg n) in the worst case. If we are trying
to render a scene with tens of 1000s of polygons the cost of sorting them
all can be significant. Further, this sorting must be done every time the
viewpoint changes!
Several solutions to this problem have been proposed. One is the z-buffer
algorithm. Another is binary space partition trees...
In this case, these polygons must be split into pieces to allow them to be
displayed correctly.
Unit 6, Part 3
3 / 13
Unit 6, Part 3
4 / 13
Consider the following 2-D example (we have line segments instead of
polygons),
Unit 6, Part 3
Line segment 5 is the root of this BSP tree. All other segments are behind
segment 5. Thus, the 5 node has descendants only on its back side. Similarly,
the 4 node has only back descendants. The line of the 3 segment splits the
space again into two halves (although if there had been segments in front of 4 or
5, those segments would already have been adopted and would not be available
as descendants of the 3 node). This process continues recursively until all
segments have been incorporated.
5 / 13
If another segment was chosen as the root node, the results could be quite
different,
A BSP tree can be traversed to visit its nodes in order of increasing depth
from the viewpoint. A modified inorder traversal is required.
Consider the root of a BSP tree. It divides the set of polygons into three
sets:
Unit 6, Part 3
7 / 13
The cost of this traversal is O(n). Compare this with the O(n lg n) sort
required by the painters algorithm. If the environment is static the cost of
building the tree (see below) is a one-time cost. Thereafter, every time the
viewpoint changes the BSP tree has to be traversed once to produce the
correct order of polygons to display.
root->front = buildSubtree(frontList)
root->back = buildSubtree(backList)
return root
}