diff --git a/Blarg.GameFramework/Blarg.GameFramework.csproj b/Blarg.GameFramework/Blarg.GameFramework.csproj index 0b3cfb8..9fc4006 100644 --- a/Blarg.GameFramework/Blarg.GameFramework.csproj +++ b/Blarg.GameFramework/Blarg.GameFramework.csproj @@ -205,6 +205,8 @@ + + diff --git a/Blarg.GameFramework/TileMap/ChunkRenderer.cs b/Blarg.GameFramework/TileMap/ChunkRenderer.cs new file mode 100644 index 0000000..b57d9b9 --- /dev/null +++ b/Blarg.GameFramework/TileMap/ChunkRenderer.cs @@ -0,0 +1,44 @@ +using System; +using Blarg.GameFramework.Graphics; + +namespace Blarg.GameFramework.TileMap +{ + public class ChunkRenderer + { + RenderState _renderState = RenderState.Default; + BlendState _defaultBlendState = BlendState.Default; + BlendState _alphaBlendState = BlendState.AlphaBlend; + + public void Render(TileChunk chunk) + { + if (chunk.Mesh == null) + return; + + var texture = chunk.TileMap.TileMeshes.Atlas.Texture; + + _renderState.Apply(Framework.GraphicsDevice); + _defaultBlendState.Apply(Framework.GraphicsDevice); + Framework.GraphicsDevice.BindTexture(texture); + Framework.GraphicsDevice.BindVertexBuffer(chunk.Mesh); + Framework.GraphicsDevice.RenderTriangles(); + Framework.GraphicsDevice.UnbindVertexBuffer(); + } + + public void RenderAlpha(TileChunk chunk) + { + if (chunk.AlphaMesh == null) + return; + + var texture = chunk.TileMap.TileMeshes.Atlas.Texture; + + _renderState.Apply(Framework.GraphicsDevice); + _alphaBlendState.Apply(Framework.GraphicsDevice); + Framework.GraphicsDevice.BindTexture(texture); + Framework.GraphicsDevice.BindVertexBuffer(chunk.AlphaMesh); + Framework.GraphicsDevice.RenderTriangles(); + Framework.GraphicsDevice.UnbindVertexBuffer(); + + } + } +} + diff --git a/Blarg.GameFramework/TileMap/TileMapRenderer.cs b/Blarg.GameFramework/TileMap/TileMapRenderer.cs new file mode 100644 index 0000000..e39df77 --- /dev/null +++ b/Blarg.GameFramework/TileMap/TileMapRenderer.cs @@ -0,0 +1,78 @@ +using System; +using Blarg.GameFramework.Graphics; + +namespace Blarg.GameFramework.TileMap +{ + public class TileMapRenderer + { + ChunkRenderer _chunkRenderer = new ChunkRenderer(); + + public void Render(TileMap tileMap, Shader shader = null) + { + var graphicsDevice = Framework.GraphicsDevice; + + if (shader == null) + { + graphicsDevice.BindShader(Framework.GraphicsDevice.SimpleColorTextureShader); + graphicsDevice.SimpleColorTextureShader.SetModelViewMatrix(graphicsDevice.ViewContext.ModelViewMatrix); + graphicsDevice.SimpleColorTextureShader.SetProjectionMatrix(graphicsDevice.ViewContext.ProjectionMatrix); + } + else + { + graphicsDevice.BindShader(shader); + } + + for (int y = 0; y < tileMap.HeightInChunks; ++y) + { + for (int z = 0; z < tileMap.DepthInChunks; ++z) + { + for (int x = 0; x < tileMap.WidthInChunks; ++x) + { + var chunk = tileMap.GetChunk(x, y, z); + var bounds = chunk.Bounds; + if (graphicsDevice.ViewContext.Camera.Frustum.Test(ref bounds)) + _chunkRenderer.Render(chunk); + } + } + } + + graphicsDevice.UnbindShader(); + } + + public void RenderAlpha(TileMap tileMap, Shader shader = null) + { + var graphicsDevice = Framework.GraphicsDevice; + + if (shader == null) + { + graphicsDevice.BindShader(Framework.GraphicsDevice.SimpleColorTextureShader); + graphicsDevice.SimpleColorTextureShader.SetModelViewMatrix(graphicsDevice.ViewContext.ModelViewMatrix); + graphicsDevice.SimpleColorTextureShader.SetProjectionMatrix(graphicsDevice.ViewContext.ProjectionMatrix); + } + else + { + graphicsDevice.BindShader(shader); + } + + for (int y = 0; y < tileMap.HeightInChunks; ++y) + { + for (int z = 0; z < tileMap.DepthInChunks; ++z) + { + for (int x = 0; x < tileMap.WidthInChunks; ++x) + { + var chunk = tileMap.GetChunk(x, y, z); + if (chunk.AlphaMesh != null) + { + var bounds = chunk.Bounds; + if (graphicsDevice.ViewContext.Camera.Frustum.Test(ref bounds)) + _chunkRenderer.Render(chunk); + } + } + } + } + + graphicsDevice.UnbindShader(); + } + } +} +