3 GLUT

3.010 What is GLUT? How is it different from OpenGL?

Because OpenGL doesn't provide routines for interfacing with a windowing system or input devices, an application must use a variety of other platform-specific routines for this purpose. The result is nonportable code.

Furthermore, these platform-specific routines tend to be full-featured, which complicates construction of small programs and simple demos.

GLUT is a library that addresses these issues by providing a platform-independent interface to window management, menus, and input devices in a simple and elegant manner. Using GLUT comes at the price of some flexibility.

A large amount of information on GLUT is at the GLUT FAQ: http://reality.sgi.com/mjk/glut3/glut-faq.html.

3.020 Should I use GLUT?

Your application might need to do things that GLUT doesn't allow, or it may need to use platform-specific libraries to accomplish nongraphical tasks. In this case, consider not using GLUT for your application's windowing and input needs, and instead use platform-specific libraries .

Ask yourself the following questions:

  • Will my application run only on one platform?
  • Do I need to use more than one rendering context?
  • Do I need to share display lists or texture objects between display lists?
  • Do I need to use input devices that GLUT doesn't provide an interface for?
  • Do I need to use platform-specific libraries for other tasks, such as sound or text?

If you answered yes to any of these questions, you need to evaluate whether GLUT is the right choice for your application.

3.030 I need to set up different tasks for left and right mouse button motion. However, I can only set one glutMotionFunc() callback, which doesn't pass the button as a parameter.

You can easily set up different tasks depending on the state of the SHIFT, ALT, and CTRL keys by checking their state with glutGetModifiers().

To set up different tasks for the left and right mouse buttons, you need to swap the motion function depending on which mouse button is in use. You can do this with a mouse function callback that you set with glutMouseFunc(). The first parameter to this routine will indicate which button caused the event (GLUT_LEFT, GLUT_MIDDLE, or GLUT_RIGHT). The second parameter indicates the button state (GLUT_UP or GLUT_DOWN).

To illustrate, here's an example glutMouseFunc() callback routine:

/* Declarations for our motion functions */
static void leftMotion (int x, int y);
static void rightMotion (int x, int y);
static void mouseCallback (int mouse, int state, int x, int y)

{
   if (state==GLUT_DOWN) {
      /* A button is being pressed. Set the correct motion function */
      if (button==GLUT_LEFT)
         glutMotionFunc (leftMotion);
      else if (button==GLUT_RIGHT)
         glutMotionFunc (GLUT_RIGHT);
    }
}

3.040 How does GLUT do…?

It is often desirable to find out how glut creates windows, handles input devices, displays menus, or any of a number of other tasks. The best way to find out how GLUT does something is to download the GLUT source and see how it is written.

3.050 How can I perform animations with GLUT?

GLUT allows your application to specify a callback routine for rendering a frame. You can force executing this routine by calling glutPostRedisplay() from another callback routine, and returning control to glutMainLoop().

To create an animation that runs as fast as possible, you need to set an idle callback with glutIdleFunc(). The callback you pass as a parameter will be executed by glutMainLoop() whenever nothing else is happening. From this callback, you call glutPostRedisplay().

To create a timed animation, use glutTimerFunc() instead of glutIdleFunc(). glutTimerFunc() will call your callback only after the specified time elapses. This callback disables itself, so for continuous updates, your callback must call both glutPostRedisplay(), then glutTimerFunc() again to reset the timer.

3.060 Is it possible to change a window's size *after* it's opened (i.e., after i called glutInitWindowSize(); and glutCreateWindow();)?

Once your code enters the glutMainLoop() and one of your callback routines is called, you can call glutReshapeWindow(int width, int height).

Note that glutReshapeWindow() doesn't instantly resize your window. It merely sends a message to GLUT to resize the window. This message is processed once you return to glutMainLoop().

3.070 I have a GLUT program that allocates memory at startup. How do I deallocate this memory when the program exits?

If the user exits your program through some input that you can catch, such as a key press or menu selection, the answer is trivial. Simply free the resources in the appropriate input event handler.

Normally, this question comes up because the user has killed the program through window frame controls, such as the Microsoft Windows Close Window icon in the upper right corner of the title bar. In this case, your program won't get an event indicating the program is exiting.

For simple resources such as memory deallocation, this should not be a problem. The OS will free any memory that was in use by the process.

Of greater concern is prompting the user to save work or flushing data held in software buffers to files.

Unfortunately, GLUT doesn't provide a mechanism to handle such events, because the OS or window manager is killing the process in a forceful way.

One option is to hack the GLUT source, and disable the Close Window icon in the window frame title bar. This is distasteful, for it means you must now include the entire GLUT window creation code in your application.

In short, there is no good answer to this problem. Users, in general, should not expect good results when they exit an application in this way.

3.080 How can I make my GLUT program detect that the user has closed the window?

The same way as the previous section 3.070 shows.

3.090 How can I make glutMainLoop() return to my calling program?

glutMainLoop() isn't designed to return to the calling routine. GLUT was designed around the idea of an event-driven application, with the exit method being captured through an input event callback routine, such as a GLUT menu or keyboard callback handler.

If you insist on returning from glutMainLoop() to your program, there is only one way to do this. You need to download the GLUT source and hack gluMainLoop() to do what you want it to. Then you need to compile and link into your program this hacked version of glutMainLoop().

3.100 How do I get rid of the console window in a Windows GLUT application?

With Visual C++ 6.0, go to the Project menu, Settings… dialog. Select the Link tab. In the Project options edit box, add /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup to the end of the present text. Link options are similar for other Windows compilers.

3.110 My GLUT question isn't answered here. Where can I get more info?

SGI's GLUT FAQ is an excellent source of information on GLUT.