示例#1
0
        public RenderWindow(string Title, int W, int H, bool NoBorder = false) :
            base(W, H, new GraphicsMode(new ColorFormat(32), 24, 8, 4, ColorFormat.Empty, 2, false), Title,
                 GameWindowFlags.FixedWindow, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Debug | GraphicsContextFlags.ForwardCompatible)
        {
            GL.Enable(EnableCap.FramebufferSrgb);
            //GL.ClampColor(ClampColorTarget.ClampReadColor, ClampColorMode.FixedOnly);
            //GL.Enable((EnableCap)GL_CONSERVATIVE_RASTERIZATION_NV);
            CheckError();

            UICam            = new Camera();
            UICam.Projection = Matrix4.CreateOrthographicOffCenter(0, 1, 0, 1, -2, 2);

            SSAOShader = new ShaderProgram("basegame\\shaders\\ssao");
            PostShader = new ShaderProgram("basegame\\shaders\\post");

            ScreenQuad = new RenderObject(DrawPrimitiveType.Triangles);

            ScreenQuad.BindBuffer(0, new VertexBuffer().SetData(new Vector3[] {
                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0),
                new Vector3(1, 1, 0),
                new Vector3(0, 0, 0),
                new Vector3(1, 1, 0),
                new Vector3(1, 0, 0)
            }));

            ScreenQuad.BindBuffer(1, DataBuffer.CreateFromData(new Vector2[] {
                new Vector2(0, 0),
                new Vector2(0, 1),
                new Vector2(1, 1),
                new Vector2(0, 0),
                new Vector2(1, 1),
                new Vector2(1, 0)
            }));

            ScreenQuad.BindIndexBuffer(new IndexBuffer().SetSequence(6));

            GFrameBuffer = new FrameBuffer();
            {
                PositionBuffer = new Texture2D(TexFilterMode.Nearest);
                PositionBuffer.LoadData(W, H, IntPtr.Zero, PixelInternalFormat.Rgb32f, PixelFormat.Rgb, PixelType.Float);
                PositionBuffer.Resident = true;
                GFrameBuffer.BindTexture(PositionBuffer, FramebufferAttachment.ColorAttachment0);

                ColorBuffer = new Texture2D(TexFilterMode.Nearest);
                ColorBuffer.LoadData(W, H, IntPtr.Zero);
                ColorBuffer.Resident = true;
                GFrameBuffer.BindTexture(ColorBuffer, FramebufferAttachment.ColorAttachment1);

                MetallicRoughnessAOHeightBuffer = new Texture2D(TexFilterMode.Nearest);
                MetallicRoughnessAOHeightBuffer.LoadData(W, H, IntPtr.Zero, PixelInternalFormat.Rgba32f, PixelFormat.Rgba, PixelType.Float);
                MetallicRoughnessAOHeightBuffer.Resident = true;
                GFrameBuffer.BindTexture(MetallicRoughnessAOHeightBuffer, FramebufferAttachment.ColorAttachment2);

                NormalBuffer = new Texture2D(TexFilterMode.Nearest);
                NormalBuffer.LoadData(W, H, IntPtr.Zero, PixelInternalFormat.Rgb16f, PixelFormat.Rgb, PixelType.Float);
                NormalBuffer.Resident = true;
                GFrameBuffer.BindTexture(NormalBuffer, FramebufferAttachment.ColorAttachment3);

                EmissiveBuffer = new Texture2D(TexFilterMode.Nearest);
                EmissiveBuffer.LoadData(W, H, IntPtr.Zero, PixelInternalFormat.Rgb, PixelFormat.Rgb);
                EmissiveBuffer.Resident = true;
                GFrameBuffer.BindTexture(EmissiveBuffer, FramebufferAttachment.ColorAttachment4);

                /*HeightBuffer = new Texture2D(TexFilterMode.Nearest);
                 * HeightBuffer.LoadData(W, H, IntPtr.Zero, PixelInternalFormat.R16f, PixelFormat.Red, PixelType.Float);
                 * HeightBuffer.Resident = true;
                 * GFrameBuffer.BindTexture(HeightBuffer, FramebufferAttachment.ColorAttachment7);*/

                DepthBuffer = new RenderBuffer(RenderbufferStorage.DepthComponent, W, H);
                GFrameBuffer.BindRenderBuffer(DepthBuffer, FramebufferAttachment.DepthAttachment);
                GFrameBuffer.DrawBuffer(DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1, DrawBuffersEnum.ColorAttachment2, DrawBuffersEnum.ColorAttachment3,
                                        DrawBuffersEnum.ColorAttachment4);
            }

            SSAOFrameBuffer = new FrameBuffer();
            {
                SSAOBuffer = new Texture2D(TexFilterMode.Linear, TexWrapMode.ClampToEdge);
                SSAOBuffer.LoadData(W, H, IntPtr.Zero, PixelInternalFormat.R32f, PixelFormat.Red, PixelType.Float);
                SSAOBuffer.Resident = true;
                SSAOFrameBuffer.BindTexture(SSAOBuffer, FramebufferAttachment.ColorAttachment0);
            }

            Random Rnd = new Random();

            SSAOKernel = new Vector3[64];
            for (int i = 0; i < SSAOKernel.Length; i++)
            {
                Vector3 Sample = new Vector3((float)Rnd.NextDouble() * 2 - 1, (float)Rnd.NextDouble() * 2 - 1, (float)Rnd.NextDouble());
                Sample.Normalize();
                Sample *= (float)Rnd.NextDouble();

                float Scale = (float)i / SSAOKernel.Length;
                Scale   = MathFuncs.Lerp(0.1f, 1.0f, Scale * Scale);
                Sample *= Scale;

                SSAOKernel[i] = Sample;
            }

            CheckError();
        }