Example #1
0
        public void RenderLightScattering(GameObject root, Camera camera)
        {
            GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One);

            root.Foreach <LightScatteringComponent>((GameObject go, LightScatteringComponent ls) => {
                foreach (DirectionalLight dl in go.GetComponents <DirectionalLight>())
                {
                    Framebuffer current = Framebuffer.Current;

                    framebuffer.Bind();
                    GL.Viewport(0, 0, width / 2, height / 2);
                    skyboxshader.Bind();
                    skyboxshader.SetUniform("lightDir", dl.Direction);
                    Matrix4 view = camera.ViewMatrix;
                    skyboxshader.SetUniform("view_matrix", false, ref view);
                    GL.DrawArrays(PrimitiveType.Triangles, 0, 36);


                    framebuffer2.Bind();
                    GL.Viewport(0, 0, width / 4, height / 4);
                    radialblurshader.Bind();

                    Vector4 orlightpos = Vector4.Transform(new Vector4(dl.Direction.Normalized(), 1), view);
                    Vector2 lightpos   = orlightpos.Xy / orlightpos.W;
                    lightpos          += new Vector2(1, 1);
                    lightpos          /= 2;

                    radialblurshader.SetUniform("lightpos", lightpos);
                    colortexture.Bind(TextureUnit.Texture4);
                    GL.DrawArrays(PrimitiveType.Triangles, 0, 6);


                    current.Bind();
                    GL.Enable(EnableCap.Blend);
                    GL.Viewport(0, 0, width, height);
                    finalshader.Bind();
                    finalshader.SetUniform("lightcolor", ls.HasSpecialColor() ? ls.RayColor : dl.Color);
                    colortexture2.Bind(TextureUnit.Texture5);
                    GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
                    GL.Disable(EnableCap.Blend);
                }
            });
        }
Example #2
0
        public void RenderLight(Camera camera, GameObject root, Vector3 ambientlight)
        {
            Matrix4 view = camera.ViewMatrix;
            Matrix4 mvp  = camera.CameraMatrix;

            skyboxshader.Bind();
            skyboxshader.SetUniform("view_matrix", false, ref view);
            GL.DepthMask(false);
            GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
            GL.DepthMask(true);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            ambientlightshader.Bind();
            ambientlightshader.SetUniform("lightColor", ambientlight);
            GL.DrawArrays(PrimitiveType.Triangles, 0, 6);

            GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One);

            directionallightshader.Bind();
            directionallightshader.SetUniform("viewPos", camera.Position);
            directionallightshader.BindUniformBlock("lights", 0);
            lightrenderer.RenderLight <DirectionalLight>(root, 6);

            pointlightshader.Bind();
            pointlightshader.SetUniform("viewPos", camera.Position);
            pointlightshader.SetUniform("mvp", false, ref mvp);
            pointlightshader.BindUniformBlock("lights", 0);
            lightrenderer.RenderLight <PointLight>(root, 36);

            spotlightshader.Bind();
            spotlightshader.SetUniform("viewPos", camera.Position);
            spotlightshader.SetUniform("mvp", false, ref mvp);
            spotlightshader.BindUniformBlock("lights", 0);
            lightrenderer.RenderLight <SpotLight>(root, 36);

            GL.Disable(EnableCap.Blend);
        }
Example #3
0
        private void RenderGame(object sender, FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            if (wireframe)
            {
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                GL.Disable(EnableCap.CullFace);

                Matrix4 mvp = camera.CameraMatrix;
                worldshader.SetUniform("mvp", false, ref mvp);
                worldshader.Bind();
                model.Draw(root);

                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                GL.Enable(EnableCap.CullFace);
            }
            else
            {
                gBuffer.Bind();

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

                worldshader.Bind();
                Matrix4 mvp = camera.CameraMatrix;
                worldshader.SetUniform("mvp", false, ref mvp);
                model.Draw(root);
                root.WorldRender(camera);

                lightBuffer.Bind();
                gPosition.Bind(TextureUnit.Texture0);
                gNormal.Bind(TextureUnit.Texture1);
                gAlbedo.Bind(TextureUnit.Texture2);
                gLight.Bind(TextureUnit.Texture3);
                lightManager.RenderLight(camera, root, ambientlight);
                root.LightRender(camera);

                if (lightscattering)
                {
                    lsr.RenderLightScattering(root, camera);
                }

                if (bloom)
                {
                    bloomshader.Bind();
                    bloomFramebuffer.Bind();
                    lightTexture.Bind(TextureUnit.Texture0);
                    GL.DrawArrays(PrimitiveType.Triangles, 0, 6);

                    GL.Viewport(0, 0, (int)(Width / 2f), (int)(Height / 2f));

                    gaussianblurshader.Bind();

                    blurFramebuffer[0].Bind();
                    bloomTexture.Bind(TextureUnit.Texture0);
                    gaussianblurshader.SetUniform("horizontal", 1);
                    GL.DrawArrays(PrimitiveType.Triangles, 0, 6);

                    blurFramebuffer[1].Bind();
                    blurTexture[0].Bind(TextureUnit.Texture0);
                    gaussianblurshader.SetUniform("horizontal", 0);
                    GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
                }



                blurFramebuffer[0].Unbind();
                GL.Viewport(0, 0, Width, Height);
                ppshader.Bind();
                Matrix4 pmvp = camera.PreviousCameraMatrix;
                ppshader.SetUniform("premvp", false, ref pmvp);
                Matrix4 invmvp = camera.CameraMatrix.Inverted();
                ppshader.SetUniform("invmvp", false, ref invmvp);
                blurTexture[1].Bind(TextureUnit.Texture1);
                lightTexture.Bind(TextureUnit.Texture0);
                gDepth.Bind(TextureUnit.Texture2);
                GL.Enable(EnableCap.Blend);
                GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
                GL.Disable(EnableCap.Blend);
            }

            this.SwapBuffers();
        }