26

As an OpenGL beginner I would like to know what do they do and why these are required. For instance in the call

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Pierluigi
  • 2,212
  • 1
  • 25
  • 39
Mohamed Amged
  • 489
  • 1
  • 5
  • 7

3 Answers3

21

GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT aren't functions, they're constants. You use them to tell glClear() which buffers you want it to clear - in your example, the depth buffer and the "buffers currently enabled for color writing". You can also pass GL_ACCUM_BUFFER_BIT to clear the accumulation buffer and/or GL_STENCIL_BUFFER_BIT to clear the stencil buffer.

The actual values of the constants shouldn't matter to you when using the library - the important implementation detail is that the binary representations for each constant don't overlap with each other. It's that characteristic that lets you pass the bitwise OR of multiple constants to a single call to glClear().

Check out the glClear() documention for more details.

Matthew
  • 3,510
  • 2
  • 23
  • 24
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 4
    I'm asking about those constants why we use them I want more Information about them please!! – Mohamed Amged Mar 29 '11 at 23:08
  • @Mohamed - what do you mean more information? The documentation has everything you need to know. Do you have a more specific question? – Carl Norum Mar 29 '11 at 23:09
  • 1
    the documentations tells me GL_COLOR_BUFFER_BIT Indicates the buffers currently enabled for color writing. GL_DEPTH_BUFFER_BIT Indicates the depth buffer. – Mohamed Amged Mar 29 '11 at 23:12
  • and I want more about those constants please!! :) – Mohamed Amged Mar 29 '11 at 23:13
  • 1
    There isn't any more information about them... I guess you could look in `gl.h`. On my machine `GL_COLOR_BUFFER_BIT` is `0x00004000` and `GL_DEPTH_BUFFER_BIT` is `0x00000100`. What else is there to know about? – Carl Norum Mar 29 '11 at 23:19
  • 1
    @Carl Norum: The numeric values of those OpenGL tokens (constants) are written down in the specification. @Mohamed Amged: The value passed to glClear is evaluated within the OpenGL driver by some code similar to this: glClearOfTheDriver(GLbitfield mask){ if(mask & GL_COLOR_BUFFER_BIT) clear_the_color_buffer; if(mask & GL_DEPTH_BUFFER_BIT) clear_the_depth_buffer; ... }; there's really nothing more to this. Those tokens passable to glClear are orthogonal (bit) vectors (look up orthogonal vectors on Wikipedia to understand what this means). – datenwolf Mar 30 '11 at 07:18
  • I have got a question. I was trying to run an OpenGL code, it didn't have GL_DEPTH_BUFFER_BIT cleared in glClear(), because of which I couldn't render my scene. I added this bit, and the scene was rendered. Why is it necessary to use this clear bit? I may know the exact reason for this, to clear the depth buffers values used by GPU prevoiously, but i just want to confirm. – 2am Oct 19 '13 at 13:40
  • @2am, you should probably post a question, not a comment. – Carl Norum Oct 19 '13 at 14:40
  • 4
    @CarlNorum, Got a prefect explanation here. Cheers. [Why do we clear GL_DEPTH_BUFFER_BIT](http://stackoverflow.com/questions/19469194/why-do-we-have-to-clear-depth-buffer-in-opengl-during-rendering/19469291#19469291) – 2am Oct 20 '13 at 10:37
  • 2
    @2am Thank you, that's what I was expecting from here. Not "check documentation". – Neerkoli Jun 09 '16 at 11:57
4

A call to glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) clears the OpenGL color and depth buffers (or any other buffer or combination of buffers). OpenGL being a state machine, it is good practice to start each frame with a clean slate.

LastBlow
  • 617
  • 9
  • 16
1

I stumble upon this question while reading about this and thought to add some details for anyone confused. I see these two variables are constant represent options in bit values.

The glClear() method needs to know what type of buffer to clear. However, there are many buffers such as color and depth buffers and others (https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glClear.xml).

By using bits to represent options it become easier to set multiple options by performing an OR operation.

For more details check out this "Bit Manipulation" tutorial, namely "When are bit flags most useful?" section at : https://www.learncpp.com/cpp-tutorial/bit-manipulation-with-bitwise-operators-and-bit-masks/