/// <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); // Start shader effect.CurrentTechnique = effect.Techniques[techniqueName]; // 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.Apply(); renderCode(); } }
/// <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.DepthStencilState = DepthStencilState.Default; // Render shadows into our shadowMapBlur render target shadowMapBlur.RenderShadows( delegate { effect.CurrentTechnique = effect.Techniques["UseShadowMap20"]; // 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) RacingGameManager.Device.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0); }
/// <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(); } finally { effect.End(); } }
/// <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; } // 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); effect.CurrentTechnique = effect.Techniques["GenerateShadowMap20"]; // 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> /// 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) // Start first pass effect.CurrentTechnique.Passes[0].Apply(); // Render renderCode(); }
/// <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> /// 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); }