Ejemplo n.º 1
0
        /// <summary>
        /// Prepares the light map to be drawn (pre-render)
        /// </summary>
        public void LightMapPrepare()
        {
            // Prepare and set the matrix
            var viewWidth  = this.GraphicsDevice.ScissorRectangle.Width;
            var viewHeight = this.GraphicsDevice.ScissorRectangle.Height;

            // Prepare the matrix with optional settings and assign it to an effect parameter
            Matrix lightMapMatrix = this.LightmapMatrixGet();

            this.mEffect.Parameters["Matrix"].SetValue(lightMapMatrix);

            // Obtain the original rendering states
            var originalRenderTargets = this.GraphicsDevice.GetRenderTargets();

            // Set and clear the target
            this.GraphicsDevice.SetRenderTarget(this.mMap);
            this.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, this.mAmbientColor, 0, 1);

            // Make sure we're culling the right way!
            this.GraphicsDevice.RasterizerState = KryptonEngine.RasterizerStateGetFromCullMode(this.mCullMode);

            // put the render target's size into a more friendly format
            var targetSize = new Vector2(this.mMap.Width, this.mMap.Height);

            // Render Light Maps
            foreach (var light in this.mLights)
            {
                // Loop through each light within the view frustum
                if (light.Bounds.Intersects(this.mBounds))
                {
                    // Clear the stencil and set the scissor rect (because we're stretching geometry past the light's reach)
                    this.GraphicsDevice.Clear(ClearOptions.Stencil, Color.Black, 0, 1);
                    this.GraphicsDevice.ScissorRectangle = KryptonEngine.ScissorRectCreateForLight(light, lightMapMatrix, targetSize);

                    // Draw the light!
                    light.Draw(this.RenderHelper, this.mHulls);
                }
            }

            if (this.mBluriness > 0)
            {
                // Blur the shadow map horizontally to the blur target
                this.GraphicsDevice.SetRenderTarget(this.mMapBlur);
                this.RenderHelper.BlurTextureToTarget(this.mMap, LightMapSize.Full, BlurTechnique.Horizontal, this.mBluriness);

                // Blur the shadow map vertically back to the final map
                this.GraphicsDevice.SetRenderTarget(this.mMap);
                this.RenderHelper.BlurTextureToTarget(this.mMapBlur, LightMapSize.Full, BlurTechnique.Vertical, this.mBluriness);
            }

            // Reset to the original rendering states
            this.GraphicsDevice.SetRenderTargets(originalRenderTargets);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prepares the light map to be drawn (pre-render)
        /// </summary>
        public void LightMapPrepare()
        {
            // Prepare the matrix with optional settings and assign it to an effect parameter
            Matrix lightMapMatrix = LightmapMatrixGet();

            _effect.Parameters["Matrix"].SetValue(lightMapMatrix);

            // Obtain the original rendering states
            var originalRenderTargets = GraphicsDevice.GetRenderTargets();

            // Set and clear the target
            GraphicsDevice.SetRenderTarget(_map);
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, AmbientColor, 0, 1);

            // Make sure we're culling the right way!
            GraphicsDevice.RasterizerState = KryptonEngine.RasterizerStateGetFromCullMode(CullMode);

            // put the render target's size into a more friendly format
            var targetSize = new Vector2(_map.Width, _map.Height);

            // Render Light Maps
            foreach (var light in Lights)
            {
                // Loop through each light within the view frustum
                if (!light.Bounds.Intersects(_bounds))
                {
                    continue;
                }

                // Clear the stencil and set the scissor rect (because we're stretching geometry past the light's reach)
                GraphicsDevice.Clear(
                    options: ClearOptions.Stencil,
                    color: Color.Black,
                    depth: 0,
                    stencil: 0);

                GraphicsDevice.ScissorRectangle =
                    ScissorRectCreateForLight(
                        light: light,
                        matrix: lightMapMatrix,
                        targetSize: targetSize);

                // Draw the light!
                light.Draw(RenderHelper, Hulls);
            }

            if (_bluriness > 0)
            {
                // Blur the shadow map horizontally to the blur target
                GraphicsDevice.SetRenderTarget(_mapBlur);

                RenderHelper.BlurTextureToTarget(
                    _map,
                    LightMapSize.Full,
                    BlurTechnique.Horizontal,
                    _bluriness);

                // Blur the shadow map vertically back to the final map
                GraphicsDevice.SetRenderTarget(_map);

                RenderHelper.BlurTextureToTarget(
                    _mapBlur,
                    LightMapSize.Full,
                    BlurTechnique.Vertical,
                    _bluriness);
            }

            // Reset to the original rendering states
            GraphicsDevice.SetRenderTargets(originalRenderTargets);
        }