diff --git a/TrueCraft.Client/Content/terrain.png b/TrueCraft.Client/Content/terrain.png index 76671ef..a412111 100644 Binary files a/TrueCraft.Client/Content/terrain.png and b/TrueCraft.Client/Content/terrain.png differ diff --git a/TrueCraft.Client/Rendering/Blocks/TallGrassRenderer.cs b/TrueCraft.Client/Rendering/Blocks/TallGrassRenderer.cs new file mode 100644 index 0000000..3c07cc1 --- /dev/null +++ b/TrueCraft.Client/Rendering/Blocks/TallGrassRenderer.cs @@ -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 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); + } + } + } +} \ No newline at end of file diff --git a/TrueCraft.Client/Rendering/FlatQuadRenderer.cs b/TrueCraft.Client/Rendering/FlatQuadRenderer.cs new file mode 100644 index 0000000..f82197a --- /dev/null +++ b/TrueCraft.Client/Rendering/FlatQuadRenderer.cs @@ -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 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) + }; + } + } +} \ No newline at end of file diff --git a/TrueCraft.Client/TrueCraft.Client.csproj b/TrueCraft.Client/TrueCraft.Client.csproj index 435fd9a..1638537 100644 --- a/TrueCraft.Client/TrueCraft.Client.csproj +++ b/TrueCraft.Client/TrueCraft.Client.csproj @@ -109,6 +109,8 @@ + + diff --git a/TrueCraft.Client/TrueCraftGame.cs b/TrueCraft.Client/TrueCraftGame.cs index 47dbeed..34eaf25 100644 --- a/TrueCraft.Client/TrueCraftGame.cs +++ b/TrueCraft.Client/TrueCraftGame.cs @@ -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) diff --git a/TrueCraft.Core/Logic/Blocks/TallGrassBlock.cs b/TrueCraft.Core/Logic/Blocks/TallGrassBlock.cs index 25e9ff6..cd7d943 100644 --- a/TrueCraft.Core/Logic/Blocks/TallGrassBlock.cs +++ b/TrueCraft.Core/Logic/Blocks/TallGrassBlock.cs @@ -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; } }