繁体   English   中英

OpenGL渲染到Framebuffer会产生白色矩形纹理

[英]OpenGL rendering to Framebuffer results in a white rectangle texture

这是我要执行的主要程序:

FrameBuffer* buffer = new FrameBuffer(960, 540);
buffer->Bind();
// render some textures
buffer->Unbind();
glViewport(0, 0, 960, 540);

Texture* texture = buffer->GetTexture();
// render the texture

但是,我看到的是白色矩形,而不是纹理。 如果在屏幕上进行渲染,则会得到预期的结果(因此渲染代码应该没有问题)。 这是FrameBuffer类:

class FrameBuffer
{
private:
    Texture* m_Texture;
    unsigned int m_FrameBufferID;
    unsigned int m_DepthBufferID;
    unsigned int m_Width;
    unsigned int m_Height;
    Vector4f m_ClearColor;

public:
    FrameBuffer(unsigned int width, unsigned int height)
        : m_Width(width), m_Height(height), m_ClearColor(Maths::Vector4f(0, 0, 0, 0))
    {
        Create(m_Width, m_Height);
    }

    ~FrameBuffer()
     {
        glDeleteFramebuffers(1, &m_FrameBufferID);
     }

    void Bind() const
    {
        glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferID);
        glViewport(0, 0, m_Width, m_Height);
    }

    void Unbind() const
    {
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }
    void Clear() const
    {
        glClearColor(m_ClearColor.x, m_ClearColor.y, m_ClearColor.z, m_ClearColor.w);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    inline const Texture* GetTexture() const { return m_Texture; }

    private:
        void Create(unsigned int width, unsigned int height)
        {
            glGenFramebuffers(1, &m_FrameBufferID);
            glGenRenderbuffers(1, &m_DepthBufferID);

            Texture::SetFilterMode(TextureFilter::Linear);
            m_Texture = new Texture(width, height, 32);  // This creates a 32 bit texture (RGBA) with GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER set to linear

            glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferID);
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetTextureID(), 0);

            glBindRenderbuffer(GL_RENDERBUFFER, m_DepthBufferID);
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);

            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_DepthBufferID);

            glBindFramebuffer(GL_FRAMEBUFFER, 0);
        }
    };

其他所有内容都经过测试,因此问题应该出在主程序的逻辑或FrameBuffer中。

顺便说一下,视口大小iz与窗口大小完全相同。

假设您的create方法中的注释正确,则将无法使用:

        m_Texture = new Texture(width, height, 32);  // This creates a 32 bit depth texture with GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER set to linear

        glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferID);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetTextureID(), 0);

您不能将深度纹理附加到颜色缓冲区。 纹理格式GL_DEPTH_COMPONENT*GL_DEPTH*_STENCIL*永远都不是可渲染的颜色

我不知道该评论是否只是一个错字,您打算创建标准的RGB还是RGBA颜色纹理,或者您是否实际上想要深度缓冲的纹理。 在后一种情况下,您可以将其附加到深度附加点(而不是像现在那样使用渲染缓冲区)。 然后,您可以进行仅深度渲染,并且根本不需要颜色缓冲区。

但是也许您只需要一个通道的32位纹理,并且可以GL_R32F使用GL_R32F格式,以便可以存储片段着色器输出的r通道。 您还可以通过这种方式将每个片段gl_FragDepth存储在颜色缓冲区中(但可能不如直接使用带有深度纹理的深度附件那样有效,特别是因为您基本上仍然需要使用深度缓冲区进行实际的深度测试) 。

        glBindRenderbuffer(GL_RENDERBUFFER, m_DepthBufferID);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);

        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_DepthBufferID);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM