From 2eaaf219a8e056e566e957fc476cbb209f85d9ac Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 27 Sep 2015 21:00:32 -0400 Subject: [PATCH] Fix torch rendering, track the highlighted face The second bit will allow the client to start doing things like placing blocks. --- TrueCraft.API/Ray.cs | 23 +++++++++-- TrueCraft.Client/Modules/DebugInfoModule.cs | 38 ++++++++++++++----- TrueCraft.Client/Modules/HighlightModule.cs | 8 ++-- .../Rendering/Blocks/TorchRenderer.cs | 4 +- TrueCraft.Client/TrueCraft.Client.csproj | 1 + TrueCraft.Client/TrueCraftGame.cs | 2 + TrueCraft.Client/VoxelCast.cs | 7 +++- 7 files changed, 62 insertions(+), 21 deletions(-) diff --git a/TrueCraft.API/Ray.cs b/TrueCraft.API/Ray.cs index 2da021e..57e06c3 100644 --- a/TrueCraft.API/Ray.cs +++ b/TrueCraft.API/Ray.cs @@ -77,10 +77,9 @@ namespace TrueCraft.API /// /// Returns the distance along the ray where it intersects the specified bounding box, if it intersects at all. /// - /// The bounding box to check intersection with. - /// - public double? Intersects(BoundingBox box) + public double? Intersects(BoundingBox box, out BlockFace face) { + face = BlockFace.PositiveY; //first test if start in box if (Position.X >= box.Min.X && Position.X <= box.Max.X @@ -131,6 +130,12 @@ namespace TrueCraft.API coord = Position.Y + maxT.X * Direction.Y; if (coord < box.Min.Y || coord > box.Max.Y) return null; + + if (Position.X < box.Min.X) + face = BlockFace.NegativeX; + else if (Position.X > box.Max.X) + face = BlockFace.PositiveX; + return maxT.X; } if (maxT.Y > maxT.X && maxT.Y > maxT.Z) @@ -145,6 +150,12 @@ namespace TrueCraft.API coord = Position.X + maxT.Y * Direction.X; if (coord < box.Min.X || coord > box.Max.X) return null; + + if (Position.Y < box.Min.Y) + face = BlockFace.NegativeY; + else if (Position.Y > box.Max.Y) + face = BlockFace.PositiveY; + return maxT.Y; } else //Z @@ -159,6 +170,12 @@ namespace TrueCraft.API coord = Position.Y + maxT.Z * Direction.Y; if (coord < box.Min.Y || coord > box.Max.Y) return null; + + if (Position.Z < box.Min.Z) + face = BlockFace.NegativeZ; + else if (Position.Z > box.Max.Z) + face = BlockFace.PositiveZ; + return maxT.Z; } } diff --git a/TrueCraft.Client/Modules/DebugInfoModule.cs b/TrueCraft.Client/Modules/DebugInfoModule.cs index ceedc80..2d8fa31 100644 --- a/TrueCraft.Client/Modules/DebugInfoModule.cs +++ b/TrueCraft.Client/Modules/DebugInfoModule.cs @@ -3,6 +3,8 @@ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using TrueCraft.Client.Input; using TrueCraft.Client.Rendering; +using TrueCraft.API; +using System; namespace TrueCraft.Client.Modules { @@ -20,6 +22,9 @@ namespace TrueCraft.Client.Modules Game = game; Font = font; SpriteBatch = new SpriteBatch(Game.GraphicsDevice); +#if DEBUG + Enabled = true; +#endif } public bool KeyDown(GameTime gameTime, KeyboardKeyEventArgs e) @@ -63,25 +68,38 @@ namespace TrueCraft.Client.Modules const int yOffset = 25; SpriteBatch.Begin(); - Font.DrawText(SpriteBatch, xOrigin, yOrigin, string.Format("§lRunning at {0}{1} FPS", - GetFPSColor(fps), fps), 1); - Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 1), string.Format("§o{0} vertices, {1} indicies", - Mesh.VerticiesRendered, Mesh.IndiciesRendered), 1); + Font.DrawText(SpriteBatch, xOrigin, yOrigin, string.Format( + ChatFormat.Bold + "Running at {0}{1} FPS", GetFPSColor(fps), fps)); + + Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 1), + string.Format(ChatFormat.Italic + "{0} vertices, {1} indicies", + Mesh.VerticiesRendered, Mesh.IndiciesRendered)); + Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 2), - string.Format("§o{0} chunks", Game.ChunkModule.ChunksRendered), 1); + string.Format(ChatFormat.Italic + "{0} chunks", Game.ChunkModule.ChunksRendered)); + Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 3), - string.Format("§o<{0:N2}, {1:N2}, {2:N2}>", - Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z), 1); + string.Format(ChatFormat.Italic + "<{0:N2}, {1:N2}, {2:N2}>", + Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z)); + + Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 3), + string.Format(ChatFormat.Italic + "<{0:N2}, {1:N2}, {2:N2}>", + Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z)); + + Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 4), + string.Format(ChatColor.Gray + "Looking at {0} ({1})", Game.HighlightedBlock, + Enum.GetName(typeof(BlockFace), Game.HighlightedBlockFace))); + SpriteBatch.End(); } private string GetFPSColor(int fps) { if (fps <= 16) - return "§c"; + return ChatColor.Red; if (fps <= 32) - return "§e"; - return "§a"; + return ChatColor.Yellow; + return ChatColor.BrightGreen; } } } diff --git a/TrueCraft.Client/Modules/HighlightModule.cs b/TrueCraft.Client/Modules/HighlightModule.cs index ff02e84..a0bec36 100644 --- a/TrueCraft.Client/Modules/HighlightModule.cs +++ b/TrueCraft.Client/Modules/HighlightModule.cs @@ -13,7 +13,6 @@ namespace TrueCraft.Client.Modules { public TrueCraftGame Game { get; set; } - private Coordinates3D HighlightedBlock { get; set; } private BasicEffect HighlightEffect { get; set; } private static readonly VertexPositionColor[] CubeVerticies; private static readonly short[] CubeIndicies; @@ -59,10 +58,11 @@ namespace TrueCraft.Client.Modules Game.BlockRepository, TrueCraftGame.Reach, TrueCraftGame.Reach + 2); if (cast == null) - HighlightedBlock = -Coordinates3D.One; + Game.HighlightedBlock = -Coordinates3D.One; else { - HighlightedBlock = cast.Item1; + Game.HighlightedBlock = cast.Item1; + Game.HighlightedBlockFace = cast.Item2; HighlightEffect.World = Matrix.CreateTranslation(new XVector3(-0.5f)) * Matrix.CreateScale(1.01f) * @@ -75,7 +75,7 @@ namespace TrueCraft.Client.Modules { Game.Camera.ApplyTo(HighlightEffect); - if (HighlightedBlock != -Coordinates3D.One) + if (Game.HighlightedBlock != -Coordinates3D.One) { foreach (var pass in HighlightEffect.CurrentTechnique.Passes) { diff --git a/TrueCraft.Client/Rendering/Blocks/TorchRenderer.cs b/TrueCraft.Client/Rendering/Blocks/TorchRenderer.cs index 21ad0d4..ff79b37 100644 --- a/TrueCraft.Client/Rendering/Blocks/TorchRenderer.cs +++ b/TrueCraft.Client/Rendering/Blocks/TorchRenderer.cs @@ -55,7 +55,8 @@ namespace TrueCraft.Client.Rendering.Blocks { var overhead = new Vector3(0.5f, 0.5f, 0.5f); var centerized = new Vector3(7f / 16f, 0, 7f / 16f); - var cube = CreateUniformCube(overhead, Texture, VisibleFaces.All, indiciesOffset, out indicies, Color.White); + var cube = CreateUniformCube(Vector3.Zero, Texture, VisibleFaces.All, + indiciesOffset, out indicies, Color.White); for (int i = 0; i < cube.Length; i++) { cube[i].Position.X *= 1f / 8f; @@ -100,7 +101,6 @@ namespace TrueCraft.Client.Rendering.Blocks cube[i].Position += offset; cube[i].Position += centerized; - cube[i].Position -= overhead; } return cube; } diff --git a/TrueCraft.Client/TrueCraft.Client.csproj b/TrueCraft.Client/TrueCraft.Client.csproj index 3655bf5..164416e 100644 --- a/TrueCraft.Client/TrueCraft.Client.csproj +++ b/TrueCraft.Client/TrueCraft.Client.csproj @@ -48,6 +48,7 @@ true + $(DefineConstants);DEBUG diff --git a/TrueCraft.Client/TrueCraftGame.cs b/TrueCraft.Client/TrueCraftGame.cs index e8a827c..8a809c7 100644 --- a/TrueCraft.Client/TrueCraftGame.cs +++ b/TrueCraft.Client/TrueCraftGame.cs @@ -31,6 +31,8 @@ namespace TrueCraft.Client public double Bobbing { get; set; } public ChunkModule ChunkModule { get; set; } public float ScaleFactor { get; set; } + public Coordinates3D HighlightedBlock { get; set; } + public BlockFace HighlightedBlockFace { get; set; } private List Modules { get; set; } private SpriteBatch SpriteBatch { get; set; } diff --git a/TrueCraft.Client/VoxelCast.cs b/TrueCraft.Client/VoxelCast.cs index ae5279b..f9e410b 100644 --- a/TrueCraft.Client/VoxelCast.cs +++ b/TrueCraft.Client/VoxelCast.cs @@ -21,6 +21,7 @@ namespace TrueCraft.Client double min = negmax * 2; var pick = -Coordinates3D.One; + var face = BlockFace.PositiveY; for (int x = -posmax; x <= posmax; x++) { for (int y = -negmax; y <= posmax; y++) @@ -37,11 +38,13 @@ namespace TrueCraft.Client var box = provider.BoundingBox; if (box != null) { - var distance = ray.Intersects(box.Value.OffsetBy(coords)); + BlockFace _face; + var distance = ray.Intersects(box.Value.OffsetBy(coords), out _face); if (distance != null && distance.Value < min) { min = distance.Value; pick = coords; + face = _face; } } } @@ -50,7 +53,7 @@ namespace TrueCraft.Client } if (pick == -Coordinates3D.One) return null; - return new Tuple(pick, BlockFace.PositiveY); + return new Tuple(pick, face); } } }