opengl gbuffer使用GL_TEXTURE_2D_MULTISAMPLE纹理问题

时间:2017-10-30 05:55:35

标签: opengl

我首先将场景渲染为gbuffer(位置,法线,颜色),但在延迟渲染传递中,颜色纹理一直抖动,所以我想改变颜色纹理到GL_TEXTURE_2D_MULTISAMPLE,如下所示,但它失败了,为什么?

enum _GL_GBUFFER_TEXTURE_TYPE_ { _position_, _normal_, _albedo_spec_,_texture_cnt_ };
bool initialize(int2 dim){
        dim2 = dim;

        //gbuffer
        glGenFramebuffers(1, &gBuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);

        //position texture
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + _position_, GL_TEXTURE_2D,
                                             textures[_position_].create2d(dim2, GL_RGBA32F/*GL_RGB16F*/, GL_RGBA/*GL_RGB*/, GL_FLOAT, NULL), 0);
        //normal texture
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + _normal_, GL_TEXTURE_2D,
                                              textures[_normal_].create2d(dim2, GL_RGBA32F/*GL_RGB16F*/, GL_RGBA/*GL_RGB*/, GL_FLOAT, NULL), 0);
        //color texture
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + _albedo_spec_, GL_TEXTURE_2D_MULTISAMPLE,
                                              textures[_albedo_spec_].create2d_msaa(dim2,GL_RGBA8,8), 0);
                                              //textures[_albedo_spec_].create2d(dim2,GL_RGBA,GL_RGBA,GL_UNSIGNED_BYTE,NULL),0);

        //tell OpenGL which color attachments we'll use (of this framebuffer) for rendering 
        GLuint attachments[_texture_cnt_]; 
        for (int i = 0; i < _texture_cnt_; i++) attachments[i] = GL_COLOR_ATTACHMENT0 + i;
        glDrawBuffers(_texture_cnt_, attachments);

        //create and attach depth buffer (renderbuffer)
        glGenRenderbuffers(1, &rboDepth);
        glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, dim2.x, dim2.y);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);

        //finally check if framebuffer is complete
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
            std::cout << "[GBuffer] : Framebuffer not complete!" << std::endl;
            return false;
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        return true;
    }

1 个答案:

答案 0 :(得分:3)

你的帧缓冲不完整,因为(见Khronos reference page - glCheckFramebufferStatus):

  如果所有连接的渲染缓冲区的GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE值不相同,则返回

GL_RENDERBUFFER_SAMPLES;如果GL_TEXTURE_SAMPLES的值与所有附加纹理不同;或者,如果附加的图像是渲染缓冲区和纹理的混合,则GL_RENDERBUFFER_SAMPLES的值与GL_TEXTURE_SAMPLES的值不匹配。

这意味着,附加到帧缓冲区的所有纹理都必须是多重采样纹理。