Skip to content

Commit

Permalink
Fix dark liquids (#6621)
Browse files Browse the repository at this point in the history
* Update light storage format
  • Loading branch information
numberZero authored and nerzhul committed Nov 18, 2017
1 parent 24c1c2f commit 0780ee5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
34 changes: 16 additions & 18 deletions src/content_mapblock.cpp
Expand Up @@ -103,7 +103,7 @@ void MapblockMeshGenerator::getTile(v3s16 direction, TileSpec *tile)
void MapblockMeshGenerator::getSpecialTile(int index, TileSpec *tile, bool apply_crack)
{
*tile = f->special_tiles[index];
TileLayer *top_layer = NULL;
TileLayer *top_layer = nullptr;

for (auto &layernum : tile->layers) {
TileLayer *layer = &layernum;
Expand Down Expand Up @@ -151,7 +151,7 @@ void MapblockMeshGenerator::drawQuad(v3f *coords, const v3s16 &normal,
// the faces in the list is up-down-right-left-back-front
// (compatible with ContentFeatures).
void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
TileSpec *tiles, int tilecount, const u16 *lights, const f32 *txc)
TileSpec *tiles, int tilecount, const LightPair *lights, const f32 *txc)
{
assert(tilecount >= 1 && tilecount <= 6); // pre-condition

Expand Down Expand Up @@ -281,15 +281,15 @@ void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
void MapblockMeshGenerator::getSmoothLightFrame()
{
for (int k = 0; k < 8; ++k) {
u16 light = getSmoothLightTransparent(blockpos_nodes + p, light_dirs[k], data);
frame.lightsA[k] = light & 0xff;
frame.lightsB[k] = light >> 8;
LightPair light(getSmoothLightTransparent(blockpos_nodes + p, light_dirs[k], data));
frame.lightsA[k] = light.lightA;
frame.lightsB[k] = light.lightB;
}
}

// Calculates vertex light level
// vertex_pos - vertex position in the node (coordinates are clamped to [0.0, 1.0] or so)
u16 MapblockMeshGenerator::blendLight(const v3f &vertex_pos)
LightPair MapblockMeshGenerator::blendLight(const v3f &vertex_pos)
{
f32 x = core::clamp(vertex_pos.X / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
f32 y = core::clamp(vertex_pos.Y / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
Expand All @@ -303,17 +303,15 @@ u16 MapblockMeshGenerator::blendLight(const v3f &vertex_pos)
lightA += dx * dy * dz * frame.lightsA[k];
lightB += dx * dy * dz * frame.lightsB[k];
}
return
core::clamp(core::round32(lightA), 0, 255) |
core::clamp(core::round32(lightB), 0, 255) << 8;
return LightPair(lightA, lightB);
}

// Calculates vertex color to be used in mapblock mesh
// vertex_pos - vertex position in the node (coordinates are clamped to [0.0, 1.0] or so)
// tile_color - node's tile color
video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos)
{
u16 light = blendLight(vertex_pos);
LightPair light = blendLight(vertex_pos);
return encode_light(light, f->light_source);
}

Expand Down Expand Up @@ -367,7 +365,7 @@ void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
tile_count = 1;
}
if (data->m_smooth_lighting) {
u16 lights[8];
LightPair lights[8];
for (int j = 0; j < 8; ++j) {
v3f d;
d.X = (j & 4) ? dx2 : dx1;
Expand All @@ -377,7 +375,7 @@ void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
}
drawCuboid(box, tiles, tile_count, lights, txc);
} else {
drawCuboid(box, tiles, tile_count, NULL, txc);
drawCuboid(box, tiles, tile_count, nullptr, txc);
}
}

Expand All @@ -397,11 +395,11 @@ void MapblockMeshGenerator::prepareLiquidNodeDrawing()
if (f->light_source != 0) {
// If this liquid emits light and doesn't contain light, draw
// it at what it emits, for an increased effect
light = decode_light(f->light_source);
light = light | (light << 8);
u8 e = decode_light(f->light_source);
light = LightPair(std::max(e, light.lightA), std::max(e, light.lightB));
} else if (nodedef->get(ntop).param_type == CPT_LIGHT) {
// Otherwise, use the light of the node on top if possible
light = getInteriorLight(ntop, 0, nodedef);
light = LightPair(getInteriorLight(ntop, 0, nodedef));
}

color_liquid_top = encode_light(light, f->light_source);
Expand Down Expand Up @@ -960,7 +958,7 @@ void MapblockMeshGenerator::drawPlantlikeRootedNode()
getSmoothLightFrame();
} else {
MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
light = getInteriorLight(ntop, 1, nodedef);
light = LightPair(getInteriorLight(ntop, 1, nodedef));
}
drawPlantlike();
p.Y--;
Expand Down Expand Up @@ -1244,7 +1242,7 @@ void MapblockMeshGenerator::drawNodeboxNode()
std::vector<aabb3f> boxes;
n.getNodeBoxes(nodedef, &boxes, neighbors_set);
for (const auto &box : boxes)
drawAutoLightedCuboid(box, NULL, tiles, 6);
drawAutoLightedCuboid(box, nullptr, tiles, 6);
}

void MapblockMeshGenerator::drawMeshNode()
Expand Down Expand Up @@ -1330,7 +1328,7 @@ void MapblockMeshGenerator::drawNode()
if (data->m_smooth_lighting)
getSmoothLightFrame();
else
light = getInteriorLight(n, 1, nodedef);
light = LightPair(getInteriorLight(n, 1, nodedef));
switch (f->drawtype) {
case NDT_FLOWINGLIQUID: drawLiquidNode(); break;
case NDT_GLASSLIKE: drawGlasslikeNode(); break;
Expand Down
22 changes: 17 additions & 5 deletions src/content_mapblock.h
Expand Up @@ -25,8 +25,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct MeshMakeData;
struct MeshCollector;

struct LightFrame
{
struct LightPair {
u8 lightA;
u8 lightB;

LightPair() = default;
explicit LightPair(u16 value) : lightA(value & 0xff), lightB(value >> 8) {}
LightPair(u8 valueA, u8 valueB) : lightA(valueA), lightB(valueB) {}
LightPair(float valueA, float valueB) :
lightA(core::clamp(core::round32(valueA), 0, 255)),
lightB(core::clamp(core::round32(valueB), 0, 255)) {}
operator u16() const { return lightA | lightB << 8; }
};

struct LightFrame {
f32 lightsA[8];
f32 lightsB[8];
};
Expand All @@ -49,15 +61,15 @@ class MapblockMeshGenerator
v3f origin;
MapNode n;
const ContentFeatures *f;
u16 light;
LightPair light;
LightFrame frame;
video::SColor color;
TileSpec tile;
float scale;

// lighting
void getSmoothLightFrame();
u16 blendLight(const v3f &vertex_pos);
LightPair blendLight(const v3f &vertex_pos);
video::SColor blendLightColor(const v3f &vertex_pos);
video::SColor blendLightColor(const v3f &vertex_pos, const v3f &vertex_normal);

Expand All @@ -73,7 +85,7 @@ class MapblockMeshGenerator

// cuboid drawing!
void drawCuboid(const aabb3f &box, TileSpec *tiles, int tilecount,
const u16 *lights , const f32 *txc);
const LightPair *lights , const f32 *txc);
void generateCuboidTextureCoords(aabb3f const &box, f32 *coords);
void drawAutoLightedCuboid(aabb3f box, const f32 *txc = NULL,
TileSpec *tiles = NULL, int tile_count = 0);
Expand Down

0 comments on commit 0780ee5

Please sign in to comment.