PDA

View Full Version : glClear and glClearColor order doesn't matter



technologist
01-13-2018, 11:23 AM
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.

Alfonse Reinheart
01-13-2018, 11:27 AM
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 `printf`s to see.

technologist
01-13-2018, 01:27 PM
swapBuffer is being called 3 times.

mhagain
01-14-2018, 03:57 AM
This will actually draw nothing:
glBegin( GL_LINES);
glVertex3d( 0, 0, 0);
glVertex3d( 0, 0, 0);
glEnd();

technologist
01-14-2018, 11:27 AM
This will actually draw nothing:
glBegin( GL_LINES);
glVertex3d( 0, 0, 0);
glVertex3d( 0, 0, 0);
glEnd();

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