A Basic GLUT Application
|The GLUT template in action.|
Unaltered, this template renders a rotating coloured triangle. Nothing fancy; nothing particularly original; this template simply provides a makefile and a GLUT framework providing the bare essentials in order to display an animation. The locations at which you should insert code, or replace existing code with your own code have been highlighed using comments. Just search for "insert" or "replace." The rest of this article covers the basic sections of the template code. This template includes a project file for Visual Studio 2008 (i.e., Windows), and a makefile for Amiga OS 4.x+. Users of other systems should be able to modify the makefile for their systems easily.
NOTE: For more serious work, I would recommend using the frame-rate independent template, as it is more versatile, and does not hog CPU time. However, this template is the easiest to get started with.
Initialization includes opening a GLUT window, and setting up the basic OpenGL state. The basic template performs all the GLUT initialization, and has a function called init() dedicated to setting up the OpenGL state. It is also responsible for initializing the call-back hooks, which are discussed below.
GLUT's architecture is based on call-back hooks. It runs a main loop that checks for events, and calls the appropriate hook. This hook is a function specified by you, that responds to that event. Amongst others, there are hooks for responding to redraw events, input device events (e.g., mouse and keyboard), and even a hook for what to do when there are no other events.
The Display Hook
Every GLUT application must have this hook. This function is called whenever the display needs to be redrawn. If a graphics engine were to be used, its "render world" function would be called from the display hook. In the template, the hook function is:
The Idle Hook
This hook is called whenever there are no other events to be processed, that is, when the program is idle. This is where a game engine (if one were writing a game) would go. The template's default code performs a primitive animation by incrementing a rotation variable. The template's idle function is:
Due to GLUT's design, global state variables are used in order to pass information between, say, the idle hook, and the display function. Some would say that this is a flaw, since excessive use of globals is discouraged. However, most games/applications are likely to have some form of global state, and this is more flexible than specifying variables to pass to the hooks as parameters.
There is one issue with using the idle function; it uses all reserve CPU power. With MiniGL this is a slightly bigger issue due to the way that the Warp3D drivers work. The Warp3D drivers have to perform hardware locking in such a way that MiniGL ends up running at a higher priority than other applications for significant periods of time. This can make other applications feel sluggish. The problem generally does not occur with full-screen GLUT applications because GLUT must wait for double-buffered screens to be swapped. Hence, whilst this template is easy to start with, the frame-rate independent animation template is better for more serious work.
Other Call-Back Hooks
There are two other hooks that are used in the template. The first is the reshape hook; the second is the key hook. They are defined as:
void reshape(GLsizei w, GLsizei h);
void key(unsigned char k, int x, int y);
The reshape function is called when the user changes the size of the window. When the user resizes the window, it may be necessary to change the display parameters in order to give a consistent-looking rendered image, regardless of size or aspect ratio.
As the name suggests, the key function responds to key presses. There are additional hooks to deal with "special" keys such as ALT, or SHIFT. For a full list of hooks, consult the GLUT manual.
Simply download (here) and decompress the archive to a directory. Windows users can open the Visual Studio solution file. Amiga users should open a shell (console) window and enter that directory. Type make. Provided that you have the SDK and the MiniGL header files installed, it will compile and produce a binary called GLUT-basic. You can run the template application by typing GLUT-basic and pressing enter. An example of the DOS commands is:
Pressing ESCAPE quits the template application.
Articles » Amiga OS 4 Articles » OpenGL/MiniGL Templates » A Basic GLUT Application