more work on lighting abstraction

This commit is contained in:
UnknownShadow200 2016-11-29 21:25:39 +11:00
parent 2986773b51
commit 4d1ef1a65b
6 changed files with 64 additions and 50 deletions

View File

@ -28,17 +28,11 @@ namespace ClassicalSharp {
public FastColour(int r, int g, int b) {
A = 255; R = (byte)r; G = (byte)g; B = (byte)b;
}
public FastColour(int argb) {
A = (byte)(argb >> 24);
R = (byte)(argb >> 16);
G = (byte)(argb >> 8);
B = (byte)argb;
}
public FastColour(Color c) {
A = c.A; R = c.R; G = c.G; B = c.B;
}
public FastColour(Color c) { A = c.A; R = c.R; G = c.G; B = c.B; }
public Color ToColor() { return Color.FromArgb(A, R, G, B); }
/// <summary> Multiplies the RGB components of this instance by the
/// specified t parameter, where 0 ≤ t ≤ 1 </summary>
@ -71,25 +65,46 @@ namespace ClassicalSharp {
zSide = FastColour.Scale(normal, ShadeZ).Pack();
yBottom = FastColour.Scale(normal, ShadeYBottom).Pack();
}
public Color ToColor() {
return Color.FromArgb(A, R, G, B);
}
/// <summary> Packs this instance into a 32 bit integer, where A occupies
/// the highest 8 bits and B occupies the lowest 8 bits. </summary>
public int ToArgb() {
return A << 24 | R << 16 | G << 8 | B;
public int ToArgb() { return A << 24 | R << 16 | G << 8 | B; }
public static FastColour Argb(int c) {
FastColour col = default(FastColour);
col.A = (byte)(c >> 24);
col.R = (byte)(c >> 16);
col.G = (byte)(c >> 8);
col.B = (byte)c;
return col;
}
/// <summary> Packs this instance into a 32 bit integer, where A occupies
/// the highest 8 bits, and the order of RGB bytes is determined by the graphics API. </summary>
public int Pack() {
#if !USE_DX
return A << 24 | B << 16 | G << 8 | R;
#if USE_DX
return A << 24 | R << 16 | G << 8 | B;
#else
return A << 24 | R << 16 | G << 8 | B;
return A << 24 | B << 16 | G << 8 | R;
#endif
}
public static FastColour Unpack(int c) {
FastColour col = default(FastColour);
col.A = (byte)(c >> 24);
col.G = (byte)(c >> 8);
#if USE_DX
col.R = (byte)(c >> 16);
col.B = (byte)c;
#else
col.B = (byte)(c >> 16);
col.R = (byte)c;
#endif
return col;
}
public override bool Equals(object obj) {
return (obj is FastColour) && Equals((FastColour)obj);
}
@ -149,6 +164,7 @@ namespace ClassicalSharp {
public static FastColour Magenta = new FastColour(255, 0, 255);
public static FastColour Cyan = new FastColour(0, 255, 255);
public static bool TryParse(string input, out FastColour value) {
value = default(FastColour);
if (input == null || input.Length < 6) return false;

View File

@ -45,12 +45,17 @@ namespace ClassicalSharp.Map {
return lightH == short.MaxValue ? CalcHeightAt(x, height - 1, z, index) : lightH;
}
public override bool IsValidPos(int x, int y, int z) {
return x >= 0 && y >= 0 && z >= 0 &&
x < width && y < height && z < length;
public override bool IsLit(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0 ||
x >= width || y >= height || z >= length) return true;
return y > GetLightHeight(x, z);
}
public override bool IsLitNoCheck(int x, int y, int z) {
return y > GetLightHeight(x, z);
}
public override void UpdateLight(int x, int y, int z, byte oldBlock, byte newBlock) {
bool didBlock = info.BlocksLight[oldBlock];
bool nowBlocks = info.BlocksLight[newBlock];

View File

@ -19,32 +19,21 @@ namespace ClassicalSharp.Map {
/// <summary> Returns the y coordinate of the highest block that is fully not in sunlight. </summary>
/// <remarks> e.g. if cobblestone was at y = 5, this method would return 4. </remarks>
public abstract int GetLightHeight(int x, int z);
/// <summary> Returns whether the given world coordinates are contained
/// within the dimensions of the map. </summary>
public abstract bool IsValidPos(int x, int y, int z);
/// <summary> Updates the lighting for the block at that position, which may in turn affect other blocks. </summary>
public abstract void UpdateLight(int x, int y, int z, byte oldBlock, byte newBlock);
public abstract void UpdateLight(int x, int y, int z, byte oldBlock, byte newBlock);
/// <summary> Returns whether the given world coordinates are fully in sunlight. </summary>
public abstract bool IsLit(int x, int y, int z);
/// <summary> Returns whether the given world coordinates are fully in sunlight. </summary>
/// <remarks> Does not check whether the coordinates are inside the map. </remarks>
public abstract bool IsLitNoCheck(int x, int y, int z);
/// <summary> Returns whether the given world coordinates are fully not in sunlight. </summary>
public bool IsLit(int x, int y, int z) {
if (!IsValidPos(x, y, z)) return true;
return y > GetLightHeight(x, z);
}
/// <summary> Returns whether the given world coordinatse are fully not in sunlight. </summary>
public bool IsLit(Vector3I p) {
if (!IsValidPos(p.X, p.Y, p.Z)) return true;
return p.Y > GetLightHeight(p.X, p.Z);
}
/// <summary> Returns whether the given world coordinatse are fully not in sunlight. </summary>
/// <summary> Returns whether the given world coordinates are fully in sunlight. </summary>
public bool IsLit(Vector3 p) {
int x = Utils.Floor(p.X), y = Utils.Floor(p.Y), z = Utils.Floor(p.Z);
if (!IsValidPos(x, y, z)) return true;
return y > GetLightHeight(x, z);
return IsLit(x, y, z);
}

View File

@ -79,7 +79,7 @@ namespace ClassicalSharp.Network.Protocols {
const int fullAlpha = 0xFF << 24;
static FastColour ParseWomColour(string value, FastColour defaultCol) {
int argb;
return Int32.TryParse(value, out argb) ? new FastColour(argb | fullAlpha) : defaultCol;
return Int32.TryParse(value, out argb) ? FastColour.Argb(argb | fullAlpha) : defaultCol;
}
static string ReadLine(ref int start, string value) {

View File

@ -69,7 +69,7 @@ namespace ClassicalSharp.Renderers {
public void Render(double deltaTime) {
if (chunks == null) return;
ChunkSorter.UpdateSortOrder(game, updater);
updater.UpdateChunks(deltaTime);
updater.UpdateChunks(deltaTime);
RenderNormalChunks(deltaTime);
}

View File

@ -39,15 +39,17 @@ namespace ClassicalSharp.Singleplayer {
int x = index % map.Width;
int y = (index / map.Width) / map.Length;
int z = (index / map.Width) % map.Length;
if (y > lighting.GetLightHeight(x, z))
if (lighting.IsLitNoCheck(x, y, z))
game.UpdateBlock(x, y, z, Block.Grass);
}
void HandleGrass(int index, byte block) {
int x = index % map.Width;
int y = (index / map.Width) / map.Length;
int z = (index / map.Width) % map.Length;
if (y <= lighting.GetLightHeight(x, z))
int z = (index / map.Width) % map.Length;
if (!lighting.IsLitNoCheck(x, y, z))
game.UpdateBlock(x, y, z, Block.Dirt);
}
@ -55,7 +57,8 @@ namespace ClassicalSharp.Singleplayer {
int x = index % map.Width;
int y = (index / map.Width) / map.Length;
int z = (index / map.Width) % map.Length;
if (y <= lighting.GetLightHeight(x, z)) {
if (!lighting.IsLitNoCheck(x, y, z)) {
game.UpdateBlock(x, y, z, Block.Air);
physics.ActivateNeighbours(x, y, z, index);
return;
@ -73,7 +76,8 @@ namespace ClassicalSharp.Singleplayer {
int x = index % map.Width;
int y = (index / map.Width) / map.Length;
int z = (index / map.Width) % map.Length;
if (y > lighting.GetLightHeight(x, z)) {
if (lighting.IsLitNoCheck(x, y, z)) {
game.UpdateBlock(x, y, z, Block.Air);
physics.ActivateNeighbours(x, y, z, index);
return;