CSc 433/533: OpenGL and GLUT Tutorial

  1. GLUT Event Loop
  2. You may have noticed in the simple example that main never calls the display function displayCB directly. How does anything get drawn? The answer is that GLUT takes over when glutMainLoop is called. From this point on, GLUT processes window events and invokes the appropriate user functions that have been registered via calls like glutDisplayFunc(displayCB). This tells GLUT that whenever it receives a display event in the window, it should call the function displayCB. The function displayCB is called a callback function or an event handler. keyCB is another callback that is invoked whenever GLUT detects a key-press event in the window.

    There are several other events that GLUT can detect and for which you can set up callbacks. These are all described in the GLUT manual.

    Each window or subwindow has its own set of callbacks. When a window is created, using glutCreateWindow or glutCreateSubWindow, it becomes the current window and any callbacks registered after this point are associated with that current window. This means that a mouse-press event in one window can invoke a completely different callback than the same event in a different window.

    canvas.c is an example of a window with a couple of subwindows that handle events. This is a screen shot:



    Press and release a mouse button over the triangle or square and it will follow the cursor within the yellow subwindow. Hold down a mouse button in the green subwindow and slide the mouse back and forth and the little hash marks slide back and forth.

    The program illustrates almost all of the OpenGL and GLUT commands that are needed in the first assignment straw. It shares the same Makefile with triangle.