private bool Render()
        {
            // Clear the scene.
            D3D.BeginScene(0.0f, 0.0f, 1.0f, 1.0f);

            // Generate the view matrix based on the camera's position.
            Camera.Render();

            // Get the world, view, projection, ortho, and base view matrices from the camera and Direct3D objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix viewCameraMatrix = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;
            Matrix orthoMatrix      = D3D.OrthoMatrix;
            Matrix baseViewMatrix   = Camera.BaseViewMatrix;

            // Render the ground model.
            GroundModel.Render(D3D.DeviceContext);
            ShaderManager.RenderTextureShader(D3D.DeviceContext, GroundModel.IndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, GroundModel.ColourTexture.TextureResource);

            // Turn on the alpha-to-coverage blending.
            D3D.EnableSecondBlendState();

            // Render the foliage.
            Foliage.Render(D3D.DeviceContext);
            if (!ShaderManager.RenderFoliageShader(D3D.DeviceContext, Foliage.VertexCount, Foliage.InstanceCount, viewCameraMatrix, projectionMatrix, Foliage.Texture.TextureResource))
            {
                return(false);
            }

            // Turn off the alpha blending.
            D3D.TurnOffAlphaBlending();

            // Render the user interface.
            UserInterface.Render(D3D, ShaderManager, worldMatrix, baseViewMatrix, orthoMatrix);

            // Present the rendered scene to the screen.
            D3D.EndScene();

            return(true);
        }
        private bool Render()
        {
            // Clear the scene.
            D3D.BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

            // Generate the view matrix based on the camera's position.
            Camera.Render();

            // Get the world, view, projection, ortho, base view and reflection matrices from the camera and Direct3D objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix viewCameraMatrix = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;
            Matrix orthoMatrix      = D3D.OrthoMatrix;
            Matrix baseViewMatrix   = Camera.BaseViewMatrix;
            Matrix reflectionMatrix = Camera.ReflectionViewMatrix;

            // Get the position of the camera.
            Vector3 cameraPosition = Camera.GetPosition();

            // Translate the sky dome to be centered around the camera position.
            Matrix.Translation(cameraPosition.X, cameraPosition.Y, cameraPosition.Z, out worldMatrix);

            // Turn off back face culling and the Z buffer.
            D3D.TurnOffCulling();
            D3D.TurnZBufferOff();

            // Render the sky dome using the sky dome shader.
            SkyDome.Render(D3D.DeviceContext);
            if (!SkyDomeShader.Render(D3D.DeviceContext, SkyDome.IndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, SkyDome.ApexColour, SkyDome.CenterColour))
            {
                return(false);
            }

            // Turn back face culling back on.
            D3D.TurnOnCulling();

            // Enable additive blending so the clouds blend with the sky dome color.
            D3D.EnableSecondBlendState();

            // Render the sky plane using the sky plane shader.
            SkyPlane.Render(D3D.DeviceContext);
            if (!SkyPlaneShader.Render(D3D.DeviceContext, SkyPlane.IndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, SkyPlane.CloudTexture.TextureResource, SkyPlane.PerturbTexture.TextureResource, SkyPlane.m_Translation, SkyPlane.m_Scale, SkyPlane.m_Brightness))
            {
                return(false);
            }

            // Turn off blending.
            D3D.TurnOffAlphaBlending();

            // Turn the Z buffer back on.
            D3D.TurnZBufferOn();

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Render the terrain using the terrain shader.
            TerrainModel.Render(D3D.DeviceContext);
            if (!TerrainShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, TerrainModel.ColorTexture.TextureResource, TerrainModel.NormalMapTexture.TextureResource, Light.DiffuseColour, Light.Direction, 2.0f))
            {
                return(false);
            }

            // Translate to the location of the water and render it.
            Matrix.Translation(240.0f, WaterModel.WaterHeight, 250.0f, out worldMatrix);
            WaterModel.Render(D3D.DeviceContext);
            if (!WaterShader.Render(D3D.DeviceContext, WaterModel.IndexCount, worldMatrix, viewCameraMatrix, projectionMatrix, reflectionMatrix, ReflectionTexture.ShaderResourceView, RefractionTexture.ShaderResourceView, WaterModel.Texture.TextureResource, Camera.GetPosition(), WaterModel.NormalMapTiling, WaterModel.WaterTranslation, WaterModel.ReflectRefractScale, WaterModel.RefractionTint, Light.Direction, WaterModel.SpecularShininess))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Turn off the Z buffer to begin all 2D rendering.
            D3D.TurnZBufferOff();

            // Turn on the alpha blending before rendering the text.
            D3D.TurnOnAlphaBlending();

            // Render the text user interface elements.
            Text.Render(D3D.DeviceContext, worldMatrix, orthoMatrix);

            // Turn off alpha blending after rendering the text.
            D3D.TurnOffAlphaBlending();

            // Turn the Z buffer back on now that all 2D rendering has completed.
            D3D.TurnZBufferOn();

            // Present the rendered scene to the screen.
            D3D.EndScene();

            return(true);
        }
        private bool RenderReflectionToTexture()
        {
            // Setup a clipping plane based on the height of the water to clip everything below it.
            Vector4 clipPlane = new Vector4(0.0f, 1.0f, 0.0f, -WaterModel.WaterHeight);

            // Set the render target to be the reflection render to texture.
            ReflectionTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the reflection render to texture.
            ReflectionTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Use the camera to render the reflection and create a reflection view matrix.
            Camera.RenderReflection(WaterModel.WaterHeight);

            // Get the camera reflection view matrix instead of the normal view matrix.
            Matrix reflectionViewMatrix = Camera.ReflectionViewMatrix;

            // Get the world and projection matrices from the d3d object.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Get the position of the camera.
            Vector3 cameraPosition = Camera.GetPosition();

            // Invert the Y coordinate of the camera around the water plane height for the reflected camera position.
            cameraPosition.Y = -cameraPosition.Y + (WaterModel.WaterHeight * 2.0f);

            // Translate the sky dome and sky plane to be centered around the reflected camera position.
            Matrix.Translation(cameraPosition.X, cameraPosition.Y, cameraPosition.Z, out worldMatrix);

            // Turn off back face culling and the Z buffer.
            D3D.TurnOffCulling();
            D3D.TurnZBufferOff();

            // Render the sky dome using the reflection view matrix.
            SkyDome.Render(D3D.DeviceContext);
            if (!SkyDomeShader.Render(D3D.DeviceContext, SkyDome.IndexCount, worldMatrix, reflectionViewMatrix, projectionMatrix, SkyDome.ApexColour, SkyDome.CenterColour))
            {
                return(false);
            }

            // Enable back face culling.
            D3D.TurnOnCulling();

            // Enable additive blending so the clouds blend with the sky dome color.
            D3D.EnableSecondBlendState();

            // Render the sky plane using the sky plane shader.
            SkyPlane.Render(D3D.DeviceContext);
            if (!SkyPlaneShader.Render(D3D.DeviceContext, SkyPlane.IndexCount, worldMatrix, reflectionViewMatrix, projectionMatrix, SkyPlane.CloudTexture.TextureResource, SkyPlane.PerturbTexture.TextureResource, SkyPlane.m_Translation, SkyPlane.m_Scale, SkyPlane.m_Brightness))
            {
                return(false);
            }

            // Turn off blending and enable the Z buffer again.
            D3D.TurnOffAlphaBlending();
            D3D.TurnZBufferOn();

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Render the terrain using the reflection view matrix and reflection clip plane.
            TerrainModel.Render(D3D.DeviceContext);
            if (!ReflectionShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, reflectionViewMatrix, projectionMatrix, TerrainModel.ColorTexture.TextureResource, TerrainModel.NormalMapTexture.TextureResource, Light.DiffuseColour, Light.Direction, 2.0f, clipPlane))
            {
                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 RenderGraphics()
        {
            // Clear the scene.
            D3D.BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

            // Generate the view matrix based on the camera's position.
            Camera.Render();

            // Get the world, view, projection, and ortho matrices from the camera and Direct3D objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix cameraViewMatrix = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;
            Matrix orthoD3DMatrix   = D3D.OrthoMatrix;

            // Get the position of the camera.
            Vector3 cameraPostion = Camera.GetPosition();

            // Translate the sky dome to be centered around the camera position.
            Matrix.Translation(cameraPostion.X, cameraPostion.Y, cameraPostion.Z, out worldMatrix);

            // Turn off back face culling.
            D3D.TurnOffCulling();

            // Turn off the Z buffer.
            D3D.TurnZBufferOff();

            // Render the sky dome using the sky dome shader.
            SkyDome.Render(D3D.DeviceContext);
            if (!SkyDomeShader.Render(D3D.DeviceContext, SkyDome.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, SkyDome.ApexColour, SkyDome.CenterColour))
            {
                return(false);
            }

            // Turn back face culling back on.
            D3D.TurnOnCulling();

            // Enable additive blending so the clouds blend with the sky dome color.
            D3D.EnableSecondBlendState();

            // Render the sky plane using the sky plane shader.
            SkyPlane.Render(D3D.DeviceContext);
            if (!SkyPlaneShader.Render(D3D.DeviceContext, SkyPlane.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, SkyPlane.CloudTexture.TextureResource, SkyPlane.PerturbTexture.TextureResource, SkyPlane.m_Translation, SkyPlane.m_Scale, SkyPlane.m_Brightness))
            {
                return(false);
            }

            // Turn off blending
            D3D.TurnOffAlphaBlending();

            // Turn the Z buffer back on.
            D3D.TurnZBufferOn();

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Render the terrain buffers.
            TerrainModel.Render(D3D.DeviceContext);

            // Render the model using the color shader.
            if (!TerrainShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, Light.AmbientColor, Light.DiffuseColour, Light.Direction, TerrainModel.Texture.TextureResource))
            {
                return(false);
            }

            //// Turn off the Z buffer to begin all 2D rendering.
            D3D.TurnZBufferOff();

            //// Turn on the alpha blending before rendering the text.
            D3D.TurnOnAlphaBlending();

            // Render the text user interface elements.
            if (!Text.Render(D3D.DeviceContext, worldMatrix, orthoD3DMatrix))
            {
                return(false);
            }

            //// Turn off alpha blending after rendering the text.
            D3D.TurnOffAlphaBlending();

            //// Turn the Z buffer back on now that all 2D rendering has completed.
            D3D.TurnZBufferOn();

            // Present the rendered scene to the screen.
            D3D.EndScene();

            return(true);
        }