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 + dThe 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:
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:
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 polygonWhen 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:
Show that z-clipping works by animating one or more objects in your scene to go behind the camera.
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[0], rgb[1], rgb[2] 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.