/// <summary> /// Method to do the deferred lighting draw call /// </summary> /// <param name="camera"></param> public void DrawDeferred(DeferredLightingCamera camera) { GraphicsDevice.Clear(camera.ClearColor); if (deferredSceneRenderEffect == null) { deferredSceneRenderEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredSceneRender"); } deferredSceneRenderEffect.Parameters["colorMap"].SetValue(camera.RenderTarget); deferredSceneRenderEffect.Parameters["lightMap"].SetValue(lightMap); //deferredSceneRenderEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap); GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; deferredSceneRenderEffect.CurrentTechnique.Passes[0].Apply(); if (sceneQuad == null) { sceneQuad = new ScreenQuad(Game); sceneQuad.Initialize(); } sceneQuad.Draw(-Vector2.One, Vector2.One); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { base.Initialize(); AssetManager = new AssetManager(); PackageManager = new PackageManager(); // "AlkaronContent" must be initialized before calling this, because // BuildPackageMap depends on it. PackageManager.BuildPackageMap(); SceneManager = new SceneManager(GraphicsDevice); ScreenQuad.Initialize(SceneManager); Graphics2D.Texture.SingleWhite = new Graphics2D.Texture(SceneManager, 1, 1, new byte[] { 255, 255, 255, 255 }); DefaultFont = Content.Load <SpriteFont>("DefaultFont"); SceneManager.PrimitiveRenderManager.EngineFont = DefaultFont; RasterizerState rasterizerState = new RasterizerState(); rasterizerState.CullMode = CullMode.CullClockwiseFace; GraphicsDevice.RasterizerState = rasterizerState; }
public virtual void Draw(GameTime gameTime) { if (Enabled) { if (sq == null) { sq = new ScreenQuad(Game); sq.Initialize(); } if (!UsesVertexShader) { spriteBatch.Begin(SortMode, Blend, Sampler, DepthStencilState.None, RasterizerState.CullCounterClockwise); } else { Game.GraphicsDevice.SamplerStates[0] = Sampler; } effect.CurrentTechnique.Passes[0].Apply(); if (UsesVertexShader) { sq.Draw(-Vector2.One, Vector2.One); } else { spriteBatch.Draw(BackBuffer, new Rectangle(0, 0, Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), Color.White); spriteBatch.End(); } } }
/// <summary> /// Render a cone light /// </summary> /// <param name="camera"></param> /// <param name="coneLight"></param> public void RenderConeLight(DeferredLightingCamera camera, DeferredConeLight coneLight) { if (deferredConeLightEffect == null) { deferredConeLightEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredConeLight"); } // Load Light parameters deferredConeLightEffect.Parameters["lightDirection"].SetValue(coneLight.Direction); deferredConeLightEffect.Parameters["LightPosition"].SetValue(coneLight.Transform.Position); deferredConeLightEffect.Parameters["Color"].SetValue(coneLight.Color.ToVector3()); deferredConeLightEffect.Parameters["ViewProjectionInv"].SetValue(Matrix.Invert(camera.View * camera.Projection)); deferredConeLightEffect.Parameters["LightViewProjection"].SetValue(coneLight.View * coneLight.Projection); deferredConeLightEffect.Parameters["CameraPosition"].SetValue(camera.Transform.Position); deferredConeLightEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap); deferredConeLightEffect.Parameters["normalMap"].SetValue(camera.NormalBuffer); deferredConeLightEffect.Parameters["depthMap"].SetValue(camera.DepthBuffer); deferredConeLightEffect.Parameters["power"].SetValue(coneLight.Intensity); deferredConeLightEffect.Parameters["ConeAngle"].SetValue(coneLight.Angle); deferredConeLightEffect.Parameters["ConeDecay"].SetValue(coneLight.Decay); deferredConeLightEffect.Parameters["CastShadow"].SetValue(coneLight.CastShadow); if (coneLight.CastShadow) { deferredConeLightEffect.Parameters["shadowMap"].SetValue(coneLight.ShadowMap); deferredConeLightEffect.Parameters["mod"].SetValue(deferredConeShadowMapMod); deferredConeLightEffect.Parameters["DiscRadius"].SetValue(1.5f); deferredConeLightEffect.Parameters["hardShadows"].SetValue(coneLight.HardShadows); deferredConeLightEffect.Parameters["Taps"].SetValue(taps); } deferredConeLightEffect.CurrentTechnique.Passes[0].Apply(); // Set sampler state to Point as the Surface type requires it in XNA 4.0 GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; if (sceneQuad == null) { sceneQuad = new ScreenQuad(Game); sceneQuad.Initialize(); } sceneQuad.Draw(-Vector2.One, Vector2.One); }
/// <summary> /// Apply the glow map /// </summary> /// <param name="camera"></param> public void ApplyGlowMap(DeferredLightingCamera camera) { if (glowMapToLightMapEffect == null) { glowMapToLightMapEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/GlowMapToLightMapShader"); } glowMapToLightMapEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap); glowMapToLightMapEffect.Techniques[0].Passes[0].Apply(); if (sceneQuad == null) { sceneQuad = new ScreenQuad(Game); sceneQuad.Initialize(); } sceneQuad.Draw(-Vector2.One, Vector2.One); }
/// <summary> /// Render a directional light. /// </summary> /// <param name="camera"></param> /// <param name="directionalLight"></param> public void RenderDirectionalLight(DeferredLightingCamera camera, DeferredDirectionalLight directionalLight) { if (deferredDirectionalLightEffect == null) { deferredDirectionalLightEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredDirectionalLight"); } // Call lighting methods. deferredDirectionalLightEffect.Parameters["lightDirection"].SetValue(directionalLight.Direction); deferredDirectionalLightEffect.Parameters["Color"].SetValue(directionalLight.Color.ToVector3()); deferredDirectionalLightEffect.Parameters["power"].SetValue(directionalLight.Intensity); deferredDirectionalLightEffect.Parameters["CameraPosition"].SetValue(camera.Transform.Position); deferredDirectionalLightEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap); deferredDirectionalLightEffect.Parameters["normalMap"].SetValue(camera.NormalBuffer); deferredDirectionalLightEffect.Parameters["depthMap"].SetValue(camera.DepthBuffer); deferredDirectionalLightEffect.Parameters["CastShadow"].SetValue(directionalLight.CastShadow); if (directionalLight.CastShadow) { deferredDirectionalLightEffect.Parameters["shadowMap"].SetValue(directionalLight.ShadowMap); deferredDirectionalLightEffect.Parameters["mod"].SetValue(deferredDirectionalShadowMapMod); deferredDirectionalLightEffect.Parameters["hardShadows"].SetValue(directionalLight.HardShadows); deferredDirectionalLightEffect.Parameters["DiscRadius"].SetValue(1f); deferredDirectionalLightEffect.Parameters["Taps"].SetValue(taps); } deferredDirectionalLightEffect.Parameters["viewProjectionInv"].SetValue(Matrix.Invert(camera.View * camera.Projection)); deferredDirectionalLightEffect.Parameters["lightViewProjection"].SetValue(directionalLight.View * directionalLight.Projection); deferredDirectionalLightEffect.Techniques[0].Passes[0].Apply(); if (sceneQuad == null) { sceneQuad = new ScreenQuad(Game); sceneQuad.Initialize(); } sceneQuad.Draw(-Vector2.One, Vector2.One); }