Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: glClear and glClearColor order doesn't matter

  1. #1
    Junior Member Regular Contributor
    Join Date
    May 2017
    Posts
    101

    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:

    Code :
     
    #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.

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,070
    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.

  3. #3
    Junior Member Regular Contributor
    Join Date
    May 2017
    Posts
    101
    swapBuffer is being called 3 times.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    This will actually draw nothing:
    Code :
        glBegin( GL_LINES);
        glVertex3d( 0, 0, 0);
        glVertex3d( 0, 0, 0);
        glEnd();

  5. #5
    Junior Member Regular Contributor
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by mhagain View Post
    This will actually draw nothing:
    Code :
        glBegin( GL_LINES);
        glVertex3d( 0, 0, 0);
        glVertex3d( 0, 0, 0);
        glEnd();
    That's correct. I was focusing on background color.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •