Search code examples
copenglglut

What is a color buffer in OpenGL?


I am struggling to understand some points in the OpenGL .C code:

  • What is this color buffer?
  • What are the glutInitDisplayMode() and glClear() doing to this buffer in the codes below?
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glClear(GL_COLOR_BUFFER_BIT);

Solution

  • There are several kinds of buffers in OpenGL. The Color Buffer you're mentioning holds the Color components of the render target. The Render target could be an off-screen buffer or a piece of a window, or a back buffer of a window, it depends.

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB) initializes the render target of the current window. GLUT_DOUBLE means, double buffering to avoid flickering. GLUT_RGB means, we need memory for colors, without alpha channel.

    glClear(GL_COLOR_BUFFER_BIT) means, only the Color component of the buffer will cleared. You may ask what is the difference. The reasons are, a render target could be split into multiple buffers, for example, a color buffer and the depth-buffer. The depth-buffer would only be cleared if you call glClear(GL_DEPTH_BUFFER_BIT), you can also combine them via glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) to clear the color and the depth component at once.