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.

3.015 Where can I get GLUT?

The GLUT FAQ and the GLUT source code tree for download.

Nate Robins web page has the most recent GLUT, v3.7.5,

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 rendering contexts?
  • 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.025 The GLUT source code license is very restrictive. Is there an alternative?

Yes. See the freeglut initiative. It claims to be a 100% compatible replacement for GLUT 3.7.

3.027 Why does glutTimerFunc() only execute my callback once?

GLUT's function for starting a timer and specifying a timer callback, glutTimerFunc(), is specified to execute the callback after the elapsed time, then delete the timer. This is the way timers work in several APIs.

Often, repeated callbacks are useful. Implement this by resetting the timer within your callback routine. For example, the following timer callback routine resets the timer for repeated callbacks:

static void timerCallback (int value) { /* Do timer processing */ /* maybe glutPostRedisplay(), if necessary */ /* call back again after elapsedUSecs have passed */ glutTimerFunc (elapsedUSecs, timerCallback, value); }

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 (rightButton); } }

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.

Usually, 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 a GLUT event indicating the program is exiting. In fact, when the window is destroyed, glutMainLoop() simply calls exit(0).

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

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

When using C++, the simplest solution to this problem is to wrap your GLUT application inside of a C++ class and create it with global scope. The C++ language guarantees that the class' destructor is called when the object goes out of scope.

Another option is to use the ANSI C/C++ atexit() call to specify the address of a function to execute when the program exits. You need to declare your buffers and data pointers with global scope so they're acccessible to the atexit() callback routine. More information can be found in any ANSI C/C++ reference. atexit() is only available with C/C++.

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 to your program from glutMainLoop(), there is only one way to do so. You need to download the GLUT source and hack gluMainLoop() to do what you want it to. Then 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?

The GLUT FAQ is an excellent source of information on GLUT.

Column Header
Column Footer