CS 376Introduction to Computer Graphics
03 / 21 / 2007
Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Today’s Topics• Questions?
• Visible Surface determination– BSP trees– Depth Sort– Scanline algorithm
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Programming assignment 3• Will deal with displaying perspective projected objects.• I will provide a file format for object descriptions and code to read the
object descriptions from a file.• I will also provide a file format for viewing parameters and code to read
the viewing parameters from a file.• You will be required to display the object for an arbitrary view (specified
by the viewing parameters) and perform backface culling.• You will also provide a way (through buttons) for the user to change the
viewing parameters and based on the changes, update the display.• A more detailed description with code that you can use will be provided
hopefully by tomorrow.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Visible Surface Determination• The visible surface determination algorithms we'll discuss are:
• Back Face Culling (removal)• Depth Buffer method (aka z-buffer)
– A-Buffer method• BSP Tree• Depth Sort• Scanline Algorithm
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
List priority algorithms• List priority algorithms
– determine a visibility ordering for objects so that a correct picture results if objects are rendered in that order
– further objects are occluded by nearer ones– if parts of an object overlap in z (depth) then either
• a) we can determine a correct order OR• b) we have to split objects so that they don't
overlap in z
• Two list priority algorithms are– BSP Trees– Depth Sorting
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• BSP = Binary Space Partition Tree
– This is a way to sort polygons and once they are sorted, we can process the tree for the polygons that are visible given an arbitrary viewpoint.
• To generate (this is the preprocessing step) the BSP Tree given objects
(polygons) in the world and which side of each polygon is front facing
we do the following:– Choose any polygon P.– Compute the plane equation that polygon P is on.– Test all the vertices of all the other polygons to determine whether
the polygons are in front of, behind, or on the same plane.– If the plane intersects a polygon, split it into two polygons at the
intersection of the plane and the polygon.– Place the polygons into a binary search tree with P as the root and
the polygons that live in front of it to the left and the polygons that live behind it to the right. What's a binary search tree?
– Recursively call this method on the left and right subtrees.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• Let's do an example.
• See the handwritten handout.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• Does anyone recall how to do an inorder traversal of a binary tree?
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• To process the BSP Tree given a view point, we do a modified inorder
traversal of the tree. We want further polygons to get written first and then overwritten by nearer ones if need be.
• If the view point is behind the root, show the polygons in the root's front then show the root then show the polygons in the root's back. Do this recursively. That is, do LEFT ROOT RIGHT
• If the view point is in front of the root, show the polygons in the root's back then show the root then show the polygons in the root's front. That is, do RIGHT ROOT LEFT
• That's the “modified” part of the inorder traversal. An inorder traversal processes LEFT, ROOT, RIGHT always.
• The “show the polygons in the root's front” and “show the polygons in the root's back” are recursive calls to the inorder traversal with the left or right subtree.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• Let's continue to look at the handout that contains two viewpoints and
how we would traverse the BSP tree.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• Alternatively we can process the polygons in the opposite way (that is, if
it was front to back, then we do back to front, or vice versa) and only write pixels if they haven't been written already.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• A note about splitting polygons.
– Triangles are often the polygon of choice for polygon meshes (where each edge is shared by at most two polygons. The polygons share edges with other polygons within the mesh to approximate a curved object.)
– If all of the polygons are triangles, then when we split a triangle we wish to split it into triangles, not arbitrary polygons (e.g. A triangle and a quadrilateral (4 sided polygon).)
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
BSP Trees• Evaluation of BSP Trees
– initial time and space of the preprocessing step is high (expensive)– simple linear display algorithm whenever new view is requested– if objects in world stay stationary and the viewpoint is the only
change, the BSP Trees are a very efficient way to do visible surface determination
– the polygon chosen as root can have a large effect on performance. Ideally we want the fewest splits of polygons (after all is said and done) to occur.
• A heuristic that works pretty well is to at each step pick a polygon that splits the fewest of it's children. It will most likely be impractical to test each polygon at each step --- it has been shown to work well if we randomly pick a few and test those --- then finally choose the one that splits the least children among them.
– Performs intersecting and sorting at object precision.– Performs splitting of polygons only during the preprocessing step, it
is expensive, but this preprocessing only needs to be done if the world changes (objects move.)
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• A simplified version of depth sorting is the “painter's algorithm”. The
painter's algorithm works when no polygons overlap each other in depth.
• For instance assuming we are viewing along the z axis, if polygon P1 has
a depth range of z1min
to z1max
and polygon P2 has a depth range of z
2min to
z2max
and z1min
> z2max
or z2min
> z1max
then their z “extents” do not overlap. If that is true for all polygons then no polygons overlap each other in depth and the painter's algorithm works fine.
• The painter's algorithm simply sorts the polygons according to smallest z coordinate of each polygon and paints the polygons in order from the smallest z to the largest (furthest to nearest). Nearer polygons are painted over the top of further ones, just like a painter might do it.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• Before we talk about Depth Sorting, let's discuss what is meant by the
term extents of a polygon – bounding boxes – draw the smallest rectangle around the polygon with edges parallel to two coordinate axes.
• Examples on the board including 3 cases where the z extents overlap.
• Depth Sorting is another way to perform visible surface detection.– The basic idea is to display the polygons in the frame buffer in order
of decreasing distance from the viewpoint.• sort polygons in order of farthest z coordinate (smallest z since
we're viewing down the -z axis)• if z extents overlap then split polygons if necessary (not always
necessary) so that there are no ambiguities.• draw each polygon in ascending order of smallest z coordinate
(back to front)
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• Resolving ambiguities is the hard part of the algorithm. Before drawing
a polygon P, it must be tested against all other polygons Q whose z extent overlaps P's z extents. This is to prove that P can be written before Q.
• Up to five tests are performed in order. If a test succeeds then P definitely does not obscure Q and therefore can be written before Q.
• If all Q's pass these tests against P then P is written and the next polygon
on the list is the next to be tested. The 5 tests are:– 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 (not their extents) onto the (x,y)
view plane not overlap?
• What are these tests really saying?
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• What are these tests really saying?
• If all Q's pass these tests against P then P is written and the next polygon
on the list is the next to be tested. The 5 tests are:– 1. Do the polygons' x extents not overlap?
• If YES (that is, their x's don't overlap), then this means that since they don't overlap in x, then drawing P before Q is ok.
– 2. Do the polygons' y extents not overlap?• If YES, then this means that since they don't overlap in y, then
drawing P before Q is ok.– 3. Is P entirely on the opposite side of Q's plane from the viewpoint?
• If YES, then this means that since P is fully behind Q's plane in relation to viewpoint, then drawing P before Q is ok.
– 4. Is Q entirely on the same side of P's plane as the viewpoint?• If YES, then this means that since Q is fully in front of P's plane
in relation to viewpoint, then drawing P before Q is ok.– 5. Do the projections of the polygons (not their extents) onto the (x,y)
view plane not overlap? (see figure 9-16 in text a) YES, b) NO)• If YES (that is, their projections don't overlap), then this means
that since the polygons themselves don't overlap (only their extents did), then drawing P before Q is ok.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• If all 5 tests fail, then interchange P and Q and perform only tests 3 and 4
again (call these 3' and 4'). (No need to perform 1,2, and 5, because these
will yield the same answer as before.)
– 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?
• If either succeeds then Q does not obscure P and Q becomes the new P
which is tested against all the other polygons that overlap it in z. (** make sure no infinite loop of this procedure --- see a couple of
slides forward **.)
• If they both fail too, then P or Q will be split by the plane of the other
and the original unsplit polygon is discarded and the pieces are placed
within the sorted list in proper z order and the algorithm continues.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• It is possible that two polygons can fail all 5 tests and then fail the other
2 tests and still be able to be drawn correctly without splitting.
• However, the depth sorting algorithm would split them (unnecessarily).
• An example showing this is in the handout.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Depth Sorting• To make sure we don't get stuck in an infinite loop of reordering
polygons, flag a polygon the first time it is reordered. Then if an attempt
it made to reorder it again, split the polygon appropriately.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Scanline Algorithm• The Scanline algorithm for visible surface determination is an image
precision algorithm. It is an extension to the scan conversion algorithm
we discussed for filling a polygon. But now, we need to deal with a set
of polygons, not just one.
• The Scanline algorithm processes one scanline of the frame buffer at a
time.
• One assumption we are making (which can be overcome with
modifications to the algorithm) is that no polygons penetrate each other.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Scanline Algorithm• 1) Create an edge table for all edges that are not horizontal (ignore those) that
are projected onto the view plane.
• 2) Given the x,y,z coordinates of the 2 endpoints of the edge,– Sort the edges in the edge table according to the smaller y (the y of the
lower endpoint) of its two endpoints and then within those with the same smaller y, sort by increasing x of the lower endpoint.
• 3) Each entry in the edge table contains the following information about each
edge– a) x coordinate of the endpoint with smaller y– b) y coordinate of the other endpoint– c) x increment – the value to increment x when moving from one scanline to
the next– d) polygon ID
• 4) A polygon table, keyed on polygon ID contains coefficients of the plane that
polygon is on, the color info for it, and a boolean in-out flag used during the
algorithm.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Scanline Algorithm• 5) An Active Edge Table is also stored during the running of the algorithm. For
the current scanline, the active edge table contains the edges, in order by
increasing x, whose projections onto the view plane intersect with the current
scanline.
• Pseudocode for this algorithm:– Initialize frame buffer to background color– Add edges to edge table– Add polygons to polygon table– For (each scanline)
• Update active edge table • Starting at the edge with the lowest x• For (each pixel in scanline)
– Determine if went in or out of any polygons (set appropriate in-out flags)
– Test in-out flags, if “in” only one polygon, draw that color– If not “in” any polygons do nothing– If “in” multiple polygons then determine which polygon is closest
(make depth calculations based on plane coefficients) and draw that color
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Scanline Algorithm• Modifications can be made for efficiency by exploiting different coherence
properties.
• To reduce depth computations -– Within a scanline, the same polygon influences the color of the pixels
between two edges, regardless of how many polygons we are “in”, so depth computations are only necessary when entering a polygon or leaving a polygon.
– Assuming we have two or more overlapping (in z) polygons and as we scan across the scan line we leave (go past right edge of) one of the polygons. If the polygon we just left was just obscured then we don't need to compute the depth among the other polygons, we can just draw the color of the polygon that was closest (because it must still be closest). Only when leaving a closest polygon are depth calculations necessary.
• Example on the board.– Also if the same edges are active and in the same order from one scanline to
the next, no depth computations are necessary because no depth relationship changes have occurred. Just draw the same colors in the same order (but possibly a different number of pixels each.)
• Example on the board.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Scanline Algorithm• This algorithm, although specified as working only for polygons can be
extended to work for arbitrary (curved) surfaces. This is accomplished
(partially) by replacing the edge table and active edge table with a surface table
and an active surface table.