Clearing and masking color attachments using FBOs and DBs

I am using framebuffer objects and draw buffers such that I have multiple color attachments. I would like to have different clear colors for each buffer and to be able to clear them independently.

I expected to use something like this:

GLdouble colors[2][4] = {{0.0, 0.0, 0.0, 0.0}, {1.0, 1.0, 1.0, 1.0}};
glClearColors(2, colors);

and:

glClear(GL_COLOR_BUFFER0_BIT | GL_DEPTH_BUFFER_BIT);

However, I haven’t been able to find anything similar in the extensions specifications (framebuffer_object_EXT and draw_buffers_ARB). Instead I’m using a fullscreen quad and a fragment shader to clear the color attachments to the values I want.

Is this sort of functionality available? (Did I miss something when reading the extension specifications?) Is there similar functionality for color masking?

I made a tutorial about it.

The thing is that glClear works on witch ever buffer your rendering to at the moment, so every time i start rendering to a framebuffer i do something like this.

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Originally posted by zeoverlord:
[b] I made a tutorial about it.

The thing is that glClear works on witch ever buffer your rendering to at the moment, so every time i start rendering to a framebuffer i do something like this.

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[/b]
Thanks for the response. :smiley: Although I’m not sure how this answers my question as this example (and the tutorial) only deals with one color buffer attached to the FBO. :confused:

My question is concerning a FBO with two attached color buffers that I would like to clear independently with different clear colors. I can’t find API commands in the FBO or DB extensions to do this, so I’ve either been using fragment shaders or something like this:

//bind framebuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

//clear first color attachment to black
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearBuffer(GL_COLOR_BUFFER_BIT);

//clear second color attachment to white
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClearBuffer(GL_COLOR_BUFFER_BIT);

//attach both (cleared) color buffers for use
GLenum drawbuffers[2] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
glDrawBuffersARB(2, drawbuffers);

//use program that writes to glFragData[0] and glFragData[1]
glUseProgramObjectARB(pg);

//render stuff
}

I’m also wondering if/how to define a different mask for each color buffer.

AFAIK, with glClear() command you can’t clear several draw buffers, according to GL specification.

You may use fragment shader with screen-quad drawing, as you’ve mentioned.