The Depth-Sort Algorithm

As was mentioned before, the painter's algorithm is a simplified version of the depth-sort algorithm. In the depth-sort algorithm, there are 3 steps that are performed:

Step 1:Sort objects from near to far (smallest to largest z coordinate).
Step 2:Resolve ambiguities, rearranging the object list and splitting faces as necessary.
Step 3:Render each face on the rearranged list in ascending order of smallest z coordinate (from back to front).

Resolving ambiguities in Step 2 is what replaces simple rendering of the whole face of the object. Let the object farthest away be called P. Before this object is rendered, it must be tested against other objects which we will call Q, where the z extent of q overlaps the z extent of P. This is done to prove that P cannot obscure Q and that P can therefore be written before Q. Up to 5 tests are performed, in order of increasing complexity (Taken from Foley, Van Dam, Feiner, Hughes, Phillips. Introduction to Computer Graphics Addison-Wesley, New York, c.1994):

1. Do the polygons' x extents not overlap?
2. Do the polygons' y extents not overlap?
3. Is P entirely on the opposite side of Q's plane from the viewpoint?
4. Is Q entirely on the same side of P's plane as the viewpoint?
5. Do the projections of the polygons onto the (x,y) plane not overlap? (This can be determined by comparing the edges of one object to the edges of another).

If all 5 tests fail, the assumption is that P obscures Q. So we test whether Q could be rendered before P. Tests 3 and 4 are performed again, with the polygons reversed:

3'. Is Q entirely on the opposite side of P's plane from the viewpoint?
4'. Is P entirely on the same side of Q's plane as the viewpoint?

The next picture is a top down view, relative to th eviewpoint, of objects P and Q.

In this case, test 3' succeeds. So, we move Q to the end of the list of objects, and the old Q becomes the new P.

The next picture is a front view. The tests are inconclusive: The objects intersect each other.

No matter which is P or Q, there is no order in which P and Q could be rendered correctly. Instead, either P or Q must be split by the plane of the other. The original unsplit object is discarded, its pieces are inserted in the list in proper order, and the algorithm proceeds as before. The following picture shows an example of splitting: (taken from http://www.cs.unc.edu/~stewart/comp236/hidden-surface2.html)

The next picture shows a more subtle case: it is possible to move each object to the end of the list to place in the correct order relative to one, but not both, of the other objects.

This would result in an infinite loop. To avoid looping, the object that moves to the end of the list are marked. If the first 5 tests fail and the current Q is marked, tests 3' and 4' are not performed. Instead, one of the objects is split, as if tests 3' and 4' had both failed, and the pieces are reinserted into the list.