You are on page 1of 4

The painters algorithm

It was mentioned earlier that sorting algorithms were useful in computer


graphics. If we have a number of polygons to display we must make sure
that those nearest to the viewpoint obscure those which are further away.

Unit 6: BSP Trees

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

July 11, 2011

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

July 11, 2011

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!

One issue with the painters algorithm arises if polygons overlap or


intersect,

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.

ENGI 4892 (MUN)

Unit 6, Part 3

July 11, 2011

3 / 13

ENGI 4892 (MUN)

Unit 6, Part 3

July 11, 2011

4 / 13

Consider the following 2-D example (we have line segments instead of
polygons),

Binary space partition trees


Binary Space Partition (BSP) Trees are binary trees which allow a set of
polygons or other objects to be displayed in order of depth from the
viewpoint. BSP trees are quite similar to binary search trees. However,
they define order in a very different way.
Assume we have a set of polygons to display. Each polygon has a normal
vector which is perpendicular to the plane of the polygon. The normal
vector defines the front and back of the polygon.
A BSP tree takes one polygon as its root. All remaining polygons are
divided into two sets by the plane of the root polygon. One set lies in
front of the root plane, the other set lies on back of the root plane. To
form the rest of the tree, a polygon from the front set is chosen as the
root of the front subtree and similarly for the back subtree.

ENGI 4892 (MUN)

Unit 6, Part 3

July 11, 2011

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

July 11, 2011

All polygons in front of the root polygon

The root polygon itself

All polygons behind the root polygon

If the viewpoint is in front of the polygon we should display these three


sets in the following order: 3, 2, 1

Choosing segment 3 as the root requires that segment 5 be split into


segments 5a and 5b. This is undesirable because it increases the total
number of polygons to be displayed, and also increases the cost of tree
traversal (below). However, this may be unavoidable.

ENGI 4892 (MUN)

If the viewpoint is behind the polygon we should display them in this


order: 1, 2, 3
If the viewpoint is co-planar with the polygon, either order will do.

7 / 13

How do we display the polygons of the two subsets? Recursively, in the


order given above with the root node now being the root of each of the
subtrees, in turn. The following is an example traversal. The viewpoint is
shown as an eye:

Another traversal example for a different viewpoint:

Note that knowledge of the direction of view is not necessary.

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.

Node *buildSubtree( List polyList ) {


if ( polyList is empty )
return NULL
else {
rootPoly = first item in polyList
remove first item in polyList
root = new Node(rootPoly)
create Lists backList and frontList
for ( each remaining polygon p in polyList ) {
if ( p is in front of rootPoly )
add p to frontList
else if ( p is in back of rootPoly )
add p to backList
else {
split p into frontPart and backPart
add frontPart to frontList
add backPart to backList
}
}

Building a BSP tree:


Each BSP tree node has three fields: polygon, front, back. With the later
two being pointers to nodes. The following is the pseudocode for building
any subtree of a BSP tree...

root->front = buildSubtree(frontList)
root->back = buildSubtree(backList)
return root
}

You might also like