CS4735 Computer Graphics

Lab 7  Nov. 8, 2004


Purpose:  To gain an understanding of how to control a camera in computer graphics.


1. As for all previous labs, log in to a Linux workstation in ITD415, and create a subdirectory in your UNIX subdirectory space called (e.g.) “L7”.


2.  Download the “camera.tar” file from the CS4735 “Examples” web site into your L7 subdirectory (use e.g. Mozilla). 


3.  untar the flycamera.tar file to get the original source code back.  Run “make” to generate an executable “flycamera” that should draw a teapot as shown in Figure 1 below.

                                    (a)                                            (b)                                                        (c)

Figure 1.  Wireframe teapot drawn (a) pressing the F (Shift-F or forward) key once, (b) pressing Ctrl-F (press and hold the Ctrl key while pressing the F key) key several times to slide backward (zoom out), and (c) pressing the F (Shift-F or forward) key several times to slide forward (zoom in).


4.  Edit the “camera.cpp” file to add roll, pitch and yaw movements to the camera.  For example, the roll movement rotates the camera by an angle a about the n axis which changes the u and v directions to

= cos(a)u + sin(a)v, = -sin(a)u + cos(a)v.  The code to implement a roll method for the camera class is given on p.369 of the text, as follows:

void Camera:: roll(float angle)

{ // roll the camera through angle degrees

  float cs = cos(3.1415926/180 * angle);

  float sn = sin(3.1415926/180 * angle);

  Vector3 t = u;  // remember u

  u.set(cs*t.x - sn*v.x, cs*t.y - sn*v.y, cs*t.z - sn*v.z);

  v.set(sn*t.x + cs*v.x, sn*t.y + cs*v.y, sn*t.z + cs*v.z);

  setModelViewMatrix(); // tell OpenGL


Map the roll motion to the R (Shift-R roll right) and Ctrl-R (roll left) keystrokes, with each key press calling for a roll of one degree.  For example, the call inside MyKeyboard would look like the following:

        case 'R':      cam.roll(1.0); break;  // roll to right

        case 'R' - 64: cam.roll(-1.0); break; // roll to left

Map the pitch motion to the P (Shift-P pitch up) and Ctrl-P (pitch down) keystrokes, and the yaw motion to the Y (Shift-Y yaw right) and Ctrl-Y (yaw left) keystrokes, with each key press calling for a rotation of one degree. 

            Once the above motions are completed, use them to navigate your camera so that you are looking at the bottom of the teapot from the outside (e.g. like in Figure 2) with the spout on the left and the handle on the right..

Figure 2.  Viewing the teapot from the bottom after naviagating to underneath and rotating so that the handle is on the right.


5.  Hook the roll, pitch and yaw movements to the mouse instead of the keyboard.  To do this, define a


callback function

     myMovedMouse(int x, int y)

that receives the x and y values of the mouse position when any mouse button is pressed and held down.  For example, define three categories of mouse motion, as follows:

            (a) for vertical mouse movement down, pitch the camera down one degree; if vertical up, pitch the camera up one degree,

            (b) for horizontal mouse movement left, roll the camera left one degree; for horizontal movement right, roll the camera right one degree,

            (c) for diagonal mouse movement to the upper right, yaw the camera to the right one degree; for diagonal movement to the lower left, yaw the camera to the left one degree.

            Hints:  (a) define

                        static int Prevx, Prevy;

            variables that keep the previous mouse position values seen by the myMovedMouse(...) function for comparison with new values.

                        (b) use a tolerance (e.g. int tol = 5) to keep the returned pixel values from having to be precisely in a vertical or horizontal line to invoke pitch and roll, respectively.

Which is easier to control the camera with; the keyboard or the mouse?


6.  What would be required to draw a solid teapot instead of a wireframe teapot?