Open GL Super Bible

Previous Table of Contents Next

Dissecting a Short OpenGL Program

In order to understand the AUX library better, let’s take a look at possibly the world’s shortest OpenGL program, which was written using the AUX library. Listing 3-1 presents the shortest.c program. Its output is shown in Figure 3-2.

Figure 3-2  Output from shortest.c

Listing 3-1 Shortest OpenGL program in the world

// shortest.c
// The shortest OpenGL program possible

#include <windows.h>   // Standard Window header required
for all programs
#include <conio.h>     // Console I/O functions
#include <gl\gl.h>     // OpenGL functions
#include <gl\glaux.h>  // AUX Library functions

void main(void)
        // These are the AUX functions to set up the window
        auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
        auxInitWindow("My first OpenGL Program");

        // These are the OpenGL functions that do something in the window
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);


        // Stop and wait for a keypress
        cprintf("Press any key to close the Window\n");

Console Modes
A console-mode application is a Win32 program that runs in a text mode window. This is very much like running a DOS program under Windows NT or Windows 95, except the program is a true 32-bit application and has access to the entire Win32 API. Console-mode programs are not limited to text mode. They can in fact create GUI windows for auxiliary output (try calling MessageBox() with a NULL window handle from the above program), and GUI-based applications can even create console windows if needed. The AUX library allows you to easily write a console-based program with only a main() function that can create an auxiliary GUI window for OpenGL output.

To build this program, you need to set your compiler and link options to build a Win32 console (or text-based) application. You will need to link to the AUX library glaux.lib and the OpenGL import library opengl32.lib. See your compiler’s documentation for individual instructions on building console applications.

The shortest.c program doesn’t do very much. When run from the command line, it creates a standard GUI window with the caption “My first OpenGL Program” and a clear blue background. It then prints the message “Press any key to close the window” in the console window. The GUI window will not respond to any mouse or keyboard activity, and the console window waits for you to press a key before terminating (you will have to switch focus back to the console window first to do this). It doesn’t even behave very well—you can’t move or resize the OpenGL window, and the window doesn’t even repaint. If you obscure the window with another window and then uncover it, the client area goes black.

This simple program contains three AUX library functions (prefixed with aux) and three “real” OpenGL functions (prefixed with gl). Let’s examine the program line by line, after which we’ll introduce some more functions and substantially improve on our first example.

The Includes

Here are the include files:

#include <windows.h>
#include <conio.h>
#include <gl\gl.h>
#include <gl\glaux.h>

These includes define the function prototypes used by the program. The windows.h header file is required by all Windows GUI applications; even though this is a console-mode program, the AUX library creates a GUI window to draw in. The file conio.h is for console I/O. It’s included because we use cprintf() to print a message, and getch() to terminate the program when a key is pressed. The file gl.h defines the OpenGL functions that are prefixed with gl; and glaux.h contains all the functions necessary for the AUX library.

The Body

Next comes the main body of the program:

void main(void)

Console mode C and C++ programs always start execution with the function main(). If you are an experienced Windows nerd, you may wonder where WinMain() is in this example. It’s not there because we start with a console-mode application, so we don’t have to start with window creation and a message loop. It is possible with Win32 to create graphical windows from console applications, just as it is possible to create console windows from GUI applications. These details are buried within the AUX library (remember, the AUX library is designed to hide these platform details).

Previous Table of Contents Next