Exemple #1
0
        private void RenderGBuffer(UniformBlock ubo, CameraData camera)
        {
            // G BUFFER GRAB PASS
            {
                gBuffer.BindAllFrameBuffersForDrawing();

                // SKYBOX PASS
                if (Debug.GetCVar("rendering / debug / render white background"))
                {
                    GL.ClearColor(System.Drawing.Color.White); MyGL.Check();
                    GL.DepthMask(true); MyGL.Check();
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); MyGL.Check();
                }
                else if (Debug.GetCVar("rendering / debug / render black background"))
                {
                    GL.ClearColor(System.Drawing.Color.Black); MyGL.Check();
                    GL.DepthMask(true); MyGL.Check();
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); MyGL.Check();
                }
                else
                {
                    GL.ClearColor(System.Drawing.Color.Black); MyGL.Check();
                    GL.DepthMask(true); MyGL.Check();
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); MyGL.Check();

                    if (SkyboxCubeMap != null)
                    {
                        GL.Disable(EnableCap.DepthTest); MyGL.Check();
                        GL.DepthMask(false); MyGL.Check();
                        GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); MyGL.Check();

                        var shader = Factory.GetShader("internal/deferred.skybox.shader");
                        shader.Uniforms.Set("skyboxCubeMap", SkyboxCubeMap);
                        shader.Bind();

                        Factory.SkyBoxMesh.Draw();
                    }
                }

                SetPolygonMode();

                // RENDER ALL OBJECTS
                {
                    gl.DepthWrite(true);
                    gl.DepthTest(true);
                    gl.Blend(false);

                    for (int i = 0; i < toRenderDefferredCount; i++)
                    {
                        var renderable = toRenderDefferred[i];
                        renderable.Material.BeforeBindCallback();
                        renderable.Material.Uniforms.SendAllUniformsTo(renderable.Material.RenderShader.Uniforms);
                        renderable.Material.RenderShader.Bind();
                        renderable.UploadUBOandDraw(camera, ubo);
                    }
                    // GL.MultiDrawElementsIndirect
                }

                gBuffer.Unbind();
            }
        }
Exemple #2
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            this.Title = statistics.ToString();


            statistics.Clear();

            statistics.fps = (1 / e.Time);

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

            if (this.Focused)
            {
                Input.Update();
            }



            if (Input.GetKeyDown(OpenTK.Input.Key.Escape))
            {
                Exit();
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F11))
            {
                if (WindowState != WindowState.Fullscreen)
                {
                    WindowState = WindowState.Fullscreen;
                }
                else
                {
                    WindowState = WindowState.Normal;
                }
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F10))
            {
                drawLines = !drawLines;
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F9))
            {
                drawGBufferContents = !drawGBufferContents;
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F8))
            {
                drawShadowMapContents = !drawShadowMapContents;
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F7))
            {
                debugBounds = !debugBounds;
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F6))
            {
                shadowsEnabled = !shadowsEnabled;
            }
            if (Input.GetKeyDown(OpenTK.Input.Key.F5))
            {
                Factory.ReloadAllShaders();
            }

            PhysicsUsage.PhysicsManager.instance.Update((float)e.Time);


            foreach (var go in Factory.allGameObjects.ToArray())
            {
                foreach (var c in go.GetComponents <MonoBehaviour>())
                {
                    c.Update_Internal(e.Time);
                }
            }

            //particleSimulation.Update(e.Time); // ITS BROKEN :c

            var frustrumPlanes = GeometryUtility.CalculateFrustumPlanes(camera);

            var allRenderers = new List <Renderer>();

            foreach (var go in Factory.allGameObjects)
            {
                allRenderers.AddRange(go.GetComponents <Renderer>());
            }


            camera.UploadDataToUBO(ubo); // bind camera view params
            //GL.BeginQuery(QueryTarget.)

            // G BUFFER GRAB PASS
            {
                gBuffer.BindAllFrameBuffersForDrawing();


                GL.Enable(EnableCap.DepthTest);
                GL.DepthMask(true);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

                // SKYBOX PASS
                {
                    GL.DepthRange(0.999, 1);
                    GL.DepthMask(false);

                    var shader = Factory.GetShader("internal/deferred.skybox.shader");
                    shader.SetUniform("skyboxCubeMap", skyboxCubeMap);
                    shader.Bind();

                    skyboxMesh.Draw();
                    GL.DepthRange(0, 1);
                }



                if (drawLines)
                {
                    GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                }
                else
                {
                    GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                }

                // RENDER ALL OBJECTS
                {
                    GL.DepthMask(true);

                    GL.Enable(EnableCap.CullFace);
                    GL.Disable(EnableCap.Blend);
                    GL.CullFace(CullFaceMode.Back);

                    foreach (var renderer in allRenderers)
                    {
                        if (renderer.shouldRenderGeometry && (!renderer.canBeFrustumCulled || GeometryUtility.TestPlanesAABB(frustrumPlanes, renderer.bounds)))
                        {
                            renderer.material.BindUniforms();
                            renderer.material.gBufferShader.Bind();
                            renderer.UploadUBOandDraw(camera, ubo);
                            statistics.countMeshesRendered++;
                        }
                    }
                }

                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
            }



            int lightIndex = 0;

            foreach (var light in Light.allLights)
            {
                var oldLightHasShadows = light.hasShadows;
                //if (!shadowsEnabled) light.hasShadows = false;

                var shadowMap = light.shadowMap;

                // SHADOW MAAPING
                if (light.hasShadows)
                {
                    //GL.Enable(EnableCap.CullFace);
                    //GL.CullFace(CullFaceMode.Back);

                    shadowMap.FrameBufferForWriting();

                    GL.Enable(EnableCap.DepthTest);
                    GL.DepthMask(true);

                    shadowMap.Clear();



                    shadowMap.shadowViewCamera.UploadDataToUBO(ubo);


                    foreach (var renderer in allRenderers)
                    {
                        if (renderer.castsShadows)
                        {
                            renderer.material.BindUniforms();
                            renderer.material.depthGrabShader.Bind();
                            renderer.UploadUBOandDraw(shadowMap.shadowViewCamera, ubo);
                        }
                    }
                }


                camera.UploadDataToUBO(ubo); // bind camera view params

                // G BUFFER LIGHT PASS

                {
                    GL.Disable(EnableCap.CullFace);
                    //GL.CullFace(CullFaceMode.Back);

                    GL.Disable(EnableCap.DepthTest);
                    GL.DepthMask(false);


                    light.UploadUBOdata(ubo, lightIndex);

                    var shader = Factory.GetShader("internal/deferred.oneLight.shader");
                    gBuffer.BindGBufferTexturesTo(shader);
                    if (light.hasShadows)
                    {
                        shadowMap.BindUniforms(shader);
                    }

                    shader.Bind();

                    GL.Enable(EnableCap.Blend);
                    //GL.BlendEquationSeparate(BlendEquationMode.FuncAdd, BlendEquationMode.FuncAdd);
                    //GL.BlendFunc(BlendingFactorSrc.SrcColor, BlendingFactorDest.SrcColor);
                    GL.BlendEquation(BlendEquationMode.FuncAdd);
                    GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One);
                    quadMesh.Draw();
                    GL.Disable(EnableCap.Blend);
                }

                lightIndex++;
            }



            // POST PROCESS EFFECTs

            /*{
             *  GL.Disable(EnableCap.DepthTest);
             *  GL.Disable(EnableCap.CullFace);
             *  GL.Disable(EnableCap.Blend);
             *
             *  GL.Disable(EnableCap.DepthTest);
             *  GL.DepthMask(false);
             *
             *  foreach (var shader in camera.postProcessEffects)
             *  {
             *      gBuffer.BindForLightingPass(shader);
             *      shader.Bind();
             *      quad.Draw();
             *      gBuffer.SwapFinalTextureTarget();
             *  }
             * }*/

            // FINAL DRAW TO SCREEN
            {
                DebugDrawTexture(gBuffer.finalTextureToWriteTo);
            }

            /*if(debugBounds)
             * {
             *  var allColiders = new List<BoxCollider>();
             *  foreach (var go in Factory.allGameObjects)
             *  {
             *      allColiders.AddRange(go.GetComponents<BoxCollider>());
             *  }
             *  GL.DepthMask(false);
             *  GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
             *  GL.Disable(EnableCap.DepthTest);
             *  GL.Disable(EnableCap.CullFace);
             *  GL.Disable(EnableCap.Blend);
             *  foreach (var c in allColiders)
             *  {
             *      var modelMat = c.gameObject.transform.GetScalePosRotMatrix();
             *      var modelViewMat = modelMat * camera.GetViewMat();
             *      ubo.model.modelMatrix = modelMat;
             *      ubo.model.modelViewMatrix = modelViewMat;
             *      ubo.model.modelViewProjectionMatrix = modelViewMat * camera.GetProjectionMat();
             *      ubo.modelUBO.UploadData();
             *      skyboxMesh.Draw();
             *  }
             * }*/
            if (drawGBufferContents)
            {
                gBuffer.DebugDrawContents();
            }
            //if (drawShadowMapContents) DebugDrawTexture(shadowMap.depthMap, new Vector4(0.5f, 0.5f, 1, 1), new Vector4(0.5f,0.5f,0,1), 1, 0);


            /*
             * {
             *  var shader = Factory.GetShader("internal/forward.allLights.shader");
             *  gBuffer.BindForWriting();
             *
             *  // transparent pass, must enable blending
             *  GL.Enable(EnableCap.DepthTest);
             *  GL.DepthMask(false);
             *  GL.Disable(EnableCap.CullFace);
             *  GL.Enable(EnableCap.Blend);
             *  GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
             *
             *
             *  shader.Bind();
             *  //shader.SetParam(Shader.screenSizeLocationName, new Vector2(Width, Height));
             *  camera.BindUniforms(shader);
             *  //Light.BindAll(shader);
             *
             *  foreach (var go in Factory.allGameObjects)
             *  {
             *      foreach (var renderer in go.GetComponents<Renderer>())
             *      {
             *          renderer.Draw(shader, camera);
             *      }
             *  }
             * }*/


            SwapBuffers();
        }