add TileMap and Chunk renderers

This commit is contained in:
Gered 2013-08-25 21:50:27 -04:00
parent 03c7a83471
commit cb60306944
3 changed files with 124 additions and 0 deletions

View file

@ -205,6 +205,8 @@
<Compile Include="TileMap\TileChunk.cs" /> <Compile Include="TileMap\TileChunk.cs" />
<Compile Include="TileMap\ChunkVertexGenerator.cs" /> <Compile Include="TileMap\ChunkVertexGenerator.cs" />
<Compile Include="TileMap\Lighting\ITileMapLighter.cs" /> <Compile Include="TileMap\Lighting\ITileMapLighter.cs" />
<Compile Include="TileMap\TileMapRenderer.cs" />
<Compile Include="TileMap\ChunkRenderer.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<ItemGroup> <ItemGroup>

View file

@ -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();
}
}
}

View file

@ -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();
}
}
}