offscreen rendering opengl 4.5 multisample FBO

1.1k views Asked by At

I'm referencing OpenGL Superbible 6 in my code. First I simply wanted to implement object picking in my 3d scene. Eventually I've decided to use framebuffer objects and I have succeeded and then I understood the problem with the need to solve the problem of polygon edge aliasing, so, i've rewritten my code again to make use of GL_TEXTURE_2D_MULTISAMPLE

Here is the initialization code for framebuffer void window_glview::init_framebuffer()

{
    //CREATE FRAMEBUFFER OBJECT
    GLenum gl_error=glGetError();

    glGenTextures(1,&texture_id_framebuffer_color);
    glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_color);
    glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,ANTIALIASING_SAMPLES,GL_RGBA8,client_area.right,client_area.bottom,GL_TRUE);

    glGenTextures(1,&texture_id_framebuffer_objectid);
    glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_objectid);
    glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,ANTIALIASING_SAMPLES,GL_RGBA8,client_area.right,client_area.bottom,GL_TRUE);

    glGenTextures(1,&texture_id_framebuffer_depth);
    glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_depth);
    glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,ANTIALIASING_SAMPLES,GL_DEPTH_COMPONENT32,client_area.right,client_area.bottom,GL_TRUE);
    gl_error=glGetError();

    glGenFramebuffers(1,&buffer_id_framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER,buffer_id_framebuffer);
    gl_error=glGetError();
    glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_color,0);
    glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_objectid,0);
    glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,texture_id_framebuffer_depth,0);

    GLenum draw_buffers[] =
    {
        GL_COLOR_ATTACHMENT0,
        GL_COLOR_ATTACHMENT1
    };
    glDrawBuffers(2,draw_buffers);
    GLenum status=glCheckFramebufferStatus(GL_FRAMEBUFFER);

    if(status!=GL_FRAMEBUFFER_COMPLETE)
        MessageBox(0,L"Failed to create framebuffer object",0,0);
    glBindFramebuffer(GL_FRAMEBUFFER,0);
}

It's pretty common to most of the internet listings on the same topic. Now here is my drawing code

void window_glview::paint()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


        //DRAW TO CUSTOM FRAMEBUFFER
        glBindFramebuffer(GL_FRAMEBUFFER,buffer_id_framebuffer);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glLineWidth(1.0);
        draw_viewport();
        viewport_object_count=0;

        draw_lights();
        glLineWidth(1.5);
        for (unsigned short i=0;i<mesh_count;i++)
        {
            draw_mesh(mesh_table[i],GL_TRIANGLES,false);
        }

    //DRAW TO DEFAULT
    glBindFramebuffer(GL_FRAMEBUFFER,0);

    //USE TEXTURE FROM FRAMEBUFFER COLOR_ATTACHMENT0
    glUseProgram(program_id_screen_render);
    glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_color);

    //HERE IS A QUAD DRAWING PROCESS
    glBindBuffer(GL_ARRAY_BUFFER,buffer_id_screen_quad);
    glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,24,0);
    glEnableVertexAttribArray(0);

    glDrawArrays(GL_QUADS,0,4);
    SwapBuffers(hDC);
}

vertex shader is simple

#version 450

layout(location=0) in vec4 _pos;
void main(void)
{
    gl_Position=_pos;
}

fragment shader is written with the purpose of iterpreting multisamples

#version 450
uniform sampler2DMS screen_texture;
layout(location=0) out vec4 out_color;

void main(void) 
{
    ivec2 coord=ivec2(gl_FragCoord.xy);
    vec4 result=vec4(0.0);
    int i;
    for (i=0;i<4;i++)
    {
        result=max(result,texelFetch(screen_texture,coord,i));
    }
    out_color=result;
}

I end up with a black screen. If i change out_color to something lice out_color=vec4(1.0,0.0,0.0,1.0) i get red screen.

  1. What could go wrong?
  2. In my initializer function for framebuffer when i pass GL_DEPTH_COMPONENT to glTexStorage2DMultisample, then i get error. I decided to pass GL_DEPTH_COMPONENT16 and it works. Why is that?
  3. Should I better use RENDERBUFFER for some perpose and if yes, how can i read it to texture?
1

There are 1 answers

1
Reto Koradi On

The texture with id texture_id_framebuffer_color, which is the texture you use for your final rendering, is not attached to the FBO while you render to the FBO:

glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_color,0);
glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_objectid,0);

Only one texture can be attached to a given attachment point at a time. So when you specify a second texture to be attached to COLOR_ATTACHMENT0, the first one automatically gets un-attached.

If you want to have two attachments, they will need to use different attachment points:

glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_color,0);
glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT1,texture_id_framebuffer_objectid,0);