/// <summary>
        /// Render single pass shader
        /// </summary>
        /// <param name="renderDelegate">Render delegate</param>
        public void RenderSinglePassShader(
			BaseGame.RenderHandler renderCode)
        {
            if (renderCode == null)
                throw new ArgumentNullException("renderCode");

            // Start effect (current technique should be set)
            try
            {
                effect.Begin(SaveStateMode.None);
                // Start first pass
                effect.CurrentTechnique.Passes[0].Begin();

                // Render
                renderCode();

                // End pass and shader
                effect.CurrentTechnique.Passes[0].End();
            } // try
            finally
            {
                effect.End();
            } // finally
        }
 public ScreenshotCapturer(BaseGame setGame)
     : base(setGame)
 {
     game = setGame;
     screenshotNum = GetCurrentScreenshotNum();
 }
        /// <summary>
        /// Render
        /// </summary>
        /// <param name="techniqueName">Technique name</param>
        /// <param name="renderDelegate">Render delegate</param>
        public void Render(string techniqueName,
			BaseGame.RenderHandler renderDelegate)
        {
            Render(null, techniqueName, renderDelegate);
        }
        /// <summary>
        /// Render
        /// </summary>
        /// <param name="setMat">Set matrix</param>
        /// <param name="passName">Pass name</param>
        /// <param name="renderDelegate">Render delegate</param>
        public void Render(Material setMat,
			string techniqueName,
			BaseGame.RenderHandler renderCode)
        {
            if (techniqueName == null)
                throw new ArgumentNullException("techniqueName");
            if (renderCode == null)
                throw new ArgumentNullException("renderCode");

            SetParameters(setMat);

            // Can we do the requested technique?
            // For graphic cards not supporting ps2.0, fall back to ps1.1
            if (BaseGame.CanUsePS20 == false &&
                techniqueName.EndsWith("20"))
                // Use same technique without the 20 ending!
                techniqueName = techniqueName.Substring(0, techniqueName.Length - 2);

            // Start shader
            effect.CurrentTechnique = effect.Techniques[techniqueName];
            try
            {
                effect.Begin(SaveStateMode.None);

                // Render all passes (usually just one)
                //foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
                {
                    EffectPass pass = effect.CurrentTechnique.Passes[num];

                    pass.Begin();
                    renderCode();
                    pass.End();
                } // foreach (pass)
            } // try
            finally
            {
                // End shader
                effect.End();
            } // finally
        }
        /// <summary>
        /// Render shadows
        /// </summary>
        /// <param name="renderCode">Render code</param>
        public void RenderShadows(BaseGame.RenderHandler renderCode)
        {
            if (renderCode == null)
                throw new ArgumentNullException("renderCode");

            // Render into our scene map texture
            sceneMapTexture.SetRenderTarget();

            // Clear render target
            sceneMapTexture.Clear(Color.White);

            // Render everything
            renderCode();

            // Resolve render target
            sceneMapTexture.Resolve();

            // Restore back buffer as render target
            BaseGame.ResetRenderTarget(false);
        }
        /// <summary>
        /// Generate shadow
        /// </summary>
        internal void GenerateShadows(BaseGame.RenderHandler renderObjects)
        {
            // Can't generate shadow if loading failed!
            if (effect == null)
                return;

            // This method sets all required shader variables.
            this.SetParameters(null);
            Matrix remViewMatrix = BaseGame.ViewMatrix;
            Matrix remProjMatrix = BaseGame.ProjectionMatrix;
            CalcSimpleDirectionalShadowMappingMatrix();

            // Time to generate the shadow texture
            DepthStencilBuffer remBackBufferSurface = null;
            // Start rendering onto the shadow map
            shadowMapTexture.SetRenderTarget();
            if (shadowMapTexture.ZBufferSurface != null)
            {
                remBackBufferSurface = BaseGame.Device.DepthStencilBuffer;
                BaseGame.Device.DepthStencilBuffer =
                    shadowMapTexture.ZBufferSurface;
            } // if (shadowMapTexture.ZBufferSurface)

            // Make sure depth buffer is on
            BaseGame.Device.RenderState.DepthBufferEnable = true;
            // Disable alpha
            BaseGame.Device.RenderState.AlphaBlendEnable = false;

            // Clear render target
            shadowMapTexture.Clear(Color.White);

            if (BaseGame.CanUsePS20)
                effect.CurrentTechnique = effect.Techniques["GenerateShadowMap20"];
            else
                effect.CurrentTechnique = effect.Techniques["GenerateShadowMap"];

            // Render shadows with help of the GenerateShadowMap shader
            RenderSinglePassShader(renderObjects);

            // Resolve the render target to get the texture (required for Xbox)
            shadowMapTexture.Resolve();

            // Set render target back to default
            BaseGame.ResetRenderTarget(false);

            if (shadowMapTexture.ZBufferSurface != null)
                BaseGame.Device.DepthStencilBuffer = remBackBufferSurface;

            BaseGame.ViewMatrix = remViewMatrix;
            BaseGame.ProjectionMatrix = remProjMatrix;
        }
        /// <summary>
        /// Calc shadows with help of generated light depth map,
        /// all objects have to be rendered again for comparing.
        /// We could save a pass when directly rendering here, but this
        /// has 2 disadvantages: 1. we can't post screen blur the shadow
        /// and 2. we can't use any other shader, especially bump and specular
        /// mapping shaders don't have any instructions left with ps_1_1.
        /// This way everything is kept simple, we can do as complex shaders
        /// as we want, the shadow shaders work seperately.
        /// </summary>
        /// <param name="renderObjects">Render objects</param>
        public void RenderShadows(BaseGame.RenderHandler renderObjects)
        {
            // Can't calc shadows if loading failed!
            if (effect == null)
                return;

            // Make sure z buffer and writing z buffer is on
            BaseGame.Device.RenderState.DepthBufferEnable = true;
            BaseGame.Device.RenderState.DepthBufferWriteEnable = true;

            // Render shadows into our shadowMapBlur render target
            shadowMapBlur.RenderShadows(
                delegate
                {
                    if (BaseGame.CanUsePS20)
                        effect.CurrentTechnique = effect.Techniques["UseShadowMap20"];
                    else
                        effect.CurrentTechnique = effect.Techniques["UseShadowMap"];

                    // This method sets all required shader variables.
                    this.SetParameters(null);

                    // Use the shadow map texture here which was generated in
                    // GenerateShadows().
                    shadowMap.SetValue(shadowMapTexture.XnaTexture);

                    // Render shadows with help of the UseShadowMap shader
                    RenderSinglePassShader(renderObjects);
                });

            // Start rendering the shadow map blur (pass 1, which messes up our
            // background), pass 2 can be done below without any render targets.
            shadowMapBlur.RenderShadows();

            // Kill background z buffer (else glass will not be rendered correctly)
            BaseGame.Device.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0);
        }