CPSC 324, Fall 2002
Lab 8a: OpenGL 3D, Part 1

THIS IS THE FIRST PART of a two part lab on 3D graphics programming in OpenGL. This part of the lab concentrates on transformations, modeling and simple viewing. Next week, the second part of the lab will add lighting and materials.

Exercise: The exercise for Part 1 of Lab 8 is the OpenGL program described on the remainder of this page. You should save several frames from the program as PNG images and put them on your Web page. However, I will not collect the program until you have also done Part 2 of Lab 8.

You should use a copy of the opengl-starter directory as a starting point for writing your program. Copy this directory into your account using a command such as

            cp  -r  /home/cs324/opengl-starter  lab8

You will be working in the files gl_canvas.cc and gl_canvas.h. Remember that to compile your program, you just have to use the make command.

A Wireframe Model

The goal of Part 1 is to create a wireframe model of an object from several simple shapes, and to be able to rotate that object as a whole. The program /home/cs324/lab8a_sample_program is an example of this. Here are two frames from this program:

wireframe car wireframe car rotated

The "car" object is constructed from two red cubes, four blue toruses, and two yellow spheres (with appropriate transformations). When you run the program, you can rotate the car using the up-, down-, left-, and right-arrow keys. If you select the "Start Animation" command from the Animate menu, the car will be rotated about both the x- and y-axis.

You should write a program that shows a similar object. If you like, you can make a car like mine except in different colors. You could make an airplane, using a stretched out sphere for the body and squished spheres or cones for the wings. How about a table with legs, and with a teapot on the table? Whatever you make, it should be composed of at least six sub-objects. For the sub-objects, you can use glut's built-in wireframe object subroutines:

        void glutWireSphere(double radius, int slices, int stacks);
        void glutWireCube(double size);
        void glutWireTorus(double innerRadius, double outerRadius,
                             int sides, int rings);
        void glutWireCone(double radius, double height,
                             int slices, int stacks);
        void glutWireIcosahedron();
        void glutWireDodecahedron();
        void glutWireOctahedron());
        void glutWireTetrahedron();
        void glueWireTeapot();

You should use wireframe objects because they will look OK without lighting. Next week, you will replace the wireframe objects with solid objects and turn on lighting.

To do 3D graphics, you will have to enable GL_DEPTH_TEST, and you will have to clear the depth buffer before drawing each frame. You should use glFrustum() to set up a perspective projection, and use a simple gluLookAt() to set the camera's viewpoint. Set up the individual sub-objects using glPushMatrix(), glPopMatrix(), glScalef(), glTranslatef(), and glRotatef(). We have discussed all this extensively in class. Most of this goes in the paintGL() routine, although some of it could be in initializeGL().

You also need to be able to rotate the object as a whole. You want two rotations, one about the x-axis and one about the y-axis. The amount of rotation will be variable, so add two variables such as rotate_x and rotate_y to the GLCanvas class. Add the rotation commands:

        glRotatef(rotate_y, 0, 1, 0);
        glRotatef(rotate_x, 1, 0, 0);

to the paintGL() routine just after the call to gluLookAt() and before drawing any objects.

To enable the arrow keys, you have to modify the keyPressEvent() routine. The left- and right-arrow keys change the value of rotate_y and the up- and down-arrow keys change the value of rotate_x. The Home key should reset both rotate_x and rotate_y to zero.

To enable the animation, you should modify the timerEvent() routine so that it adds small amounts to the values of rotate_x and rotate_y..

David Eck, October 2002