OpenGL window often blank until redraw

I’m working on my first opengl program using GLUT to draw a simple 2-D triangle and print some measurements. For some reason, usually, the opengl window will open up blank until I either resize it or maximize it. Sometimes, though less frequently, maybe 10% of the time, it will come up just fine. Does this seem like a driver or coding issue? I’m using freeglut on linux.

Do you use the Idle or the Display callback or you just do the rendering in the Reshape callback? Maybe the problem is with your usage. Can you give us some code or pseudocode?

when I don’t use ‘glClear (GL_COLOR_BUFFER_BIT)’,it will show a empty window with Maximize/Minimize/Close button,and when my code is wrong it will show a white window.

I am green hand,I just can tell what I got.

I do the rendering in the Display callback. What the Reshape callback adds that the Display doesn’t have is glViewport, which I have tried adding prior to the Display, but it made no difference.

And glClear(GL_COLOR_BUFFER_BIT) is being used in the Display callback.


void process(int * argc, char *** argv)	// Initiate GLUT for drawing
{
	glutInit(argc, *argv);
	glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(1020,340);
	glutInitWindowPosition(0,128);
	glutCreateWindow("Super Triangle Calculator");
	gluOrtho2D(-1,2,0,1);
	glutDisplayFunc(display);
	glutReshapeFunc(aspect);
	glutKeyboardFunc(keyboard);
	glutMainLoop();
}

void display(void)		// Call drawing functions
{
	glClear(GL_COLOR_BUFFER_BIT);
	
	measurements();
	triangle();
	
	glFlush();
}

void measurements(void)		// Print measurements to screen
{
	glColor3f(.1875,.3125,.25);

	print("Counter-clockwise from lower left",-.95,.9);
	//print(,-.95,.85);
}

void print(char * str, float x, float y)
{
	glRasterPos2f(x,y);
	while (*str)
	{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str);
		str++;
	}
}

void triangle(void)		// Draw the triangle to screen
{
	float r, a, x, y;
	
	r = sc / sb;
	a = M_PI_2 - naA;
	x = r * sin(a);
	y = r * cos(a);
	
	glColor3f(.0625,.1875,.125);

	glBegin(GL_TRIANGLES);
	glVertex2f(0,0);
	glVertex2f(1,0);
	glVertex2f(x,y);
	glEnd();
}

void aspect(int w, int h)	// Maintain aspect ratio to prevent skewing
{
	int adjW, adjH;
	int r = 3;
	float win = (float) w / (float) h;
	
	if ( win > r )
	{
		adjW = h * r;
		adjH = h;
	}
	else if ( win < r )
	{
		adjH = w / r;
		adjW = w;
	}
	
	glViewport(0,0,adjW,adjH);
}

void keyboard(unsigned char key, int x, int y)	// Close on key press
{
	if (key)
		glutLeaveMainLoop();
}

Change this:

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

to this:

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

Then add this after your glFlush call in “display”:

glutSwapBuffers ();

OK, so what’s happening here? Basically you’re requesting a single-buffered rendering context but it sounds as though GLUT is actually giving you a double-buffered one. So you need a buffer swap before you’ll actually see things on-screen. When you resize/minimise/etc the app window, the OS does a complete redraw of it and you get to see your scene.