OpenTK绘图到FBO,着色不如预期

时间:2015-06-27 21:32:38

标签: c# opengl graphics opentk fbo

我有一个Framebuffer对象的实用程序类,我用它来生成FBO,绑定它们,渲染它们,然后将它们渲染成屏幕为2d纹理。

使用这个类时,我可以成功制作fbo纹理,渲染它们,然后将它们渲染到屏幕上。

但是,一旦我将它们渲染到屏幕上,颜色就会变得混乱。

似乎正在发生的事情是我设置为绘图颜色的颜色最终为FBO的背景颜色(但只有在设置颜色后才进行实际绘制调用),以及我完成的绘图总是黑色

根据我的代码,我期待的是一个带有白色背景的纹理,以及两个小的彩色矩形(一个紫色和一个青色),两次绘制到屏幕上。

我得到的是:

青色背景,有两个小黑色矩形。

Output

源代码如下:

FboRenderTexture.cs

    public class FboRenderTexture : IDisposable
    {
        public int textureId = 0;
        private int fboId = 0;

        public int Width;
        public int Height;

        public FboRenderTexture(int width, int height)
        {
            Width = width;
            Height = height;

            Init();
        }

        //semi pseudocode
        public void DrawToScreen(float xoffset = 0, float yoffset = 0)
        {
            if (textureId != -1)
            {
                GL.BindTexture(TextureTarget.Texture2D, textureId);

                GL.Begin(BeginMode.Quads);
                //todo : might also flip the texture since fbo's have right handed coordinate systems
                GL.TexCoord2(0.0, 0.0);
                GL.Vertex3(xoffset, yoffset, 0.0);

                GL.TexCoord2(0.0, 1.0);
                GL.Vertex3(xoffset, yoffset+Height, 0.0);

                GL.TexCoord2(1.0, 1.0);
                GL.Vertex3(xoffset+Width, yoffset+Height, 0.0);

                GL.TexCoord2(1.0, 0.0);
                GL.Vertex3(xoffset+Width, yoffset, 0.0);

                GL.End();

            }
        }

        private void Init()
        {
            // Generate the texture.
            textureId = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, textureId);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);

            // Create a FBO and attach the texture.
            GL.Ext.GenFramebuffers(1, out fboId);
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboId);
            GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt,
                FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureId, 0);

            // Disable rendering into the FBO
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
        }


        // Track whether Dispose has been called.
        private bool disposed = false;

        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // Clean up what we allocated before exiting
                    if (textureId != 0)
                        GL.DeleteTextures(1, ref textureId);

                    if (fboId != 0)
                        GL.Ext.DeleteFramebuffers(1, ref fboId);

                    disposed = true;
                }
            }
        }

        public void BeginDrawing()
        {   
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fboId);
            GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
            GL.PushAttrib(AttribMask.ViewportBit);

            GL.Viewport(0, 0, Width, Height);
        }

        public void EndDrawing()
        {
            GL.PopAttrib();
            GL.Ext.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); // disable rendering into the FBO
        }
    }

使用

public class Buffers : CreativeSharp.core.CSInstance //inherits from OpenTK.GameWindow, adds some simple drawing stuff.
{
    private FboRenderTexture buffer;

    public Buffers() : base(800, 600, "buffers")
    {
        GL.Enable(EnableCap.Texture2D);
        buffer = new FboRenderTexture(128, 128);
        NoStroke();
        this.Closed += Buffers_Closed;
    }

    private void SetupBuffer()
    {
        buffer.BeginDrawing();
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
        GL.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);

        GL.Color4(1f, 0f, 1f, 0f);
        DrawRectangle(10,10, 30, 30);

        GL.Color4(0f, 1f, 1f, 1f);
        DrawRectangle(50, 10, 30, 30);

        GL.Color4(1f, 1f, 1f);
        buffer.EndDrawing();

    }

    void Buffers_Closed(object sender, EventArgs e)
    {
        buffer.Dispose();
    }


    public override void DrawContent()
    {
        SetupBuffer();

        GL.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        GL.Color4(1f, 1f, 1f, 1f);
        buffer.DrawToScreen(0,0);
        buffer.DrawToScreen(512,0);

    }

    public override void Update()
    {
    }

    private void DrawRectangle(float x, float y, float w, float h)
    {
        GL.Begin(PrimitiveType.Quads);
        GL.Vertex3(x, y, 0);
        GL.Vertex3(x + w, y, 0);
        GL.Vertex3(x + w, y + h, 0);
        GL.Vertex3(x, y + h, 0);
        GL.End();
    }
}

1 个答案:

答案 0 :(得分:0)

有点尴尬,我的问题的原因是我在绘制fbo的纹理之前没有将白色设置为绘图颜色。 这导致颜色出错了。