private bool RenderRefractionToTexture()
        {
            // Setup a clipping plane based on the height of the water to clip everything above it to create a refraction.
            Vector4 clipPlane = new Vector4(0.0f, -1.0f, 0.0f, WaterModel.WaterHeight + 0.1f);

            // Set the render target to be the refraction render to texture.
            RefractionTexture.SetRenderTarget(D3D.DeviceContext);

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

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

            // Get the matrices from the camera and d3d objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix viewMatrix       = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Render the terrain using the reflection shader and the refraction clip plane to produce the refraction effect.
            TerrainModel.Render(D3D.DeviceContext);
            if (!ReflectionShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, viewMatrix, 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);
        }
        public void Shutdown()
        {
            // Release the position object.
            Position = null;
            // Release the light object.
            Light = null;
            // Release the fps object.
            FPS = null;
            // Release the camera object.
            Camera = null;

            // Release the text object.
            Text?.Shutdown();
            Text = null;
            // Release the cpu object.
            CPU?.Shutdown();
            CPU = null;
            // Release the water shader object.
            WaterShader?.ShutDown();
            WaterShader = null;
            // Release the water object.
            WaterModel?.ShutDown();
            WaterModel = null;
            // Release the reflection shader object.
            ReflectionShader?.ShutDown();
            ReflectionShader = null;
            // Release the reflection render to texture object.
            ReflectionTexture?.Shutdown();
            ReflectionTexture = null;
            // Release the refraction render to texture object.
            RefractionTexture?.Shutdown();
            RefractionTexture = null;
            // Release the sky plane shader object.
            SkyPlaneShader?.ShutDown();
            SkyPlaneShader = null;
            // Release the sky plane object.
            SkyPlane?.ShurDown();
            SkyPlane = null;
            // Release the sky dome shader object.
            SkyDomeShader?.ShutDown();
            SkyDomeShader = null;
            // Release the sky dome object.
            SkyDome?.ShutDown();
            SkyDome = null;
            // Release the terrain shader object.
            TerrainShader?.ShutDown();
            TerrainShader = null;
            // Release the tree object.
            TerrainModel?.ShutDown();
            TerrainModel = null;
            // Release the input object.
            Input?.Shutdown();
            Input = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }
        private bool RenderScene()
        {
            // Clear the buffer to begin the scene as Black.
            D3D.BeginScene(0, 0, 0, 1f);

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

            // Get the world, view, and projection matrices from camera and d3d objects.
            var viewMatrix       = Camera.ViewMatrix;
            var worldMatrix      = D3D.WorldMatrix;
            var projectionMatrix = D3D.ProjectionMatrix;

            //// Rotate the world matrix by the rotation value so that the triangle will spin.
            Matrix.RotationY(Rotation, out worldMatrix);

            // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
            Model.Render(D3D.DeviceContext);

            // Render the model with the texture shader.
            if (!TextureShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.TextureCollection.Select(item => item.TextureResource).First()))
            {
                return(false);
            }

            // Get the world matrix again and translate down for the floor model to render underneath the cube.
            worldMatrix = D3D.WorldMatrix;
            Matrix.Translation(0, -1.5f, 0, out worldMatrix);

            // Get the camera reflection view matrix.
            var reflectionMatrix = Camera.ReflectionViewMatrix;

            // Put the floor model vertex and index buffers on the graphics pipeline to prepare them for drawing.
            FloorModel.Render(D3D.DeviceContext);

            // Render the floor model using the reflection shader, reflection texture, and reflection view matrix.
            if (!ReflectionShader.Render(D3D.DeviceContext, FloorModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, FloorModel.TextureCollection.Select(item => item.TextureResource).First(), RenderTexture.ShaderResourceView, reflectionMatrix))
            {
                return(false);
            }

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

            return(true);
        }
        public void Shutdown()
        {
            // Release the camera object.
            Camera = null;

            // Release the reflection shader object.
            ReflectionShader?.ShutDown();
            ReflectionShader = null;
            // Release the texture shader object.
            TextureShader?.ShutDown();
            TextureShader = null;
            // Release the render to texture object.
            RenderTexture?.Shutdown();
            RenderTexture = null;
            // Release the model object.
            Model?.Shutdown();
            Model = null;
            FloorModel?.Shutdown();
            FloorModel = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }
        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);
        }
Ejemplo n.º 6
0
        public void Shutdown()
        {
            // Release the reflection shader object.
            if (ReflectionShader != null)
            {
                ReflectionShader.Shuddown();
                ReflectionShader = null;
            }

            // Release the transparent shader object.
            if (TransparentShader != null)
            {
                TransparentShader.Shuddown();
                TransparentShader = null;
            }

            // Release the translate shader object.
            if (TranslateShader != null)
            {
                TranslateShader.Shuddown();
                TranslateShader = null;
            }

            // Release the clip plane shader object.
            if (ClipPlaneShader != null)
            {
                ClipPlaneShader.Shuddown();
                ClipPlaneShader = null;
            }

            // Release the fog shader object.
            if (FogShader != null)
            {
                FogShader.Shuddown();
                FogShader = null;
            }

            // Release the texture shader object.
            if (TextureShader != null)
            {
                TextureShader.Shuddown();
                TextureShader = null;
            }

            // Release the debug window object.
            if (DebugWindow != null)
            {
                DebugWindow.Shutdown();
                DebugWindow = null;
            }

            // Release the debug render to texture object.
            if (RenderDebugTexture != null)
            {
                RenderDebugTexture.Shutdown();
                RenderDebugTexture = null;
            }

            // Release the render to texture object.
            if (RenderReflectionTexture != null)
            {
                RenderReflectionTexture.Shutdown();
                RenderReflectionTexture = null;
            }

            // Release the light object.
            Light = null;

            // Release the shader object.
            if (LightShader != null)
            {
                LightShader.Shuddown();
                LightShader = null;
            }

            // Release the model object.
            if (Model != null)
            {
                Model.Shutdown();
                Model = null;
            }

            // Release the light shader object.
            if (BumpMapShader != null)
            {
                BumpMapShader.Shuddown();
                BumpMapShader = null;
            }

            // Release the model object.
            if (BumpMapModel != null)
            {
                BumpMapModel.Shutdown();
                BumpMapModel = null;
            }

            // Release the text object.
            if (Text != null)
            {
                Text.Shutdown();
                Text = null;
            }

            // Release the camera object.
            if (Camera != null)
            {
                Camera = null;
            }

            // Release the Direct3D object.
            if (D3D != null)
            {
                D3D.Shutdown();
                D3D = null;
            }
        }
Ejemplo n.º 7
0
        public bool Initialize(SystemConfiguration configuration, IntPtr windowHandle)
        {
            try
            {
                #region Initialize System
                // Create the Direct3D object.
                D3D = new DX11();
                // Initialize the Direct3D object.
                if (!D3D.Initialize(configuration, windowHandle))
                {
                    MessageBox.Show("Could not initialize Direct3D", "Error", MessageBoxButtons.OK);
                    return(false);
                }
                #endregion

                #region Initialize Camera
                // Create the camera object
                Camera = new Camera();
                #endregion

                #region Initialize Models
                // Create the model class.
                Model = new Model();

                // Initialize the model object.
                if (!Model.Initialize(D3D.Device, "sphere.txt", new[] { "seafloor.dds" }))
                {
                    MessageBox.Show("Could not initialize the model object", "Error", MessageBoxButtons.OK);
                    return(false);
                }

                // Create the model class.
                FloorModel = new Model();

                // Initialize the model object.
                if (!FloorModel.Initialize(D3D.Device, "floor.txt", new[] { "blue01.dds" }))
                {
                    MessageBox.Show("Could not initialize the model object", "Error", MessageBoxButtons.OK);
                    return(false);
                }
                #endregion

                #region Initialize Shaders
                // Create the shader object.
                TextureShader = new TextureShader();

                // Initialize the shader object.
                if (!TextureShader.Initialize(D3D.Device, windowHandle))
                {
                    MessageBox.Show("Could not initialize the shader", "Error", MessageBoxButtons.OK);
                    return(false);
                }

                // Create the shader object.
                ReflectionShader = new ReflectionShader();

                // Initialize the shader object.
                if (!ReflectionShader.Initialize(D3D.Device, windowHandle))
                {
                    MessageBox.Show("Could not initialize the shader", "Error", MessageBoxButtons.OK);
                    return(false);
                }
                #endregion

                #region Initialize Data
                // Create the render to texture object.
                RenderReflectionTexture = new RenderTexture();

                // Initialize the render to texture object.
                if (!RenderReflectionTexture.Initialize(D3D.Device, configuration))
                {
                    return(false);
                }

                // Create the light object.
                Light = new Light();

                // Initialize the light object.
                Light.SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
                Light.SetDiffuseColor(1, 1, 1, 1f);
                Light.SetDirection(0, 0, 1);
                Light.SetSpecularColor(0, 1, 1, 1);
                Light.SetSpecularPower(16);
                #endregion

                #region Debug Window Initialize
                if (SystemConfiguration.DebugWindowOn)
                {
                    // Create the render to texture object.
                    RenderDebugTexture = new RenderTexture();

                    // Initialize the render to texture object.
                    if (!RenderDebugTexture.Initialize(D3D.Device, configuration))
                    {
                        return(false);
                    }

                    // Create the debug window object.
                    DebugWindow = new DebugWindow();

                    // Initialize the debug window object.
                    if (!DebugWindow.Initialize(D3D.Device, configuration.Width, configuration.Height, 100, 100 * configuration.Height / configuration.Width))
                    {
                        MessageBox.Show("Could not initialize the debug window object.", "Error", MessageBoxButtons.OK);
                        return(false);
                    }

                    // Create the texture shader object.
                    TextureShader = new TextureShader();

                    // Initialize the texture shader object.
                    if (!TextureShader.Initialize(D3D.Device, windowHandle))
                    {
                        MessageBox.Show("Could not initialize the texture shader object.", "Error", MessageBoxButtons.OK);
                        return(false);
                    }
                }
                #endregion

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'");
                return(false);
            }
        }