/// <summary> /// Renders the Game /// </summary> public void render() { //ActiveCamera.Position = Vector.Zero; Matrix transform = ActiveCamera != null ? ActiveCamera.TranformMatrix : Matrix.Identity; int bbWidth = GraphicsDevice.PresentationParameters.BackBufferWidth; int bbHeight = GraphicsDevice.PresentationParameters.BackBufferHeight; lightMapTarget = checkRenderTarget(lightMapTarget, bbWidth, bbHeight); layerScreenShader = checkRenderTarget(layerScreenShader, bbWidth, bbHeight); currentLayerTarget = checkRenderTarget(currentLayerTarget, bbWidth, bbHeight); bloomComponent.Settings = GameManager.Level.BloomSettings; bloomLightComponent.Settings = GameManager.Level.BloomLightSettings; //DRAW LIGHTMAP renderLightMap(GameManager.Objects.getAllObjects().Where(g => g.Active && !g.HudObject).ToList()); renderLayers(); bloomComponent.BeginDraw(); GraphicsDevice.Clear(Level.BackgroundColor); spriteBatch.Begin(); spriteBatch.Draw(layerScreenShader, Vector.Zero, XnaColor.White); spriteBatch.End(); bloomComponent.draw(); GraphicsDevice.SetRenderTarget(null); GraphicsDevice.Textures[1] = bloomLightComponent.OutputTarget; Vector4 ambience = ((XnaColor)GameManager.Level.AmbientLight).ToVector4(); lightEffect.Parameters["ambience"].SetValue(ambience); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone, GameManager.Level.LightingEnabled ? lightEffect : null); spriteBatch.Draw(bloomComponent.OutputTarget, Vector.Zero, XnaColor.White); spriteBatch.End(); }
public void renderLightMap(List <GameObject> gobs, List <InstanceDescriptor> insts) { ConvexHull.InitializeStaticMembers(GraphicsDevice); EffectParameter widthParameter = lightEffect.Parameters["screenWidth"]; EffectParameter heightParameter = lightEffect.Parameters["screenHeight"]; setShaderValue(widthParameter, lightMapTarget.Width); setShaderValue(heightParameter, lightMapTarget.Height); GraphicsDevice.SetRenderTarget(lightMapTarget); GraphicsDevice.Clear(XnaColor.Transparent); insts.ForEach(i => { if (i.Light.Visible) { ClearAlphaToOne(); GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.BlendState = CustomBlendStates.MultiplyShadows; //shadowing algorithm taken from http://www.catalinzima.com/xna/samples/dynamic-2d-shadows/ if (Level.PreviewShadowing) { insts.ForEach(hull => { if (hull.Shadows.CastsShadows) { Boolean pass = hull != i; if (i.Shadows.SelfShadows) { pass = true; } if (pass) { Convex convex = hull.Bounds.Convex; convex.Origin = hull.Position; convex.Rotation = hull.Sprite.Rotation; ConvexHull convexHull = new ConvexHull(convex, WhiskeyColor.White); convexHull.DrawShadows(i.Light, CameraTransform, hull.Shadows.IncludeLight, hull.Shadows.Solidness, hull.Shadows.Height); } } }); } spriteBatch.Begin(SpriteSortMode.Immediate, CustomBlendStates.MultiplyWithAlpha, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone, null, CameraTransform); i.renderLight(RenderInfo); spriteBatch.End(); } }); ClearAlphaToOne(); lightBloomComponent.BeginDraw(); GraphicsDevice.Clear(XnaColor.Transparent); spriteBatch.Begin(); spriteBatch.Draw(lightMapTarget, Vector.Zero, null, XnaColor.White); spriteBatch.End(); lightBloomComponent.draw(); lightMapTarget = lightBloomComponent.OutputTarget; }
public void renderAll(List <GameObject> gobs, List <InstanceDescriptor> insts) { //get width and height of window int bbWidth = GraphicsDevice.PresentationParameters.BackBufferWidth; int bbHeight = GraphicsDevice.PresentationParameters.BackBufferHeight; //initialize bloom component if (bloomComponent == null) { bloomComponent = new BloomComponent(GraphicsDevice, WhiskeyControl.Content); bloomComponent.loadContent(); bloomComponent.Settings = BloomSettings.PresetSettings[0]; } if (lightBloomComponent == null) { lightBloomComponent = new BloomComponent(GraphicsDevice, WhiskeyControl.Content); lightBloomComponent.loadContent(); lightBloomComponent.Settings = BloomSettings.PresetSettings[5]; } //load bloom settings if (Level.BloomSettings != null) { bloomComponent.Settings = Level.BloomSettings; } if (Level.LightBloomSettings != null) { lightBloomComponent.Settings = Level.LightBloomSettings; } //ensure render targets are correctly sized hudTarget = checkRenderTarget(hudTarget, bbWidth, bbHeight); lightMapTarget = checkRenderTarget(lightMapTarget, bbWidth, bbHeight); sceneTarget = checkRenderTarget(sceneTarget, bbWidth, bbHeight); hudObjectsTarget = checkRenderTarget(hudObjectsTarget, 1280, 720); layerScreenShader = checkRenderTarget(layerScreenShader, bbWidth, bbHeight); currentLayerTarget = checkRenderTarget(currentLayerTarget, bbWidth, bbHeight); //get game objects, and ui gameobjects List <GameObject> uiGameObjects = new List <GameObject>(); List <GameObject> gobsToRender = new List <GameObject>(); gobs.ForEach(g => uiGameObjects.Add(g)); insts.Where(g => !g.HudObject).ToList().ForEach(g => gobsToRender.Add(g)); //DRAW HUD renderHud(); //draw light radius gobsToRender.ForEach(g => { if (g.Light.Visible) { Convex convex = new Convex(g.Position, 0, VectorSet.Dodecahedren * (g.Light.Radius / 2)); convex.render(GraphicsDevice, CameraTransform, new RenderHints().setColor(Level.BackgroundColor.Inverted)); } }); //DRAW GAME OBJECTS // bloomComponent.BeginDraw(); render(gobsToRender); bloomComponent.BeginDraw(); GraphicsDevice.Clear(Level.BackgroundColor); spriteBatch.Begin(); spriteBatch.Draw(layerScreenShader, Vector.Zero, XnaColor.White); spriteBatch.End(); bloomComponent.draw(); //DRAW LIGHTMAP renderLightMap(gobs, insts.Where(g => !g.HudObject).ToList()); //DRAW SCENE WITH LIGHTMAP GraphicsDevice.SetRenderTarget(null); GraphicsDevice.Textures[1] = lightMapTarget; Vector4 ambience = ((XnaColor)Level.AmbientLight).ToVector4(); lightEffect.Parameters["ambience"].SetValue(ambience); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone, Level.PreviewLighting ? lightEffect : null); spriteBatch.Draw(bloomComponent.OutputTarget, Vector.Zero, XnaColor.White); spriteBatch.End(); //DRAW HUD spriteBatch.Begin(); spriteBatch.Draw(hudTarget, Vector.Zero, XnaColor.White); spriteBatch.End(); }