Example #1
0
        /// <summary>
        /// Draws the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="world">The world.</param>
        /// <param name="render">The render.</param>
        protected void Draw(GameTime gameTime, IWorld world, RenderHelper render)
        {
            Matrix view       = world.CameraManager.ActiveCamera.View;
            Matrix projection = world.CameraManager.ActiveCamera.Projection;

            world.Culler.StartFrame(ref view, ref projection, world.CameraManager.ActiveCamera.BoundingFrustum);
            List <IObject> AllnotCulledObjectsList      = world.Culler.GetNotCulledObjectsList(null);
            List <IObject> DeferrednotCulledObjectsList = world.Culler.GetNotCulledObjectsList(MaterialType.DEFERRED);
            List <IObject> ForwardnotCulledObjectsList  = world.Culler.GetNotCulledObjectsList(MaterialType.FORWARD);

            if (desc.OrderAllObjectsBeforeDraw != null)
            {
                AllnotCulledObjectsList = desc.OrderAllObjectsBeforeDraw(AllnotCulledObjectsList);
            }

            if (desc.OrderDeferredObjectsBeforeDraw != null)
            {
                DeferrednotCulledObjectsList = desc.OrderDeferredObjectsBeforeDraw(DeferrednotCulledObjectsList);
            }

            if (desc.OrderForwardObjectsBeforeDraw != null)
            {
                ForwardnotCulledObjectsList = desc.OrderForwardObjectsBeforeDraw(ForwardnotCulledObjectsList);
            }

            render.SetSamplerStates(ginfo.SamplerState);
            render.DettachBindedTextures();

            deferredGBuffer.PreDrawScene(gameTime, world, render, ginfo, AllnotCulledObjectsList);

            render.SetSamplerStates(ginfo.SamplerState);
            render.DettachBindedTextures();

            deferredGBuffer.SetGBuffer(render);
            deferredGBuffer.ClearGBuffer(render);
            deferredGBuffer.DrawScene(gameTime, world, render, ginfo, DeferrednotCulledObjectsList);
            deferredGBuffer.ResolveGBuffer(render);

            render[PrincipalConstants.DephRT]   = deferredGBuffer[GBufferTypes.DEPH];
            render[PrincipalConstants.normalRt] = deferredGBuffer[GBufferTypes.NORMAL];

            render.DettachBindedTextures();
            render.ValidateSamplerStates();

            deferredLightMap.SetLightMap(render);
            deferredLightMap.DrawLights(gameTime, world, deferredGBuffer, render);
            deferredLightMap.ResolveLightMap(render);
            render[PrincipalConstants.lightRt] = deferredLightMap[DeferredLightMapType.LIGHTMAP];

            render.DettachBindedTextures(5);
            render.ValidateSamplerStates();

            render.PushRenderTarget(scenerender);
            render.Clear(desc.BackGroundColor);
            foreach (IObject item in AllnotCulledObjectsList)
            {
                if (item.Material.IsVisible)
                {
                    item.Material.PosDrawnPhase(gameTime, item, world.CameraManager.ActiveCamera, world.Lights, render);
                }
            }

            render.DettachBindedTextures(3);
            render.ValidateSamplerStates();

            if (world.PhysicWorld.isDebugDraw)
            {
                world.PhysicWorld.iDebugDrawn(render, gameTime, world.CameraManager.ActiveCamera);
            }
            if (world.ParticleManager != null)
            {
                world.ParticleManager.iDraw(gameTime, world.CameraManager.ActiveCamera.View, world.CameraManager.ActiveCamera.Projection, render);
                render.ResyncStates();
                render.SetSamplerStates(ginfo.SamplerState);
            }

            render.DettachBindedTextures(6);
            render.ValidateSamplerStates();

            render.RenderPosWithDepthComponents(gameTime, ref view, ref projection);

            render.DettachBindedTextures(6);
            render.ValidateSamplerStates();

            render[PrincipalConstants.colorRT]      = scenerender;
            render[PrincipalConstants.normalRt]     = deferredGBuffer[GBufferTypes.NORMAL];
            render[PrincipalConstants.lightRt]      = deferredLightMap[DeferredLightMapType.LIGHTMAP];
            render[PrincipalConstants.DephRT]       = deferredGBuffer[GBufferTypes.DEPH];
            render[PrincipalConstants.extra1RT]     = deferredGBuffer[GBufferTypes.Extra1];
            render[PrincipalConstants.CurrentImage] = render[PrincipalConstants.CombinedImage] = render.PopRenderTargetAsSingleRenderTarget2D();

            for (int i = 0; i < PostEffects.Count; i++)
            {
                if (PostEffects[i].Enabled)
                {
                    render.PushRenderTarget(PostEffectTarget);
                    render.Clear(Color.Black);
                    PostEffects[i].Draw(render[PrincipalConstants.CurrentImage], render, gameTime, ginfo, world, true);
                    Texture2D tex = render.PopRenderTarget()[0].RenderTarget as Texture2D;
                    render[PrincipalConstants.CurrentImage] = tex;
                    SwapTargetBuffers();
                }
            }

            render.SetSamplerStates(ginfo.SamplerState);
            render.DettachBindedTextures(6);

            render.Clear(Color.Black);
            render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, SamplerState.PointClamp);
        }
        /// <summary>
        /// Draws the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="world">The world.</param>
        /// <param name="render">The render.</param>
        protected void Draw(GameTime gameTime, IWorld world, RenderHelper render)
        {
            Matrix view       = world.CameraManager.ActiveCamera.View;
            Matrix projection = world.CameraManager.ActiveCamera.Projection;

            world.Culler.StartFrame(ref view, ref projection, world.CameraManager.ActiveCamera.BoundingFrustum);

            List <IObject> AllnotCulledObjectsList      = world.Culler.GetNotCulledObjectsList(null);
            List <IObject> DeferrednotCulledObjectsList = world.Culler.GetNotCulledObjectsList(MaterialType.DEFERRED, CullerComparer.ComparerFrontToBack, world.CameraManager.ActiveCamera.Position);
            List <IObject> ForwardnotCulledObjectsList  = world.Culler.GetNotCulledObjectsList(MaterialType.FORWARD, CullerComparer.ComparerBackToFront, world.CameraManager.ActiveCamera.Position);

            if (desc.OrderAllObjectsBeforeDraw != null)
            {
                AllnotCulledObjectsList = desc.OrderAllObjectsBeforeDraw(AllnotCulledObjectsList, world);
            }

            if (desc.OrderDeferredObjectsBeforeDraw != null)
            {
                DeferrednotCulledObjectsList = desc.OrderDeferredObjectsBeforeDraw(DeferrednotCulledObjectsList, world);
            }

            if (desc.OrderForwardObjectsBeforeDraw != null)
            {
                ForwardnotCulledObjectsList = desc.OrderForwardObjectsBeforeDraw(ForwardnotCulledObjectsList, world);
            }

            render.SetSamplerStates(ginfo.SamplerState);
            render.DettachBindedTextures();

            deferredGBuffer.PreDrawScene(gameTime, world, render, ginfo, AllnotCulledObjectsList);

            render.SetSamplerStates(ginfo.SamplerState);
            render.DettachBindedTextures();

            deferredGBuffer.SetGBuffer(render);
            deferredGBuffer.ClearGBuffer(render);
            deferredGBuffer.DrawScene(gameTime, world, render, ginfo, DeferrednotCulledObjectsList);
            deferredGBuffer.ResolveGBuffer(render);

            render.DettachBindedTextures();
            render.ValidateSamplerStates();

            deferredLightMap.SetLightMap(render);
            deferredLightMap.DrawLights(gameTime, world, deferredGBuffer, render);
            deferredLightMap.ResolveLightMap(render);

            render.DettachBindedTextures(5);
            render.ValidateSamplerStates();

            deferredFinalCombination.SetFinalCombination(render);
            deferredFinalCombination.DrawScene(gameTime, world, deferredGBuffer, deferredLightMap, render);

            render.DettachBindedTextures(3);
            render.ValidateSamplerStates();

            if (desc.ExtraForwardPass)
            {
                render[PrincipalConstants.colorRT]       = deferredGBuffer[GBufferTypes.COLOR];
                render[PrincipalConstants.normalRt]      = deferredGBuffer[GBufferTypes.NORMAL];
                render[PrincipalConstants.lightRt]       = deferredLightMap[DeferredLightMapType.LIGHTMAP];
                render[PrincipalConstants.DephRT]        = deferredGBuffer[GBufferTypes.DEPH];
                render[PrincipalConstants.extra1RT]      = deferredGBuffer[GBufferTypes.Extra1];
                render[PrincipalConstants.CombinedImage] = deferredFinalCombination[GBufferTypes.FINALIMAGE];
                render[PrincipalConstants.CurrentImage]  = deferredFinalCombination[GBufferTypes.FINALIMAGE];

                if (desc.RestoreDepthOption == RestoreDepthOption.BEFORE_POSTEFFECT)
                {
                    restoreDepth.PerformForwardPass(render[PrincipalConstants.CombinedImage], render[PrincipalConstants.DephRT], render, ginfo);

                    render.DettachBindedTextures(2);
                    render.ValidateSamplerStates();

                    if (world.PhysicWorld.isDebugDraw)
                    {
                        world.PhysicWorld.iDebugDrawn(render, gameTime, world.CameraManager.ActiveCamera);
                    }
                    if (world.ParticleManager != null)
                    {
                        world.ParticleManager.iDraw(gameTime, world.CameraManager.ActiveCamera.View, world.CameraManager.ActiveCamera.Projection, render);
                        render.ResyncStates();
                        render.SetSamplerStates(ginfo.SamplerState);
                    }

                    render.DettachBindedTextures(6);
                    render.ValidateSamplerStates();

                    forwardPass.Draw(gameTime, world, render, DeferrednotCulledObjectsList, ForwardnotCulledObjectsList);

                    render.DettachBindedTextures(6);
                    render.ValidateSamplerStates();

                    render.RenderPosWithDepthComponents(gameTime, ref view, ref projection);

                    render.DettachBindedTextures(6);
                    render.ValidateSamplerStates();

                    render[PrincipalConstants.CurrentImage]  = restoreDepth.EndForwardPass(render);
                    render[PrincipalConstants.CombinedImage] = render[PrincipalConstants.CurrentImage];

                    for (int i = 0; i < PostEffects.Count; i++)
                    {
                        if (PostEffects[i].Enabled)
                        {
                            render.PushRenderTarget(PostEffectTarget);
                            render.Clear(Color.Black);
                            PostEffects[i].Draw(render[PrincipalConstants.CurrentImage], render, gameTime, ginfo, world, desc.UseFloatingBufferForLightMap);
                            Texture2D tex = render.PopRenderTarget()[0].RenderTarget as Texture2D;
                            render[PrincipalConstants.CurrentImage] = tex;
                            SwapTargetBuffers();
                        }
                    }

                    render.SetSamplerStates(ginfo.SamplerState);
                    render.DettachBindedTextures(6);

                    if (desc.UseFloatingBufferForLightMap)
                    {
                        render.Clear(Color.Black);
                        render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, SamplerState.PointClamp);
                    }
                    else
                    {
                        render.Clear(Color.Black);
                        render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, ginfo.SamplerState);
                    }
                }
                else if (desc.RestoreDepthOption == RestoreDepthOption.AFTER_POSTEFFECT)
                {
                    for (int i = 0; i < PostEffects.Count; i++)
                    {
                        if (PostEffects[i].Enabled)
                        {
                            render.PushRenderTarget(PostEffectTarget);
                            render.Clear(Color.Black);
                            PostEffects[i].Draw(render[PrincipalConstants.CurrentImage], render, gameTime, ginfo, world, desc.UseFloatingBufferForLightMap);
                            Texture2D tex = render.PopRenderTarget()[0].RenderTarget as Texture2D;
                            render[PrincipalConstants.CurrentImage] = tex;
                            SwapTargetBuffers();
                        }
                    }

                    render.SetSamplerStates(ginfo.SamplerState);
                    render.DettachBindedTextures(16);

                    restoreDepth.PerformForwardPass(render[PrincipalConstants.CurrentImage], render[PrincipalConstants.DephRT], render, ginfo);

                    render.DettachBindedTextures(2);

                    if (world.PhysicWorld.isDebugDraw)
                    {
                        world.PhysicWorld.iDebugDrawn(render, gameTime, world.CameraManager.ActiveCamera);
                    }
                    if (world.ParticleManager != null)
                    {
                        world.ParticleManager.iDraw(gameTime, world.CameraManager.ActiveCamera.View, world.CameraManager.ActiveCamera.Projection, render);
                        render.ResyncStates();
                        render.SetSamplerStates(ginfo.SamplerState);
                    }

                    render.DettachBindedTextures(16);
                    forwardPass.Draw(gameTime, world, render, DeferrednotCulledObjectsList, ForwardnotCulledObjectsList);

                    render.DettachBindedTextures(16);
                    render.RenderPosWithDepthComponents(gameTime, ref view, ref projection);
                    render.DettachBindedTextures(16);

                    render[PrincipalConstants.CurrentImage] = restoreDepth.EndForwardPass(render);

                    if (desc.UseFloatingBufferForLightMap)
                    {
                        System.Diagnostics.Debug.Assert(render.PeekBlendState() == BlendState.Opaque);
                        render.Clear(Color.Black);
                        render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, SamplerState.PointClamp);
                        System.Diagnostics.Debug.Assert(render.PeekBlendState() == BlendState.Opaque);
                    }
                    else
                    {
                        render.Clear(Color.Black);
                        render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, ginfo.SamplerState);
                    }
                }
                else if (desc.RestoreDepthOption == RestoreDepthOption.NONE)
                {
                    for (int i = 0; i < PostEffects.Count; i++)
                    {
                        if (PostEffects[i].Enabled)
                        {
                            render.PushRenderTarget(PostEffectTarget);
                            render.Clear(Color.Black);
                            PostEffects[i].Draw(render[PrincipalConstants.CurrentImage], render, gameTime, ginfo, world, desc.UseFloatingBufferForLightMap);
                            Texture2D tex = render.PopRenderTarget()[0].RenderTarget as Texture2D;
                            System.Diagnostics.Debug.Assert(tex != null);
                            render[PrincipalConstants.CurrentImage] = tex;
                            SwapTargetBuffers();
                        }
                    }

                    if (desc.UseFloatingBufferForLightMap)
                    {
                        render.Clear(Color.Black);
                        render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, SamplerState.PointClamp);
                    }
                    else
                    {
                        render.Clear(Color.Black);
                        render.RenderTextureComplete(render[PrincipalConstants.CurrentImage], Color.White, ginfo.FullScreenRectangle, Matrix.Identity, null, true, SpriteSortMode.Deferred, ginfo.SamplerState);
                    }
                }

                render.RenderPosComponents(gameTime, ref view, ref projection);

                if (desc.DefferedDebug)
                {
                    render.Clear(Color.Black);
                    if (desc.UseFloatingBufferForLightMap)
                    {
                        render.RenderBegin(Matrix.Identity, null, SpriteSortMode.Immediate, SamplerState.PointClamp);
                    }
                    else
                    {
                        render.RenderBegin(Matrix.Identity, null, SpriteSortMode.Immediate, ginfo.SamplerState);
                    }

                    render.RenderTexture(render[desc.RenderTargetsNameToDefferedDebug[0]], Color.White, new Rectangle(0, 0, halfWidth, halfHeight));
                    render.RenderTexture(render[desc.RenderTargetsNameToDefferedDebug[1]], Color.White, new Rectangle(0, halfHeight, halfWidth, halfHeight));
                    render.RenderTexture(render[desc.RenderTargetsNameToDefferedDebug[2]], Color.White, new Rectangle(halfWidth, 0, halfWidth, halfHeight));
                    render.RenderTexture(render[desc.RenderTargetsNameToDefferedDebug[3]], Color.White, new Rectangle(halfWidth, halfHeight, halfWidth, halfHeight));

                    render.RenderEnd();
                }
            }
            else
            {
                render.RenderPosComponents(gameTime, ref view, ref projection);
            }

            render.ValidateSamplerStates();
        }