glutFullScreen won't clear screen on exit

Hello all. My program is exhibiting some strange behavior - all help appreciated.

Basically, I am using double-buffering and glutFullScreen together. The program runs great. But when I exit, the program doesn’t just exit. It reverts to the prior frame of animation, THEN exits. Search though I may, I can’t see why. Neither can I see how to fix this simple annoying problem.

When I am not using glutFullScreen, the program seems to just exit when I tell it to. Imbedded tests that I have done show that the reversion is not a “real” render (the display function is NOT called). But it looks so lame during an animation to see a character moving across the screen and then Backtrack when I hit exit! For no reason.

In hopes that someone can explain the (presumed) simplicities of glutFullScreen and double-buffering, I have created a simple example. Please load it and try it to see what I mean. The example program shows just a sphere. Hitting any key toggles between a fully-solid sphere and a wireframe version. If you stop on the solid one, then hit ESC, you will see the afterimage of the prior (wire)frame before the program closes.

I am using openGL 1.3 I think and you will need GLUT as well. Code was written using the free command-line Borland Compiler.

In the following post I am placing the code itself. The problem might be anywhere, but the code is as short as I could make it.

Someone must know how to use glutFullScreen!

Thanks a million,
Sol Hawk

// This is called mysphere.c
//
// an experiment in double-buffering and fullscreen mode
//
// Hit any key to toggle between circle and wireframe
// But if you stop on the solid circle
// And then hit ESC
// It should just exit.
// But it doesn’t. Instead, it reverts to the PRIOR frame, THEN exits
// If I turn fullscreen off, this doesn’t appear to occur
// Why does it revert to the prior frame?
// How can this be fixed?
//

#include <windows.h>

#include <GL\glut.h>
#include <stdlib.h>
#include <stdio.h>

int toggle = 0;

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glTranslatef( 0, 0, 3 );

// Wireframe
if( toggle == 1 ){
glutWireSphere(2.0, 120.0, 40.0);
}

// Solid Spheroid
if( toggle == 0 ){
glutSolidSphere(2.0, 120.0, 40.0);
}

glPopMatrix();

glFlush();
glutSwapBuffers();

}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if( h == 0 ){
gluPerspective(65.0, (GLfloat) w, 0.5, 80.0);
}
else{
gluPerspective(65.0, (GLfloat) w/ (GLfloat) h, 0.5, 80.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
}

void keyboard(unsigned char key , int x, int y )
{

switch (key) {
case 27: /* esc key ends the program */
exit(0);
break;
default:
toggle++;
if ( toggle > 1 ){
toggle = 0;
}
glutPostRedisplay();
break;
} // end switch

}

int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow(argv[0]);
glutFullScreen();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Maybe the back and front buffers get swapped once more just before exit revealing the old frame from the back buffer…

So simply using glClear;Swap before exiting would solve the problem.

Sure enough, the following code before exit fixes the problem:

glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT);

glDrawBuffer(GL_FRONT);
glClear(GL_COLOR_BUFFER_BIT);

Thank you all.

Still curious why the program feels the need to draw both buffers on exit.

Hello all, me one more time.

Another related problem. Even if I employ the fix above, let’s say I do an ALT-Printscreen while the program is running. It captures the wrong buffer! In other words, if I do a screencapture for the above-program (on the solid) then paste it into MS-Paint, the image that appears is - you guessed it - the wireframe!

Could someone here with Fullscreen experience let me know if that’s normal? It seems like something I should be able to repair, and I am sure it must be related to the buffer problem above.

OK, more weird evidence - the Alt-Printscreen works fine if I comment out glutFullScreen(). But if I use glutFullScreen(), it always seems to capture from the back buffer! Either glutFullScreen is broken or I am doing the double-buffering wrong - hm!

Ideas? Especially interested in hearing from someone who has written a double-buffered FullScreen prog - does it happen for you as it does for me?

what video card and driver are you using?

anon,

Mine is a fairly recent card:

ATI Connect 3D Radeon 9600 Series - 128M

Of course the thing that surprises me the most is that this anomoly occurs in Fullscreen Mode but not in the “regular” mode. I found that if I swap the buffers whenever a key is hit (and then again before I exit the keyboard function) this allows Print Screen to work. So it is definitely a buffer thing. But then for some reason, this screws up my exit again, producing the same error that we already fixed above.

Just as a note, I have tried all of this using enterGameMode with the same results.