PDA

View Full Version : single fbo and multiple texture attachments



Triangle
03-02-2013, 11:31 AM
I want to use a single fbo and multiple texture attachments. I attach textures to different color attachments.
Then I use glDrawBuffers to switch rendering to different color attachments.

Textures have different formats and resolutions:


glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindTexture(GL_TEXTURE_2D, tex1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 1024, 1024, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);

glBindTexture(GL_TEXTURE_2D, tex2);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);


glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex1, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex2, 0);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

GLenum drawBuffers0[] = {GL_COLOR_ATTACHMENT0};
GLenum drawBuffers1[] = {GL_COLOR_ATTACHMENT1};



Rendering:



glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawBuffers(1, drawBuffers1);
glViewport(0, 0, 256, 256);
shader1.enable();
object1.draw();
shader1.disable();

glDrawBuffers(1, drawBuffers0);
glViewport(0, 0, 1024, 1024);
shader2.enable();
object2.draw();
shader2.disable();

glBindFramebuffer(GL_FRAMEBUFFER, 0);


tex1 contains only part of the screen (1/4x, 1/4y). What is wrong ?

tonyo_au
03-02-2013, 11:18 PM
AFAIK all attachments must be the same size. You should check for completeness when constructing FBOs



GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if( status != GL_FRAMEBUFFER_COMPLETE)

Triangle
03-04-2013, 05:35 AM
Ok, I have one more question. Is it possible to clear only one texture this way ?:


glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glDrawBuffers(1, drawBuffers0);
glClear(GL_COLOR_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

tonyo_au
03-04-2013, 08:42 PM
It should work. I actually use a shader to clear each of buffers to different values.