/// <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); }); }