Clipping and introduction to the rendering pipeline (Oct 16 lecture)
course notes and homework due Oct 23 before class

This week we talked about z-clipping and introduced the general framework of the rendering pipeline.

Clipping:

Within an N dimensional space you can embed an unbounded N-1 manifold that divides the space into two parts. For example, a plane can be divided in two by a line or by a circle (ie: into inside the circle and outside the circle). Similary, 3D space can be divided in two by a plane or some other unbounded surface (eg: paraboloid, ellipsoid, etc).

"Clipping" is an operation in which you throw away everything in the space that is on one side of the manifold. When rendering scenes in computer graphics, it is very useful to use planes to clip away those parts of 3D space that are out of view. In particular, it is useful to clip away everything that is not in front of the camera.

A plane in 3D space can be defined by using a linear equation:

f(P) = aPx + bPy + cPz + d
The plane contains those points P for which f(P) = 0. Let's say we want to clip our objects prior to rendering, throwing away all parts of the object where f(P) > 0, and keeping all parts of the object where f(P) <= 0. As we discussed in class, the key to this will be to clip the model one triangle at a time - before applying perspective.

We want to keep everything for which z <= -ε, where ε is some small number such as 0.001. So our clipping function will be:

f(P) = 0Px + 0Py + 1Pz + ε

As we said in class, clipping one triangle by a plane will produce one of the following shapes:

• The entire triangle (when F(V) <= 0 for all three vertices)
• A four sided polygon (when F(V) > 0 for one vertex)
• A smaller triangle (when F(V) > 0 for two vertices)
• Nothing (when F(V) > 0 for all three vertices)
To clip a triangle by a plane, first it is necessary to learn how to clip a line segment [A,B] from point A to point B by the plane where linear function f(P) = 0.

First the two trivial cases: If f(A) <= 0 and f(B) <= 0 then the result is just [A,B]. If f(A) > 0 and f(B) > 0 then the result is nothing.

Suppose that f(A) <= 0 and f(B) > 0. We want to find a point C where f(C) = 0, and return the result [A,C]. Similarly, if f(A) > 0 and f(B) <= 0. We would return the result [C,B].

Finding C is done in two steps:

• Compute the fraction t along the line segment at which the function f(P) reaches zero;

• Linearly interpolate between A and B to get C.
More formally: t = (f(C) - f(A)) / (f(B) - f(A)), which can also be written as: -f(A) / (f(B) - f(A)).

Given the above machinery, we can clip a triangle by going round the triangle, vertex by vertex, to construct a new polygon. Here is the procedure:

```  For each triangle vertex A:
Let B be the next vertex around the triangle after A

• If f(A) <= 0
• Add A to the new polygon

• If A and B are on opposite sides of the clip plane (in other words, if f(A) * f(B) < 0)
• Compute the point C where edge [A,B] crosses the clip plane
• Add C to the new polygon
```
When you are done, you will have either 0, 3 or 4 vertices in the new polygon. If you end up with 4 vertices, then split the polygon into two triangles, and proceed onto the rest of the rendering pipeline with each of these two new triangles.

Homework:

Your assignment, due by next class, is two-fold:

• Integrate the above z-clipping algorithm into an animation. You can either use an animated scene you already made for this class, or else feel free to create a new animated scene if you're feeling creative. I know I would. :-)

Show that z-clipping works by animating one or more objects in your scene to go behind the camera.

• I want you to start feeling comfortable using the support routines I've provided that allow you to efficiently set an RGB color at each pixel of an image. To that end, go to my simple Blue Moon example, copy the two java files ExampleMISApplet and MISApplet, and replace ExampleMISApplet by code that produces something interesting and kooky. Have fun with it. :-)

The goal for this part of the assignment is really to show that you can use this library to get somehing up on the screen. You shouldn't need to modify MISApplet at all. In the class you implement, you will only need to implement two methods:

```   public void initFrame(double time) {
// here you should put any computations you want to do once per frame.
}

public void setPixel(int x, int y, int rgb[]) {
// x and y are the image pixel coords.
// your computation should place values into rgb[].
// rgb, rgb, rgb are the red,green,blue components, respectively.
// remember that red,green and blue should range from 0...255.
}
```

By the way, here is a link to a simple example of how to load and play a sound in an Applet.