Skip to main content
added 89 characters in body
Source Link
LongLT
  • 117
  • 6
  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)

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

Lastly, How can I fix this?

  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)

Why this happens?

How can I fix this?

  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?

Source Link
LongLT
  • 117
  • 6

How to render specific FBO Texture attachment (assume attachment1) with 2 attached

  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)

Why this happens?

How can I fix this?