Exercise 5

Modified

Download Nate Robin's tutorial. Run the projection application to get a feel for the visual effect of varying parameters.

The camera analogy has four parts:

  1. Viewing - positioning the viewing volume in the world, points the camera at the scene.
  2. Modeling - positioning the models in the world, arrange the scene.
  3. Projection - determining the shape of the viewing volume, choose the camera lens or adjust the zoom.
  4. Viewport - determine how large the final image.

The following program illustrates each.

  1. The text presents front and back clipping planes in Figure 5.26 on p257. In the following program:
    1. Vary the camera position and orientation using gluLookAt.
    2. Comment out gluLookAt. Explain the results.
    3. Change gluLookAt so that the front side of the cube is not visible but at least parts of the other five are.
    4. Change gluLookAt to glTranslatef(0.0,0.0,-5.0). Determine why the result looks exactly the same.
    5. Determine the order display and reshape are called. Does it matter?
    6. Vary the glTranslatef parameters until you understand the effect on the size and position of the cube.
    7. Vary the glFrustum parameters until you understand the effect on the view volume.
    8. Comment out glScalef and rerun. Change glFrustum to glOrtho. Explain the results.
    9. The gluLookAt sets the image up-vector at (0.0,1.0,0.0). Determine the effect of changing the vector.
    10. Change the lower-left corner of the viewport to (100,-100). Change the viewport to double the size of the display image of the cube. Are parameters in world or screen system?
    11. Change the initial window size to reduce the size of the display image by one half.
    #include <GL/glut.h>
     
    void reshape(int w, int h) {
        glViewport(0,0,(GLsizei)w, (GLsizei)h);                     // Viewport transformation
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);                     // Projection tranformation
        glMatrixMode(GL_MODELVIEW);
    }
     
    void display() {
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f(1.0,1.0,1.0);
       glLoadIdentity();
       gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);         // Viewing transformation
       glScalef(1.0,2.0,1.0);                                               // Modeling transformation
       glutWireCube(1.0);
       glFlush();
    }
     
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
        glutInitWindowSize(500, 500);
        glutInitWindowPosition(100,100);
        glutCreateWindow("cube");
        glClearColor(0.0,0.0,0.0,0.0);
        glShadeModel(GL_FLAT);
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutMainLoop();
    }