Beispiel #1
0
        protected void RenderShadowMap(ShadowBatch batch, bool flipNormal, RenderTarget2D shadowTarget, ref Matrix proj, ref Matrix view)
        {
            GraphicsDevice.SetRenderTarget(shadowTarget);
            GraphicsDevice.Clear(Color.White);
            GraphicsDevice.Clear(Color.Black);

            _shadowEffect.Projection = proj;
            _shadowEffect.View       = view;
            _shadowEffect.CurrentTechnique.Passes[0].Apply(); // CurrentTechnique is "LightDrawing"

            if (batch.VerticesCount >= 3)
            {
                int primitiveCount = batch.VerticesCount / 3;
                // submit the draw call to the graphics card
                GraphicsDevice.BlendState        = BlendState.Opaque;
                GraphicsDevice.SamplerStates[0]  = SamplerState.LinearClamp;
                GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                GraphicsDevice.RasterizerState   = (flipNormal) ? RasterizerState.CullCounterClockwise : RasterizerState.CullClockwise;
                GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, batch.Vertices, 0, primitiveCount);
            }

            return;
        }
Beispiel #2
0
        public void RenderShadowMap(ShadowBatch batch, bool flipNormal = false, float nearPlane = 0.001f)
        {
            var pos = new Vector3(body.Position, 0.0f);

            _shadowEffect.FogStart = 0.0f;
            _shadowEffect.FogEnd   = LightRadius;

            var proj  = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 2f, 1f, nearPlane, LightRadius);
            var viewU = Matrix.CreateLookAt(pos, pos + Vector3.Up, Vector3.Backward);
            var viewR = Matrix.CreateLookAt(pos, pos + Vector3.Right, Vector3.Backward);
            var viewD = Matrix.CreateLookAt(pos, pos + Vector3.Down, Vector3.Backward);
            var viewL = Matrix.CreateLookAt(pos, pos + Vector3.Left, Vector3.Backward);

#if XNA
            proj = proj * Matrix.CreateTranslation(0f, 1.0f, 0f); // one pixel up
#endif

            RenderShadowMap(batch, flipNormal, ShadowMapU, ref proj, ref viewU);
            RenderShadowMap(batch, flipNormal, ShadowMapR, ref proj, ref viewR);
            RenderShadowMap(batch, flipNormal, ShadowMapD, ref proj, ref viewD);
            RenderShadowMap(batch, flipNormal, ShadowMapL, ref proj, ref viewL);

            GraphicsDevice.SetRenderTarget(null);
        }