Our project was to construct a simple example of an Opengl program using overlays. We constructed a simple picture with a background. Drawing the picture to the foreground allowing the background to be preserved. In this program you can show or hide the background revealing the overlay. You will be able to see the entire background undamaged by the foreground due to the use of overlays.Inside main, to use overlays you need to check for system compatibiliuty with overylays and then establish an overlay.
int overlaySupport, transparent, red, white;
glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
overlaySupport = glutLayerGet(GLUT_OVERLAY_POSSIBLE);
if(overlaySupport){ //Set up the overlay and hide it initially.
glutEstablishOverlay();
glutHideOverlay( );
glOrtho(0.0,29.0,0.0,19.0,-1.0,1.0);
transparent = glutLayerGet(GLUT_TRANSPARENT_INDEX);
glClearIndex(transparent);
red = (transparent + 1) % glutGet(GLUT_WINDOW_COLORMAP_SIZE);
white = (transparent + 2) % glutGet(GLUT_WINDOW_COLORMAP_SIZE);
glutSetColor(red, 1.0, 0.0, 0.0); // Red
glutSetColor(white, 1.0, 1.0, 1.0); // White
glutOverlayDisplayFunc(redrawOverlay);
}else {
cout << "Sorry overlays are not supported by this machine." << endl;
}Before you can use overlays you must be in indexed color mode, to do so issue the comand
glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);.
The overlaySupport variable is used to test for overlays compatibility. Using the glutLayerGet command with the class constant GLUT_OVERLAY_POSSIBLE, will return a 1 for a compatible system and a 0 otherwise.
Once you have checked for compatibility you must create an overlay using glutEstablishOverlay(). The following command glutHideOverlay() is used to keep the overlay from apearing on the screen, glutShowOverlay ()has the opposite affect. The command glOrtho(0.0,29.0,0.0,19.0,-1.0,1.0); is used to map the points of your image to the overlay. Next the comand transparent = glutLayerGet(GLUT_TRANSPARENT_INDEX); copies the value for the tranparent(see through) color into the indxed color mode array. Next to create the colors you want to use you must position holding variable using the command
red = (transparent + 1) % glutGet(GLUT_WINDOW_COLORMAP_SIZE). This creates a variable red which holds the index for the second position in the indexed color array. Next you must assign this position a color using the command glutSetColor(red, 1.0, 0.0, 0.0);. This assigns the RGB color specified to the position red in the indexed color array. Finally you must create a display function wich holds the image you wish to draw using glutOverlayDisplayFunc(redrawOverlay);.Inside your display function that is used for you original standard display you only need on extra command glutUseLayer(GLUT_NORMAL);. This sets the current layer to the normal plane i.e. the layer normaly used by default in non-layered programs. This must be done at the begining of the display function to insure you are drawing to the proper overlay.
In the redrawOverlay function you must once again use the glutUseLayer(GLUT_OVERLAY); command. This time using the constant GLUT_OVERLAY defining that you wish to draw to the overlay .
In our mouse function:
void choice_selected(int value){
switch(value){
case 1: glutShowOverlay( ); //Show overlay if selected
break;
case 2: glutHideOverlay( ); //Hide overlay is selected
break;
}
}We built a case around our two menu options allowing us to display or hide our overlay. The commands glutShowOverlay( ); and glutHideOverlay( ); allow the overlay to be seen or not, respectively.
The source code for this example can be found at:
group1.cpp
A example points file for group1.cpp:
tank
Here is another example of overlays, this time they are being used for rubberbanding.
Dino.c
Here is the file you can use to compile it, use it just like useglut.
rundino