[WPI] [cs4731] [Classes]

cs4731, D96/97 Class 21

Clipping in 4D

See the appended page.

z-Buffering

Z buffering is a method for figuring out what color to paint each pixel in the digital image, without doing clipping. It definitely fits into the category of "slow but effective". The basic algorithm is this:

Step 1 - create and initialize a z-buffer;
for (each digital_image_x,digital_image_y coordinate)
   Step 2 - find the line in viewport x,y,z coordinates that passes through the point.
   for (eacy polygon in the scene)
      {
      Step 3 - calculate the polygon normal;
      Step 4 - backface cull (ignore the polygon if its normal points away);
      Step 5 - if ((the line intersects the polygon) &&
      Step 6 -    (it's closer to the viewer than the last intersection))
      Step 7 -     replace the z-buffer by the new intersection z coordinate and replace the pixel color;
      }

Step 1: Create and initialize a z-buffer

Make a z-buffer, an array of floats exactly the same size as the digital image. Here we will show both as 2D arrays, although 1D arrays are often used. Set each pixel in the digital image to the background color and each element in the z-buffer to a very large value, larger than the largest z value in any polygon in the scene. Good values to use are +FLT_MAX or -FLT_MAX, depending on which sign convention you use for z (left- or right-handed coordinates). If we use the convention that z is away from the viewer, then set the z-buffer values to +FLT_MAX.

Step 2: Find the corresponding line in viewport coordinates

For each pixel in the digital image, which will have 2D int coordinates, find the corresponding 3D coordinate in the float viewport coordinates (x, y ,0) by inverting the transforms from Class 3.

Turn that coordinate into a 3D line directed along the z-axis by using the point-vector method described in the vector notes. The coordinates of any point on that line will be (x, y , t) where t is a parameter that can have any float value. It's value is 0 where the line intersects the viewplane.

Step 3: Find the normal to a polygon

The easiest way to do this is to use the Newell method described in Class 14. Don't forget to normalize the normal.

Step 4: Backface Cull

Look at the z coordinate of the normal. If it is pointing away from the viewer, ignore it -- which usually means continuing to the end of the polygon loop. This means examining the sign of . In our example, we keep polygons for which the sign is negative and ignore those with powitive signs.

Step 5: Find where the line intersects the polygon

This is done in two steps. First, find where it intersects the plane of the polygon. Then, find whether that intersection is inside the polygon.

 

Step 5a: Find where the line intersects the plane of the polygon

Use the line-plane intersection method to solve for the intersection point. That is, find the value of t which puts the point in the plane. Any point in the plane can be written in point-normal form:

In this equation, is any point known to be in the plane -- just use any of the polygon vertices. Solving this equation for t -- the intersection is at:

Note 1: If the polygon surface is parallel to the z-axis, the denominator in the equation for t is zero and there is no intersection. However, such a polygon should already have been eliminated by backface culling.

Note 2: If the value of t has the wrong sign (using the sign convention of this example, that is when the value of t is negative), then the polygon face is behind the viewer and invisible. Ignore the intersection and go on to the next polygon

Step 5a: Find whether that intersection point is inside the polygon

Loop through the points in the polygon testing whether the P calculated above inside the polygon; we already know it is inside the plane of the polygon, but the subset of those inside the polygon can be quite small. Two tests are available, depending on what is known about the polygon.

If the polygon is known to be convex, then P is inside the polygon if it is inside all of it's edges. Look for an edge that P lies outside of. If none can be found, it must be inside the polygon:

If the polygon is not known to be convex, of if you are not sure, then use the general form of the inside/outside test described in Class 12.

Be sure to do the angle calculation using the <math.h> 4-quadrant inverse tangent function with two parameters:

double atan2(double numerator, double denominator); /* prototype */

If the intersection is outside the polygon, ignore the polygon. This is usually done by continuing to the end of the polygon loop.

Step 6: Is that intersection closer than the last one found?

If the intersection is inside the polygon, compare the z value with the corresponding value in the z-buffer. If the new polygon is further away than the z-buffer value, the polygon will not be visible and can be ignored. In our example, that means we ignore any intersection whose z value is more positive than the one in the z-buffer

Step 7: Is that intersection closer than the last one found?

If we've made it this far and the polygon has not been eliminated, then it will be visible (unless another polygon is found later that is even closer). Copy the polygon color into the digital image pixel and copy the intersection z value into the z-buffer.

 


 
WPI Home Page
[cs4731] [Classes]

Contents ©1997, Norman Wittels
Updated 29Apr97