屏幕外渲染 OpenGL 4.5 多样本 FBO

offscreen rendering opengl 4.5 multisample FBO

本文关键字:样本 FBO OpenGL 屏幕      更新时间:2023-10-16

我在代码中引用了OpenGL Superbible 6。首先,我只想在3D场景中实现对象拾取。最终我决定使用帧缓冲对象并且我成功了,然后我理解了需要解决多边形边缘锯齿问题的问题,因此,我再次重写了代码以利用GL_TEXTURE_2D_MULTISAMPLE

这是帧缓冲的初始化代码无效 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更改为虱子out_color=vec4(1.0,0.0,0.0,1.0),我会得到红屏。

  1. 可能出现什么问题?
  2. 在帧缓冲的初始值设定项函数中,当我将GL_DEPTH_COMPONENT传递给 glTexStorage2DMultisample 时,我得到错误。我决定通过GL_DEPTH_COMPONENT16,它有效。为什么?
  3. 我是否应该更好地使用RENDERBUFFER进行某些处理,如果是,我如何将其读取为纹理?

渲染到 FBO 时,ID 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);