Processessing the Mesh Model

Once the scene has been projected and normalized, we must do some post-processing in order to display a view of the model.

Post-processing involves:

  1. Removing backfaces (faces turned away from the camera)
  2. Clipping faces against the view volume
  3. Remove faces or parts of faces that are hidden behind other parts of the model (hidden surface problem).

Removing Backfaces

We need to traverse the model and check each polygon to see if it is visible. For each face of an object we can calculate the 'outward' normal by normalizing the cross product of two non-collinear vectors lying on the polygon face. Alternatively, we can use Newell's method to calculate the face normal. This method works for non-planar polygons and we do not need to test for collinarity. Graphics Gems III, David Kirk (editor), Academic Press, 1992, ISBN: 0124096735

\[ \vec{\mathbf{n}}=|\vec{\mathbf{a}} \times \vec{\mathbf{b}}| \]

Determining Backface

Once the outward normal has been calculated, we need to choose any point $ \vec{q} $ on the face.

Determining a Backface

We are interested in the angle $\theta$ , between the vector $(\vec{e} -\vec{q} )$ and the normal vector $\vec{n} $ (figure below). If this angle is greater than $90^{\circ}$ , then the face is a backface (the face is oriented away from the eye). We can perform this test with a simple dot product, the dot product of two vectors is related to the angle between them. A face is a backface if the following holds true:

\[ \vec{n} .(\vec{e} -\vec{q} )<0 \]

If this expression is zero then the face lies parallel to the line of sight and is therefore visible.

We can now process the model to remove the backfaces, typically removing backfaces will halve the number of polygons to involved in further processing.

Clipping Faces

Before we start filling polygons, we must clip them to the edges of the view volume. We can do this with the Sutherland-Hodgeman polygon clipping algorithm. This process involves clipping individual edges against the sides of the 'clipping polygon'. A separate algorithm is available for edge clipping, this is called the Cohen-Sutherland algorithm.

Cohen-Sutherland Edge Clipping

As with most graphics procedures, edge clipping needs to be very efficient, as there are typically thousands of edges in a scene. Edge clipping involves testing a line segment against a clipping rectangle and then clipping the line segment until it lies entirely within the clipping rectangle.

We must first determine if the end points of the edge lie inside or outside the clipping rectangle (window). Each edge of the window defines an infinite line that divides the plane in to an inside and outside half-space.

Inside-Outside Half-Spaces

For each end-point (x,y), we associate a 4-bit code, with one bit for each edge of the window. This code will be used to record if the end-point is in any of the 4 outside half-spaces. The bits will be set as follows;

This allows us to make the following two tests;

  1. If all bits are zero, the point lies entirely inside the window. If both end-points lie inside the window, then the line-segment lies within the widow and does not need to be clipped, no further processing is required.
  2. If both end-points lie within the same outside half-space (corresponding bits are set), then the line segment lies entirely outside the window and can be rejected without further processing.

In all other cases, the line-segment needs to be clipped against the window. A divide and conquer strategy is used. The line segment is clipped against one of the edges, this produces a new line segment. The new line segment is the subjected to the tests above. This process continues until the line-segment is rejected or accepted according to the tests above. A line segment needs to be clipped against an edge, if the corresponding bit has been set for one of its end-points.

Clipping an line against an edge

In the figure above, we wish to clip the line segment $P_{1}(x_{1},y_{1}), P_{2}(x_{2},y_{2})$ to the clipping edge given as $x_{right}$ . The desired line segment is $A(x_{a}, y_{a}), P_{2}$ . We know that$ x_{a}\equiv x_{right}$ . Using the equation of a line where $m$ is the slope;

\[ a_{y}=m(x_{right}-x_{2})+y_{2} \]

We can use similar reasoning in order to clip against other edges. The full clipping algorithm can now proceed as follows;

  1. Construct inside-outside codes for both end points$ (P_{1} \& P_{2})$ .
  2. If both codes are zero, accept line segment and exit.
  3. If both codes are set for same outside half-space, reject line and exit.
  4. If $P_{1}$ is inside the window, swap $P_{1}$ and $P_{2}$ (the following tests only applies to $P_{1}$ ).
  5. If $P_{1}$ is outside right edge, clip to right edge and return to step 1.
  6. If $P_{1} $ is outside left edge, clip to left edge and return to step 1.
  7. If $P_{1}$ is outside top edge, clip to top edge and return to step 1.
  8. $P_{1}$ must be outside bottom edge, clip and return to step 1.

Sutherland-Hodgeman Polygon Clipping

This algorithm, like the Cohen-Sutherland edge clipping algorithm, uses a divide an conquer approach and uses the edges of the clipping rectangle to divide space into inside and outside half space.

Result of Sutherland-Hodgeman clipping

The basic operation is to clip the polygon against one edge of the clipping rectangle, this operation produces a new (clipped) polygon (see figure below), which we then clip against the remaining edges in turn. To clip the polygon against one edge, we simply clip all the edges of the polygon against the edge of the clipping rectangle. As each edge is clipped, new vertices are output according to certain rules. Each edge has and start point $s$ , and an end point $f$ , there are 4 possible configurations between and edge and the inside/outside half-spaces;

  1. both $s$ and $f$ are inside: output $f$ .
  2. $s$ is inside and $f$ is outside: output the intersection point , $i$ .
  3. both $s$ and $f$ are outside: output nothing.
  4. $s$ is outside and $f$ is inside: output intersection $i$ and then $f$ .

Possible intersection Configurations

The set of vertices output after traversing the polygon becomes the new (partially clipped polygon) for the next clipping edge.

Result of Clipping against one edge