Fix torch rendering, track the highlighted face

The second bit will allow the client to start doing things like placing
blocks.
This commit is contained in:
Drew DeVault 2015-09-27 21:00:32 -04:00
parent 10a84b94ee
commit 2eaaf219a8
7 changed files with 62 additions and 21 deletions

View File

@ -77,10 +77,9 @@ namespace TrueCraft.API
/// <summary> /// <summary>
/// Returns the distance along the ray where it intersects the specified bounding box, if it intersects at all. /// Returns the distance along the ray where it intersects the specified bounding box, if it intersects at all.
/// </summary> /// </summary>
/// <param name="box">The bounding box to check intersection with.</param> public double? Intersects(BoundingBox box, out BlockFace face)
/// <returns></returns>
public double? Intersects(BoundingBox box)
{ {
face = BlockFace.PositiveY;
//first test if start in box //first test if start in box
if (Position.X >= box.Min.X if (Position.X >= box.Min.X
&& Position.X <= box.Max.X && Position.X <= box.Max.X
@ -131,6 +130,12 @@ namespace TrueCraft.API
coord = Position.Y + maxT.X * Direction.Y; coord = Position.Y + maxT.X * Direction.Y;
if (coord < box.Min.Y || coord > box.Max.Y) if (coord < box.Min.Y || coord > box.Max.Y)
return null; return null;
if (Position.X < box.Min.X)
face = BlockFace.NegativeX;
else if (Position.X > box.Max.X)
face = BlockFace.PositiveX;
return maxT.X; return maxT.X;
} }
if (maxT.Y > maxT.X && maxT.Y > maxT.Z) if (maxT.Y > maxT.X && maxT.Y > maxT.Z)
@ -145,6 +150,12 @@ namespace TrueCraft.API
coord = Position.X + maxT.Y * Direction.X; coord = Position.X + maxT.Y * Direction.X;
if (coord < box.Min.X || coord > box.Max.X) if (coord < box.Min.X || coord > box.Max.X)
return null; return null;
if (Position.Y < box.Min.Y)
face = BlockFace.NegativeY;
else if (Position.Y > box.Max.Y)
face = BlockFace.PositiveY;
return maxT.Y; return maxT.Y;
} }
else //Z else //Z
@ -159,6 +170,12 @@ namespace TrueCraft.API
coord = Position.Y + maxT.Z * Direction.Y; coord = Position.Y + maxT.Z * Direction.Y;
if (coord < box.Min.Y || coord > box.Max.Y) if (coord < box.Min.Y || coord > box.Max.Y)
return null; return null;
if (Position.Z < box.Min.Z)
face = BlockFace.NegativeZ;
else if (Position.Z > box.Max.Z)
face = BlockFace.PositiveZ;
return maxT.Z; return maxT.Z;
} }
} }

View File

@ -3,6 +3,8 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using TrueCraft.Client.Input; using TrueCraft.Client.Input;
using TrueCraft.Client.Rendering; using TrueCraft.Client.Rendering;
using TrueCraft.API;
using System;
namespace TrueCraft.Client.Modules namespace TrueCraft.Client.Modules
{ {
@ -20,6 +22,9 @@ namespace TrueCraft.Client.Modules
Game = game; Game = game;
Font = font; Font = font;
SpriteBatch = new SpriteBatch(Game.GraphicsDevice); SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
#if DEBUG
Enabled = true;
#endif
} }
public bool KeyDown(GameTime gameTime, KeyboardKeyEventArgs e) public bool KeyDown(GameTime gameTime, KeyboardKeyEventArgs e)
@ -63,25 +68,38 @@ namespace TrueCraft.Client.Modules
const int yOffset = 25; const int yOffset = 25;
SpriteBatch.Begin(); SpriteBatch.Begin();
Font.DrawText(SpriteBatch, xOrigin, yOrigin, string.Format("§lRunning at {0}{1} FPS", Font.DrawText(SpriteBatch, xOrigin, yOrigin, string.Format(
GetFPSColor(fps), fps), 1); ChatFormat.Bold + "Running at {0}{1} FPS", GetFPSColor(fps), fps));
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 1), string.Format("§o{0} vertices, {1} indicies",
Mesh.VerticiesRendered, Mesh.IndiciesRendered), 1); 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), 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), Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 3),
string.Format("§o<{0:N2}, {1:N2}, {2:N2}>", string.Format(ChatFormat.Italic + "<{0:N2}, {1:N2}, {2:N2}>",
Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z), 1); 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(); SpriteBatch.End();
} }
private string GetFPSColor(int fps) private string GetFPSColor(int fps)
{ {
if (fps <= 16) if (fps <= 16)
return "§c"; return ChatColor.Red;
if (fps <= 32) if (fps <= 32)
return "§e"; return ChatColor.Yellow;
return "§a"; return ChatColor.BrightGreen;
} }
} }
} }

View File

@ -13,7 +13,6 @@ namespace TrueCraft.Client.Modules
{ {
public TrueCraftGame Game { get; set; } public TrueCraftGame Game { get; set; }
private Coordinates3D HighlightedBlock { get; set; }
private BasicEffect HighlightEffect { get; set; } private BasicEffect HighlightEffect { get; set; }
private static readonly VertexPositionColor[] CubeVerticies; private static readonly VertexPositionColor[] CubeVerticies;
private static readonly short[] CubeIndicies; private static readonly short[] CubeIndicies;
@ -59,10 +58,11 @@ namespace TrueCraft.Client.Modules
Game.BlockRepository, TrueCraftGame.Reach, TrueCraftGame.Reach + 2); Game.BlockRepository, TrueCraftGame.Reach, TrueCraftGame.Reach + 2);
if (cast == null) if (cast == null)
HighlightedBlock = -Coordinates3D.One; Game.HighlightedBlock = -Coordinates3D.One;
else else
{ {
HighlightedBlock = cast.Item1; Game.HighlightedBlock = cast.Item1;
Game.HighlightedBlockFace = cast.Item2;
HighlightEffect.World = HighlightEffect.World =
Matrix.CreateTranslation(new XVector3(-0.5f)) * Matrix.CreateTranslation(new XVector3(-0.5f)) *
Matrix.CreateScale(1.01f) * Matrix.CreateScale(1.01f) *
@ -75,7 +75,7 @@ namespace TrueCraft.Client.Modules
{ {
Game.Camera.ApplyTo(HighlightEffect); Game.Camera.ApplyTo(HighlightEffect);
if (HighlightedBlock != -Coordinates3D.One) if (Game.HighlightedBlock != -Coordinates3D.One)
{ {
foreach (var pass in HighlightEffect.CurrentTechnique.Passes) foreach (var pass in HighlightEffect.CurrentTechnique.Passes)
{ {

View File

@ -55,7 +55,8 @@ namespace TrueCraft.Client.Rendering.Blocks
{ {
var overhead = new Vector3(0.5f, 0.5f, 0.5f); var overhead = new Vector3(0.5f, 0.5f, 0.5f);
var centerized = new Vector3(7f / 16f, 0, 7f / 16f); 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++) for (int i = 0; i < cube.Length; i++)
{ {
cube[i].Position.X *= 1f / 8f; cube[i].Position.X *= 1f / 8f;
@ -100,7 +101,6 @@ namespace TrueCraft.Client.Rendering.Blocks
cube[i].Position += offset; cube[i].Position += offset;
cube[i].Position += centerized; cube[i].Position += centerized;
cube[i].Position -= overhead;
} }
return cube; return cube;
} }

View File

@ -48,6 +48,7 @@
<DebugType> <DebugType>
</DebugType> </DebugType>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DefineConstants>$(DefineConstants);DEBUG</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
</PropertyGroup> </PropertyGroup>

View File

@ -31,6 +31,8 @@ namespace TrueCraft.Client
public double Bobbing { get; set; } public double Bobbing { get; set; }
public ChunkModule ChunkModule { get; set; } public ChunkModule ChunkModule { get; set; }
public float ScaleFactor { get; set; } public float ScaleFactor { get; set; }
public Coordinates3D HighlightedBlock { get; set; }
public BlockFace HighlightedBlockFace { get; set; }
private List<IGameplayModule> Modules { get; set; } private List<IGameplayModule> Modules { get; set; }
private SpriteBatch SpriteBatch { get; set; } private SpriteBatch SpriteBatch { get; set; }

View File

@ -21,6 +21,7 @@ namespace TrueCraft.Client
double min = negmax * 2; double min = negmax * 2;
var pick = -Coordinates3D.One; var pick = -Coordinates3D.One;
var face = BlockFace.PositiveY;
for (int x = -posmax; x <= posmax; x++) for (int x = -posmax; x <= posmax; x++)
{ {
for (int y = -negmax; y <= posmax; y++) for (int y = -negmax; y <= posmax; y++)
@ -37,11 +38,13 @@ namespace TrueCraft.Client
var box = provider.BoundingBox; var box = provider.BoundingBox;
if (box != null) 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) if (distance != null && distance.Value < min)
{ {
min = distance.Value; min = distance.Value;
pick = coords; pick = coords;
face = _face;
} }
} }
} }
@ -50,7 +53,7 @@ namespace TrueCraft.Client
} }
if (pick == -Coordinates3D.One) if (pick == -Coordinates3D.One)
return null; return null;
return new Tuple<Coordinates3D, BlockFace>(pick, BlockFace.PositiveY); return new Tuple<Coordinates3D, BlockFace>(pick, face);
} }
} }
} }