Skip to content

Commit

Permalink
Merge pull request ddevault#224 from mrpimpunicorn/master
Browse files Browse the repository at this point in the history
Fixed multiple issues with lighting
  • Loading branch information
ddevault committed Nov 24, 2015
2 parents 7e2d657 + b7603a8 commit 8255224
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 18 deletions.
95 changes: 88 additions & 7 deletions TrueCraft.Client/Rendering/BlockRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using Microsoft.Xna.Framework.Graphics;
using System.Linq;
using Microsoft.Xna.Framework;
using TrueCraft.Core.Logic;
using TrueCraft.API.Logic;
using System.Linq;
using TrueCraft.API.World;
using TrueCraft.Core.World;
using Coordinates3D = TrueCraft.API.Coordinates3D;

namespace TrueCraft.Client.Rendering
{
Expand Down Expand Up @@ -37,15 +38,26 @@ public virtual VertexPositionNormalColorTexture[] Render(BlockDescriptor descrip
texCoords,
texCoords + Vector2.UnitX
};

for (int i = 0; i < texture.Length; i++)
texture[i] *= new Vector2(16f / 256f);
return CreateUniformCube(offset, texture, faces, indiciesOffset, out indicies, Color.White, descriptor.BlockLight);

var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

return CreateUniformCube(offset, texture, faces, indiciesOffset, out indicies, Color.White, lighting);
}

public static VertexPositionNormalColorTexture[] CreateUniformCube(Vector3 offset, Vector2[] texture,
VisibleFaces faces, int indiciesOffset, out int[] indicies, Color color, int light = 15)
VisibleFaces faces, int indiciesOffset, out int[] indicies, Color color, int[] lighting = null)
{
faces = VisibleFaces.All; // Temporary
if (lighting == null)
lighting = DefaultLighting;

int totalFaces = 0;
uint f = (uint)faces;
Expand All @@ -68,9 +80,11 @@ public static VertexPositionNormalColorTexture[] CreateUniformCube(Vector3 offse
textureIndex += 4;
continue;
}
var lightColor = LightColor.ToVector3() * CubeBrightness[lighting[_side]];

var side = (CubeFace)_side;
var quad = CreateQuad(side, offset, texture, textureIndex % texture.Length, indiciesOffset,
out _indicies, new Color(color.ToVector3() * CubeBrightness[light]));
out _indicies, new Color(lightColor * color.ToVector3()));
Array.Copy(quad, 0, verticies, sidesSoFar * 4, 4);
Array.Copy(_indicies, 0, indicies, sidesSoFar * 6, 6);
textureIndex += 4;
Expand All @@ -95,15 +109,51 @@ protected static VertexPositionNormalColorTexture[] CreateQuad(CubeFace face, Ve
}
return quad;
}


#region Lighting

/// <summary>
/// The per-vertex light color to apply to blocks.
/// </summary>
protected static readonly Color LightColor =
new Color(245, 245, 225);

/// <summary>
/// The default lighting information for rendering a block;
/// i.e. when the lighting param to CreateUniformCube == null.
/// </summary>
protected static readonly int[] DefaultLighting =
new int[]
{
0, 0, 0,
0, 0, 0
};

/// <summary>
/// The per-face brightness modifier for lighting.
/// </summary>
protected static readonly float[] FaceBrightness =
new float[]
{
0.6f, 0.6f, // North / South
0.8f, 0.8f, // East / West
1.0f, 0.5f // Top / Bottom
};

/// <summary>
/// The offset coordinates used to get the position of a block for a face.
/// </summary>
protected static readonly Coordinates3D[] FaceCoords =
new Coordinates3D[]
{
Coordinates3D.South, Coordinates3D.North,
Coordinates3D.East, Coordinates3D.West,
Coordinates3D.Up, Coordinates3D.Down
};

/// <summary>
/// Maps a light level [0..15] to a brightness modifier for lighting.
/// </summary>
protected static readonly float[] CubeBrightness =
new float[]
{
Expand All @@ -113,6 +163,37 @@ protected static VertexPositionNormalColorTexture[] CreateQuad(CubeFace face, Ve
0.525f, 0.638f, 0.789f, 1.000f // [12..15]
};

/// <summary>
///
/// </summary>
/// <param name="chunk"></param>
/// <param name="coords"></param>
/// <returns></returns>
protected static int GetLight(IChunk chunk, Coordinates3D coords)
{
// Handle icon renderer.
if (chunk == null)
return 15;

// Handle top (and bottom) of the world.
if (coords.Y < 0)
return 0;
if (coords.Y >= Chunk.Height)
return 15;

// Handle coordinates outside the chunk.
if ((coords.X < 0) || (coords.X >= Chunk.Width) ||
(coords.Z < 0) || (coords.Z >= Chunk.Depth))
{
// TODO: Handle chunk boundaries properly.
return 0;
}

return Math.Min(chunk.GetBlockLight(coords) + chunk.GetSkyLight(coords), 15);
}

#endregion

protected enum CubeFace
{
PositiveZ = 0,
Expand Down
9 changes: 8 additions & 1 deletion TrueCraft.Client/Rendering/Blocks/CraftingTableRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ static CraftingTableRenderer()
public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
VisibleFaces faces, Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
return CreateUniformCube(offset, Texture, faces, indiciesOffset, out indicies, Color.White);
var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

return CreateUniformCube(offset, Texture, faces, indiciesOffset, out indicies, Color.White, lighting);
}
}
}
9 changes: 8 additions & 1 deletion TrueCraft.Client/Rendering/Blocks/FarmlandRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,16 @@ public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descri
var texture = DryTexture;
if (descriptor.Metadata == (byte)FarmlandBlock.MoistureLevel.Moist)
texture = MoistTexture;

var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

var overhead = new Vector3(0.5f, 0.5f, 0.5f);
var cube = CreateUniformCube(overhead, texture, faces, indiciesOffset, out indicies, Color.White);
var cube = CreateUniformCube(overhead, texture, faces, indiciesOffset, out indicies, Color.White, lighting);
for (int i = 0; i < cube.Length; i++)
{
if (cube[i].Position.Y > 0)
Expand Down
10 changes: 9 additions & 1 deletion TrueCraft.Client/Rendering/Blocks/GrassRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descri
texture = SnowTexture;
}
}
var cube = CreateUniformCube(offset, texture, faces, indiciesOffset, out indicies, Color.White);

var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

var cube = CreateUniformCube(offset, texture, faces, indiciesOffset, out indicies, Color.White, lighting);
// Apply biome colors to top of cube
for (int i = (int)(CubeFace.PositiveY) * 4; i < (int)(CubeFace.PositiveY) * 4 + 4; i++)
{
Expand Down
13 changes: 10 additions & 3 deletions TrueCraft.Client/Rendering/Blocks/LeavesRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,25 @@ static LeavesRenderer()
public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
VisibleFaces faces, Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

switch ((WoodBlock.WoodType)descriptor.Metadata)
{
case WoodBlock.WoodType.Spruce:
return CreateUniformCube(offset, SpruceTextures, VisibleFaces.All,
indiciesOffset, out indicies, GrassRenderer.BiomeColor);
indiciesOffset, out indicies, GrassRenderer.BiomeColor, lighting);
case WoodBlock.WoodType.Birch:
return CreateUniformCube(offset, BaseTextures, VisibleFaces.All,
indiciesOffset, out indicies, GrassRenderer.BiomeColor);
indiciesOffset, out indicies, GrassRenderer.BiomeColor, lighting);
case WoodBlock.WoodType.Oak:
default:
return CreateUniformCube(offset, BaseTextures, VisibleFaces.All,
indiciesOffset, out indicies, GrassRenderer.BiomeColor);
indiciesOffset, out indicies, GrassRenderer.BiomeColor, lighting);
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions TrueCraft.Client/Rendering/Blocks/LogRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,22 @@ static LogRenderer()
public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
VisibleFaces faces, Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

switch ((WoodBlock.WoodType)descriptor.Metadata)
{
case WoodBlock.WoodType.Spruce:
return CreateUniformCube(offset, SpruceTexture, faces, indiciesOffset, out indicies, Color.White);
return CreateUniformCube(offset, SpruceTexture, faces, indiciesOffset, out indicies, Color.White, lighting);
case WoodBlock.WoodType.Birch:
return CreateUniformCube(offset, BirchTexture, faces, indiciesOffset, out indicies, Color.White);
return CreateUniformCube(offset, BirchTexture, faces, indiciesOffset, out indicies, Color.White, lighting);
case WoodBlock.WoodType.Oak:
default:
return CreateUniformCube(offset, BaseTexture, faces, indiciesOffset, out indicies, Color.White);
return CreateUniformCube(offset, BaseTexture, faces, indiciesOffset, out indicies, Color.White, lighting);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion TrueCraft.Client/Rendering/Blocks/TNTRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ static TNTRenderer()
public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
VisibleFaces faces, Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
return CreateUniformCube(offset, Texture, faces, indiciesOffset, out indicies, Color.White);
var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

return CreateUniformCube(offset, Texture, faces, indiciesOffset, out indicies, Color.White, lighting);
}
}
}
9 changes: 8 additions & 1 deletion TrueCraft.Client/Rendering/Blocks/WaterRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ static WaterRenderer()
public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset,
VisibleFaces faces, Tuple<int, int> textureMap, int indiciesOffset, out int[] indicies)
{
var lighting = new int[6];
for (int i = 0; i < 6; i++)
{
var coords = (descriptor.Coordinates + FaceCoords[i]);
lighting[i] = GetLight(descriptor.Chunk, coords);
}

// TODO: Rest of water rendering (shape and level and so on)
var overhead = new Vector3(0.5f, 0.5f, 0.5f);
var cube = CreateUniformCube(overhead, Texture, faces, indiciesOffset, out indicies, Color.Blue);
var cube = CreateUniformCube(overhead, Texture, faces, indiciesOffset, out indicies, Color.Blue, lighting);
for (int i = 0; i < cube.Length; i++)
{
if (cube[i].Position.Y > 0)
Expand Down

0 comments on commit 8255224

Please sign in to comment.