Ejemplo n.º 1
0
        /// <summary>
        /// Method to do the deferred lighting draw call
        /// </summary>
        /// <param name="camera"></param>
        public void DrawDeferred(DeferredLightingCamera camera)
        {
            GraphicsDevice.Clear(camera.ClearColor);

            if (deferredSceneRenderEffect == null)
            {
                deferredSceneRenderEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredSceneRender");
            }

            deferredSceneRenderEffect.Parameters["colorMap"].SetValue(camera.RenderTarget);
            deferredSceneRenderEffect.Parameters["lightMap"].SetValue(lightMap);
            //deferredSceneRenderEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap);

            GraphicsDevice.BlendState        = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            GraphicsDevice.SamplerStates[0]  = SamplerState.LinearWrap;

            deferredSceneRenderEffect.CurrentTechnique.Passes[0].Apply();

            if (sceneQuad == null)
            {
                sceneQuad = new ScreenQuad(Game);
                sceneQuad.Initialize();
            }

            sceneQuad.Draw(-Vector2.One, Vector2.One);
        }
Ejemplo n.º 2
0
        public void DeferredLighting(GameTime gameTime, DeferredLightingCamera camera)
        {
            if (lightMap == null)
            {
                lightMap = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            }

            GraphicsDevice.SetRenderTarget(lightMap);

            GraphicsDevice.Clear(Color.Black);
            GraphicsDevice.BlendState = BlendState.Additive;

            //use the same operation on the alpha channel
            GraphicsDevice.DepthStencilState = DepthStencilState.None;
            GraphicsDevice.SamplerStates[0]  = SamplerState.PointWrap;

            // Apply the glow value to the light map.
            ApplyGlowMap(camera);

            int lc = 0;

            lc = DirectionalLights.Count;
            for (int l = 0; l < lc; l++)
            {
                if (DirectionalLights[l].Intensity > 0 && DirectionalLights[l].Color != Color.Black)
                {
                    RenderDirectionalLight(camera, DirectionalLights[l]);
                }
            }

            lc = ConeLights.Count;
            for (int l = 0; l < lc; l++)
            {
                if (ConeLights[l].Intensity > 0 && ConeLights[l].Color != Color.Black)
                {
                    RenderConeLight(camera, ConeLights[l]);
                }
            }

            lc = PointLights.Count;
            for (int l = 0; l < lc; l++)
            {
                if (PointLights[l].Intensity > 0 && PointLights[l].Color != Color.Black)
                {
                    RenderPointLight(camera, PointLights[l]);
                }
            }

            GraphicsDevice.SetRenderTarget(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Render a cone light
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="coneLight"></param>
        public void RenderConeLight(DeferredLightingCamera camera, DeferredConeLight coneLight)
        {
            if (deferredConeLightEffect == null)
            {
                deferredConeLightEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredConeLight");
            }

            // Load Light parameters
            deferredConeLightEffect.Parameters["lightDirection"].SetValue(coneLight.Direction);
            deferredConeLightEffect.Parameters["LightPosition"].SetValue(coneLight.Transform.Position);
            deferredConeLightEffect.Parameters["Color"].SetValue(coneLight.Color.ToVector3());

            deferredConeLightEffect.Parameters["ViewProjectionInv"].SetValue(Matrix.Invert(camera.View * camera.Projection));
            deferredConeLightEffect.Parameters["LightViewProjection"].SetValue(coneLight.View * coneLight.Projection);

            deferredConeLightEffect.Parameters["CameraPosition"].SetValue(camera.Transform.Position);
            deferredConeLightEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap);
            deferredConeLightEffect.Parameters["normalMap"].SetValue(camera.NormalBuffer);
            deferredConeLightEffect.Parameters["depthMap"].SetValue(camera.DepthBuffer);
            deferredConeLightEffect.Parameters["power"].SetValue(coneLight.Intensity);

            deferredConeLightEffect.Parameters["ConeAngle"].SetValue(coneLight.Angle);
            deferredConeLightEffect.Parameters["ConeDecay"].SetValue(coneLight.Decay);

            deferredConeLightEffect.Parameters["CastShadow"].SetValue(coneLight.CastShadow);
            if (coneLight.CastShadow)
            {
                deferredConeLightEffect.Parameters["shadowMap"].SetValue(coneLight.ShadowMap);
                deferredConeLightEffect.Parameters["mod"].SetValue(deferredConeShadowMapMod);
                deferredConeLightEffect.Parameters["DiscRadius"].SetValue(1.5f);
                deferredConeLightEffect.Parameters["hardShadows"].SetValue(coneLight.HardShadows);

                deferredConeLightEffect.Parameters["Taps"].SetValue(taps);
            }

            deferredConeLightEffect.CurrentTechnique.Passes[0].Apply();
            // Set sampler state to Point as the Surface type requires it in XNA 4.0
            GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;

            if (sceneQuad == null)
            {
                sceneQuad = new ScreenQuad(Game);
                sceneQuad.Initialize();
            }

            sceneQuad.Draw(-Vector2.One, Vector2.One);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Apply the glow map
        /// </summary>
        /// <param name="camera"></param>
        public void ApplyGlowMap(DeferredLightingCamera camera)
        {
            if (glowMapToLightMapEffect == null)
            {
                glowMapToLightMapEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/GlowMapToLightMapShader");
            }

            glowMapToLightMapEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap);

            glowMapToLightMapEffect.Techniques[0].Passes[0].Apply();

            if (sceneQuad == null)
            {
                sceneQuad = new ScreenQuad(Game);
                sceneQuad.Initialize();
            }

            sceneQuad.Draw(-Vector2.One, Vector2.One);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Render a directional light.
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="directionalLight"></param>
        public void RenderDirectionalLight(DeferredLightingCamera camera, DeferredDirectionalLight directionalLight)
        {
            if (deferredDirectionalLightEffect == null)
            {
                deferredDirectionalLightEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredDirectionalLight");
            }

            // Call lighting methods.
            deferredDirectionalLightEffect.Parameters["lightDirection"].SetValue(directionalLight.Direction);
            deferredDirectionalLightEffect.Parameters["Color"].SetValue(directionalLight.Color.ToVector3());
            deferredDirectionalLightEffect.Parameters["power"].SetValue(directionalLight.Intensity);

            deferredDirectionalLightEffect.Parameters["CameraPosition"].SetValue(camera.Transform.Position);
            deferredDirectionalLightEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap);
            deferredDirectionalLightEffect.Parameters["normalMap"].SetValue(camera.NormalBuffer);
            deferredDirectionalLightEffect.Parameters["depthMap"].SetValue(camera.DepthBuffer);

            deferredDirectionalLightEffect.Parameters["CastShadow"].SetValue(directionalLight.CastShadow);
            if (directionalLight.CastShadow)
            {
                deferredDirectionalLightEffect.Parameters["shadowMap"].SetValue(directionalLight.ShadowMap);
                deferredDirectionalLightEffect.Parameters["mod"].SetValue(deferredDirectionalShadowMapMod);
                deferredDirectionalLightEffect.Parameters["hardShadows"].SetValue(directionalLight.HardShadows);

                deferredDirectionalLightEffect.Parameters["DiscRadius"].SetValue(1f);

                deferredDirectionalLightEffect.Parameters["Taps"].SetValue(taps);
            }
            deferredDirectionalLightEffect.Parameters["viewProjectionInv"].SetValue(Matrix.Invert(camera.View * camera.Projection));
            deferredDirectionalLightEffect.Parameters["lightViewProjection"].SetValue(directionalLight.View * directionalLight.Projection);



            deferredDirectionalLightEffect.Techniques[0].Passes[0].Apply();

            if (sceneQuad == null)
            {
                sceneQuad = new ScreenQuad(Game);
                sceneQuad.Initialize();
            }

            sceneQuad.Draw(-Vector2.One, Vector2.One);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Render a point light
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="pointLight"></param>
        public void RenderPointLight(DeferredLightingCamera camera, DeferredPointLight pointLight)
        {
            if (deferredPointLightEffect == null)
            {
                deferredPointLightEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredPointLight");
            }

            if (pointLightVolumeModel == null)
            {
                pointLightVolumeModel = Game.Content.Load <Model>("Models/PointLightVolume");
            }

            //set the G-Buffer parameters
            deferredPointLightEffect.Parameters["normalMap"].SetValue(camera.NormalBuffer);
            deferredPointLightEffect.Parameters["depthMap"].SetValue(camera.DepthBuffer);
            deferredPointLightEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap);

            //compute the light world matrix
            //scale according to light radius, and translate it to light position
            Matrix sphereWorldMatrix = Matrix.CreateScale(pointLight.Radius) * Matrix.CreateTranslation(pointLight.Transform.Position);

            //light position
            deferredPointLightEffect.Parameters["lightPosition"].SetValue(pointLight.Transform.Position);

            //set the color, radius and Intensity
            deferredPointLightEffect.Parameters["Color"].SetValue(pointLight.Color.ToVector3());
            deferredPointLightEffect.Parameters["lightRadius"].SetValue(pointLight.Radius);
            deferredPointLightEffect.Parameters["lightIntensity"].SetValue(pointLight.Intensity);

            //parameters for specular computations
            deferredPointLightEffect.Parameters["cameraPosition"].SetValue(camera.Transform.Position);
            deferredPointLightEffect.Parameters["InvertViewProjection"].SetValue(Matrix.Invert(camera.View * camera.Projection));

            Matrix[] transforms = new Matrix[pointLightVolumeModel.Bones.Count];
            pointLightVolumeModel.CopyAbsoluteBoneTransformsTo(transforms);
            Matrix meshWorld;

            int mmpc;

            for (int m = 0; m < pointLightVolumeModel.Meshes.Count; m++)
            {
                if (pointLightVolumeModel.Meshes[m].ParentBone != null)
                {
                    meshWorld = transforms[pointLightVolumeModel.Meshes[m].ParentBone.Index] * sphereWorldMatrix;
                }
                else
                {
                    meshWorld = transforms[0] * sphereWorldMatrix;
                }

                mmpc = pointLightVolumeModel.Meshes[m].MeshParts.Count;
                for (int mp = 0; mp < mmpc; mp++)
                {
                    deferredPointLightEffect.Parameters["wvp"].SetValue(meshWorld * camera.View * camera.Projection);

                    int pCnt = deferredPointLightEffect.CurrentTechnique.Passes.Count;
                    for (int p = 0; p < pCnt; p++)
                    {
                        deferredPointLightEffect.CurrentTechnique.Passes[p].Apply();

                        GraphicsDevice.SetVertexBuffer(pointLightVolumeModel.Meshes[m].MeshParts[mp].VertexBuffer);
                        GraphicsDevice.Indices = pointLightVolumeModel.Meshes[m].MeshParts[mp].IndexBuffer;
                        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, pointLightVolumeModel.Meshes[m].MeshParts[mp].VertexOffset, 0, pointLightVolumeModel.Meshes[m].MeshParts[mp].PrimitiveCount);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.GraphicsProfile = GraphicsProfile.HiDef;

            graphics.PreferredBackBufferWidth  = 1920;
            graphics.PreferredBackBufferHeight = 1080;

            IsMouseVisible = true;

            kbm = new KeyboardStateManager(this);

            lightingManager = new DeferredLightingManager(this);

            camera            = new DeferredLightingCamera(this, .5f, 20000);
            camera.Position   = new Vector3(0, 1, 0);
            camera.ClearColor = Color.Black;
            Components.Add(camera);
            Services.AddService(typeof(Base3DCamera), camera);

            BaseSkyBox skyBox = new BaseSkyBox(this, "Textures/SkyBox/HMcubemap");

            skyBox.effectAsset = "Shaders/DeferredRender/SkyBoxShader";
            Components.Add(skyBox);

            Plane plane = new Plane(this, "Shaders/DeferredRender/DeferredModelRender");

            plane.ColorAsset     = "Textures/cobbles";
            plane.BumpAsset      = "Textures/cobblesNormal";
            plane.OcclusionAsset = "Textures/cobbleOcclusion";
            plane.SpecularAsset  = "Textures/cobblesSpecular";
            plane.Scale          = Vector3.One * 2;
            plane.UVMultiplier   = Vector2.One * 5;
            plane.Position       = new Vector3(0, -1, -20);
            Components.Add(plane);

            Base3DObject bunny = new Base3DObject(this, "Models/bunny", "Shaders/DeferredRender/DeferredModelRender");

            bunny.Position = new Vector3(0, -1f, -20);
            Components.Add(bunny);

            Cube cube = new Cube(this, "Shaders/DeferredRender/DeferredModelRender");

            cube.ColorAsset = "Textures/xnauguk";
            cube.Position   = new Vector3(-3, -.5f, -20);
            Components.Add(cube);

            Sphere sphere = new Sphere(this, "Shaders/DeferredRender/DeferredModelRender");

            sphere.Scale          = Vector3.One * 2;
            sphere.Position       = new Vector3(3, -0, -20);
            sphere.ColorAsset     = "Textures/brick";
            sphere.BumpAsset      = "Textures/brickNormal";
            sphere.OcclusionAsset = "Textures/brickOcclusion";
            sphere.SpecularAsset  = "Textures/brickSpecular";
            sphere.UVMultiplier   = Vector2.One * 2;
            Components.Add(sphere);

            Cube sandBlock = new Cube(this, "Shaders/DeferredRender/DeferredModelRender");

            sandBlock.ColorAsset = "Textures/sand";
            sandBlock.BumpAsset  = "Textures/sandNormal";
            sandBlock.Position   = new Vector3(-3, 1.5f, -20);
            sandBlock.Rotate(Vector3.Up + Vector3.Forward + Vector3.Left, 15);

            Components.Add(sandBlock);

            DeferredDirectionalLight directionalLight = new DeferredDirectionalLight(this, new Vector3(10, 10, 10), Color.AliceBlue, 1, true);

            directionalLight.Transform.LookAt(Vector3.Zero, 1, Vector3.Forward);
            Components.Add(directionalLight);
            lightingManager.AddLight(directionalLight);


            DeferredPointLight pointLight = new DeferredPointLight(this, new Vector3(0, 1, -20), Color.Red, 10, 1, false);

            Components.Add(pointLight);
            lightingManager.AddLight(pointLight);

            coneLight = new DeferredConeLight(this, new Vector3(-10, 5, -25), Color.Gold, 1, MathHelper.ToRadians(45), 5, true);
            coneLight.Transform.LookAt(new Vector3(0, 0, -20), 1, Vector3.Forward);
            Components.Add(coneLight);
            lightingManager.AddLight(coneLight);

            DeferredConeLight coneLight2 = new DeferredConeLight(this, new Vector3(0, 20, -20), Color.Green, 1, MathHelper.ToRadians(25), 50, true);

            coneLight2.Transform.LookAt(new Vector3(0, 0, -20), 1, Vector3.Forward);
            Components.Add(coneLight2);
            lightingManager.AddLight(coneLight2);

            // Shadow casters and receivers..
            lightingManager.AddShadowCaster(bunny);
            lightingManager.AddShadowCaster(cube);
            lightingManager.AddShadowCaster(sphere);
            lightingManager.AddShadowCaster(sandBlock);
        }