Пример #1
0
        /// <summary>
        /// Generate the shadow map for a given spot light
        /// </summary>
        /// <param name="renderer"></param>
        /// <param name="meshes"></param>
        /// <param name="light"></param>
        /// <param name="shadowMap"></param>
        public void GenerateShadowTextureSpotLight(Renderer renderer, BaseSceneGraph renderWorld, 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;

            _visibleMeshes.Clear();
            //cull meshes outside the light volume
            renderWorld.GetShadowCasters(frustum, _visibleMeshes);

            renderer.InstancingGroupManager.Reset();
            for (int index = 0; index < _visibleMeshes.Count; index++)
            {
                Mesh.SubMesh subMesh = _visibleMeshes[index];
                //render it
                if (!subMesh.InstanceEnabled)
                    subMesh.RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                else
                    renderer.InstancingGroupManager.AddInstancedSubMesh(subMesh);
            }
            renderer.InstancingGroupManager.GenerateInstanceInfo(renderer.GraphicsDevice);
            renderer.InstancingGroupManager.RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
        }
Пример #2
0
 public ShadowRenderer(Renderer renderer)
 {
     //create the render targets
     for (int i = 0; i < NUM_SPOT_SHADOWS; i++)
     {
         SpotShadowMapEntry entry = new SpotShadowMapEntry();
         //we store the linear depth, in a float render target. We need also the HW zbuffer
         entry.Texture = new RenderTarget2D(renderer.GraphicsDevice, SPOT_SHADOW_RESOLUTION,
                                            SPOT_SHADOW_RESOLUTION, false, SurfaceFormat.Single,
                                            DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents);
         entry.LightViewProjection = Matrix.Identity;
         _spotShadowMaps.Add(entry);
     }
 }
Пример #3
0
 public ShadowRenderer(Renderer renderer)
 {
     //create the render targets
     for (int i = 0; i < NUM_SPOT_SHADOWS; i++)
     {
         SpotShadowMapEntry entry = new SpotShadowMapEntry();
         //we store the linear depth, in a float render target. We need also the HW zbuffer
         entry.Texture = new RenderTarget2D(renderer.GraphicsDevice, SPOT_SHADOW_RESOLUTION,
                                            SPOT_SHADOW_RESOLUTION, false, SurfaceFormat.Single,
                                            DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents);
         entry.LightViewProjection = Matrix.Identity;
         _spotShadowMaps.Add(entry);
     }
 }
Пример #4
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);
                                }
                        }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Generate the shadow map for a given spot light
        /// </summary>
        /// <param name="renderer"></param>
        /// <param name="meshes"></param>
        /// <param name="light"></param>
        /// <param name="shadowMap"></param>
        public void GenerateShadowTextureSpotLight(Renderer renderer, BaseSceneGraph renderWorld, 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;

            _visibleMeshes.Clear();
            //cull meshes outside the light volume
            renderWorld.GetShadowCasters(frustum, _visibleMeshes);

            renderer.InstancingGroupManager.Reset();
            for (int index = 0; index < _visibleMeshes.Count; index++)
            {
                Mesh.SubMesh subMesh = _visibleMeshes[index];
                //render it
                if (!subMesh.InstanceEnabled)
                {
                    subMesh.RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
                }
                else
                {
                    renderer.InstancingGroupManager.AddInstancedSubMesh(subMesh);
                }
            }
            renderer.InstancingGroupManager.GenerateInstanceInfo(renderer.GraphicsDevice);
            renderer.InstancingGroupManager.RenderShadowMap(ref viewProj, renderer.GraphicsDevice);
        }