opengl最简单的帧缓冲区示例是什么?

时间:2019-09-17 18:42:53

标签: c# opengl opentk

我试图在我的游戏中添加后期处理,但是我无法理解learnopengl和opengl-tutorial上的帧缓冲区教程,我只需要一个简单的带有颜色和深度纹理以及如何使用和渲染它的帧缓冲区示例

我不确定如何实现帧缓冲区

  public class FrameBuffer
    {
        public int FBO,DBO;
        public int Width, Height;
        public int tex,depth;

        public FrameBuffer(int width, int height)
        {
            this.Width = width;
            this.Height = height;

            FBO = GL.GenFramebuffer();
            Bind();

            tex = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, tex);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Width, Height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, (IntPtr)null);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, tex, 0);
        }
        public void Bind()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBO);
        }
        public void Unbind()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }

1 个答案:

答案 0 :(得分:1)

您的代码使用色平面设置了适当的帧缓冲区。

如果framebufer必须具有深度缓冲区,则必须创建深度纹理,其格式为PixelFormat.DepthComponent和内部格式之一PixelInternalFormat.DepthComponent16PixelInternalFormat.DepthComponent24,{ {1}}或PixelInternalFormat.DepthComponent32

PixelInternalFormat.DepthComponent32f

将其附加到帧缓冲区,FramebufferAttachment.DepthAttachment

depth = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, depth);
GL.TexParameter(TextureTarget.Texture2D,
                TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D,
                TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent24,
              Width, Height, 0, PixelFormat.DepthComponent, PixelType.Float, (IntPtr)null);

请注意,在附加颜色缓冲区和深度缓冲区之后,您应该验证帧缓冲区的完整性,帧缓冲区的完整性应返回GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2D, depth, 0); 的{​​{1}}值。:

status

或者,您可以在深度纹理上附加一个(深度)渲染缓冲区:

FramebufferErrorCode.FramebufferComplete