Permalink
Browse files

Multiple points lights with dif only

  • Loading branch information...
1 parent 9621ba9 commit 979c8b21f1b448e1019cbcb5abb7241f5831949d @liquidmetal committed Apr 12, 2012
Showing with 311 additions and 81 deletions.
  1. BIN ChipsChallenge.suo
  2. +1 −0 ChipsChallenge/ChipsChallenge/ChipsChallenge.csproj.Debug.cachefile
  3. +1 −0 ChipsChallenge/ChipsChallenge/obj/x86/Debug/ChipsChallenge.csproj.FileListAbsolute.txt
  4. +1 −0 ...enge/ChipsChallenge/obj/x86/Debug/cachefile-{E8D9DF49-E0A2-4624-8D0B-888775114165}-targetpath.txt
  5. +7 −0 ChipsChallenge/ChipsChallengeContent/ChipsChallengeContent.contentproj
  6. +10 −1 ChipsChallenge/ChipsChallengeContent/obj/x86/Debug/ContentPipeline.xml
  7. +6 −0 Engine/ChipEngine.cs
  8. +129 −52 Engine/Renderer.cs
  9. BIN Engine/bin/x86/Debug/Engine.dll
  10. BIN Engine/bin/x86/Debug/Engine.pdb
  11. BIN Engine/obj/x86/Debug/Engine.dll
  12. BIN Engine/obj/x86/Debug/Engine.pdb
  13. +9 −0 LevelEditorXna/LevelEditorXna/FormRenderer.cs
  14. +1 −0 LevelEditorXna/LevelEditorXna/LevelEditorXna.csproj.Debug.cachefile
  15. +91 −28 LevelEditorXna/LevelEditorXna/MainForm.Designer.cs
  16. +53 −0 LevelEditorXna/LevelEditorXna/MainForm.cs
  17. BIN LevelEditorXna/LevelEditorXna/bin/x86/Debug/Engine.dll
  18. BIN LevelEditorXna/LevelEditorXna/bin/x86/Debug/Engine.pdb
  19. BIN LevelEditorXna/LevelEditorXna/bin/x86/Debug/LevelEditorXna.exe
  20. BIN LevelEditorXna/LevelEditorXna/bin/x86/Debug/LevelEditorXna.pdb
  21. BIN LevelEditorXna/LevelEditorXna/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache
  22. +1 −0 LevelEditorXna/LevelEditorXna/obj/x86/Debug/LevelEditorXna.csproj.FileListAbsolute.txt
  23. BIN LevelEditorXna/LevelEditorXna/obj/x86/Debug/LevelEditorXna.exe
  24. BIN LevelEditorXna/LevelEditorXna/obj/x86/Debug/LevelEditorXna.pdb
  25. BIN LevelEditorXna/LevelEditorXna/obj/x86/Debug/ResolveAssemblyReference.cache
  26. +1 −0 ...rXna/LevelEditorXna/obj/x86/Debug/cachefile-{E8D9DF49-E0A2-4624-8D0B-888775114165}-targetpath.txt
View
Binary file not shown.
@@ -5,6 +5,7 @@ Content\shaders\ClearBuffers.xnb
Content\shaders\ChannelRender.xnb
Content\shaders\NormalSprite.xnb
Content\shaders\PointLight.xnb
+Content\shaders\Combine.xnb
Content\floorTest.xnb
Content\floorTest_heightmap.xnb
Content\sprites\test\teapot\dif.xnb
@@ -30,3 +30,4 @@ D:\Projects\ChipsChallenge\ChipsChallenge\ChipsChallenge\bin\x86\Debug\Content\s
D:\Projects\ChipsChallenge\ChipsChallenge\ChipsChallenge\bin\x86\Debug\Content\sprites\test\plane\dif.xnb
D:\Projects\ChipsChallenge\ChipsChallenge\ChipsChallenge\bin\x86\Debug\Content\sprites\test\plane\hgt.xnb
D:\Projects\ChipsChallenge\ChipsChallenge\ChipsChallenge\bin\x86\Debug\Content\sprites\test\plane\nrm.xnb
+D:\Projects\ChipsChallenge\ChipsChallenge\ChipsChallenge\bin\x86\Debug\Content\shaders\Combine.xnb
@@ -5,6 +5,7 @@ Content\shaders\ClearBuffers.xnb
Content\shaders\ChannelRender.xnb
Content\shaders\NormalSprite.xnb
Content\shaders\PointLight.xnb
+Content\shaders\Combine.xnb
Content\floorTest.xnb
Content\floorTest_heightmap.xnb
Content\sprites\test\teapot\dif.xnb
@@ -205,6 +205,13 @@
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
+ <ItemGroup>
+ <Compile Include="shaders\Combine.fx">
+ <Name>Combine</Name>
+ <Importer>EffectImporter</Importer>
+ <Processor>EffectProcessor</Processor>
+ </Compile>
+ </ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -61,7 +61,16 @@
<Processor>EffectProcessor</Processor>
<Options>None</Options>
<Output>D:\Projects\ChipsChallenge\LevelEditorXna\LevelEditorXna\bin\x86\Debug\Content\shaders\PointLight.xnb</Output>
- <Time>2012-04-09T22:08:40.298994-07:00</Time>
+ <Time>2012-04-11T19:54:21.3356423-07:00</Time>
+ </Item>
+ <Item>
+ <Source>shaders\Combine.fx</Source>
+ <Name>shaders\Combine</Name>
+ <Importer>EffectImporter</Importer>
+ <Processor>EffectProcessor</Processor>
+ <Options>None</Options>
+ <Output>D:\Projects\ChipsChallenge\LevelEditorXna\LevelEditorXna\bin\x86\Debug\Content\shaders\Combine.xnb</Output>
+ <Time>2012-04-11T20:32:53.9679173-07:00</Time>
</Item>
<Item>
<Source>floorTest.png</Source>
View
@@ -65,6 +65,7 @@ private void LoadEffects()
assetManager.LoadEffect("shaders/DepthSprite");
assetManager.LoadEffect("shaders/ChannelRender");
assetManager.LoadEffect("shaders/PointLight");
+ assetManager.LoadEffect("shaders/Combine");
}
/// <summary>
@@ -90,5 +91,10 @@ public void Render()
{
renderer.RenderWorld(currentWorld);
}
+
+ public void SetRenderSceneType(RenderSceneType rst)
+ {
+ renderer.SetRenderSceneType(rst);
+ }
}
}
View
@@ -16,7 +16,7 @@
namespace Engine
{
- public enum RenderSceneType { Diffuse, Height, Normal };
+ public enum RenderSceneType { Beauty, Diffuse, Height, Normal, Lights };
public class Renderer
{
@@ -29,6 +29,8 @@ public class Renderer
private RenderTarget2D m_rtDif2; // Used to quick swapping
private RenderTarget2D m_rtDepthStencil;
private RenderTarget2D m_rtNormal;
+ private RenderTarget2D m_rtLights; // The lighting pass (multiplier for each pixel)
+ private RenderTarget2D m_rtOverlay; // stuff that's on top of everything else
private RenderTarget2D finalResult;
private RenderTarget2D temp;
@@ -42,6 +44,7 @@ public Renderer(AssetManager tex, GraphicsDevice device)
graphicsDevice = device;
m_posCamera = new Vector2(0, 0);
m_bDrawLights = false;
+ m_rstType = RenderSceneType.Beauty;
ResizeViewport(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
}
@@ -71,6 +74,8 @@ public void ResizeViewport(int width, int height)
m_rtDif2 = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
m_rtDepthStencil = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
m_rtNormal = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
+ m_rtLights = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
+ m_rtOverlay = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
finalResult = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
temp = new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.DiscardContents);
@@ -100,60 +105,54 @@ private void ClearScreen()
}
- /// <summary>
- /// Given a world object, the renderer draws it using the
- /// XNA frameworks
- /// </summary>
- /// <param name="world">The world object to render</param>
- /// <returns></returns>
- public bool RenderWorld(World world)
+ private void ClearRenderTargets()
{
- world.SortEntities();
- ClearScreen();
-
- Effect shader = assetManager.GetEffect("shaders/SimpleSprite");
- Effect channel = assetManager.GetEffect("shaders/ChannelRender");
- Effect depthSpriteShader = assetManager.GetEffect("shaders/DepthSprite");
- Vector2 viewportTrans = new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2);
-
- SpriteBatch spriteBatch = new SpriteBatch(graphicsDevice);
-
- // Step 1: Generate a stencil of which pixels to render
-
-
- // Draw the actual sprites
- Color[] t = { Color.White, Color.Yellow, Color.Purple, Color.Blue };
- int i = 0;
-
-
-
+ // The normal pass
graphicsDevice.SetRenderTarget(m_rtNormal);
graphicsDevice.Clear(Color.Transparent);
+ // The depth pass
graphicsDevice.SetRenderTarget(m_rtDepthStencil);
graphicsDevice.Clear(Color.Transparent);
+ // The diffuse pass
graphicsDevice.SetRenderTarget(m_rtDif);
graphicsDevice.Clear(Color.Black);
+ // The final result temporarily stored
graphicsDevice.SetRenderTarget(finalResult);
graphicsDevice.Clear(Color.Black);
- List<Entity> lstEntities = world.GetEntities();
+ // The lighting pass
+ graphicsDevice.SetRenderTarget(m_rtLights);
+ graphicsDevice.Clear(Color.Transparent);
+
+ // The overlay pass
+ graphicsDevice.SetRenderTarget(m_rtOverlay);
+ graphicsDevice.Clear(Color.Transparent);
+ }
+
+ private void RenderEntities(List<Entity> lstEntities, SpriteBatch spriteBatch, RenderTarget2D difTarget, RenderTarget2D hgtTarget, RenderTarget2D nrmTarget, RenderTarget2D temp)
+ {
+ Effect shader = assetManager.GetEffect("shaders/SimpleSprite");
+ Effect depthSpriteShader = assetManager.GetEffect("shaders/DepthSprite");
+ Vector2 viewportTrans = new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2);
+
foreach (Entity ent in lstEntities)
{
- if(ent.Type!=EntityType.Sprite)
+ if (ent.Type != EntityType.Sprite)
continue;
// Gather information
Texture2D tex = assetManager.GetTexture(ent.Sprite);
Texture2D texHeightmap = assetManager.GetHeightmap(ent.Sprite);
Texture2D texNormal = assetManager.GetNormalmap(ent.Sprite);
+ // This render target has to be made for every asset
RenderTarget2D mask = new RenderTarget2D(graphicsDevice, tex.Width, tex.Height);
- Vector2 pos = ent.Position;
- pos = pos + m_posCamera + viewportTrans;
+ // Gather information about the sprite
+ Vector2 pos = ent.Position + m_posCamera + viewportTrans;
SpriteEffects spriteEffects;
spriteEffects = ent.MirrorHorizontal ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
@@ -175,7 +174,7 @@ public bool RenderWorld(World world)
depthSpriteShader.Parameters["position"].SetValue(pos);
depthSpriteShader.Parameters["bufferSize"].SetValue(new Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height));
depthSpriteShader.Parameters["texSize"].SetValue(new Vector2(texHeightmap.Width, texHeightmap.Height));
- graphicsDevice.Textures[1] = m_rtDepthStencil;
+ graphicsDevice.Textures[1] = hgtTarget;
depthSpriteShader.Techniques[0].Passes[0].Apply();
// Draw the sprite
@@ -184,7 +183,7 @@ public bool RenderWorld(World world)
// Comp it all together onto the depth buffer
- graphicsDevice.SetRenderTarget(m_rtDepthStencil);
+ graphicsDevice.SetRenderTarget(hgtTarget);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(temp, Vector2.Zero, Color.White);
spriteBatch.End();
@@ -211,7 +210,7 @@ public bool RenderWorld(World world)
spriteBatch.End();
// Comp it al onto the normal buffer
- graphicsDevice.SetRenderTarget(m_rtNormal);
+ graphicsDevice.SetRenderTarget(nrmTarget);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(temp, Vector2.Zero, Color.White);
spriteBatch.End();
@@ -232,64 +231,133 @@ public bool RenderWorld(World world)
spriteBatch.End();
// Comp it all onto the dif buffer
- graphicsDevice.SetRenderTarget(finalResult);
+ graphicsDevice.SetRenderTarget(difTarget);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(temp, Vector2.Zero, Color.White);
spriteBatch.End();
-
- i++;
}
+ }
-
- // Render the lights!
-
+ private void RenderLights(List<Entity> lstEntities, SpriteBatch spriteBatch, RenderTarget2D difTarget, RenderTarget2D hgtTarget, RenderTarget2D nrmTarget, RenderTarget2D lgtTarget, RenderTarget2D temp)
+ {
- Texture2D lightPos = assetManager.GetTexture("sprites/test/lightpos");
- Texture2D lightBulb = assetManager.GetTexture("sprites/test/lightbulb");
Effect pointLight = assetManager.GetEffect("shaders/PointLight");
+
+ Vector2 viewportTrans = new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2);
+
graphicsDevice.SetRenderTarget(null);
- graphicsDevice.SetRenderTarget(m_rtDif2);
- graphicsDevice.Clear(Color.Transparent);
foreach (Entity ent in lstEntities)
{
if (ent.Type != EntityType.Light)
continue;
+ graphicsDevice.SetRenderTarget(temp);
+ graphicsDevice.Clear(Color.Transparent);
+
Entities.Lights.Light light = (Entities.Lights.Light)ent;
Vector2 pos = light.Position + m_posCamera + viewportTrans;
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, pointLight);
- graphicsDevice.Textures[1] = m_rtDepthStencil;
- graphicsDevice.Textures[2] = m_rtNormal;
+ graphicsDevice.Textures[1] = hgtTarget;
+ graphicsDevice.Textures[2] = nrmTarget;
pointLight.Parameters["intensity"].SetValue(light.Intensity);
pointLight.Parameters["range"].SetValue(light.Range);
pointLight.Parameters["color"].SetValue(new Vector4(light.Color.R / 255.0f, light.Color.G / 255.0f, light.Color.B / 255.0f, light.Color.A / 255.0f));
pointLight.Parameters["pos"].SetValue(new Vector3(pos.X, pos.Y, light.Z));
pointLight.Parameters["size"].SetValue(new Vector2(m_rtDif2.Width, m_rtDif2.Height));
-
+
Console.WriteLine("Applying the point light shader");
pointLight.Techniques[0].Passes[0].Apply();
- spriteBatch.Draw(finalResult, Vector2.Zero, Color.White);
+ spriteBatch.Draw(difTarget, Vector2.Zero, Color.White);
spriteBatch.End();
- if (m_bDrawLights)
+ graphicsDevice.SetRenderTarget(lgtTarget);
+ spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
+ spriteBatch.Draw(temp, Vector2.Zero, Color.White);
+ spriteBatch.End();
+ }
+ }
+
+ /// <summary>
+ /// Given a world object, the renderer draws it using the
+ /// XNA frameworks
+ /// </summary>
+ /// <param name="world">The world object to render</param>
+ /// <returns></returns>
+ public bool RenderWorld(World world)
+ {
+ world.SortEntities();
+ ClearScreen();
+
+ Effect shader = assetManager.GetEffect("shaders/SimpleSprite");
+ Effect channel = assetManager.GetEffect("shaders/ChannelRender");
+
+ Vector2 viewportTrans = new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2);
+
+ SpriteBatch spriteBatch = new SpriteBatch(graphicsDevice);
+
+ // Step 1: Generate a stencil of which pixels to render
+
+
+ // Draw the actual sprites
+ Color[] t = { Color.White, Color.Yellow, Color.Purple, Color.Blue };
+ int i = 0;
+
+ ClearRenderTargets();
+
+ List<Entity> lstEntities = world.GetEntities();
+
+ // First pass: Draw the entities
+ // This pass generates the dif, nrm and hgt textures
+ // Objects intersect as if in 3D
+ RenderEntities(lstEntities, spriteBatch, m_rtDif, m_rtDepthStencil, m_rtNormal, temp);
+
+ // Render the lights!
+ RenderLights(lstEntities, spriteBatch, m_rtDif, m_rtDepthStencil, m_rtNormal, m_rtLights, temp);
+
+ // Render the overlays!
+ Texture2D lightPos = assetManager.GetTexture("sprites/test/lightpos");
+ Texture2D lightBulb = assetManager.GetTexture("sprites/test/lightbulb");
+ graphicsDevice.SetRenderTarget(m_rtOverlay);
+ if (m_bDrawLights)
+ {
+ foreach (Entity ent in lstEntities)
{
-
+ if (ent.Type != EntityType.Light)
+ continue;
+
+ Entities.Lights.Light light = (Entities.Lights.Light)ent;
+ Vector2 pos = light.Position + m_posCamera + viewportTrans;
+
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(lightPos, pos, light.Color);
- spriteBatch.Draw(lightBulb, new Vector2(pos.X, pos.Y - light.Z/1.414f), light.Color);
+ spriteBatch.Draw(lightBulb, new Vector2(pos.X, pos.Y - light.Z / 1.414f), light.Color);
spriteBatch.End();
}
}
+ ////////////////////////////////////////////////////////////////////
+ // finalResult stores the final beauty pass
+ // That is, a combination of the dif and the lighting outputs
+ graphicsDevice.SetRenderTarget(finalResult);
+ graphicsDevice.Textures[1] = m_rtLights;
+
+ Effect combine = assetManager.GetEffect("shaders/Combine");
+
+ spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
+ combine.Techniques[0].Passes[0].Apply();
+ spriteBatch.Draw(m_rtDif, Vector2.Zero, Color.White);
+ spriteBatch.End();
+
+ //////////////////////////////////////////////////////////////////
graphicsDevice.SetRenderTarget(null);
graphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
switch(m_rstType)
{
case RenderSceneType.Diffuse:
- spriteBatch.Draw(m_rtDif2, Vector2.Zero, Color.White);
+ spriteBatch.Draw(m_rtDif, Vector2.Zero, Color.White);
break;
case RenderSceneType.Height:
@@ -299,7 +367,16 @@ public bool RenderWorld(World world)
case RenderSceneType.Normal:
spriteBatch.Draw(m_rtNormal, Vector2.Zero, Color.White);
break;
+
+ case RenderSceneType.Lights:
+ spriteBatch.Draw(m_rtLights, Vector2.Zero, Color.White);
+ break;
+
+ case RenderSceneType.Beauty:
+ spriteBatch.Draw(finalResult, Vector2.Zero, Color.White);
+ break;
}
+ spriteBatch.Draw(m_rtOverlay, Vector2.Zero, Color.White);
spriteBatch.End();
return true;
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.

0 comments on commit 979c8b2

Please sign in to comment.