OpenTK FBO不显示任何内容以显示屏幕

时间:2020-05-18 21:11:47

标签: c# opengl opentk fbo

我正在使用OpenTK并尝试将几何图形绘制到FBO,然后将其无边框地重新拉回到屏幕上。问题是没有东西被吸引到屏幕上。我回来了一个开放的GL错误“ InvalidOperation”,但我不太确定出什么问题了。注意,如果删除围绕DrawInternals的代码,则我的几何图形会很好地绘制,因此我至少知道它已被绘制到FBO。另外请注意,我的Src和Dest大小始终相同。

编辑:为了突出我的目标,我进一步简化了代码,不再出现错误,只是出现黑屏。

这是我创建的FBO类:

public class FrameBuffer
{
    public int FrameBufferId { get; private set; }
    public int TextureId { get; private set; }

    public int Width { get; private set; }
    public int Height { get; private set; }

    public FrameBuffer(int width, int height)
    {
        Width = width;
        Height = height;
        FrameBufferId = CreateFrameBuffer();
        Bind();
        TextureId = CreateTextureAttachment();
        Unbind();
    }

    public void Bind()
    {
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBufferId);
        GL.Viewport(0, 0, Width, Height);
        GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
    }

    public void Unbind()
    {
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        GL.Viewport(0, 0, Width, Height);
    }

    public void UpdateSize(int width, int height)
    {
        Width = width;
        Height = height;
        GL.DeleteTexture(TextureId);
        Bind();
        TextureId = CreateTextureAttachment();
        Unbind();
    }

    public void CleanUp()
    {
        GL.DeleteFramebuffer(FrameBufferId);
        GL.DeleteTexture(TextureId);
    }

    private int CreateFrameBuffer()
    {
        int frameBufferObject = GL.GenFramebuffer();
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferObject);
        return frameBufferObject;
    }

    private int CreateTextureAttachment()
    {
        int textureId = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, textureId);
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Width, Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, (IntPtr)null);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
        GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, textureId, 0);
        return textureId;
    }
}

这是我的使用方式:

public void Draw()
{
    offScreenframeBuffer.Bind();

    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

    DrawInternal();

    GL.ReadBuffer(ReadBufferMode.ColorAttachment0);

    offScreenframeBuffer.Unbind();

    GL.BlitNamedFramebuffer(offScreenframeBuffer.FrameBufferId ,0,  0, 0,offScreenframeBuffer.Width, offScreenframeBuffer.Height, 0, 0, offScreenframeBuffer.Width, offScreenframeBuffer.Height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);
}

0 个答案:

没有答案