It is desirable that I wipe the different color attachments of an FBO with different clear colors. Is this possible with GL commands or will I have to do it within shaders?
glClear(GL_COLOR_BUFFER_BIT)
clears the current color buffers with the same color that is globally set (GL_COLOR_CLEAR_VALUE
).
To specify a different color for each buffer you have to change the current buffer through glDrawBuffer
:
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawBuffer(GL_COLOR_ATTACHMENT1);
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
If you already have your draw buffers set up, then you can use glClearBuffer
to shorten that to:
static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, draw_buffers);
// ...
static const float transparent[] = { 0, 0, 0, 0 };
glClearBufferfv(GL_COLOR, 0, transparent);
static const float red[] = { 1, 0, 0, 1 };
glClearBufferfv(GL_COLOR, 1, red);
However, if the FBO is backed by textures, then the easiest way would be to use glClearTexImage
on those textures directly:
// clear to transparent:
glClearTexImage(color_texture0, 0, GL_RGBA, GL_FLOAT, 0);
// clear to specified color:
static const float red = { 1, 0, 0, 1 };
glClearTexImage(color_texture1, 0, GL_RGBA, GL_FLOAT, red);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With