public void TestAdjustedAverageLuminance()
        {
            var game = new DX11Game();

            game.InitDirectX();
            var device  = game.Device;
            var context = device.ImmediateContext;



            var hdrImage1 = Texture2D.FromFile(device, HdrImageDDS);

            var hdrImage1RV = new ShaderResourceView(device, hdrImage1);



            var desc = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Width             = 300,
                Height            = 300,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.GenerateMipMaps
            };
            var hdrImage = new Texture2D(device, desc);

            var hdrImageRTV = new RenderTargetView(device, hdrImage);
            var hdrImageRV  = new ShaderResourceView(device, hdrImage);

            var calculater = new AverageLuminanceCalculater(game, hdrImageRV);


            game.GameLoopEvent += delegate
            {
                context.OutputMerger.SetTargets(hdrImageRTV);

                if (game.Keyboard.IsKeyPressed(Key.D1))
                {
                    game.TextureRenderer.Draw(hdrImage1RV, new Vector2(), new Vector2(300, 300));
                }
                if (game.Keyboard.IsKeyPressed(Key.D2))
                {
                    context.ClearRenderTargetView(hdrImageRTV, new Color4(1, 1, 1, 1));
                }


                calculater.DrawUpdatedLogLuminance();
                calculater.DrawUpdatedAdaptedLogLuminance();

                context.ClearState();
                game.SetBackbuffer();

                game.TextureRenderer.Draw(calculater.AverageLuminanceRV, new Vector2(10, 10),
                                          new Vector2(300, 300));
                game.TextureRenderer.Draw(hdrImageRV, new Vector2(320, 10),
                                          new Vector2(300, 300));
                game.TextureRenderer.Draw(calculater.CurrAverageLumRV, new Vector2(10, 320),
                                          new Vector2(270, 270));
            };

            game.Run();
        }
        public void TestDirectionalLightShadowing()
        {
            var game = new DX11Game();

            game.InitDirectX();
            var device  = game.Device;
            var context = device.ImmediateContext;

            var filledGBuffer = new TestFilledGBuffer(game, 800, 600);

            var light = new DirectionalLightRenderer(game, filledGBuffer.GBuffer);

            light.ShadowsEnabled = true;

            var toggle = false;


            game.GameLoopEvent += delegate
            {
                filledGBuffer.DrawUpdatedGBuffer();


                light.UpdateShadowmap(delegate(OrthographicCamera lightCamera)
                {
                    game.Camera = lightCamera;

                    filledGBuffer.Draw();

                    game.Camera = game.SpectaterCamera;
                }, game.SpectaterCamera);


                game.SetBackbuffer();

                if (game.Keyboard.IsKeyPressed(Key.C))
                {
                    toggle = !toggle;
                }

                if (toggle)
                {
                    light.LightDirection = game.SpectaterCamera.CameraDirection;
                }


                if (game.Keyboard.IsKeyDown(Key.I))
                {
                    GBufferTest.DrawGBuffer(game, filledGBuffer.GBuffer);
                }
                else
                {
                    light.Draw();
                    game.TextureRenderer.Draw(light.CSMRenderer.ShadowMapRV, new Vector2(10, 10), new Vector2(590, 200));
                    for (int i = 0; i < 6; i++)
                    {
                        //game.LineManager3D.AddViewFrustum(light.LightCameras[i].ViewProjection,
                        //new Color4(0, 1, 0));
                    }
                }
            };

            game.Run();
        }
        public void TestMultipleLightAccumulation()
        {
            //TODO: add a way to show the specular in the alpha channel

            var game = new DX11Game();

            game.InitDirectX();
            var device  = game.Device;
            var context = device.ImmediateContext;

            var filledGBuffer = new TestFilledGBuffer(game, 800, 600);

            var spot        = new SpotLightRenderer(game, filledGBuffer.GBuffer);
            var point       = new PointLightRenderer(game, filledGBuffer.GBuffer);
            var directional = new DirectionalLightRenderer(game, filledGBuffer.GBuffer);

            var state = 0;

            var bsDesc = new BlendStateDescription();
            var b      = new RenderTargetBlendDescription();

            b.BlendEnable           = true;
            b.BlendOperation        = BlendOperation.Add;
            b.BlendOperationAlpha   = BlendOperation.Add;
            b.DestinationBlend      = BlendOption.One;
            b.DestinationBlendAlpha = BlendOption.One;
            b.SourceBlend           = BlendOption.One;
            b.SourceBlendAlpha      = BlendOption.One;
            b.RenderTargetWriteMask = ColorWriteMaskFlags.All;
            bsDesc.RenderTargets[0] = b;


            var blendState = BlendState.FromDescription(device, bsDesc);
            var depthState = DepthStencilState.FromDescription(device, new DepthStencilStateDescription
            {
                IsDepthEnabled   = false,
                IsStencilEnabled = false,
            });


            game.GameLoopEvent += delegate
            {
                filledGBuffer.DrawUpdatedGBuffer();

                game.SetBackbuffer();

                if (game.Keyboard.IsKeyPressed(Key.D1))
                {
                    state = 0;
                }
                if (game.Keyboard.IsKeyPressed(Key.D2))
                {
                    state = 1;
                }
                if (game.Keyboard.IsKeyPressed(Key.D3))
                {
                    state = 2;
                }
                if (game.Keyboard.IsKeyPressed(Key.D4))
                {
                    state = 3;
                }

                switch (state)
                {
                case 0:
                    break;

                case 1:
                    directional.LightDirection = game.SpectaterCamera.CameraDirection;
                    break;

                case 2:
                    point.LightPosition = game.SpectaterCamera.CameraPosition;

                    break;

                case 3:
                    spot.LightPosition = game.SpectaterCamera.CameraPosition;
                    spot.SpotDirection = game.SpectaterCamera.CameraDirection;
                    break;
                }



                if (game.Keyboard.IsKeyDown(Key.I))
                {
                    GBufferTest.DrawGBuffer(game, filledGBuffer.GBuffer);
                }
                else
                {
                    context.OutputMerger.DepthStencilState = depthState;
                    directional.Draw();
                    context.OutputMerger.BlendState = blendState;
                    spot.Draw();
                    point.Draw();
                    context.OutputMerger.BlendState        = null;
                    context.OutputMerger.DepthStencilState = null;
                }
            };

            game.Run();
        }
            public void DrawUpdatedDeferredRendering()
            {
                FilledGBuffer.DrawUpdatedGBuffer();


                if (game.Keyboard.IsKeyPressed(Key.D1))
                {
                    state = 0;
                }
                if (game.Keyboard.IsKeyPressed(Key.D2))
                {
                    state = 1;
                }
                if (game.Keyboard.IsKeyPressed(Key.D3))
                {
                    state = 2;
                }
                if (game.Keyboard.IsKeyPressed(Key.D4))
                {
                    state = 3;
                }

                switch (state)
                {
                case 0:
                    break;

                case 1:
                    directional.LightDirection = game.SpectaterCamera.CameraDirection;
                    break;

                case 2:
                    point.LightPosition = game.SpectaterCamera.CameraPosition;

                    break;

                case 3:
                    spot.LightPosition = game.SpectaterCamera.CameraPosition;
                    spot.SpotDirection = game.SpectaterCamera.CameraDirection;
                    break;
                }



                if (game.Keyboard.IsKeyDown(Key.I))
                {
                    context.ClearState();
                    game.SetBackbuffer();
                    GBufferTest.DrawGBuffer(game, FilledGBuffer.GBuffer);
                }
                else
                {
                    combineFinal.ClearLightAccumulation();
                    combineFinal.SetLightAccumulationStates();
                    directional.Draw();
                    spot.Draw();
                    point.Draw();



                    context.ClearState();
                    game.SetBackbuffer(); // This is to set viewport, not sure this is correct

                    context.OutputMerger.SetTargets(hdrImageRTV);
                    DrawCombined();
                }



                if (game.Keyboard.IsKeyPressed(Key.O))
                {
                    Resource.SaveTextureToFile(game.Device.ImmediateContext, hdrImage, ImageFileFormat.Dds,
                                               TWDir.Test.CreateSubdirectory("Deferred") + "\\HdrImage.dds");
                }
            }