[WPI] [cs4731] [Classes] [Class21]

cs4731, D96/97 Class 21a - Clipping

2D Clipping in 3D

In Class 11, Class 12 , and Class 15 we discussed 2D clipping as a way of chopping a line so it stays within the boundaries of a polygon, usually the rectangular viewport. One way to do that is to do a orthographic projection of the points (set their z coordinates to zero) and then do the clipping in the 2D viewport plane (z = 0).

Another way to do the clipping is to imagine infinite 2D planes in the 3D world at x = 0, x = 1, y = 0, and y = 1. Then we can clip the line to these planes

The line is still a 3D line, but it fits within the viewplane - that means if we ignore the z coordinates or if we ortographic project the line into the viewplane, it is properly clipped. The easiest way to do this clipping is to consider each of the 4 planes in the point- normal form (see the vector notes). The vector equations for points P in the planes are: with these values:

 plane

 N

 d

 x = 0

 {-1, 0, 0}

 0

 x = 1

 {1, 0, 0}

 1

 y = 0

 {0, -1 0}

0

 y = 1

 {0, 1, 0}

1

This table can be constructed by thinking about the directions of the normals for each plane. The d value is just the perpendicular distance from the plane to the origin. When the planes are orthogonal to coordinate axes, the values are obtained directly from the plane locations.

Then use the standard line/plane clipping method to find where the line defined by the two points P1 and P2 intersects each planes:

These intersections are used in any of the starndard clipping algorithms to clip the line.

3D Clipping

Often we wish to clip the line in the z direction as well. For example, suppose the viewport is at z = 0 and the viewer is at z = -5. Then the line connecting the points {.25, .25, -10} and {.75, .75, -15} is behind the viewer and should not be rendered, despite the fact that the xy coordinates are totally within the viewport. Orthographic projection or clipping to the four planes shown above won't work. A simple extension of the idea is to do the clipping in 3D by adding two more planes and clipping to all 6 planes.

 plane

 N

 d

 z = zmin

 {0, 0, -1}

 zmin

 z = zmax

 {0, 0, 1}

 zmax

The parallelepiped desribed by the min/max values {0, 0, zmin}, {1, 1, zmax} is called the clip box (plus other similar names). This form of 3D clipping works properly if the clip planes are at a finite distances, if the front clip plane is put at or in front of the viewer, and if no perspective transformation is used.

4D Clipping

If, however, the perspective transformation is used and if the z-clipping is not done properly, errors can occur. To see this, consider how clipping works in a 4D diagram similar to those used in Class 20. Consider how the x coordinates of two points change under perspective transformation. Only the x and w coordinates are shown - y and z have been hidden to make the diagram clearer.

The 3D points, shown in blue, initially have w values w = 1. After perspective transformation, the x , y, and z values remain the same but the w value has changed to w = 1 + z/d. When we unhomogenize the coordinates (divide by w), the new points, shown in red, have moved to smaller x coordinates. If the initial x coordinates were within the clipping range 0<=x<=1 and if z>0 then the transformed coordinates will still fit within the viewport. Note that we showed both points having the same w values to make the diagram easier to understand. In general, they will move different distances in the w direction.

Now, consider what happens if z<0 (the points are in front of the viewport);

Now the point move outward and can overrun the viewport borders. Clipping the initial points using 2D or 3D clipping will not prevent this overrun. If a point has a z value less than -d (behind the viewer), something even stranger happens:

We put P1 behind the viewer (z < -d) and P2 in front of the viewport (z > 0). The second point moves in towards the axis, as we expect, but the first point changed sign and is now at a negative value of x! And, if you start at P2 on the 3D line and move towards P1, (the point is moving towards the viewer) the transformed point moves right, not left. Where the line passes through the viewer's plane (z = -d) the point has moved right all the way to infinity. When the point comes even closer, it wraps around and approaches the w axis (x = 0) from the left (negative x values). In fact, if we move our perspective axis to the center of the viewport instead of at the corner, we will see this:

The solution is to do the clipping in 4D coordinates. Instead of clipping to 6 planes in 3D space, shown above, we clip to 6 planes in 4D space. We want the planes which bound the clip box described above. To find their N and d values, look at the two corresponding to x=0 and x=1.

Since the planes correspond to "real" (3D) coordinates, they must pass through the origin. Thus each plane has the perpendicular distance d = 0. The N value can be calculated from the points at which the planes intersect the plane w = 1.

 plane

 N

 x = 0

 {-1, 0, 0, 0}

 x = 1

 {1, 0, 0, -1}

 y = 0

 {0, -1, 0, 0}

  y = 1

 {0, 1, 0, -1}

 z = zmin

 {0, 0, -1, zmin}

 z = zmax

 {0, 0, 1, -zmax}

Note that these vectors do not all have the same length so normalization is required.

Using these values the 4D line can be clipped to the six planes using exactly the method described above for 3D. The new 4D points (if the line survived clipping) are then unhomogenized to produce a 3D line that is rendered using orthographic projection (delete the z value). This method always works properly, so it is the preferred clipping method.


 
WPI Home Page
[cs4731] [Class21]

Contents ©1997, Norman Wittels
Updated 07May97