OpenTK FBO blitting not Drawing anything to screen

196 Views Asked by At

I am using OpenTK and trying to draw geometry to an FBO, and then blit it back to the screen without a quad. The issue is nothing is being drawn to the Screen. I am getting back an open GL error "InvalidOperation", but I am not too sure what is wrong. Note, If I remove the code surrounding DrawInternals, my geometry draws fine, so I know at least it's being drawn to the FBO. Also note, my Src and Dest sizes are always the same.

Edit: I simplified the code a bit more, to try to highlight my goal, I am no longer getting an error, just blitting a black screen.

Here is the FBO class I created:

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;
    }
}

And here is how I am using it:

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

There are 0 best solutions below