glClear and glClearColor order doesn't matter

In the following code it doesn’t matter to the background color I am drawing, whether glClear comes first or glClearColor comes first. I don’t understand that: I thought glClear would clear the color buffers so at least in the example if I had glClearColor called first then glClear, the screen should be changed since the color information is cleared:



#include <gl/freeglut.h>
#include <math.h>

void keyboard(unsigned char key, int x, int y)
{
	switch ( key )
	case 27:
	exit( 0 );
}

void render()
{

       glClearColor(1.0, 1.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT); // should celar the color from glClearColor right?
	glBegin( GL_LINES);
	glVertex3d( 0, 0, 0);
	glVertex3d( 0, 0, 0);
	glEnd();
	glutSwapBuffers() ;
}

int main( int argc, char** argv )
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(0,0);
  glutInitWindowSize(1920,1080);
  glutCreateWindow("Lines freeGLUT x64 Window");
  glutFullScreen();
  glutDisplayFunc( render );
  glutKeyboardFunc(keyboard);
  glutMainLoop();
}

I’m sure this is a major “newbism”. Thanks in advance.

OpenGL retains all state that you provide. glClearColor sets state which glClear uses.

The first time render gets called, glClearColor has the default value of 0. But the second time it gets called, it has the previously set value. So there is only one clear call that would use the default value.

The reason you don’t see the “first time” clear color could be that, for whatever reason, at the start of your application FreeGLUT feels the need to call render twice. Maybe it calls it a second time due to the window’s size being set or for some other reason. Do some printfs to see.

swapBuffer is being called 3 times.

This will actually draw nothing:

    glBegin( GL_LINES);
    glVertex3d( 0, 0, 0);
    glVertex3d( 0, 0, 0);
    glEnd();

[QUOTE=mhagain;1290084]This will actually draw nothing:

    glBegin( GL_LINES);
    glVertex3d( 0, 0, 0);
    glVertex3d( 0, 0, 0);
    glEnd();

[/QUOTE]

That’s correct. I was focusing on background color.