I am using an FBO with 4 color buffers to implement deferred shaders. It all works quite well, except that I can’t find out how to do selective clear of buffers. I want to initiate the first buffer (the diffuse data to gray), and set the others to 0. How are you supposed to do that?
I try as follows:
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboName);
float zero[] = { 0.0f, 0.0f, 0.0f, 0.0f };
float gray[] = { 0.584f, 0.620f, 0.698f, 1.0f };
GLenum bufferlist[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, bufferlist);
glClear(GL_DEPTH_BUFFER_BIT);
glClearBufferfv(GL_COLOR, 0, gray);
glClearBufferfv(GL_COLOR, 1, zero);
glClearBufferfv(GL_COLOR, 2, zero);
glClearBufferfv(GL_COLOR, 3, zero);
But this will not initialize buffer 0 to gray. If I remove the clearing of buffer 1 to 3, then buffer 0 will be gray! It is as if the second argument (drawbuffer) is ignored.
Next try, I did as follows:
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboName);
glDrawBuffer(GL_COLOR_ATTACHMENT0); // Select the diffuse buffer
glClearColor(0.584f, 0.620f, 0.698f, 1.0f); // Gray background
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
GLenum bufferlist[] = { GL_NONE, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, bufferlist); // Select all but the diffuse buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set everything else to zero.
glClear(GL_COLOR_BUFFER_BIT);
Again, the second call to glClear() will also clear buffer 0. Maybe you can’t use glDrawBuffers() to control what buffers are cleared.