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);
        }
		protected override void Initialise()
		{
			//setup the view camera first
			//--------------------------------------

			viewCamera = new Xen.Camera.FirstPersonControlledCamera3D(this.UpdateManager);
			viewCamera.Projection.FieldOfView *= 0.65f;
			viewCamera.MovementSensitivity *= 0.05f;
			viewCamera.LookAt(new Vector3(-3, 4, 2), new Vector3(6, 6, 2), new Vector3(0, 1, 0));
			viewCamera.Projection.NearClip = 0.1f;

			//shadow map setup:
			//--------------------------------------

			const float shadowArea = 4;
			const int shadowMapResolution = 1024;

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

			//setup the shadow map projection to roughly cover the character
			shadowCamera.Projection.Orthographic = true;
			shadowCamera.Projection.NearClip = shadowArea * 2;
			shadowCamera.Projection.FarClip = -shadowArea * 2;
			shadowCamera.Projection.Region = new Vector4(1, -1.8f, -1, 0.2f) * shadowArea;

			//setup the shadow map draw target

			//create the shadow map
			shadowMap = new DrawTargetTexture2D(shadowCamera, shadowMapResolution, shadowMapResolution, SurfaceFormat.HalfSingle, DepthFormat.Depth24);
			shadowMap.ClearBuffer.ClearColour = Color.White;

			//setup the shadow map drawer..
			shadowDrawer = new Tutorial_25.ShadowMapDrawer(null, new Tutorial_25.ShadowOutputShaderProvider());
			this.shadowMap.Add(shadowDrawer);



			//create the main draw targets.
			//--------------------------------------

			drawToScreen = new DrawTargetScreen(new Camera2D());
			drawToScreen.ClearBuffer.ClearColourEnabled = false;

			drawToRenderTarget = new DrawTargetTexture2D(viewCamera, this.WindowWidth, this.WindowHeight, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, false, PreferredMultiSampleLevel.FourSamples, RenderTargetUsage.PlatformContents);
			drawToRenderTarget.ClearBuffer.ClearColourEnabled = false;

			//setup the bloom draw targets
			//--------------------------------------

			//scale to reduce the size of the bloom target, compared to main render target
			const int bloomDownsample = 8;	//eight times smaller

			bloomRenderTarget = new DrawTargetTexture2D(new Camera2D(), Math.Max(1, drawToRenderTarget.Width / bloomDownsample), Math.Max(1, drawToRenderTarget.Height / bloomDownsample), SurfaceFormat.Color, DepthFormat.None);
			bloomRenderTarget.ClearBuffer.ClearColourEnabled = false;

			bloomIntermediateRenderTarget = null;

			//the bloom intermediate target is not needed on the xbox, as the full bloom target fits in EDRAM
			bloomIntermediateRenderTarget = new DrawTargetTexture2D(viewCamera, bloomRenderTarget.Width, bloomRenderTarget.Height, SurfaceFormat.Color, DepthFormat.None);
			bloomIntermediateRenderTarget.ClearBuffer.ClearColourEnabled = false;

			//setup the blur filter, with a large 31 sample radius.
			bloomBlurPass = new Xen.Ex.Filters.BlurFilter(Xen.Ex.Filters.BlurFilterFormat.ThirtyOneSampleBlur_FilteredTextureFormat, 1.0f, bloomRenderTarget, bloomIntermediateRenderTarget);


			//setup the character model
			this.model = new ModelInstance();	//(the model is setup in LoadContent)
			this.modelRotation = new DrawRotated(model);
			this.modelRotation.RotationAngle = 3;

			//add the model to be drawn
			drawToRenderTarget.Add(modelRotation);

			//setup the shaders
			this.characterRenderShader = new Shaders.Character();

			//setup the output and bloom shaders
			outputShader = new Shaders.RgbmDecode();
			drawToScreen.Add(new ShaderElement(outputShader, new Vector2(1, 1), true));

			bloomPassShader = new Shaders.RgbmDecodeBloomPass();
			bloomRenderTarget.Add(new ShaderElement(bloomPassShader, new Vector2(1, 1), true));

			//add a background to be drawn
			drawToRenderTarget.Add(new BackgroundDrawer());


			//setup the debug image displays
			//--------------------------------------

			this.rgmbTextureAlphaShader = new Shaders.AlphaWrite();
			this.bloomTextureDisplay = new TexturedElement(this.bloomRenderTarget, new Vector2(0.2f, 0.2f), true);
			this.rgbmTextureDisplay = new TexturedElement(this.drawToRenderTarget, new Vector2(0.2f, 0.2f), true);
			this.rgbmTextureAlphaDisplay = new ShaderElement(this.rgmbTextureAlphaShader, new Vector2(0.2f, 0.2f), true);

			this.rgbmTextureAlphaDisplay.Position = new Vector2(0.7f, 0.2f);
			this.rgbmTextureDisplay.Position = new Vector2(0.7f, 0.4f);
			this.bloomTextureDisplay.Position = new Vector2(0.7f, 0.6f);

			this.drawToScreen.Add(this.rgbmTextureDisplay);
			this.drawToScreen.Add(this.rgbmTextureAlphaDisplay);
			this.drawToScreen.Add(this.bloomTextureDisplay);

			//setup the render config
			this.configEditor = new RenderConfigEditor(this.Content);

			this.drawToScreen.Add(configEditor);
			this.UpdateManager.Add(configEditor);


			//add a statistics overlay.
			drawStats = new Xen.Ex.Graphics2D.Statistics.DrawStatisticsDisplay(this.UpdateManager);
			drawToScreen.Add(drawStats);
			
		}
		protected override void Initialise()
		{
			//setup ambient lighting
			this.ambientLight = new MaterialLightCollection();
			ambientLight.LightingEnabled = true;
			ambientLight.AmbientLightColour = new Vector3(0.4f, 0.2f, 0.1f);
			ambientLight.CreateDirectionalLight(new Vector3(-1, -1, 0), new Vector3(3,2,1)); // add some backlighting
			ambientLight.SphericalHarmonic.AddLight(new Vector3(2, 0.5f, 0.25f), new Vector3(0, 0, 1), 0.2f);

			//the camera for the shadows point of view, represents the direction of the light.
			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)
			var textureFormat = SurfaceFormat.Color;
			const int resolution = 256;

			//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;

			//technically not required on the xbox if the render target is small enough to fit in EDRAM in one tile, but xna insists
			blurIntermediate = new DrawTargetTexture2D(shadowCamera, resolution, resolution, textureFormat, DepthFormat.None);

			//create a blur filter
			shadowDepthBlurFilter = new Xen.Ex.Filters.BlurFilter(Xen.Ex.Filters.BlurFilterFormat.SevenSampleBlur,1.0f, drawShadowDepth, blurIntermediate);

			//create the scene camera
			var 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(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.2f, 4-x*2-y));
			}

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


			//setup the draw targets...


			//create the shader provider
			var 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);

			Vector3 lightColour = new Vector3(2, 1.5f, 1);

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

			//add a nice faded background
			Tutorial_20.BackgroundGradient background = new Tutorial_20.BackgroundGradient(new Color(1, 0.5f, 0.3f), new Color(0.2f, 0.1f, 0.2f));
			background.DrawAtMaxZDepth = true;
			drawToScreen.Add(background);


			//create a textured element that will display the shadow map texture
			var shadowDepthDisplay = new TexturedElement(drawShadowDepth, new Vector2(256, 256));
			shadowDepthDisplay.VerticalAlignment = VerticalAlignment.Top;
			this.drawToScreen.Add(shadowDepthDisplay);
		}
Example #4
0
        protected override void Initialise()
        {
            Resource.EnableResourceTracking();

            //setup the view camera first
            //--------------------------------------

            viewCamera = new Xen.Ex.Camera.FirstPersonControlledCamera3D(this.UpdateManager);
            viewCamera.Projection.FieldOfView *= 0.65f;
            viewCamera.MovementSensitivity    *= 0.05f;
            viewCamera.LookAt(new Vector3(-3, 4, 2), new Vector3(6, 6, 2), new Vector3(0, 1, 0));
            viewCamera.Projection.NearClip = 0.1f;

            //shadow map setup:
            //--------------------------------------

            const float shadowArea          = 4;
            const int   shadowMapResolution = 1024;

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

            //setup the shadow map projection to roughly cover the character
            shadowCamera.Projection.Orthographic = true;
            shadowCamera.Projection.NearClip     = shadowArea * 2;
            shadowCamera.Projection.FarClip      = -shadowArea * 2;
            shadowCamera.Projection.Region       = new Vector4(1, -1.8f, -1, 0.2f) * shadowArea;

            //setup the shadow map draw target

            //find a desirable format for the shadow map,
            SurfaceFormat format = SurfaceFormat.Color;

            //ideally use a high precision format, but only if it's supported. Avoid full 32bit float
            if (DrawTargetTexture2D.SupportsFormat(SurfaceFormat.Rg32))
            {
                format = SurfaceFormat.Rg32;                                                                                                            //ushort * 2
            }
            else if (DrawTargetTexture2D.SupportsFormat(SurfaceFormat.HalfVector2))
            {
                format = SurfaceFormat.HalfVector2;                                                                                     //fp16 * 2
            }
            else if (DrawTargetTexture2D.SupportsFormat(SurfaceFormat.HalfVector4))
            {
                format = SurfaceFormat.HalfVector4;                                                                                 //fp16 * 4
            }
            //create the shadow map
            shadowMap = new DrawTargetTexture2D(shadowCamera, shadowMapResolution, shadowMapResolution, format, DepthFormat.Depth24);
            shadowMap.ClearBuffer.ClearColour = Color.White;

            //setup the shadow map drawer..
            shadowDrawer = new Tutorial_25.ShadowMapDrawer(null, new Tutorial_25.ShadowOutputShaderProvider());
            this.shadowMap.Add(shadowDrawer);



            //create the main draw targets.
            //--------------------------------------

            drawToScreen = new DrawTargetScreen(this, new Camera2D());
            drawToScreen.ClearBuffer.ClearColourEnabled = false;

            drawToRenderTarget = new DrawTargetTexture2D(viewCamera, this.WindowWidth, this.WindowHeight, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, false, MultiSampleType.FourSamples, RenderTargetUsage.PlatformContents);
            drawToRenderTarget.ClearBuffer.ClearColourEnabled = false;

            //setup the bloom draw targets
            //--------------------------------------

            //scale to reduce the size of the bloom target, compared to main render target
            const int bloomDownsample = 8;              //eight times smaller

            bloomRenderTarget = new DrawTargetTexture2D(new Camera2D(), Math.Max(1, drawToRenderTarget.Width / bloomDownsample), Math.Max(1, drawToRenderTarget.Height / bloomDownsample), SurfaceFormat.Color);
            bloomRenderTarget.ClearBuffer.ClearColourEnabled = false;

            bloomIntermediateRenderTarget = null;
#if WINDOWS
            //the bloom intermediate target is not needed on the xbox, as the full bloom target fits in EDRAM
            bloomIntermediateRenderTarget = new DrawTargetTexture2D(viewCamera, bloomRenderTarget.Width, bloomRenderTarget.Height, SurfaceFormat.Color);
            bloomIntermediateRenderTarget.ClearBuffer.ClearColourEnabled = false;
#endif
            //setup the blur filter, with a large 31 sample radius.
            bloomBlurPass = new Xen.Ex.Filters.BlurFilter(Xen.Ex.Filters.BlurFilterFormat.ThirtyOneSampleBlur_FilteredTextureFormat, 1.0f, bloomRenderTarget, bloomIntermediateRenderTarget);


            //setup the character model
            this.model         = new ModelInstance();           //(the model is setup in LoadContent)
            this.modelRotation = new DrawRotated(model);
            this.modelRotation.RotationAngle = 3;

            //add the model to be drawn
            drawToRenderTarget.Add(modelRotation);

            //setup the shaders
            this.characterRenderShader      = new Shaders.Character();
            this.characterBlendRenderShader = new Shaders.CharacterBlend();

            //setup the output and bloom shaders
            outputShader = new Shaders.RgbmDecode();
            drawToScreen.Add(new ShaderElement(outputShader, new Vector2(1, 1), true));

            bloomPassShader = new Shaders.RgbmDecodeBloomPass();
            bloomRenderTarget.Add(new ShaderElement(bloomPassShader, new Vector2(1, 1), true));

            //add a background to be drawn
            drawToRenderTarget.Add(new BackgroundDrawer());


            //setup the debug image displays
            //--------------------------------------

            this.rgmbTextureAlphaShader  = new Shaders.AlphaWrite();
            this.bloomTextureDisplay     = new TexturedElement(this.bloomRenderTarget, new Vector2(0.2f, 0.2f), true);
            this.rgbmTextureDisplay      = new TexturedElement(this.drawToRenderTarget, new Vector2(0.2f, 0.2f), true);
            this.rgbmTextureAlphaDisplay = new ShaderElement(this.rgmbTextureAlphaShader, new Vector2(0.2f, 0.2f), true);

            this.rgbmTextureAlphaDisplay.Position = new Vector2(0.7f, 0.2f);
            this.rgbmTextureDisplay.Position      = new Vector2(0.7f, 0.4f);
            this.bloomTextureDisplay.Position     = new Vector2(0.7f, 0.6f);

            this.drawToScreen.Add(this.rgbmTextureDisplay);
            this.drawToScreen.Add(this.rgbmTextureAlphaDisplay);
            this.drawToScreen.Add(this.bloomTextureDisplay);

            //setup the render config
            this.configEditor = new RenderConfigEditor(this.Content);

            this.drawToScreen.Add(configEditor);
            this.UpdateManager.Add(configEditor);


            //add a statistics overlay.
            drawStats = new Xen.Ex.Graphics2D.Statistics.DrawStatisticsDisplay(this.UpdateManager);
            drawToScreen.Add(drawStats);
        }