Viewing, Viewports, and OpenGL Control

Tom Kelliher, CS 320

Mar. 27, 2000

Administrivia

Announcements

Project 1 due in one week. Written final?

Midterm on 4/10.

Assignment

Read 3.1--3.5.

From Last Time

Classes, maze class.

Outline

  1. 2-D viewing.

  2. OpenGL control functions.

  3. Viewports, viewport.c

Coming Up

Interactive graphics.

Viewing

2-D viewing:

  1. Objects inside the viewing/clipping rectangle are visible:

    What is clipping?

  2. The default viewing volume is and centered at the origin.

The orthographic projection:

  1. Projects the point onto .

    Suppose we have two objects, each at and .

  2. In 2-D, we generally want to place objects at z=0.

  3. OpenGL specification:
    void glOrtho(GLdouble left, GLdouble right,
                 GLdouble bottom, GLdouble top,
                 GLdouble near, GLdouble far);
    
    void glOrtho2d(GLdouble left, GLdouble right,
                   GLdouble bottom, GLdouble top);
    
    ``Multiply the current matrix with an orthographic matrix'' --- make the projection matrix the current matrix.

  4. The OpenGL camera defaults to being positioned at the origin, pointed in the -z direction. The camera can ``see'' behind itself.

OpenGL matrices:

  1. Modelview matrix: positions objects in front of the camera.

    Translation, rotation, scaling.

  2. Projection matrix: determines shape of the viewing volume.

    Orthographic, perspective.

Control Functions

Several we've already seen:

  1. glutInit(int *argcp, char *argv[])

  2. glutCreateWindow(char *title)

  3. glutInitDisplayMode(unsigned int mode) Example:
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    Mode bit examples:
    1. GLUT_RGBA, GLUT_RGB, GLUT_INDEX.

    2. GLUT_SINGLE, GLUT_DOUBLE.

    3. GLUT_DEPTH.

Controlling the Viewport

  1. The viewport is used to control what part of the window you render into. It is used to maintain aspect ratio.

  2. Aspect ratio: ratio of rectangle's width to height.

    The aspect ratio's of the clipping rectangle and viewing window should match. Otherwise, distortion results.

  3. Controlling the viewport:
    void glViewport(GLint x, GLint y, GLsizei w, GLsizei h);
    
    where , is the lower-left corner of the viewport, and w and h are its width and height, respectively.

    Example. Assume you have a viewing window:

    glViewport(100, 100, 50, 50)
    
    This sets the viewport to the upper righthand quarter of the viewing window.

Discovering the Window Size

  1. The program initially specifies the window size:
    glutInitWindowSize(gww, gwh);
    
    But, the user can resize the window at will.

  2. Register a reshape callback:
    #define WINDOW_WIDTH 200
    #define WINDOW_HEIGHT 200
    
    void reshape(int, int);
    
    int maintainAspectRatio = 1;
    int gww = WINDOW_WIDTH;
    int gwh = WINDOW_HEIGHT;
    
    int main()
    {
       /* ... */
    
       glutInitWindowSize(gww, gwh);
       glutReshapeFunc(reshape);
    
       /* ... */
    }
    
    
    void reshape(int w, int h)
    {
       if (maintainAspectRatio)
          gww = gwh = (w < h) ? w : h;   /* aspect ratio = 1 */
       else
       {
          gww = w;
          gwh = h;
       }
    }
    

viewport.c

Points to note:

  1. Does not maintain aspect ratio.

  2. The display list glues together multiple polygons.

  3. display() sets the viewport to several portions of the window, and then renders the display list.

    Note the overlap and observe the result.

  4. The rendering of the text is delayed until idle() is called.

  5. The position to begin rendering text is specified in world coordinates, and then bitblt ops are used to copy the bitmap characters.

    The user could easily write a small function to position and render strings.



Thomas P. Kelliher
Mon Mar 27 12:31:23 EST 2000
Tom Kelliher