diff options
-rw-r--r-- | editor/imgui.cpp | 4 | ||||
-rw-r--r-- | shaders/lightmap.cpp | 30 | ||||
-rw-r--r-- | shaders/lightmap.hpp | 5 |
3 files changed, 25 insertions, 14 deletions
diff --git a/editor/imgui.cpp b/editor/imgui.cpp index 7206a318..57c10100 100644 --- a/editor/imgui.cpp +++ b/editor/imgui.cpp @@ -217,11 +217,10 @@ void app::do_lightmap_test() auto& shader = M->lightmap_shader(); auto ch = e.coord.chunk(); auto z = e.coord.z(); - constexpr auto ns = lightmap_shader::neighbor_count; + const auto ns = shader.iter_bounds(); shader.begin_occlusion(); -#if 1 for (int j = ch.y - ns; j < ch.y + ns; j++) for (int i = ch.x - ns; i < ch.x + ns; i++) { @@ -232,7 +231,6 @@ void app::do_lightmap_test() shader.add_chunk(offset, *chunk); } } -#endif shader.end_occlusion(); shader.bind(); diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp index 293e8f2e..c676808e 100644 --- a/shaders/lightmap.cpp +++ b/shaders/lightmap.cpp @@ -25,15 +25,26 @@ namespace floormat { namespace { +template<typename T> +constexpr T poor_mans_ceil(T x) +{ + static_assert(std::is_floating_point_v<T>); + const auto x0 = (int64_t)x; + if (x > x0) + return T(x0 + (int64_t)1); + else + return x0; +} + +constexpr auto neighbor_count = 4; constexpr float fuzz_pixels = 16; constexpr float shadow_wall_depth = 8; constexpr float real_image_size = 1024; -constexpr auto neighbor_count = lightmap_shader::neighbor_count; -constexpr auto image_size = TILE_SIZE2 * TILE_MAX_DIM * neighbor_count; +constexpr auto half_neighbors = (int)poor_mans_ceil(neighbor_count/2.f); +constexpr auto image_size = TILE_SIZE2 * TILE_MAX_DIM * neighbor_count; constexpr auto chunk_size = TILE_SIZE2 * TILE_MAX_DIM; -constexpr auto chunk_offset = TILE_SIZE2/2; constexpr auto clip_start = Vector2{-1, -1}; constexpr auto clip_scale = 2/image_size; @@ -209,7 +220,7 @@ std::array<UnsignedShort, 6> lightmap_shader::quad_indexes(size_t N) void lightmap_shader::add_light(Vector2 neighbor_offset, const light_s& light) { - neighbor_offset += half_neighbors; + neighbor_offset += Vector2((float)half_neighbors); constexpr auto tile_size = TILE_SIZE2.sum()/2; float range; @@ -229,7 +240,7 @@ void lightmap_shader::add_light(Vector2 neighbor_offset, const light_s& light) range *= tile_size; range = std::fmax(0.f, range); - auto center_fragcoord = light.center + chunk_offset + neighbor_offset * chunk_size; // window-relative coordinates + auto center_fragcoord = light.center + neighbor_offset * chunk_size; // window-relative coordinates auto center_clip = clip_start + center_fragcoord * clip_scale; // clip coordinates float alpha = light.color.a() / 255.f; @@ -286,9 +297,14 @@ GL::Texture2D& lightmap_shader::accum_texture() return framebuffer.accum; } +int lightmap_shader::iter_bounds() +{ + return half_neighbors; +} + void lightmap_shader::add_rect(Vector2 neighbor_offset, Vector2 min, Vector2 max) { - auto off = neighbor_offset*chunk_size + chunk_offset; + auto off = neighbor_offset*chunk_size; min += off; max += off; @@ -336,7 +352,7 @@ void lightmap_shader::add_rect(Vector2 neighbor_offset, Pair<Vector2, Vector2> m void lightmap_shader::add_chunk(Vector2 neighbor_offset, chunk& c) { - neighbor_offset += half_neighbors; + neighbor_offset += Vector2(half_neighbors); add_geometry(neighbor_offset, c); add_entities(neighbor_offset, c); diff --git a/shaders/lightmap.hpp b/shaders/lightmap.hpp index 0479a9b3..5bd38069 100644 --- a/shaders/lightmap.hpp +++ b/shaders/lightmap.hpp @@ -46,13 +46,10 @@ struct lightmap_shader final : GL::AbstractShaderProgram void add_light(Vector2 neighbor_offset, const light_s& light); void bind(); void finish(); + static int iter_bounds(); GL::Texture2D& accum_texture(); - // todo allow 16 neighbors on new gpu's - static constexpr auto neighbor_count = 4; - static constexpr auto half_neighbors = Vector2(neighbor_count)/2; - using Position = GL::Attribute<0, Vector3>; private: |