OpenGL GLUT Tutorial
home: http://www.geocities.com/SiliconValley/Code/1219/
e-mail: craterz@hotmail.com

Why GLUT?

I can think of three great reasons to use GLUT. First, it's OpenGL, you can't beat that. Second, it's amazingly simple to use compared to other windowing APIs. Third, it's portable to likely anything under the sun: Win32, Linux, UNIX, and maybe even Macintosh. Use GLUT, OpenGL, stick to ANSI C/C++ and other multi-platform libraries and you'll have great portability. For my own example, in using MIDAS on The Fuzz with GLUT, I have the potential to be able to mearly recompile and have the program run with minimal hassle on either Win32 or Linux.

GLUT provides the obvious window interface. But it also provides input. In the latest version (GLUT 3.7), you have the ability to use keyboard (and indirectly determine if a key is down at any point in time), mouse, and joystick under Win32.

glutGameModeString

glutGameModeString is a useful function for setting the screen resolution, refresh rate, and bit depth for fullscreen applications (like games). The sole parameter is a pointer to a string which specifies the screen properties desired.

glutGameModeString( "640x480:16@60" );

In the above example, this selects a resolution of 640x480, 16 bits per pixel (bit depth), and 60Hz refresh rate. You can create your own game mode string using the following sprintf statement or your own method.

char temp[64];
sprintf( temp, "%dx%d:%d@%d", width, height,
         pixelDepth, refreshRate );

Think of this as a request to the GLUT system. You aren't necessarily guarenteed the mode you requested.

glutGameModeGet

To see what you are working with, simply use the glutGameModeGet function. The single arguement specifies what you are trying to get. Here's a list of possible arguements.

Arguement     Description    
GLUT_GAME_MODE_ACTIVE     Whether game mode is active.    
GLUT_GAME_MODE_POSSIBLE     Whether game mode is possible.    
GLUT_GAME_MODE_WIDTH     The game mode screen width (ex. 640 pixels).    
GLUT_GAME_MODE_HEIGHT     The game mode screen height (ex. 480 pixels).    
GLUT_GAME_MODE_PIXEL_DEPTH     The game mode pixel depth (ex. 16bits).    
GLUT_GAME_MODE_REFRESH_RATE     The game mode screen pixel depth (ex. 60Hz).    
GLUT_GAME_MODE_DISPLAY_CHANGED     Whether game mode changed the display.    

To determine what mode we are dealing with, the following code snippet should suffice for most cases.

width = glutGameModeGet( GLUT_GAME_MODE_WIDTH );
height = glutGameModeGet( GLUT_GAME_MODE_WIDTH );
pixelDepth = glutGameModeGet( GLUT_GAME_MODE_PIXEL_DEPTH );
refreshRate = glutGameModeGet( GLUT_GAME_MODE_REFRESH_RATE );

glutEnterGameMode

Instead of worrying about a bunch of calls to the Operating System to initialize a fullscreen window and create the OpenGL interface, you can do it in one single call to glutEnterGameMode. It's that simple. Think of it as a replacement to your usual call of glutCreateWindow.

Then you must setup your callbacks (display, keyboard, etc) after the call to glutEnterGameMode just like an ordinary GLUT program.

int main(int argc, char * argv[])
{
    // initialize GLUT
    glutInit( &argc, argv );

    // set RGBA mode with double and depth buffers
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );

    // 640x480, 16bit pixel depth, 60Hz refresh rate
    glutGameModeString( "640x480:16@60" );

    // start fullscreen game mode
    glutEnterGameMode();

    // setup window callbacks
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutIdleFunc( idle );

    ...

    // enter main loop
    glutMainLoop();

    return 0;
}

glutLeaveGameMode

After you are finished with the fullscreen window, you can get rid of it with a call to glutLeaveGameMode somewhere where you are shutting down.

HOWTO: Check if a Key is Down

One thing I liked in both Allegro and DirectInput, is that you could determine if any key on the keyboard was down at any time by simply looking up an entry in an array. You can do the exact same thing with GLUT by setting up two callback functions. This is shown below. You will need GLUT 3.7 for this to work, as glutKeyboardUpFunc is required to set the necessary callback.

int KeyDown[256];

void keyboard(unsigned char key, int x, int y)
{
    KeyDown[key] = 1;
}

void keyboardUp(unsigned char key, int x, int y)
{
    KeyDown[key] = 0;
}

...

    memset( KeyDown, 0, sizeof( KeyDown ) );
    glutKeyboardFunc( keyboard );
    glutKeyboardUpFunc( keyboardUp );

...

    if ( KeyDown['a'] )
        shootGun();

...

[ powered by Latte and Python | last updated Sat Aug 25 00:55:02 2001 PST ]