CS 424: Computer Graphics, Fall 2013
Lab 4: Introducing OpenGL
For this first lab on 3D graphics, you will write some simple programs using old-fashioned OpenGL 1.0. The basic information that you need about OpenGL can be found in Section 6 of the notes.
You have your choice of working either in C or in Java. In C, you will work on the command line, using a text editor for programming, and the programs will use the GLUT library for window and event handling. In Java, you will use the JOGL API and will probably work in Eclipse, although the command line is also possible. The background that you need is in Section 7 of the notes. See that section for information about compiling C programs that use OpenGL and GLUT or for instructions on setting up Eclipse to use JOGL.
The files that you will need for C can be found in the directory /classes/cs424/lab4-glut. The files for use with JOGL are in /classes/cs424/lab4-jogl. When configuring Eclipse for JOGL, you can use the jar files in the directory /classes/cs424/jogl-support. There is no need to make copies of the jar files; you can tell Eclipse to use them from that directory.
This lab is due at the beginning of lab next week. You should copy your work into your homework folder in /classes/cs424/homework. Your work should be in a folder named lab4 or something very similar.
Exercise 1: Drawing with glBegin/glEnd
For this exercise, start with the file glut-basic.c for C programming or JoglBasic.java for Java programming. Add code to the display function to draw the following pyramid, where the five vertices of the pyramid should be five different colors:
Draw the pyramid as a solid object, not a wireframe. The program is already set up so that the arrow keys can be used to rotate the view, so that you can see the pyramid from all sides. The "Home" key will reset the rotations to zero. Change the "scale" in the program so that you get a good view of the pyramid. Here are two views of my version of the pyramid:
Note that lighting should be left turned off for this exercise, since you are not providing the normal vectors that are required for correct lighting calculations. This makes the pyramid look odd, as if it is glowing with its own light rather than illuminated by an external light source.
Exercise 2: Hierarchy
For this exercise, you will build an animated, hierarchical 3D model. Start with the file glut-animation.c for C programming or JoglAnimation.java for Java programming. You will construct the model from GLUT shapes. (The Java program already contains an object named glut of type GLUT that you can use for calling the GLUT shape functions; for C, you don't need an object because you can just call the functions directly.)
Use the OpenGL transformation functions glScale*, glRotate*, and glTranslate* to apply the modeling transformations that you need. Don't forget to use glPushMatrix and glPopMatrix to save and restore the transformation, as required. You will need to use them a lot!
For this program, use a procedural version of hierarchical modeling. That is, you will write subroutines to draw subobjects of the complete model. There is already a variable named frameNumber that you can use to drive the animation. The animation does not start automatically. Press the space bar to start the animation, press it again to pause. You can use the arrow keys to rotate the model as a whole. (Note to C programmers: Remember that in C, variables and functions should be declared before they are used, so any function that you write should come after the definition of frameNumber and before the point where the function is called.)
To begin, write a subroutine that makes a cylinder with a rotating cube on each end. The cubes must rotate around the axis of the cylinder. The three objects must be different colors. The center of the cylinder must be at the origin. Note that when you call glutSolidCylinder, the axis of the cylinder lies along the z-axis, and the base of the cylinder lies in the xy-plane. (In particular, the center of the cylinder is NOT at the origin, where you want it.) Call your subroutine in the display subroutine to test it. It should look something like this:
If you want, you can use teapots instead of cubes, but note that a teapot has its axis along the y-axis, and you will have to take that into account.
Next, write another subroutine, to create a model consisting of a long cylinder, with a rotating copy of the previous model on each end. The two models on the end should both rotate about the axis of the cylinder, at different speeds. As always, test your work! It should look something like this:
Finally, make an object that is made up of a cone and the previous model. The center of the cylinder should be positioned at the tip of the cone and should rotate about the axis of the cone. When drawing the cone, note that the glut routine will draw the cone, like the cylinder, with its base in the xy-plane and its axis along the positive direction of the z-axis. The final model should look something like this: