Creating a 3D System
CS 357 HW2
due Monday, October 13

 

In this assignment you will create a 3D viewing system from scratch, using only the 2D features of OpenGL. It would be fairly simple to modify this program to run in any windowing system that has basic line-drawing capabilities.

When your program opens up it should look like this:

 

 

The drawing window should show a rectangular viewport shaded in a color different from the window's background, so it is easily seen. Within this viewport there is a line drawing of a cube. This cube should have one vertex at the origin, a sides extending along the standard axes. An adjustable SIZE parameter controls the size of the cube.

In addition to the SIZE, you have a number of controls. One set allows a change in the x, y, or z coordinates of the viewer's position. Another allows changes in the x, y, z coordinates of the point the viewer is looking at. If you set the viewer to look at one of the vertices of the cube and then move the viewer around, the cube will appear to rotate around this vertex. A third set of controls allows you to adjust the clipping parameters: distance from the viewer of the Hither plane, the Yon plane, and the picture plane. Finally, there is a control for Theta, the view angle.

There is a lot to do to set this up. I suggest that you break it up as follows. Of course, if another plan makes more sense for the way you work, go with it; the important thing is to get this done and to allow time for bug fixes. Here is how I would go at it:

1. The interface is easy to build, so do that first. Assign variables with reasonable defaults to each of the controls. At this point you don't need to implement callbacks for the controls; either use empty functions or the Null function.

2. Make a viewport within your drawing area and color it in.

3. Now for the lengthy part. You need to implement the entire view tranformation as a product of matrices. Remember to define the matrices in terms of the paramters that can be changed via your controls. Use some sense here -- changing the eye position or the look-at point will change <ax, bx, cx> and <az, bz, cz>, so you will need a callback that looks at variables directly changed by the controls, and then reassigns the vectors and updates the pipeline. Similarly, changing Theta changes the window boundaries, and so forth. If you set the matrices up with reasonable parameters, you can always change the parameters in your control callbacks. You will have to implement matrix multiplication, which should not be a big problem, in order to compute the matrix pipeline.

4. Implement a line drawing method. This should input world coordinates, and use the view transformation you implemented in step 3 to convert world coordinates to screen coordinates, then call openGL routines to actually draw the lines in your viewport.

5. Add callbacks to your viewer controls, so you can move the viewer and the point being looked at. It should be easy to tell if everything so far is working correctly -- just have the viewer look at a vertex, and move the viewer around. It should appear as though the cube is moving around this fixed vertex.

6. Add clipping, to cut off any line at the edges of your viewport, and also at the Hither and Yon planes. If you move the Hither plane slowly away from the viewer towards the cube you should see the edges of the cube gradually cut off.

7. Add the Theta callback, and anything else that is missing. By the time you hand in the program all of the controls should be working correctly.

Spend some time working with your program after it seems to be complete. There are lots of interactions between the various controls; make sure that everything works correctly in all situations. In particular, ask yourself if the right thing happens when you change theta, and when you change the perspective constants H, D, and Y.

If you finish this early and are looking for something extra to do, try to make your cube a solid with polygonal sides rather than lines for edges. The difficulty here is clipping a polygon rather than a line. Draw some pictures and work out an algorithm for clipping a rectangle to the viewport. Since you are only displaying a cube here, you only need to clip rectangles, not arbitrary polygons, to the viewport. The text by Foley and van Dam (which I used to use in this course until it got too far out of date) has a good discussion of polygon clipping in general. You can find this in the library or borrow it from me.