Example #1
0
        protected override void Initialise()
        {
            //setup ambient lighting
            this.ambientLight               = new MaterialLightCollection();
            ambientLight.LightingEnabled    = true;
            ambientLight.AmbientLightColour = new Vector3(0.25f, 0.25f, 0.25f);
            ambientLight.AddDirectionalLight(false, new Vector3(-1, -1, 0), new Vector3(2, 2, 2));           // add some backlighting

            //setup the shadow render camera
            Camera3D shadowCamera = new Camera3D();

            shadowCamera.LookAt(new Vector3(1, 1, 3), new Vector3(-15, 20, 20), new Vector3(0, 0, 1));

            //set the clip plane distances
            shadowCamera.Projection.FarClip      = 40;
            shadowCamera.Projection.NearClip     = 20;
            shadowCamera.Projection.FieldOfView *= 0.25f;


            //8bit is actually enough accuracy for this sample (given the limited range of the shadow)
            SurfaceFormat textureFormat = SurfaceFormat.Color;
            const int     resolution    = 512;

            //create the shadow map texture:
            drawShadowDepth = new DrawTargetTexture2D(shadowCamera, resolution, resolution, textureFormat, DepthFormat.Depth24);
            drawShadowDepth.ClearBuffer.ClearColour = Color.White;

            //for the shadow technique used, the shadow buffer is blurred.
            //this requires an intermediate render target on the PC
            DrawTargetTexture2D blurIntermediate = null;

#if !XBOX360
            //not required on the xbox if the render target is small enough to fit in EDRAM in one tile
            blurIntermediate = new DrawTargetTexture2D(shadowCamera, resolution, resolution, textureFormat);
#endif
            //create a blur filter
            shadowDepthBlurFilter = new Xen.Ex.Filters.BlurFilter(Xen.Ex.Filters.BlurFilterFormat.SevenSampleBlur, 1.0f, drawShadowDepth, blurIntermediate);

            //create the scene camera
            Camera3D camera = new Camera3D();
            camera.LookAt(new Vector3(0, 0, 3), new Vector3(10, 10, 6), new Vector3(0, 0, 1));
            camera.Projection.FieldOfView *= 0.55f;

            //create the draw target.
            drawToScreen = new DrawTargetScreen(this, camera);
            drawToScreen.ClearBuffer.ClearColour = Color.Black;

            //the 'scene'
            //A DrawList from Tutorial 23 is used here, this stores the 'scene',
            //which is just a set of actors and the ground
            Tutorials.Tutorial_23.DrawList scene = new Tutorials.Tutorial_23.DrawList();

            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    //create the actor instances
                    if (x != 0 || y != 0)
                    {
                        scene.Add(new Actor(this.Content, new Vector3(x * 6 - 3, y * 6 - 3, 0), (x + y * 2 + 1) * 0.1f, 4 - x * 2 - y));
                    }
                }
            }

            //add the ground
            GroundDisk ground = new GroundDisk(this.Content, 10, ambientLight);
            scene.Add(ground);


            //setup the draw targets...


            //create the shader provider
            ShadowOutputShaderProvider shadowOutputShaderProvider = new ShadowOutputShaderProvider();

            //add a ShadowMapDrawer to the shadow map texture
            drawShadowDepth.Add(new ShadowMapDrawer(scene, shadowOutputShaderProvider));

            //setup the scene to be drawn to the screen
            //draw the scene normally (no shadow, just ambient)
            drawToScreen.Add(scene);

            //then draw the scene with a shadow (blended on top)
            drawToScreen.Add(new ShadowedSceneDrawer(scene, shadowOutputShaderProvider, drawShadowDepth));

            //add a nice faded background
            Tutorial_20.BackgroundGradient background = new Tutorial_20.BackgroundGradient(Color.WhiteSmoke, Color.Black);
            background.DrawAtMaxZDepth = true;
            drawToScreen.Add(background);


            //create a textured element that will display the shadow map texture
            TexturedElement shadowDepthDisplay = new TexturedElement(drawShadowDepth, new Vector2(256, 256));
            shadowDepthDisplay.VerticalAlignment = VerticalAlignment.Top;
            this.drawToScreen.Add(shadowDepthDisplay);
        }