Ejemplo n.º 1
0
 public MovingLight(Light light, Vector3 startPoint, Vector3 endPoint, float speed)
 {
     _light = light;
     _startPoint = startPoint;
     _endPoint = endPoint;
     _speed = speed;
     _currentTime = 0;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Add directional light. Only one call. Use this method if you want to draw with shadow mapping.
        /// </summary>
        /// <param name="DirectionalYaw">Yaw angle of light</param>
        /// <param name="DirectionalPitch">Pitch angle of light</param>
        /// <param name="DirectionalRoll">Roll angle of light</param>
        /// <param name="LightColor">The ambinet color light</param> 
        /// <param name="ShadowDistance">Maximum distance of shadow to be rendered</param>
        /// <param name="ShadowDepthBias">Set the depth bias</param>      
        public void AddDirectionalLight(float DirectionalYaw, float DirectionalPitch, float DirectionalRoll, Color LightColor, float Intensity, float ShadowDistance, float ShadowDepthBias)
        {
            CheckDirectionalLight();

            Light dirLight = new Light();
            dirLight.LightType = Light.Type.Directional;
            dirLight.Transform = Matrix.CreateFromYawPitchRoll(DirectionalYaw, DirectionalPitch, DirectionalRoll);
            dirLight.Color = LightColor;
            dirLight.Intensity = Intensity;
            dirLight.ShadowDistance = ShadowDistance;
            dirLight.ShadowDepthBias = ShadowDepthBias;
            dirLight.CastShadows = true;
            lights.Add(dirLight);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add directional light. Only one call. Use this method if you want to draw without shadow mapping.
        /// </summary>
        /// <param name="DirectionalYaw">Yaw angle of light</param>
        /// <param name="DirectionalPitch">Pitch angle of light</param>
        /// <param name="DirectionalRoll">Roll angle of light</param>
        /// <param name="LightColor">The ambinet color light</param>
        /// <param name="Intensity">The intensity of light</param>
        /// <param name="CastShadows">Set false if you don't want shadow mapping. Othewise set (float)ShadowDistance and (float)ShadowDepthBias</param> 
        public void AddDirectionalLight(float DirectionalYaw, float DirectionalPitch, float DirectionalRoll, Color LightColor, float Intensity, bool CastShadows)
        {
            CheckDirectionalLight();

            Light dirLight = new Light();
            dirLight.LightType = Light.Type.Directional;
            dirLight.Transform = Matrix.CreateFromYawPitchRoll(DirectionalYaw, DirectionalPitch, DirectionalRoll);
            dirLight.Color = LightColor;
            dirLight.Intensity = Intensity;
            dirLight.CastShadows = CastShadows;
            if (CastShadows)
                throw new InvalidOperationException("Use AddDirectionalLight(float DirectionalYaw, float DirectionalPitch, float DirectionalRoll, Color LightColor, float Intensity, float ShadowDistance, float ShadowDepthBias) method for shadow mapping!");

            lights.Add(dirLight);
        }
Ejemplo n.º 4
0
        public void GenerateShadowTextureDirectionalLight(Renderer renderer, object[] meshes, object[] InstancedMeshes, Light light, CascadeShadowMapEntry cascadeShadowMap, Camera.Camera camera)
        {
            //bind the render target
            renderer.GraphicsDevice.SetRenderTarget(cascadeShadowMap.Texture);
            //clear it to white, ie, far far away
            renderer.GraphicsDevice.Clear(Color.White);
            renderer.GraphicsDevice.BlendState = BlendState.Opaque;
            renderer.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            // Get the corners of the frustum
            camera.Frustum.GetCorners(frustumCornersWS);
            Matrix eyeTransform = camera.View;
            Vector3.Transform(frustumCornersWS, ref eyeTransform, frustumCornersVS);

            float near = camera.NearPlane, far = MathHelper.Min(camera.FarPlane, light.ShadowDistance);

            splitDepthsTmp[0] = near;
            splitDepthsTmp[NUM_CSM_SPLITS] = far;

            //compute each distance the way you like...
            for (int i = 1; i < splitDepthsTmp.Length - 1; i++)
                splitDepthsTmp[i] = near + (far - near) * (float)Math.Pow((i / (float)NUM_CSM_SPLITS), 3);

            Viewport splitViewport = new Viewport();
            Vector3 lightDir = -Vector3.Normalize(light.Transform.Forward);

            BoundingFrustum frustum = new BoundingFrustum(Matrix.Identity);

            for (int i = 0; i < NUM_CSM_SPLITS; i++)
            {
                cascadeShadowMap.LightClipPlanes[i].X = -splitDepthsTmp[i];
                cascadeShadowMap.LightClipPlanes[i].Y = -splitDepthsTmp[i + 1];

                cascadeShadowMap.LightViewProjectionMatrices[i] = CreateLightViewProjectionMatrix(lightDir, far, camera, splitDepthsTmp[i], splitDepthsTmp[i + 1], i);
                Matrix viewProj = cascadeShadowMap.LightViewProjectionMatrices[i];

                // Set the viewport for the current split
                splitViewport.MinDepth = 0;
                splitViewport.MaxDepth = 1;
                splitViewport.Width = CASCADE_SHADOW_RESOLUTION;
                splitViewport.Height = CASCADE_SHADOW_RESOLUTION;
                splitViewport.X = i * CASCADE_SHADOW_RESOLUTION;
                splitViewport.Y = 0;
                renderer.GraphicsDevice.Viewport = splitViewport;

                frustum.Matrix = viewProj;

                foreach (object mesh in meshes)
                {
                    if (mesh is List<Models>)
                        for (int index = 0; index < ((List<Models>)mesh).Count; index++)
                        {
                            Models m = ((List<Models>)mesh)[index];
                            //cull meshes outside the light volume
                         //   if (!frustum.Intersects(m.BoundingSphere))
                         //       continue;

                            //render it
                            m.RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                        }
                    if (mesh is Models)
                    {
                        //cull meshes outside the light volume
                       // if (!frustum.Intersects(((Models)mesh).BoundingSphere))
                        //    continue;

                        //render it
                        ((Models)mesh).RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                    }
                    if (mesh is CarPlayer)
                    {
                        ((CarPlayer)mesh).RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                    }
                    if (mesh is Terrain.Terrain)
                    {
                        for (int index = 0; index < ((Terrain.Terrain)mesh).QuadTrees.Count; index++)
                        {
                            //cull meshes outside the light volume
                          //  if (!frustum.Intersects(((Terrain.Terrain)mesh).QuadTrees[index].BoundingSphere))
                          //      continue;

                            //render it
                            ((Terrain.Terrain)mesh).QuadTrees[index].RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                        }

                    }
                }
                foreach (object mesh in InstancedMeshes)
                {
                    if (mesh is Billboards.Billboard)
                    {
                        ((Billboards.Billboard)mesh).TreePreDraw();

                        for (int lod = 0; lod < ((Billboards.Billboard)mesh).LOD; lod++)
                            if (((Billboards.Billboard)mesh).instanceTransforms[lod].Length != 0)
                                ((Billboards.Billboard)mesh).trunck[lod][0].RenderShadowMap(ref viewProj, renderer.GraphicsDevice);

                        if (((Billboards.Billboard)mesh).Leaves)
                            for (int tree = 0; tree < ((Billboards.Billboard)mesh).NoTrees; tree++)
                            {
                                if (((Billboards.Billboard)mesh).LeavesAreVisible[tree])
                                    for (int j = 0; j < ((Billboards.Billboard)mesh).NoLeaves; j++)
                                    {
                                        ((Billboards.Billboard)mesh).leaves[tree][j].UpdateTransformationMatrix(((Billboards.Billboard)mesh).instanceTransforms1[tree]);
                                        if (j == 0)
                                            ((Billboards.Billboard)mesh).leaves[tree][j].RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                                    }
                            }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void GenerateShadowTextureSpotLight(Renderer renderer, object[] meshes, object[] InstancedMeshes, Light light, SpotShadowMapEntry shadowMap)
        {
            //bind the render target
            renderer.GraphicsDevice.SetRenderTarget(shadowMap.Texture);
            //clear it to white, ie, far far away
            renderer.GraphicsDevice.Clear(Color.White);
            renderer.GraphicsDevice.BlendState = BlendState.Opaque;
            renderer.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            Matrix viewProj = light.ViewProjection;
            shadowMap.LightViewProjection = viewProj;

            BoundingFrustum frustum = light.Frustum;

            foreach (object mesh in meshes)
            {
                if (mesh is List<Models>)
                    for (int index = 0; index < ((List<Models>)mesh).Count; index++)
                    {
                        Models m = ((List<Models>)mesh)[index];
                        //cull meshes outside the light volume
                        //   if (!frustum.Intersects(m.BoundingSphere))
                        //       continue;

                        //render it
                        m.RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                    }
                if (mesh is Models)
                {
                    //cull meshes outside the light volume
                    // if (!frustum.Intersects(((Models)mesh).BoundingSphere))
                    //    continue;

                    //render it
                    ((Models)mesh).RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                }
                if (mesh is CarPlayer)
                {
                    ((CarPlayer)mesh).RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                }
                if (mesh is Terrain.Terrain)
                {
                    for (int index = 0; index < ((Terrain.Terrain)mesh).QuadTrees.Count; index++)
                    {
                        //cull meshes outside the light volume
                        //  if (!frustum.Intersects(((Terrain.Terrain)mesh).QuadTrees[index].BoundingSphere))
                        //      continue;

                        //render it
                        ((Terrain.Terrain)mesh).QuadTrees[index].RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                    }

                }
            }
            foreach (object mesh in InstancedMeshes)
            {
                if (mesh is Billboards.Billboard)
                {
                    ((Billboards.Billboard)mesh).TreePreDraw();

                    for (int lod = 0; lod < ((Billboards.Billboard)mesh).LOD; lod++)
                        if (((Billboards.Billboard)mesh).instanceTransforms[lod].Length != 0)
                            ((Billboards.Billboard)mesh).trunck[lod][0].RenderShadowMap(ref viewProj, renderer.GraphicsDevice);

                    if (((Billboards.Billboard)mesh).Leaves)
                        for (int tree = 0; tree < ((Billboards.Billboard)mesh).NoTrees; tree++)
                        {
                            if (((Billboards.Billboard)mesh).LeavesAreVisible[tree])
                                for (int j = 0; j < ((Billboards.Billboard)mesh).NoLeaves; j++)
                                {
                                    ((Billboards.Billboard)mesh).leaves[tree][j].UpdateTransformationMatrix(((Billboards.Billboard)mesh).instanceTransforms1[tree]);
                                    if (j == 0)
                                        ((Billboards.Billboard)mesh).leaves[tree][j].RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                                }
                        }
                }
            }
        }
Ejemplo n.º 6
0
 public void AddMovingPointLight(float Radius, float Intensity, Color LightColor, Vector3 startPoint, Vector3 endPoint, float speed)
 {
     Light Plight = new Light();
     Plight.LightType = Light.Type.Point;
     Plight.Radius = Radius;
     Plight.Intensity = Intensity;
     Plight.Color = LightColor;
     Plight.Transform = Matrix.CreateTranslation(startPoint);
     lights.Add(Plight);
     moveLight.Add(new MovingLight(Plight, startPoint, endPoint, speed));
 }
Ejemplo n.º 7
0
        public Light DirLight()
        {
            Light DL = new Light();

            foreach (Light light in lights)
            {
                if (light.LightType == Light.Type.Directional)
                {
                    DL = light;
                }
            }
            return DL;
        }
Ejemplo n.º 8
0
        public void AddSpotLight(float Radius, float Intensity, float SpotAngle, float SpotYaw, float SpotPitch, float SpotRoll, Color LightColor, Vector3 LightPosition, float ShadowDepthBias)
        {
            Light spot = new Light();
            spot.LightType = Light.Type.Spot;
            spot.ShadowDepthBias = ShadowDepthBias;
            spot.Radius = Radius;
            spot.Intensity = Intensity;
            spot.Color = LightColor;
            spot.CastShadows = true;
            spot.SpotAngle = SpotAngle;

            Matrix rotation = Matrix.CreateFromYawPitchRoll(SpotYaw, SpotPitch, SpotRoll);
            Matrix transform = rotation;
            transform.Translation = LightPosition;

            spot.Transform = transform;
            lights.Add(spot);
        }
Ejemplo n.º 9
0
        public void AddSpotLight(float Radius, float Intensity, float SpotAngle, float SpotYaw, float SpotPitch, float SpotRoll, Color LightColor, Vector3 LightPosition, bool CastShadows)
        {
            Light spot = new Light();
            spot.LightType = Light.Type.Spot;
            spot.ShadowDepthBias = 0.0001f;
            spot.Radius = Radius;
            spot.Intensity = Intensity;
            spot.Color = LightColor;
            spot.CastShadows = CastShadows;
            spot.SpotAngle = SpotAngle;

            Matrix rotation = Matrix.CreateFromYawPitchRoll(SpotYaw, SpotPitch, SpotRoll);
            Matrix transform = rotation;
            transform.Translation = LightPosition;

            float tan = (float)Math.Tan(MathHelper.ToRadians(SpotAngle));
            Matrix scale = Matrix.CreateScale(Radius * tan, Radius * tan, Radius);
            transform = scale * transform;

            spot.Transform = transform;
            lights.Add(spot);
        }
Ejemplo n.º 10
0
 public void AddPointLight(float Radius, float Intensity, Color LightColor, Vector3 LightPosition)
 {
     Light Plight = new Light();
     Plight.LightType = Light.Type.Point;
     Plight.Radius = Radius;
     Plight.Intensity = Intensity;
     Plight.Color = LightColor;
     Plight.Transform = Matrix.CreateTranslation(LightPosition);
     lights.Add(Plight);
 }