Is it possible to clear just certain textures in a framebuffer with multi target rendering?
Asked Answered
W

2

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?

Wooldridge answered 3/8, 2013 at 4:30 Comment(0)
C
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.

Chaulmoogra answered 3/8, 2013 at 5:9 Comment(5)
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)Wooldridge
@darius: "glClearBufferfv" takes an array of floats. That's what the "fv" always means. 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.Chaulmoogra
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, iWooldridge
@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.Chaulmoogra
Thank you for your patience there! I'll read that article again.Wooldridge
T
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

Tigges answered 3/8, 2013 at 5:1 Comment(5)
There are two versions. Click on my link. The classic one masks components, the second one masks components per buffer.Tigges
Thanks, this glColorMaski seems a good solution. Why did this answer go a -1?Wooldridge
@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.Chaulmoogra
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 ).Tigges
@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.Chaulmoogra

© 2022 - 2024 — McMap. All rights reserved.