簡體   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