Page 78 - Computer Graphics Handout
P. 78

display flag. Thus, at the end of the event loop, if the flag is set, the display callback is invoked and the flag unset. This method
                                                                                        14
          prevents the display from being redrawn multiple times in a single pass through the event loop.  Returning to our example, we see
          that each successive three depressions of the left mouse button specifies a new triangle that replaces the previous triangle on the
          display. Although we have a program that has some interactivity, introducing a few more callbacks will lead to a much more
          interesting program that can be expanded to a painting or CAD program.

          2.11.2 Window Events
          Most window systems allow a user to resize the window interactively, usually by using  he mouse to drag a corner of the window to
          a new location. This event is called a reshape event and is an example of a window event. Other window events include iconifying
          a window and exposing a window that was covered by another window. Each can have a callback that specified which actions to
          take if the event occurs. Unlike most other callbacks, there is a default reshape callback that simply changes the viewport to the
          new window size, an action that might not be what the user desires. If the window size changes, we have to consider the three
          questions:
          1. Do we redraw all the objects that were in the window before it was resized?
          2. What do we do if the aspect ratio of the new window is different from that of the old window?
          3. Do we change the sizes or attributes of new primitives if the size of the new window is different from that of the old?
          There is no single answer to any of these questions. If we are displaying the image of a real-world scene, our reshape function
          probably should make sure that no shape distortions occur. But this choicemay mean that part of the resized window is unused or
          that part of the scene cannot be displayed in the window. If we want to redraw the objects that were in the window before it was
          resized, we need a mechanism for storing and recalling them. Often we do this recall by encapsulating all drawing in a single function,
          such as the display callback function used in our previous examples. In interactive applications that is probably not the best choice,
          because we decide what we draw interactively.
          The reshape event returns in its measure the height and width of the new window. We can use these values to rescale the data that
          we use to specify the geometry. Thus, we have the callback
          GLint windowHeight, windowWidth;
          void reshape(GLsizei w, GLsizei h)
          {
          windowWidth = w;
          windowHeight = h;
          glViewport(0, 0, windowWidth, windowHeight);
          }
          This function creates a new viewport that covers the entire resized window and copies the returned values of the new window width
          and height to the global variables indowWidth and windowHeight so they can be used by the mouse callback. Note that because
          the reshape callback generates a display callback, we do not need to call glutPostRedisplay.

          2.11.3 Keyboard Events
          We can also use the keyboard as an input device. Keyboard events can be generated when the mouse is in the window and one of
          the keys is depressed or released.  The GLUT function glutKeyboardFunc is the callback for events generated by depressing a key,
                                      15
          whereas glutKeyboardUpFunc is the callback for events generated by release of a key. When a keyboard event occurs, the ASCII
          code for the key that generated the event and the location of the mouse are returned. All the key-press callbacks are registered in
          a single callback function, such as
          glutKeyboardFunc(myKey);
          For example, if we wish to use the keyboard only to exit the program, we can use the callback function
          void myKey(unsigned char key, int x, int y)
          {
          if(key==’q’ || key == ’Q’) exit(0);
          }
          GLUT includes a function glutGetModifiers that allows the user to define actions using the meta keys, such as the Control and Alt
          keys. These special keys can be important when we are using one- or two-button mice because we can then define the same


          14  Some interactive applications may need to execute the display callback directly.
          15  Depending on the operating system, the mouse focus may have to set by first clicking inside the window before events are recognized.

                                                              78
   73   74   75   76   77   78   79   80   81   82   83