Exemplo n.º 1
0
        void RenderScene(ref Matrix4d view, ref Matrix4d offsetmat, ref Matrix4d projection, ref Matrix4d viewproj)
        {
            ///////////////// SKY ///////////////////
            if (Settings.DrawBackdrop == true)
            {
                GL.Disable(EnableCap.DepthTest);
                backdrop.Render(view, offsetmat);
                GL.Enable(EnableCap.DepthTest);
                GL.Clear(ClearBufferMask.DepthBufferBit);
            }
            else
            {
                GL.ClearColor(Renderer.ClearColor);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            }

            //Far field
            //GL.Disable(EnableCap.DepthTest);
            //render a planet if we are near one
            if (MPlanetHandler.CurrentNear != null)
            {
                projection = Camera.GetFullProjection(MPlanetHandler.CurrentNear.AvatarDistanceToSurface / 2.0, 20000000);
                viewproj   = view * projection;
                AstroRoot.Render(viewproj, offsetmat);
            }

            GL.Clear(ClearBufferMask.DepthBufferBit);

            //============================
            if (Background.Modules.Count > 0)
            {
                projection = Camera.GetFullProjection();
                viewproj   = view * projection;
                Background.Render(viewproj, offsetmat);
                Background2.Render(viewproj, offsetmat);
            }


            ModelRoot.Render(viewproj, offsetmat);

            // Near Field
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            //============================
            //GL.Clear(ClearBufferMask.DepthBufferBit);
            projection = Camera.GetProjection(true);
            viewproj   = view * projection;

            // Background.Render(viewproj, offsetmat);
            //GL.Clear(ClearBufferMask.DepthBufferBit);
            ModelRoot.Render(viewproj, offsetmat);

            if (SelectedObject != null)
            {
                GL.Clear(ClearBufferMask.DepthBufferBit);
                ScreenPick.PrepareRender();
                SelectedObject.Render(viewproj, offsetmat);
                Globals.ShaderOverride = null;;
                ScreenPick.AfterRender();
            }

            if (OnPostRender != null)
            {
                OnPostRender(this, new RenderEvent());
            }

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            //GL.Enable(EnableCap.DepthTest);
            //////////////////////// MAIN RENDER COMPLETE

            if (Physics.DebugWorld == true)
            {
                Physics.Render(viewproj, offsetmat);
            }

            //selection overlay

            ///////////// SELECTION ///////////////////



            if (SelectionRoot.Modules.Count > 0)
            {
                //GL.Disable(EnableCap.DepthTest);
                ScreenPick.PrepareRender();
                // Globals.RenderSelectedOnly = true;
                //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                // GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                SelectionRoot.Render(viewproj, offsetmat);
                Globals.ShaderOverride = null;;
                //Globals.RenderSelectedOnly = false;
                ScreenPick.AfterRender();
            }
        }
Exemplo n.º 2
0
        /**
         * During the Rendering phase all matrices are pre-multiplied (to bring coordinates back to local 32bit space)
         * and all offsets and set relative to the camera, to aid with shadow calculations -
         * */
        public void Render()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            Globals.ShaderOverride   = null;
            Globals.DrawCalls        = 0;
            Globals.Index            = 0;
            Globals.GlobalOffset     = Camera.transform.Position;
            Globals.GlobalOffsetCalc = Camera.transform.Position;
            Matrix4d offsetmat   = Matrix4d.CreateTranslation(-Globals.GlobalOffset);
            Matrix4d lightmatrix = light.GetLightSpaceMatrix();

            Matrix4d view       = Camera.GetViewMatrix();
            Matrix4d projection = Camera.GetProjection(true);
            Matrix4d viewproj   = view * projection;

            // GL.Viewport(0, 0, MScreen.Width, MScreen.Height);
            //============================
            // Globals.GlobalOffset = Vector3d.Zero;

            // GL.CullFace(CullFaceMode.Back);
            // reset viewport
            // 2. render scene as normal using the generated depth/shadow map
            // --------------------------------------------------------------

            for (int i = 0; i < MaterialRoot.Modules.Count; i++)
            {
                MMaterial mat = (MMaterial)MaterialRoot.Modules[i];
                if (mat.IsUsed == false)
                {
                    continue;
                }
                mat.shader.Bind();
                mat.shader.SetMat4("view", MTransform.GetFloatMatrix(view));
                mat.shader.SetMat4("projection", MTransform.GetFloatMatrix(projection));
                mat.shader.SetVec4("Tweak", new Vector4(Settings.Tweak1, Settings.Tweak2, Settings.Tweak3, Settings.Tweak4));

                // set light uniforms
                Fog.Bind(mat.shader);

                //TODO: use nearest lights
                mat.shader.SetInt("NumLights", LightRoot.Modules.Count);
                //TODO sort lights closest to furthest (because there is max lights =10 in shader)

                for (int il = 0; il < LightRoot.Modules.Count; il++)
                {
                    if (il >= MAXLIGHTS)
                    {
                        continue;
                    }
                    //pointLightPositions[0]
                    MPointLight p = (MPointLight)LightRoot.Modules[il];
                    p.Bind(mat, il);
                }

                mat.shader.SetVec3("viewPos", MTransform.GetVector3(Camera.transform.Position - Globals.GlobalOffset));
                mat.shader.SetVec3("sunPos", MTransform.GetVector3(light.transform.Position - Globals.GlobalOffset));
                mat.shader.SetInt("Closeup", Closeup);
                light.Bind(mat);
            }

            /////////////////////// DEPTH LIGHT FOR SHADOWS ///////////////////////
            GL.Viewport(0, 0, MScreen.Width, MScreen.Height);

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

            if (light.Shadows)
            {
                GL.Enable(EnableCap.CullFace);
                GL.CullFace(CullFaceMode.Back);
                //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                Globals.RenderPass = Globals.eRenderPass.ShadowDepth;
                //============================
                //render depthmap from light using depth shader
                light.Render(lightmatrix, offsetmat);
                simpleDepthShader.Bind();

                Globals.ShaderOverride = simpleDepthShader;
                GL.DepthFunc(DepthFunction.Less);
                GL.Enable(EnableCap.DepthTest);

                //Background.Render(lightmatrix, offsetmat);
                //GL.Clear(ClearBufferMask.DepthBufferBit);
                Background2.Render(lightmatrix, offsetmat);
                ModelRoot.Render(lightmatrix, offsetmat);
                Globals.RenderPass     = Globals.eRenderPass.Normal;
                Globals.ShaderOverride = null;
            }

            //============================
            ////////////////// NORMAL RENDER ////////////////////
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            //Helper.CheckGLError(this, "Render 1");
            //set FBO
            Renderer.Render(viewproj, offsetmat);
            //Helper.CheckGLError(this, "Render 1");

            //load the shadow map into texture slot 4
            GL.ActiveTexture(TextureUnit.Texture0 + MShader.LOCATION_SHADOWMAP);
            GL.BindTexture(TextureTarget.Texture2D, light.depthMap);
            ////////////////////////////////////////////////////////
            //  GL.PatchParameter(PatchParameterInt.PatchVertices, 16);

            RenderScene(ref view, ref offsetmat, ref projection, ref viewproj);

            //////////// OVERLAY LAYER ////////////
            //============================
            GL.Disable(EnableCap.DepthTest);
            GL.Clear(ClearBufferMask.DepthBufferBit);
            if (Overlay.Modules.Count > 0)
            {
                projection = Camera.GetOverlayProjection();
                view       = Camera.GetOverlayViewMatrix();
                viewproj   = view * projection;
                //UpdateOverlay();
                Overlay.Render(viewproj, Matrix4d.Identity);
            }

            if (Stereo == true)
            {
                GL.Viewport(0, 0, MScreen.Width / 2, MScreen.Height);
                Vector3d original = Camera.transform.Position;
                Camera.transform.Position -= Camera.transform.Right();
                view     = Camera.GetViewMatrix();
                viewproj = view * projection;
                GL.Viewport(MScreen.Width / 2, 0, MScreen.Width / 2, MScreen.Height);
                RenderScene(ref view, ref offsetmat, ref projection, ref viewproj);
                Camera.transform.Position = original;
                GL.Viewport(0, 0, MScreen.Width, MScreen.Height);
            }

            Renderer.AfterRender();

            // GL.Disable(EnableCap.DepthTest);
            // GL.Disable(EnableCap.DepthTest);
            // render Depth map to quad for visual debugging
            // ---------------------------------------------
            //GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
            if (Settings.DebugDepth == true)
            {
                debugQuad.Bind();

                debugQuad.material.shader.SetFloat("near_plane", light.NearPlane);
                debugQuad.material.shader.SetFloat("far_plane", light.FarPlane);
                GL.ActiveTexture(TextureUnit.Texture0);
                GL.BindTexture(TextureTarget.Texture2D, light.depthMap);
                GL.Clear(ClearBufferMask.DepthBufferBit);
                debugQuad.Render(lightmatrix, Matrix4d.Identity);
            }

            ///do not reset
            /// Globals.GlobalOffset = Vector3d.Zero;

            //projection = Camera.GetProjection(true);
            //GUIRoot.Render(Matrix4d.Identity, Matrix4d.Identity);



            ErrorCode err = GL.GetError();

            if (err != ErrorCode.NoError)
            {
                //Console.WriteLine("MScene Render:" + err);
            }
        }