/// <summary> /// Initializes a new instance of the <see cref="ProjectedShadowRenderer"/> class. /// </summary> /// <param name="graphicsService">The graphics service.</param> public ProjectedShadowRenderer(IGraphicsService graphicsService) { if (graphicsService == null) { throw new ArgumentNullException("graphicsService"); } _effect = new BasicEffect(graphicsService.GraphicsDevice) { DiffuseColor = Vector3.Zero, LightingEnabled = false, TextureEnabled = false }; ShadowedPlane = new Plane(new Vector3(0, 1, 0), 0.01f); LightPosition = new Vector4(1, 1, 1, 0); }
/// <summary> /// Creates the projection matrix for planar projected shadows. /// </summary> /// <param name="plane">The plane which receives the shadows.</param> /// <param name="lightPosition"> /// The light position in homogeneous coordinates. /// Set W to 1, to create a local light. Set W to 0, to create a directional light. /// </param> /// <returns>The shadow projection matrix.</returns> public static Matrix CreateShadowMatrix(Plane plane, Vector4 lightPosition) { var planeEquation = new Vector4(plane.Normal, -plane.DistanceFromOrigin); float a = planeEquation.X; float b = planeEquation.Y; float c = planeEquation.Z; float d = planeEquation.W; float lx = lightPosition.X; float ly = lightPosition.Y; float lz = lightPosition.Z; float lw = lightPosition.W; // Note: XNA matrix layout, not DigitalRune layout. Matrix shadowMatrix = new Matrix( b * ly + c * lz + d * lw, -a * ly, -a * lz, -a * lw, -b * lx, a * lx + c * lz + d * lw, -b * lz, -b * lw, -c * lx, -c * ly, a * lx + b * ly + d * lw, -c * lw, -d * lx, -d * ly, -d * lz, a * lx + b * ly + c * lz); return(shadowMatrix); }
/// <summary> /// Creates the projection matrix for planar projected shadows. /// </summary> /// <param name="plane">The plane which receives the shadows.</param> /// <param name="lightPosition"> /// The light position in homogeneous coordinates. /// Set W to 1, to create a local light. Set W to 0, to create a directional light. /// </param> /// <returns>The shadow projection matrix.</returns> public static Matrix CreateShadowMatrix(Plane plane, Vector4F lightPosition) { var planeEquation = new Vector4F(plane.Normal, -plane.DistanceFromOrigin); float a = planeEquation.X; float b = planeEquation.Y; float c = planeEquation.Z; float d = planeEquation.W; float lx = lightPosition.X; float ly = lightPosition.Y; float lz = lightPosition.Z; float lw = lightPosition.W; // Note: XNA matrix layout, not DigitalRune layout. Matrix shadowMatrix = new Matrix( b * ly + c * lz + d * lw, -a * ly, -a * lz, -a * lw, -b * lx, a * lx + c * lz + d * lw, -b * lz, -b * lw, -c * lx, -c * ly, a * lx + b * ly + d * lw, -c * lw, -d * lx, -d * ly, -d * lz, a * lx + b * ly + c * lz); return shadowMatrix; }
//-------------------------------------------------------------- #region Creation & Cleanup //-------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="ProjectedShadowRenderer"/> class. /// </summary> /// <param name="graphicsService">The graphics service.</param> public ProjectedShadowRenderer(IGraphicsService graphicsService) { if (graphicsService == null) throw new ArgumentNullException("graphicsService"); _effect = new BasicEffect(graphicsService.GraphicsDevice) { DiffuseColor = Vector3.Zero, LightingEnabled = false, TextureEnabled = false }; ShadowedPlane = new Plane(new Vector3F(0, 1, 0), 0.01f); LightPosition = new Vector4F(1, 1, 1, 0); }