Esempio n. 1
0
        public void Initialize()
        {
            gbufferEffect = EffectLoader.Load(@"Graphics/Shaders/GBuffer.fx");

            QuadVert[] verts = new QuadVert[6]
            {
                new QuadVert(-1, -1, 0),
                new QuadVert(-1, 1, 0),
                new QuadVert(1, 1, 0),
                new QuadVert(1, 1, 0),
                new QuadVert(1, -1, 0),
                new QuadVert(-1, -1, 0)
            };

            vb = SharpDX.Toolkit.Graphics.Buffer.New <QuadVert>(context.Device, verts, BufferFlags.VertexBuffer);

            LightQuadVertex[] lightVerts = new LightQuadVertex[6]
            {
                new LightQuadVertex(-1, -1, 0, 0, 1, 3),
                new LightQuadVertex(-1, 1, 0, 0, 0, 2),
                new LightQuadVertex(1, 1, 0, 1, 0, 1),

                new LightQuadVertex(1, 1, 0, 1, 0, 1),
                new LightQuadVertex(1, -1, 0, 1, 1, 0),
                new LightQuadVertex(-1, -1, 0, 0, 1, 3)
            };

            lightVb = SharpDX.Toolkit.Graphics.Buffer.New <LightQuadVertex>(context.Device, lightVerts, BufferFlags.VertexBuffer);
        }
Esempio n. 2
0
        public void PreProcess()
        {
            order = 2;

            GraphicsDevice device = Engine.GraphicsContext.Device;

            device.SetRasterizerState(device.RasterizerStates.CullNone);
            device.SetBlendState(device.BlendStates.Default);
            device.SetDepthStencilState(device.DepthStencilStates.None);

            // compute transmittance texture T (line 1 in algorithm 4.1)
            device.SetRenderTargets(transmittanceT);
            device.SetViewport(new ViewportF(0, 0, TRANSMITTANCE_W, TRANSMITTANCE_H));
            device.DrawQuad(transmittance);

            // compute irradiance texture deltaE (line 2 in algorithm 4.1)
            device.SetRenderTargets(deltaET);
            device.SetViewport(new ViewportF(0, 0, SKY_W, SKY_H));
            irradiance1.Parameters["transTex"].SetResource(transmittanceT);
            device.DrawQuad(irradiance1);

            //// compute single scattering texture deltaS (line 3 in algorithm 4.1)
            //// Rayleigh and Mie separated in deltaSR + deltaSM
            device.SetRenderTargets(deltaSRT, deltaSMT);
            device.SetViewport(new ViewportF(0, 0, RES_MU_S * RES_NU, RES_MU));
            device.SetVertexBuffer(vb);
            device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, vb));
            inscatter1.Parameters["_transTex"].SetResource(transmittanceT);
            inscatter1.Parameters["_transSamp"].SetResource(device.SamplerStates.LinearClamp);
            for (int layer = 0; layer < RES_R; layer++)
            {
                setLayer(inscatter1, layer);
                inscatter1.CurrentTechnique.Passes[0].Apply();
                device.Draw(PrimitiveType.TriangleList, 6);
            }

            // copy deltaE into irradiance texture E (line 4 in algorithm 4.1)
            // really this just clears irradianceT
            device.SetRenderTargets(irradianceT);
            device.SetViewport(new ViewportF(0, 0, SKY_W, SKY_H));
            copyIrradiance.Parameters["k"].SetValue(0f);
            copyIrradiance.Parameters["deltaE"].SetResource(deltaET);
            device.DrawQuad(copyIrradiance);

            for (; order < 3; order++)
            {
                // copy deltaS into inscatter texture S (line 5 in algorithm 4.1)
                device.SetRenderTargets(inscatterT);
                device.SetViewport(new ViewportF(0, 0, RES_MU_S * RES_NU, RES_MU));
                device.SetVertexBuffer(vb);
                device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, vb));
                copyInscatter1.Parameters["deltaSR"].SetResource(deltaSRT);
                copyInscatter1.Parameters["deltaSM"].SetResource(deltaSMT);
                for (int layer = 0; layer < RES_R; layer++)
                {
                    setLayer(copyInscatter1, layer);
                    copyInscatter1.CurrentTechnique.Passes[0].Apply();
                    device.Draw(PrimitiveType.TriangleList, 6);
                }

                // compute deltaJ (line 7 in algorithm 4.1)
                device.SetRenderTargets(deltaJT);
                device.SetViewport(new ViewportF(0, 0, RES_MU_S * RES_NU, RES_MU));
                device.SetVertexBuffer(vb);
                device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, vb));
                inscatterS.Parameters["first"].SetValue(order == 2 ? 1.0f : 0.0f);
                inscatterS.Parameters["trans"].SetResource(transmittanceT);
                inscatterS.Parameters["deltaE"].SetResource(deltaET);
                inscatterS.Parameters["deltaSR"].SetResource(deltaSRT);
                inscatterS.Parameters["deltaSM"].SetResource(deltaSMT);
                for (int layer = 0; layer < RES_R; layer++)
                {
                    setLayer(inscatterS, layer);
                    inscatterS.CurrentTechnique.Passes[0].Apply();
                    device.Draw(PrimitiveType.TriangleList, 6);
                }

                // compute deltaE (line 8 in algorithm 4.1)
                device.SetRenderTargets(deltaET);
                device.SetViewport(new ViewportF(0, 0, SKY_W, SKY_H));
                irradianceN.Parameters["first"].SetValue(order == 2 ? 1.0f : 0.0f);
                irradianceN.Parameters["deltaSR"].SetResource(deltaSRT);
                irradianceN.Parameters["deltaSM"].SetResource(deltaSMT);
                device.DrawQuad(irradianceN);

                // compute deltaS (line 9 in algorithm 4.1)
                device.SetRenderTargets(deltaSRT);
                device.SetViewport(new ViewportF(0, 0, RES_MU_S * RES_NU, RES_MU));
                device.SetVertexBuffer(vb);
                device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, vb));
                inscatterN.Parameters["trans"].SetResource(transmittanceT);
                inscatterN.Parameters["deltaJ"].SetResource(deltaJT);
                for (int layer = 0; layer < RES_R; layer++)
                {
                    setLayer(inscatterN, layer);
                    inscatterN.CurrentTechnique.Passes[0].Apply();
                    device.Draw(PrimitiveType.TriangleList, 6);
                }

                device.SetBlendState(BlendState.New(device,
                                                    SharpDX.Direct3D11.BlendOption.One,
                                                    SharpDX.Direct3D11.BlendOption.One,
                                                    SharpDX.Direct3D11.BlendOperation.Add,
                                                    SharpDX.Direct3D11.BlendOption.One,
                                                    SharpDX.Direct3D11.BlendOption.One,
                                                    SharpDX.Direct3D11.BlendOperation.Add));

                // add deltaE into irradiance texture E (line 10 in algorithm 4.1)
                device.SetRenderTargets(irradianceT);
                device.SetViewport(new ViewportF(0, 0, SKY_W, SKY_H));
                copyIrradiance.Parameters["k"].SetValue(1.0f);
                copyIrradiance.Parameters["deltaE"].SetResource(deltaET);
                device.DrawQuad(copyIrradiance);

                // add deltaS into inscatter texture S (line 11 in algorithm 4.1)
                device.SetRenderTargets(inscatterT);
                device.SetViewport(new ViewportF(0, 0, RES_MU_S * RES_NU, RES_MU));
                device.SetVertexBuffer(vb);
                device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, vb));
                copyInscatterN.Parameters["deltaS"].SetResource(deltaSRT);
                for (int layer = 0; layer < RES_R; layer++)
                {
                    setLayer(copyInscatterN, layer);
                    copyInscatterN.CurrentTechnique.Passes[0].Apply();
                    device.Draw(PrimitiveType.TriangleList, 6);
                }

                device.SetBlendState(device.BlendStates.Opaque);
            } // end multiple scattering loop

            LightQuadVertex[] lightVerts = new LightQuadVertex[6]
            {
                new LightQuadVertex(-1, -1, 0, 0, 1, 3),
                new LightQuadVertex(-1, 1, 0, 0, 0, 2),
                new LightQuadVertex(1, 1, 0, 1, 0, 1),

                new LightQuadVertex(1, 1, 0, 1, 0, 1),
                new LightQuadVertex(1, -1, 0, 1, 1, 0),
                new LightQuadVertex(-1, -1, 0, 0, 1, 3)
            };

            atmosVb = SharpDX.Toolkit.Graphics.Buffer.New <LightQuadVertex>(device, lightVerts, BufferFlags.VertexBuffer);
        }