[ChunkBuilder] Smooth lighting fixed on back faces. Code refactored a bit. [recipes.xml] Slab recipe added.

This commit is contained in:
Quentin Bazin 2018-07-07 16:24:35 +02:00
parent f4f313acea
commit 3104cf9cfb
3 changed files with 62 additions and 84 deletions

View File

@ -33,11 +33,13 @@ class ChunkBuilder {
private:
void addFace(u8 x, u8 y, u8 z, u8 i, const Chunk &chunk, const Block *block, const Block *surroundingBlock);
float getAverageTorchlight(u8 x, u8 y, u8 z, s8 offsetX, s8 offsetY, s8 offsetZ, const Chunk &chunk);
float getAverageSunlight(u8 x, u8 y, u8 z, s8 offsetX, s8 offsetY, s8 offsetZ, const Chunk &chunk);
enum class Light {
Sun,
Torch
};
float getSunlightForVertex(u8 x, u8 y, u8 z, u8 i, u8 j, const Chunk &chunk);
float getTorchlightForVertex(u8 x, u8 y, u8 z, u8 i, u8 j, const Chunk &chunk);
float getAverageLight(Light light, u8 x, u8 y, u8 z, s8 offsetX, s8 offsetY, s8 offsetZ, const Chunk &chunk);
float getLightForVertex(Light light, u8 x, u8 y, u8 z, u8 i, u8 j, const Chunk &chunk);
std::array<std::vector<Vertex>, layers> m_vertices;

View File

@ -145,6 +145,14 @@
<result item="14" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="###" />
<key char="#" item="11" />
<result item="16" amount="6" />
</recipe>
<recipe type="smelt">
<input id="15" amount="1" />
<output id="39" amount="1" />

View File

@ -162,8 +162,8 @@ void ChunkBuilder::addFace(u8 x, u8 y, u8 z, u8 i, const Chunk &chunk, const Blo
// else
if (Config::isSmoothLightingEnabled) {
vertex.lightValue[0] = getSunlightForVertex(x, y, z, i, j, chunk);
vertex.lightValue[1] = getTorchlightForVertex(x, y, z, i, j, chunk);
vertex.lightValue[0] = getLightForVertex(Light::Sun, x, y, z, i, j, chunk);
vertex.lightValue[1] = getLightForVertex(Light::Torch, x, y, z, i, j, chunk);
}
else {
vertex.lightValue[0] = chunk.lightmap().getSunlight(x, y, z);
@ -181,99 +181,67 @@ void ChunkBuilder::addFace(u8 x, u8 y, u8 z, u8 i, const Chunk &chunk, const Blo
}
}
float ChunkBuilder::getAverageSunlight(u8 x, u8 y, u8 z, s8 offsetX, s8 offsetY, s8 offsetZ, const Chunk &chunk) {
return (chunk.lightmap().getSunlight(x, y + offsetY, z)
+ chunk.lightmap().getSunlight(x + offsetX, y + offsetY, z)
+ chunk.lightmap().getSunlight(x, y + offsetY, z + offsetZ)
+ chunk.lightmap().getSunlight(x + offsetX, y + offsetY, z + offsetZ)) / 4;
float ChunkBuilder::getAverageLight(Light light, u8 x, u8 y, u8 z, s8 offsetX, s8 offsetY, s8 offsetZ, const Chunk &chunk) {
if (light == Light::Sun)
return (chunk.lightmap().getSunlight(x, y + offsetY, z)
+ chunk.lightmap().getSunlight(x + offsetX, y + offsetY, z)
+ chunk.lightmap().getSunlight(x, y + offsetY, z + offsetZ)
+ chunk.lightmap().getSunlight(x + offsetX, y + offsetY, z + offsetZ)) / 4;
else
return (chunk.lightmap().getTorchlight(x, y + offsetY, z)
+ chunk.lightmap().getTorchlight(x + offsetX, y + offsetY, z)
+ chunk.lightmap().getTorchlight(x, y + offsetY, z + offsetZ)
+ chunk.lightmap().getTorchlight(x + offsetX, y + offsetY, z + offsetZ)) / 4;
}
float ChunkBuilder::getAverageTorchlight(u8 x, u8 y, u8 z, s8 offsetX, s8 offsetY, s8 offsetZ, const Chunk &chunk) {
return (chunk.lightmap().getTorchlight(x, y + offsetY, z)
+ chunk.lightmap().getTorchlight(x + offsetX, y + offsetY, z)
+ chunk.lightmap().getTorchlight(x, y + offsetY, z + offsetZ)
+ chunk.lightmap().getTorchlight(x + offsetX, y + offsetY, z + offsetZ)) / 4;
}
float ChunkBuilder::getLightForVertex(Light light, u8 x, u8 y, u8 z, u8 i, u8 j, const Chunk &chunk) {
float lightValues[4] = {
getAverageLight(light, x, y, z, -1, 0, -1, chunk),
getAverageLight(light, x, y, z, -1, 0, 1, chunk),
getAverageLight(light, x, y, z, 1, 0, -1, chunk),
getAverageLight(light, x, y, z, 1, 0, 1, chunk),
};
float ChunkBuilder::getSunlightForVertex(u8 x, u8 y, u8 z, u8 i, u8 j, const Chunk &chunk) {
if (i == Face::Left) {
if (j == 0) return getAverageSunlight(x, y, z, -1, 0, -1, chunk);
else if (j == 1) return getAverageSunlight(x, y, z, -1, 0, 1, chunk);
else if (j == 2) return getAverageSunlight(x, y, z, -1, 0, 1, chunk);
else if (j == 3) return getAverageSunlight(x, y, z, -1, 0, -1, chunk);
if (j == 0) return lightValues[0];
else if (j == 1) return lightValues[1];
else if (j == 2) return lightValues[1];
else if (j == 3) return lightValues[0];
}
else if (i == Face::Front) {
if (j == 0) return getAverageSunlight(x, y, z, 1, 0, -1, chunk);
else if (j == 1) return getAverageSunlight(x, y, z, -1, 0, -1, chunk);
else if (j == 2) return getAverageSunlight(x, y, z, -1, 0, -1, chunk);
else if (j == 3) return getAverageSunlight(x, y, z, 1, 0, -1, chunk);
if (j == 0) return lightValues[2];
else if (j == 1) return lightValues[0];
else if (j == 2) return lightValues[0];
else if (j == 3) return lightValues[2];
}
else if (i == Face::Top) {
if (j == 0) return getAverageSunlight(x, y, z, -1, 0, 1, chunk);
else if (j == 1) return getAverageSunlight(x, y, z, 1, 0, 1, chunk);
else if (j == 2) return getAverageSunlight(x, y, z, 1, 0, -1, chunk);
else if (j == 3) return getAverageSunlight(x, y, z, -1, 0, -1, chunk);
if (j == 0) return lightValues[1];
else if (j == 1) return lightValues[3];
else if (j == 2) return lightValues[2];
else if (j == 3) return lightValues[0];
}
else if (i == Face::Right) {
if (j == 0) return getAverageSunlight(x, y, z, 1, 0, 1, chunk);
else if (j == 1) return getAverageSunlight(x, y, z, 1, 0, -1, chunk);
else if (j == 2) return getAverageSunlight(x, y, z, 1, 0, -1, chunk);
else if (j == 3) return getAverageSunlight(x, y, z, 1, 0, 1, chunk);
if (j == 0) return lightValues[3];
else if (j == 1) return lightValues[2];
else if (j == 2) return lightValues[2];
else if (j == 3) return lightValues[3];
}
else if (i == Face::Back) {
if (j == 0) return getAverageSunlight(x, y, z, -1, 0, 1, chunk);
else if (j == 1) return getAverageSunlight(x, y, z, -1, 0, 1, chunk);
else if (j == 2) return getAverageSunlight(x, y, z, 1, 0, 1, chunk);
else if (j == 3) return getAverageSunlight(x, y, z, 1, 0, 1, chunk);
if (j == 0) return lightValues[1];
else if (j == 1) return lightValues[3];
else if (j == 2) return lightValues[3];
else if (j == 3) return lightValues[1];
}
else if (i == Face::Bottom) {
if (j == 0) return getAverageSunlight(x, y, z, -1, 0, -1, chunk);
else if (j == 1) return getAverageSunlight(x, y, z, 1, 0, -1, chunk);
else if (j == 2) return getAverageSunlight(x, y, z, 1, 0, 1, chunk);
else if (j == 3) return getAverageSunlight(x, y, z, -1, 0, 1, chunk);
if (j == 0) return lightValues[0];
else if (j == 1) return lightValues[2];
else if (j == 2) return lightValues[3];
else if (j == 3) return lightValues[1];
}
return chunk.lightmap().getSunlight(x, y, z);
}
float ChunkBuilder::getTorchlightForVertex(u8 x, u8 y, u8 z, u8 i, u8 j, const Chunk &chunk) {
if (i == Face::Left) {
if (j == 0) return getAverageTorchlight(x, y, z, -1, 0, -1, chunk);
else if (j == 1) return getAverageTorchlight(x, y, z, -1, 0, 1, chunk);
else if (j == 2) return getAverageTorchlight(x, y, z, -1, 0, 1, chunk);
else if (j == 3) return getAverageTorchlight(x, y, z, -1, 0, -1, chunk);
}
else if (i == Face::Front) {
if (j == 0) return getAverageTorchlight(x, y, z, 1, 0, -1, chunk);
else if (j == 1) return getAverageTorchlight(x, y, z, -1, 0, -1, chunk);
else if (j == 2) return getAverageTorchlight(x, y, z, -1, 0, -1, chunk);
else if (j == 3) return getAverageTorchlight(x, y, z, 1, 0, -1, chunk);
}
else if (i == Face::Top) {
if (j == 0) return getAverageTorchlight(x, y, z, -1, 0, 1, chunk);
else if (j == 1) return getAverageTorchlight(x, y, z, 1, 0, 1, chunk);
else if (j == 2) return getAverageTorchlight(x, y, z, 1, 0, -1, chunk);
else if (j == 3) return getAverageTorchlight(x, y, z, -1, 0, -1, chunk);
}
else if (i == Face::Right) {
if (j == 0) return getAverageTorchlight(x, y, z, 1, 0, 1, chunk);
else if (j == 1) return getAverageTorchlight(x, y, z, 1, 0, -1, chunk);
else if (j == 2) return getAverageTorchlight(x, y, z, 1, 0, -1, chunk);
else if (j == 3) return getAverageTorchlight(x, y, z, 1, 0, 1, chunk);
}
else if (i == Face::Back) {
if (j == 0) return getAverageTorchlight(x, y, z, -1, 0, 1, chunk);
else if (j == 1) return getAverageTorchlight(x, y, z, -1, 0, 1, chunk);
else if (j == 2) return getAverageTorchlight(x, y, z, 1, 0, 1, chunk);
else if (j == 3) return getAverageTorchlight(x, y, z, 1, 0, 1, chunk);
}
else if (i == Face::Bottom) {
if (j == 0) return getAverageTorchlight(x, y, z, -1, 0, -1, chunk);
else if (j == 1) return getAverageTorchlight(x, y, z, 1, 0, -1, chunk);
else if (j == 2) return getAverageTorchlight(x, y, z, 1, 0, 1, chunk);
else if (j == 3) return getAverageTorchlight(x, y, z, -1, 0, 1, chunk);
}
return chunk.lightmap().getTorchlight(x, y, z);
if (light == Light::Sun)
return chunk.lightmap().getSunlight(x, y, z);
else
return chunk.lightmap().getTorchlight(x, y, z);
}