/// <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 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> /// Test create render to texture /// </summary> public static void TestCreateRenderToTexture() { Model testPlate = null; RenderToTexture renderToTexture = null; TestGame.Start( delegate { testPlate = new Model("CarSelectionPlate"); renderToTexture = new RenderToTexture( //SizeType.ShadowMap1024); //.QuarterScreen); SizeType.FullScreen); //SizeType.HalfScreen); }, delegate { bool renderToTextureWay = Input.Keyboard.IsKeyUp(Keys.Space) && Input.GamePadAPressed == false; if (renderToTextureWay) { // Set render target to our texture renderToTexture.SetRenderTarget(); // Clear background renderToTexture.Clear(Color.Blue); // Draw background lines BaseGame.DrawLine(new Point(0, 200), new Point(200, 0), Color.Blue); BaseGame.DrawLine(new Point(0, 0), new Point(400, 400), Color.Red); BaseGame.FlushLineManager2D(); // And draw object testPlate.Render(Matrix.CreateScale(1.5f)); // And flush render manager to draw all objects BaseGame.MeshRenderManager.Render(); // Resolve renderToTexture.Resolve(true); // Reset background buffer //obs: RenderToTexture.ResetRenderTarget(true); // Show render target in a rectangle on our screen renderToTexture.RenderOnScreen( //tst: new Rectangle(100, 100, 256, 256)); //BaseGame.ResolutionRect); } // if (renderToTextureWay) else { // Copy backbuffer way, render stuff normally first // Clear background BaseGame.Device.Clear(Color.Blue); // Draw background lines BaseGame.DrawLine(new Point(0, 200), new Point(200, 0), Color.Blue); BaseGame.DrawLine(new Point(0, 0), new Point(400, 400), Color.Red); BaseGame.FlushLineManager2D(); // And draw object testPlate.Render(Matrix.CreateScale(1.5f)); } // else TextureFont.WriteText(2, 30, "renderToTexture.Width=" + renderToTexture.Width); TextureFont.WriteText(2, 60, "renderToTexture.Height=" + renderToTexture.Height); TextureFont.WriteText(2, 90, "renderToTexture.Valid=" + renderToTexture.IsValid); TextureFont.WriteText(2, 120, "renderToTexture.XnaTexture=" + renderToTexture.XnaTexture); TextureFont.WriteText(2, 150, "renderToTexture.ZBufferSurface=" + renderToTexture.ZBufferSurface); TextureFont.WriteText(2, 180, "renderToTexture.Filename=" + renderToTexture.Filename); }); }
/// <summary> /// Execute shaders and show result on screen, Start(..) must have been /// called before and the scene should be rendered to sceneMapTexture. /// </summary> public override void Show() { // Only apply post screen glow if texture is valid and effect is valid if (sceneMapTexture == null || effect == null || started == false) { return; } started = false; // Resolve sceneMapTexture render target for Xbox360 support sceneMapTexture.Resolve(); // Don't use or write to the z buffer BaseGame.Device.RenderState.DepthBufferEnable = false; BaseGame.Device.RenderState.DepthBufferWriteEnable = false; // Also don't use any kind of blending. //Update: allow writing to alpha! BaseGame.Device.RenderState.AlphaBlendEnable = false; if (windowSize != null) { windowSize.SetValue( new float[] { sceneMapTexture.Width, sceneMapTexture.Height }); } if (sceneMap != null) { sceneMap.SetValue(sceneMapTexture.XnaTexture); } RadialBlurScaleFactor = // Warning: To big values will make the motion blur look to // stepy (we see each step and thats not good). -0.02 should be max. -(0.0025f + RacingGameManager.Player.Speed * 0.005f / Player.DefaultMaxSpeed); effect.CurrentTechnique = effect.Techniques["ScreenGlow20"]; // We must have exactly 5 passes! if (effect.CurrentTechnique.Passes.Count != 5) { throw new InvalidOperationException( "This shader should have exactly 5 passes!"); } try { effect.Begin(); for (int pass = 0; pass < effect.CurrentTechnique.Passes.Count; pass++) { if (pass == 0) { radialSceneMapTexture.SetRenderTarget(); } else if (pass == 1) { downsampleMapTexture.SetRenderTarget(); } else if (pass == 2) { blurMap1Texture.SetRenderTarget(); } else if (pass == 3) { blurMap2Texture.SetRenderTarget(); } else { // Do a full reset back to the back buffer BaseGame.ResetRenderTarget(true); } EffectPass effectPass = effect.CurrentTechnique.Passes[pass]; effectPass.Begin(); // For first effect we use radial blur, draw it with a grid // to get cooler results (more blur at borders than in middle). if (pass == 0) { VBScreenHelper.Render10x10Grid(); } else { VBScreenHelper.Render(); } effectPass.End(); if (pass == 0) { radialSceneMapTexture.Resolve(); if (radialSceneMap != null) { radialSceneMap.SetValue(radialSceneMapTexture.XnaTexture); } effect.CommitChanges(); } else if (pass == 1) { downsampleMapTexture.Resolve(); if (downsampleMap != null) { downsampleMap.SetValue(downsampleMapTexture.XnaTexture); } effect.CommitChanges(); } else if (pass == 2) { blurMap1Texture.Resolve(); if (blurMap1 != null) { blurMap1.SetValue(blurMap1Texture.XnaTexture); } effect.CommitChanges(); } else if (pass == 3) { blurMap2Texture.Resolve(); if (blurMap2 != null) { blurMap2.SetValue(blurMap2Texture.XnaTexture); } effect.CommitChanges(); } } } finally { effect.End(); // Restore z buffer state BaseGame.Device.RenderState.DepthBufferEnable = true; BaseGame.Device.RenderState.DepthBufferWriteEnable = true; } }