屏外渲染opengl 4.5多重采样FBO

时间:2014-11-14 12:05:05

标签: c++ antialiasing framebuffer fbo opengl-4

我在我的代码中引用了OpenGL Superbible 6。 首先,我只是想在我的3D场景中实现对象拾取。最终我决定使用framebuffer对象并且我已经成功然后我理解了解决多边形边缘别名问题的问题,因此,我已经重新编写了我的代码以使用GL_TEXTURE_2D_MULTISAMPLE < / p>

这是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);
}

对于同一主题的大多数互联网列表而言,它很常见。 现在这是我的绘图代码

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);
}

顶点着色器很简单

#version 450

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

片段着色器的编写目的是为了解析多重样本

#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;
}

我最终得到了黑屏。如果我将out_color更改为lice out_color = vec4(1.0,0.0,0.0,1.0),我会看到红屏。

  1. 可能出现什么问题?
  2. 当我将GL_DEPTH_COMPONENT传递给glTexStorage2DMultisample时,在帧缓冲区的初始化函数中,我得到错误。我决定通过GL_DEPTH_COMPONENT16并且它有效。那是为什么?
  3. 我是否应该更好地使用RENDERBUFFER进行一些操作?如果是,我该如何阅读纹理呢?

1 个答案:

答案 0 :(得分:1)

当您渲染到FBO时,标识为texture_id_framebuffer_color的纹理(用于最终渲染的纹理)不会附加到FBO:

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

一次只能将一个纹理附加到给定的附着点。因此,当您指定要附加到COLOR_ATTACHMENT0的第二个纹理时,第一个纹理会自动取消连接。

如果您想要两个附件,他们需要使用不同的附件点:

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