public TerrainObject(TerrainRenderer renderer, Device renderingDevice)
        {
            this.renderingDevice = renderingDevice;
            this.renderer = renderer;

            // Default position
            Position = new Vector3(0, 20, 0);

            // Load textures
            noiseTexture = ShaderResourceView.FromFile(renderingDevice, renderer.Directory + "\\Resources\\Noise.dds");
            grassTexture = ShaderResourceView.FromFile(renderingDevice, renderer.Directory + "\\Resources\\Grass.dds");
            sandTexture = ShaderResourceView.FromFile(renderingDevice, renderer.Directory + "\\Resources\\Sand.dds");
            snowTexture = ShaderResourceView.FromFile(renderingDevice, renderer.Directory + "\\Resources\\Snow.dds");
            snowRockTexture = ShaderResourceView.FromFile(renderingDevice, renderer.Directory + "\\Resources\\SnowRock.dds");

            // Create shader
            SetSourceCode(" float4 getColor(float4 position, float4 normal, float4 camera) { return float4(0, 0, 0, 0); } ");

            // Read data from file
            Vector3[] vertices; int[] indices;

            new XLoader().LoadFile(renderer.Directory + "\\Resources\\Terrain.X", out vertices, out indices);

            //
            DataStream vertexStream = new DataStream(vertices.Length * 12, true, true);
            vertexStream.WriteRange(vertices);
            vertexStream.Position = 0;

            DataStream indexStream = new DataStream(indices.Length * 4, true, true);
            indexStream.WriteRange(indices);
            indexStream.Position = 0;

            //
            BufferDescription vbufferDescription = new BufferDescription();
            vbufferDescription.BindFlags = BindFlags.VertexBuffer;
            vbufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            vbufferDescription.OptionFlags = ResourceOptionFlags.None;
            vbufferDescription.SizeInBytes = (int)vertexStream.Length;
            vbufferDescription.Usage = ResourceUsage.Immutable;

            vertexBuffer = new Buffer(renderingDevice, vertexStream, vbufferDescription);

            //
            BufferDescription ibufferDescription = new BufferDescription();
            ibufferDescription.BindFlags = BindFlags.IndexBuffer;
            ibufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            ibufferDescription.OptionFlags = ResourceOptionFlags.None;
            ibufferDescription.SizeInBytes = (int)indexStream.Length;
            ibufferDescription.Usage = ResourceUsage.Immutable;

            indexBuffer = new Buffer(renderingDevice, indexStream, ibufferDescription);
            indexCount = (int)indexStream.Length / 4;
        }
        public TerrainScene(TerrainRenderer terrainRenderer)
        {
            // Create a description of the display mode
            ModeDescription modeDescription = new ModeDescription()
            {
                Format = Format.R8G8B8A8_UNorm,
                RefreshRate = new Rational(60, 1),

                Width = 512,
                Height = 512
            };

            // Create a description of the multisampler
            SampleDescription sampleDescription = new SampleDescription()
            {
                Count = 1,
                Quality = 0
            };

            // Create a description of the swap chain
            SwapChainDescription swapChainDescription = new SwapChainDescription()
            {
                ModeDescription = modeDescription,
                SampleDescription = sampleDescription,

                BufferCount = 1,
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput,

                IsWindowed = false
            };

            // Create a hardware accelarated rendering device
            renderingDevice = new Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport);

            // Create the shared texture
            Texture2DDescription colordesc = new Texture2DDescription();
            colordesc.BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource;
            colordesc.Format = Format.B8G8R8A8_UNorm;
            colordesc.Width = 512;
            colordesc.Height = 512;
            colordesc.MipLevels = 1;
            colordesc.SampleDescription = new SampleDescription(1, 0);
            colordesc.Usage = ResourceUsage.Default;
            colordesc.OptionFlags = ResourceOptionFlags.Shared;
            colordesc.CpuAccessFlags = CpuAccessFlags.None;
            colordesc.ArraySize = 1;

            SharedTexture = new Texture2D(renderingDevice, colordesc);

            // Create the render target view
            renderTargetView = new RenderTargetView(renderingDevice, SharedTexture);

            // Creat the depth/stencil buffer
            DepthStencilStateDescription depthStencilStateDescription = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = false,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less,
            };

            depthStencilState = DepthStencilState.FromDescription(renderingDevice, depthStencilStateDescription);

            Texture2DDescription depthStencilTextureDescription = new Texture2DDescription()
            {
                Width = 512,
                Height = 512,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.D32_Float,
                SampleDescription = sampleDescription,
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None
            };

            DepthStencilViewDescription depthStencilViewDescription = new DepthStencilViewDescription()
            {
                Format = depthStencilTextureDescription.Format,
                Dimension = depthStencilTextureDescription.SampleDescription.Count > 1 ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D,
                MipSlice = 0
            };

            using (Texture2D depthStencilTexture = new Texture2D(renderingDevice, depthStencilTextureDescription))
            {
                depthStencilView = new DepthStencilView(renderingDevice, depthStencilTexture, depthStencilViewDescription);
            }

            // Setup the default output targets
            renderingDevice.ImmediateContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);

            // Setup the viewport
            Viewport viewPort = new Viewport()
            {
                X = 0,
                Y = 0,
                Width = 512,
                Height = 512,
                MinZ = 0,
                MaxZ = 1
            };

            renderingDevice.ImmediateContext.Rasterizer.SetViewports(viewPort);

            // Create the teappot
            terrainObject = new TerrainObject(terrainRenderer, renderingDevice);

            // Create the camera
            camera = new ThirdPersonCamera(terrainObject);

        }