CS 455 - Programming Assignment 6

This assignment is about viewing and transformations in 3D. It also introduces you to the use of 3D models, and the creation of a simple animation. This assignment is due Wednesday, April 13.

Download this zip file. In the zip file you will find a demo program, CS455Hw6-Demo.exe. You are to write the code for this program. There is no skeleton code for this assignment. Start your program by picking one of the example programs from class and modifying it.

When you run the demo program, notice the following. The three sliders control the location of the camera. In this program, the camera is on the surface of an imaginary sphere and, at least initially, the camera is looking at the origin (the corner of the room in the scene). The top slider controls the radius of the sphere, so it moves the camera closer to, or further away from, the "look at" point. The second slider controls the longitude of the camera on the sphere, which means, more or less, that it moves the camera from side to side. The vertical slider controls the latitude of the camera, which means, more or less, that it moves the camera up and down. In mathematical terms, the sliders are controlling the spherical coordinates in space of the camera. But the gluLookAt() function uses rectangular coordinates. So you will need to convert the spherical coordinates controlled by the sliders into rectangular coordinates. Here is a nice reference for spherical coordinates and the formulas for converting from spherical coordinates to rectangular coordinates.

Spherical Coordinates
One thing you need to keep in mind is that OpenGL has its axes in a different orientation from the usual mathematical orientation. So you will need to reorder the spherical to rectangular formulas accordingly.

The "look at" point in the call to gluLookAt() can be controlled in this program using the keyboard. The 'x', 'X', 'y', 'Y', 'z', 'Z' keys move the "look at" point by plus or minus 0.5 each time one of these keys is struck.

The "look at" point used in gluLookAt() should be denoted in the scene by a small red sphere (when you run the demo program, strike the 'y' key a bunch of times; you should see the red "look at" point rising up the y-axis at the corner of the room). Use the GLUT function glutWireSphere() to draw the sphere.

glutWireSphere()

Notice that you can use the keyboard to switch the program between perspective and orthogonal projections (using the 'p' and 'o' keys). My dimensions for the (perspective and orthogonal) view volume were plus or minus three for left, right, top, and bottom, two for near, and 100 for far.

When the program is running, the console window is used to output some information that might help you to understand how perspective and parallel projections work with gluLookAt(). The console displays the camera and "look at" coordinates, and the camera coordinates can be displayed in either rectangular or spherical coordinates.

When you write your program, first just draw the floor and two walls and look at them from a fixed camera location. Then add one model to the scene. Then have the sliders control the rectangular coordinates of the camera, so that you can move the camera around (make the top slider control the x-coordinate of the camera, the second slider the z-coordinate, and the vertical slider the y-coordinate). Then have the keyboard move the "look at" point. When that is working, start to work on having the sliders move the camera on the surface of a sphere. When that works, add the other models to the scene. Finally, animate the dolphins.

When you put a model in the scene, it helps to first call the function glmUnitize() to scale the model to a known size. Then it is a lot easier to rescale and position the model. There is an example program, loadModel.c, in the zip file demonstrating how to load OBJ model files into an OpenGL scene.

All the models you need are contained in the subfolder models. There is another subfolder, obj file viewers, that contains two small programs that you can use to open and view the OBJ models. You should also open the OBJ files using a text editor, since they are just text files that list all of the vertices that make up the model.

Here is a reference that I used to find r,g,b values for the colors used in the scene.

VisiBone Webmaster's Color Lab
When you set a color in OpenGL using integer r,g,b values that are between 0 and 255, use the function glColor3ub() (that is, use the "unsigned byte" data type).

Here is the code that I used to get the three sliders in place.

   // parent window, x, y, width, height, initial value, callback function
   gluiHorizontalSlider(window, 1,  0, -1, 20, sliderInit, slider1);  // top edge
   gluiHorizontalSlider(window, 1, 20, -1, 20, sliderInit, slider2);
   gluiVerticalSlider  (window, 0, 40, 20, -1, sliderInit, slider3);  // left edge

To animate the movement of the dolphins, use a GLUT "idle" callback function. You register a GLUT idle callback function using glutIdleFunc().

glutIdleFunc()
There is an example program, loadAndAnimateModel.c, in the zip file demonstrating a simple animation.

Here is a function that will draw a lined surface for the floor and walls. Treat this as a model that can be transformed into place to form the two walls.

void grid(int n, int m)
{
   int i, j;
   glPushAttrib(GL_ALL_ATTRIB_BITS);
   glFrontFace(GL_CCW);
   glPolygonMode(GL_FRONT, GL_FILL);
   glPolygonMode(GL_BACK, GL_FILL);
   // Draw an n by m grid of (filled in) quads.
   glColor3f(1.0, 1.0, 1.0);  // Draw in white.
   glBegin(GL_QUADS);
     for (i = 0; i <= m; i++)
     {
        for (j = 0; j <= n; j++)
        {
           glVertex3f(  i, 0.0, j);
           glVertex3f(i+1, 0.0, j);
           glVertex3f(i+1, 0.0, j+1);
           glVertex3f(  i, 0.0, j+1);
        }
     }
   glEnd();
   // Now draw grid lines over the edges of the quads.
   glColor3f(0.7, 0.7, 0.7);  // Draw in light gray.
   glBegin(GL_LINES);
     for (i = 0; i <= m; i++)
     {
        glVertex3f( i, 0.1, 0.0);  // a tiny bit above the quads
        glVertex3f( i, 0.1, n);
     }
     for (i = 0; i <= n; i++)
     {
        glVertex3f(0.0, 0.1, i);
        glVertex3f(  m, 0.1, i);
     }
   glEnd();
   glPopAttrib();
}

Turn in a zip file called CS455Hw6Surname.zip containing your version of CS455Hw6Surname.c. Please remember to put your name inside of each of your source files. This assignment is due Wednesday, April 13.


Return to the main homework page.
Return to the CS 45500 home page.


compliments and criticisms