Add common renderer for quad-based blocks

This also includes a derivative renderer for tall grass.
This commit is contained in:
Drew DeVault 2015-06-03 18:42:26 -06:00
parent 2994b7c65e
commit 0ae063456f
6 changed files with 185 additions and 2 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

@ -0,0 +1,60 @@
using System;
using TrueCraft.Core.Logic.Blocks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TrueCraft.API.Logic;
namespace TrueCraft.Client.Rendering.Blocks
{
public class TallGrassRenderer : FlatQuadRenderer
{
static TallGrassRenderer()
{
BlockRenderer.RegisterRenderer(TallGrassBlock.BlockID, new TallGrassRenderer());
BlockRenderer.RegisterRenderer(DeadBushBlock.BlockID, new TallGrassRenderer());
}
protected override Vector2 TextureMap { get { return new Vector2(7, 2); } }
protected Vector2 DeadBushTextureMap { get { return new Vector2(7, 3); } }
protected Vector2 FernTextureMap { get { return new Vector2(8, 3); } }
protected Vector2[] DeadBushTexture, FernTexture;
public TallGrassRenderer()
{
DeadBushTexture = new[]
{
DeadBushTextureMap + Vector2.UnitX + Vector2.UnitY,
DeadBushTextureMap + Vector2.UnitY,
DeadBushTextureMap,
DeadBushTextureMap + Vector2.UnitX,
};
FernTexture = new[]
{
FernTextureMap + Vector2.UnitX + Vector2.UnitY,
FernTextureMap + Vector2.UnitY,
FernTextureMap,
FernTextureMap + Vector2.UnitX,
};
for (int i = 0; i < Texture.Length; i++)
{
DeadBushTexture[i] *= new Vector2(16f / 256f);
FernTexture[i] *= new Vector2(16f / 256f);
}
}
public override VertexPositionNormalTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
switch ((TallGrassBlock.TallGrassType)descriptor.Metadata)
{
case TallGrassBlock.TallGrassType.DeadBush:
return RenderQuads(descriptor, offset, DeadBushTexture, indiciesOffset, out indicies);
case TallGrassBlock.TallGrassType.Fern:
return RenderQuads(descriptor, offset, FernTexture, indiciesOffset, out indicies);
case TallGrassBlock.TallGrassType.TallGrass:
default:
return RenderQuads(descriptor, offset, Texture, indiciesOffset, out indicies);
}
}
}
}

View File

@ -0,0 +1,114 @@
using System;
using TrueCraft.API.Logic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace TrueCraft.Client.Rendering
{
public abstract class FlatQuadRenderer : BlockRenderer
{
protected abstract Vector2 TextureMap { get; }
protected Vector2[] Texture;
protected FlatQuadRenderer()
{
Texture = new[]
{
TextureMap + Vector2.UnitX + Vector2.UnitY,
TextureMap + Vector2.UnitY,
TextureMap,
TextureMap + Vector2.UnitX,
};
for (int i = 0; i < Texture.Length; i++)
Texture[i] *= new Vector2(16f / 256f);
}
public override VertexPositionNormalTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
return RenderQuads(descriptor, offset, Texture, indiciesOffset, out indicies);
}
protected VertexPositionNormalTexture[] RenderQuads(BlockDescriptor descriptor, Vector3 offset,
Vector2[] textureMap, int indiciesOffset, out int[] indicies)
{
indicies = new int[6 * 4];
var verticies = new VertexPositionNormalTexture[4 * 4];
int[] _indicies;
int textureIndex = 0;
for (int side = 0; side < 4; side++)
{
var quad = CreateAngledQuad(side, offset, textureMap, textureIndex % textureMap.Length, indiciesOffset, out _indicies);
Array.Copy(quad, 0, verticies, side * 4, 4);
Array.Copy(_indicies, 0, indicies, side * 6, 6);
textureIndex += 4;
}
return verticies;
}
protected static VertexPositionNormalTexture[] CreateAngledQuad(int face, Vector3 offset, Vector2[] texture, int textureOffset,
int indiciesOffset, out int[] indicies)
{
indicies = new[] { 0, 1, 3, 1, 2, 3 };
for (int i = 0; i < indicies.Length; i++)
indicies[i] += (face * 4) + indiciesOffset;
var quad = new VertexPositionNormalTexture[4];
var unit = QuadMesh[face];
var normal = CubeNormals[face];
for (int i = 0; i < 4; i++)
{
quad[i] = new VertexPositionNormalTexture(offset + unit[i], normal, texture[textureOffset + i]);
}
return quad;
}
protected static readonly Vector3[] QuadNormals =
{
new Vector3(0, 0, 1),
new Vector3(0, 0, -1),
new Vector3(1, 0, 0),
new Vector3(-1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, -1, 0)
};
protected static readonly Vector3[][] QuadMesh;
static FlatQuadRenderer()
{
QuadMesh = new Vector3[4][];
QuadMesh[0] = new[]
{
new Vector3(0.5f, -0.5f, 0.5f),
new Vector3(-0.5f, -0.5f, -0.5f),
new Vector3(-0.5f, 0.5f, -0.5f),
new Vector3(0.5f, 0.5f, 0.5f)
};
QuadMesh[1] = new[]
{
new Vector3(-0.5f, -0.5f, -0.5f),
new Vector3(0.5f, -0.5f, 0.5f),
new Vector3(0.5f, 0.5f, 0.5f),
new Vector3(-0.5f, 0.5f, -0.5f)
};
QuadMesh[2] = new[]
{
new Vector3(-0.5f, -0.5f, 0.5f),
new Vector3(0.5f, -0.5f, -0.5f),
new Vector3(0.5f, 0.5f, -0.5f),
new Vector3(-0.5f, 0.5f, 0.5f)
};
QuadMesh[3] = new[]
{
new Vector3(0.5f, -0.5f, -0.5f),
new Vector3(-0.5f, -0.5f, 0.5f),
new Vector3(-0.5f, 0.5f, 0.5f),
new Vector3(0.5f, 0.5f, -0.5f)
};
}
}
}

View File

@ -109,6 +109,8 @@
<Compile Include="Rendering\Blocks\LogRenderer.cs" />
<Compile Include="Rendering\ChunkRenderer.cs" />
<Compile Include="Rendering\Blocks\LeavesRenderer.cs" />
<Compile Include="Rendering\Blocks\TallGrassRenderer.cs" />
<Compile Include="Rendering\FlatQuadRenderer.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@ -275,10 +275,10 @@ namespace TrueCraft.Client
Graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.BlendState = BlendState.NonPremultiplied;
int verticies = 0, chunks = 0;
GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
GraphicsDevice.DepthStencilState = new DepthStencilState { DepthBufferEnable = true };
for (int i = 0; i < ChunkMeshes.Count; i++)
{
if (CameraView.Intersects(ChunkMeshes[i].BoundingBox) && !ChunkMeshes[i].Empty)

View File

@ -7,6 +7,13 @@ namespace TrueCraft.Core.Logic.Blocks
{
public class TallGrassBlock : BlockProvider
{
public enum TallGrassType
{
DeadBush = 0,
TallGrass = 1,
Fern = 2
}
public static readonly byte BlockID = 0x1F;
public override byte ID { get { return 0x1F; } }