0
\$\begingroup\$
  1. Create FBO & its textures like normal.

    // FBO & Texture
    glGenFramebuffers(1, &mFbo);
    glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
    glGenTextures(2, mTexture);
    
    for(int i=0; i<2; i++)
    {
        // Init texture
        glBindTexture(GL_TEXTURE_2D, mTexture[i]);
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_W, SCREEN_H, \
            0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glBindTexture( GL_TEXTURE_2D, 0 );
    
        // attach to FBO
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, GL_TEXTURE_2D, \
            mTexture[i], 0);
    }
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    glUseProgram(0);
    
  2. Bind FBO and render it inside draw loop.

    // Preparing FBO texture.
    glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    GLuint at0[1] =  {GL_COLOR_ATTACHMENT0};
    GLuint at1[1] = {GL_COLOR_ATTACHMENT0+1};
    
    glDrawBuffers(1, at0);
    // Filled texture attachment0 less white (transparency = 0.5f)
    GUtils::VideoDriver::GetInstance()->DrawFillRect(glm::vec2(0,0), \
            SCREEN_W, SCREEN_H, glm::vec4(glm::vec3(1.0f/(1)), 0.5f));
    
    glDrawBuffers(1, at1);
    // Filled texture attachment0+1 white.
    GUtils::VideoDriver::GetInstance()->DrawFillRect(glm::vec2(0,0), \
            SCREEN_W, SCREEN_H, glm::vec4(glm::vec3(1.0f/(1)), 1.0f));
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    

After all things I did above:

  • texture attachment0 (mTexture[0]) will be filled in less white.
  • texture attachment1 (mTexture[1]) will be filled in white.

Right?

I cannot understand when render FBO texture:

glBindTexture(GL_TEXTURE_2D, mTexture[index]);
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0 );
glBindTexture(GL_TEXTURE_2D, 0);
  1. mTexture[0] , screen = white. (It must be less White)
  2. mTexture[1] , screen = black. (It must be White)

Firstly, Did i do a right way to render attached FBO texture or am I misunderstood fbo attachment?

Lastly, How can I fix this?

\$\endgroup\$
2
  • \$\begingroup\$ Does your fragment shader write to gl_FragColor? I suspect that when you try to write your second texture you are actually overwriting your first one. glDrawBuffers only defines where writes to user defined outputs (i.e. those using layout(location = 0) out ...) go, so all of your fragment writes are going to the texture attached to GL_COLOR_ATTACHMENT0 \$\endgroup\$
    – GuyRT
    Feb 12, 2015 at 16:59
  • \$\begingroup\$ ah ha, I think I got a bit of understanding :) need more time to investigate. But thank all of you, guys. You are my angles. \$\endgroup\$
    – LongLT
    Feb 12, 2015 at 17:06

1 Answer 1

0
\$\begingroup\$

To use Multiple Render Targets you need to use pixel shaders to tell the GPU where to send each color values.

layout(location = 0) out vec4 my_first_FBO_color_buffer;
layout(location = 1) out vec4 my_second_FBO_color_buffer;

void main() {
   my_first_FBO_color_buffer = vec4(0, 1, 0, 1);
   my_second_FBO_color_buffer = vec4(1, 0, 1, 1);
}

See What is the recommended way to output values to FBO targets? (OpenGL 3.3 + GLSL 330)

\$\endgroup\$
2
  • \$\begingroup\$ Sorry that I do not really understood. Because I think each texture has its paticular data. Can I render each texture in sequence? I mean the first will be rendered at position 0,0. The second will be rendered at 100, 100 (assume that) or just render the only one like my dummy code above. attach 2, but render only second... Can I do that? \$\endgroup\$
    – LongLT
    Feb 12, 2015 at 16:29
  • \$\begingroup\$ Thank you, Stephane. Now i know what I have to do. Thank you very much \$\endgroup\$
    – LongLT
    Feb 12, 2015 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .