Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL - Different clear color for individual color attachments?

Tags:

opengl

glsl

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?

like image 916
livin_amuk Avatar asked Jun 26 '17 09:06

livin_amuk


1 Answers

glClear(GL_COLOR_BUFFER_BIT) clears the current color buffers with the same color that is globally set (GL_COLOR_CLEAR_VALUE).

OpenGL 2+

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);

OpenGL 3.0+

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);

OpenGL 4.4+

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);
like image 73
Yakov Galka Avatar answered Sep 28 '22 06:09

Yakov Galka