16

I have a framebuffer object in which I use Multi Target Rendering on N textures binded to it. At a certain time, I want to clear the content of some of those textures, but not all of them. If I call

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

every texture binded to the FBO is going to be cleared (am I right?). Is there a way to do this on specific draw buffers/textures?

darius
  • 837
  • 9
  • 22

2 Answers2

20

The GL_COLOR_BUFFER_BIT in the glClear call will clear all of the active draw color buffers, as specified via glDrawBuffers. So you could change the draw buffers before executing a clear.

But that's needless state changing. You can simply call glClearBuffer, which will clear a particular buffer.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thank you. What is the proper use of glClearBufferfv? Is glClearBufferfv( GL_COLOR_ATTACHEMENT0, 0, &color[0] ) ok? (where color is a float[4] array) – darius Aug 03 '13 at 05:24
  • 2
    @darius: "*glClearBufferfv*" takes an array of floats. That's what [the "fv" always means](https://www.opengl.org/wiki/Nomenclature#Function_suffixes). In this case, it's always 4 floats. Just like the `iv` version takes 4 signed integers (for clearing integer format buffers) and `uiv` takes 4 unsigned integers. – Nicol Bolas Aug 03 '13 at 05:39
  • Thank you for the link! Actually, the part I really had doubts about is the first two arguments. If I understood correctly, they are the GLenum of the buffer, and the its place in the color buffer array of GLenum. So, generally, stuff like GL_COLOR_ATTACHEMENT0+i, i – darius Aug 03 '13 at 05:49
  • @darius: The man page directly states what those two arguments mean. And *that's not it*. Indeed, the wiki article on buffer clearing that I linked to also explains the parameters. – Nicol Bolas Aug 03 '13 at 05:54
  • Thank you for your patience there! I'll read that article again. – darius Aug 03 '13 at 05:58
1

It will be all buffers. You can mask out buffers for clear with glColorMask though. http://www.opengl.org/sdk/docs/man/xhtml/glColorMask.xml

starmole
  • 4,974
  • 1
  • 28
  • 48
  • 1
    There are two versions. Click on my link. The classic one masks components, the second one masks components per buffer. – starmole Aug 03 '13 at 05:08
  • Thanks, this glColorMaski seems a good solution. Why did this answer go a -1? – darius Aug 03 '13 at 05:08
  • @darius: It got a -1 because OpenGL has a function to directly clear a specific buffer. Masking off components to prevent a buffer clear is the wrong tool for the job. – Nicol Bolas Aug 03 '13 at 05:09
  • You are right in bringing up ClearBuffer. I did not think of it and it will probably make his code cleaner. I still can come up with scenarios where masking would have less gl calls, but well :) In the spirit of learning: I still think you are missing the fact that there is a second glColorMask version that is ( bufferindex, r, g, b, a ). – starmole Aug 03 '13 at 05:22
  • @starmole: No, I see it (that's why I deleted my comment and upvoted yours). It's still the wrong tool for the job. There is no need to set global state just to do buffer clearing when it is at all avoidable. – Nicol Bolas Aug 03 '13 at 05:40