Exemple #1
0
        /// <summary>
        /// Parses a <see cref="PointLight" /> from the given XML node.
        /// </summary>
        /// <param name="node">The XML node.</param>
        /// <returns>A <see cref="SceneNode" /> instance.</returns>
        public SceneNode ParsePointLight(XElement node)
        {
            var pointLight = new PointLight(
                this.ParseVector3(node.Element("position")),
                Quaternion.Identity,
                Vector3.One,
                float.Parse(node.Attribute("radius").Value),
                this.ParseColor(node.Element("color")),
                float.Parse(node.Attribute("intensity").Value),
                bool.Parse(node.Attribute("cast-shadows").Value),
                int.Parse(node.Attribute("resolution").Value),
                float.Parse(node.Attribute("bias").Value));

            var sceneNode = new SceneNode();
            sceneNode.AttachEntity(pointLight);
            return sceneNode;
        }
        private void RenderShadowMap(PointLight pointLight)
        {
            Matrix[] views = new Matrix[6];

            views [0] = Matrix.CreateLookAt(pointLight.Position, pointLight.Position + Vector3.Forward, Vector3.Up);
            views [1] = Matrix.CreateLookAt(pointLight.Position, pointLight.Position + Vector3.Backward, Vector3.Up);
            views [2] = Matrix.CreateLookAt(pointLight.Position, pointLight.Position + Vector3.Left, Vector3.Up);
            views [3] = Matrix.CreateLookAt(pointLight.Position, pointLight.Position + Vector3.Right, Vector3.Up);
            views [4] = Matrix.CreateLookAt(pointLight.Position, pointLight.Position + Vector3.Down, Vector3.Forward);
            views [5] = Matrix.CreateLookAt(pointLight.Position, pointLight.Position + Vector3.Up, Vector3.Backward);

            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90.0f), 1.0f, 0.1f, pointLight.Radius);

            this.DepthWriterEffect.Parameters ["Projection"].SetValue(projection);
            this.DepthWriterEffect.Parameters ["LightPosition"].SetValue(pointLight.Position);
            this.DepthWriterEffect.Parameters ["DepthPrecision"].SetValue(pointLight.Radius);

            // Forward
            this.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapForward);
            this.GraphicsDevice.Clear(Color.Transparent);
            this.DepthWriterEffect.Parameters ["View"].SetValue(views [0]);
            this.RenderSceneForShadows();

            // Backward
            this.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapBackward);
            this.GraphicsDevice.Clear(Color.Transparent);
            this.DepthWriterEffect.Parameters ["View"].SetValue(views [1]);
            this.RenderSceneForShadows();

            // Left
            this.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapLeft);
            this.GraphicsDevice.Clear(Color.Transparent);
            this.DepthWriterEffect.Parameters ["View"].SetValue(views [2]);
            this.RenderSceneForShadows();

            // Right
            this.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapRight);
            this.GraphicsDevice.Clear(Color.Transparent);
            this.DepthWriterEffect.Parameters ["View"].SetValue(views [3]);
            this.RenderSceneForShadows();

            // Down
            this.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapDown);
            this.GraphicsDevice.Clear(Color.Transparent);
            this.DepthWriterEffect.Parameters ["View"].SetValue(views [4]);
            this.RenderSceneForShadows();

            // Up
            this.GraphicsDevice.SetRenderTarget(pointLight.ShadowMapUp);
            this.GraphicsDevice.Clear(Color.Transparent);
            this.DepthWriterEffect.Parameters ["View"].SetValue(views [5]);
            this.RenderSceneForShadows();
        }