From 776bcd43cc434d8b349323bacf1854935d1501cb Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 12 Feb 2023 17:54:34 -0700 Subject: [PATCH] light: octree lightgrid: don't subdivide further if mostly unoccluded --- light/lightgrid.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/light/lightgrid.cc b/light/lightgrid.cc index af26dc1e..2817a4e3 100644 --- a/light/lightgrid.cc +++ b/light/lightgrid.cc @@ -322,18 +322,21 @@ void LightGrid(bspdata_t *bspdata) return mins + (size / 2); }; - auto is_all_occluded = [&](qvec3i mins, qvec3i size) -> bool { + auto count_occluded_unoccluded = [&](qvec3i mins, qvec3i size) -> std::tuple { + std::tuple occluded_unoccluded; for (int z = mins[2]; z < (mins[2] + size[2]); ++z) { for (int y = mins[1]; y < (mins[1] + size[1]); ++y) { for (int x = mins[0]; x < (mins[0] + size[0]); ++x) { int sample_index = get_grid_index(x, y, z); - if (!occlusion[sample_index]) { - return false; + if (occlusion[sample_index]) { + std::get<0>(occluded_unoccluded)++; + } else { + std::get<1>(occluded_unoccluded)++; } } } } - return true; + return occluded_unoccluded; }; constexpr int MAX_DEPTH = 5; @@ -374,7 +377,8 @@ void LightGrid(bspdata_t *bspdata) assert(size[2] > 0); // special case: fully occluded leaf, just represented as a flag bit - if (is_all_occluded(mins, size)) { + auto [occluded_count, unoccluded_count] = count_occluded_unoccluded(mins, size); + if (!unoccluded_count) { occluded_cells += size[0] * size[1] * size[2]; return FLAG_OCCLUDED; } @@ -386,6 +390,11 @@ void LightGrid(bspdata_t *bspdata) if (depth == MAX_DEPTH) make_leaf = true; + if (occluded_count < 8) { + // force a leaf if it's mostly unoccluded + make_leaf = true; + } + if (make_leaf) { // make a leaf const uint32_t leafnum = static_cast(octree_leafs.size());