private bool RenderSceneToTexture() { // Set the render target to be the render to texture. RenderTexture.SetRenderTarget(D3D.DeviceContext); // Clear the render to texture. RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f); // Get the world matrix from the d3d object. Matrix worldMatrix = D3D.WorldMatrix; // Generate the light view matrix based on the light's position. Light.GenerateViewMatrix(); // Get the view and orthographic matrices from the light object. Matrix lightViewMatrix = Light.ViewMatrix; Matrix lightOrthoMatrix = Light.OrthoMatrix; // Translate to the position of the tree. Vector3 treePosition = TreeModel.GetPosition(); Matrix.Translation(treePosition.X, treePosition.Y, treePosition.Z, out worldMatrix); // Render the tree trunk with the depth shader. TreeModel.RenderTrunk(D3D.DeviceContext); if (!DepthShader.Render(D3D.DeviceContext, TreeModel.TrunkIndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix)) { return(false); } // Render the tree leaves using the depth transparency shader. TreeModel.RenderLeaves(D3D.DeviceContext); if (!TransparentDepthShader.Render(D3D.DeviceContext, TreeModel.LeafIndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix, TreeModel.LeafTexture.TextureResource)) { return(false); } // Reset the world matrix. worldMatrix = D3D.WorldMatrix; // Translate to the position of the ground model. Vector3 groundPosition = GroundModel.GetPosition(); Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMatrix); // Render the ground model with the depth shader. GroundModel.Render(D3D.DeviceContext); if (!DepthShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix)) { return(false); } // Reset the render target back to the original back buffer and not the render to texture anymore. D3D.SetBackBufferRenderTarget(); // Reset the viewport back to the original. D3D.ResetViewPort(); return(true); }
private bool Render() { // Render the depth of the scene to a texture. if (!RenderSceneToTexture()) { return(false); } // Clear the scene to Grey D3D.BeginScene(0.0f, 0.5f, 0.8f, 1.0f); // Generate the view matrix based on the camera's position. Camera.Render(); // Generate the light view matrix based on the light's position. Light.GenerateViewMatrix(); // Get the matrices from the camera and d3d objects. Matrix viewCameraMatrix = Camera.ViewMatrix; Matrix worldMatrix = D3D.WorldMatrix; Matrix projectionMatrix = D3D.ProjectionMatrix; // Get the light's view and projection matrices from the light object. Matrix lightViewMatrix = Light.ViewMatrix; Matrix lightOrthoMatrix = Light.OrthoMatrix; // Set the light color attributes. Light.SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); Light.SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f); // Translate to the position of the ground model. Vector3 groundPosition = GroundModel.GetPosition(); Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMatrix); // Render the ground model using the shadow shader. GroundModel.Render(D3D.DeviceContext); if (!ShadowShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, lightViewMatrix, lightOrthoMatrix, GroundModel.Texture.TextureResource, RenderTexture.ShaderResourceView, Light.Direction, Light.AmbientColor, Light.DiffuseColour)) { return(false); } // Reset the world matrix. worldMatrix = D3D.WorldMatrix; // Translate to the position of the tree model. Vector3 treePosition = TreeModel.GetPosition(); Matrix.Translation(treePosition.X, treePosition.Y, treePosition.Z, out worldMatrix); // Render the tree trunk. TreeModel.RenderTrunk(D3D.DeviceContext); if (!ShadowShader.Render(D3D.DeviceContext, TreeModel.TrunkIndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, lightViewMatrix, lightOrthoMatrix, TreeModel.TrunkTexture.TextureResource, RenderTexture.ShaderResourceView, Light.Direction, Light.AmbientColor, Light.DiffuseColour)) { return(false); } // Enable blending and D3D.TurnOnAlphaBlending(); // Render the tree leaves. TreeModel.RenderLeaves(D3D.DeviceContext); if (!ShadowShader.Render(D3D.DeviceContext, TreeModel.LeafIndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, lightViewMatrix, lightOrthoMatrix, TreeModel.LeafTexture.TextureResource, RenderTexture.ShaderResourceView, Light.Direction, Light.AmbientColor, Light.DiffuseColour)) { return(false); } // Turn off Alpha Blending. D3D.TurnOffAlphaBlending(); // Reset the world matrix. worldMatrix = D3D.WorldMatrix; // Present the rendered scene to the screen. D3D.EndScene(); return(true); }