From 04b9df316971e0cb897605fdabb16da712f3f791 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 11 Jun 2023 02:40:06 +0200 Subject: wip --- editor/imgui.cpp | 1 - main/draw.cpp | 25 +++++++++++++++++++++++++ main/main-impl.hpp | 2 ++ shaders/lightmap.cpp | 13 ++++++------- shaders/lightmap.frag | 10 +++++++--- shaders/lightmap.hpp | 7 ++----- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/editor/imgui.cpp b/editor/imgui.cpp index c48dbed1..82fe42b1 100644 --- a/editor/imgui.cpp +++ b/editor/imgui.cpp @@ -218,7 +218,6 @@ void app::draw_lightmap_test() if (e_ && ImGui::Begin("Lightmap", &is_open, flags)) { - constexpr auto tile_size = TILE_SIZE2.sum()/2; fm_assert(e_->type() == entity_type::light); const auto& li = static_cast(*e_); light_s L { diff --git a/main/draw.cpp b/main/draw.cpp index 40e438c6..909dc13f 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -114,6 +114,22 @@ auto main_impl::get_draw_bounds() const noexcept -> draw_bounds return {x0, x1, y0, y1}; } +void main_impl::draw_lights_for_chunk(chunk& c, chunk_coords_ ch, Vector2b neighbor_offset) noexcept +{ +} + +void main_impl::draw_lights(chunk& c, chunk_coords_ ch, const std::array& ns) noexcept +{ + for (auto i = 0uz; i < 8; i++) + if (ns[i] != nullptr) + { + auto off = world::neighbor_offsets[i]; + draw_lights_for_chunk(*ns[i], ch + off, off); + } + + draw_lights_for_chunk(c, ch, {}); +} + void main_impl::draw_world() noexcept { const auto [z_min, z_max, z_cur, only] = app.get_z_bounds(); @@ -143,7 +159,16 @@ void main_impl::draw_world() noexcept auto* c_ = _world.at(ch); if (!c_) continue; + std::array ns = {}; + for (auto i = 0uz; i < 8; i++) + { + auto off = world::neighbor_offsets[i]; + auto n = chunk_coords_{int16_t(x + off.x()), int16_t(y + off.y()), z}; + ns[i] = _world.at(n); + } auto& c = *c_; + draw_lights(c, ch, ns); + // tex = _lightmap_shader.texture(); // todo const with_shifted_camera_offset o{_shader, ch, {minx, miny}, {maxx, maxy}}; if (check_chunk_visible(_shader.camera_offset(), sz)) { diff --git a/main/main-impl.hpp b/main/main-impl.hpp index 97640d15..19edce8b 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -121,6 +121,8 @@ private: void recalc_viewport(Vector2i fb_size, Vector2i win_size) noexcept; void draw_world() noexcept; + void draw_lights(chunk& c, chunk_coords_ ch, const std::array& neighbors) noexcept; + void draw_lights_for_chunk(chunk& c, chunk_coords_ ch, Vector2b neighbor_offset) noexcept; draw_bounds get_draw_bounds() const noexcept override; diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp index bc61a434..7c955e1f 100644 --- a/shaders/lightmap.cpp +++ b/shaders/lightmap.cpp @@ -24,14 +24,14 @@ constexpr auto buffer_size = 256uz; } // namespace -auto lightmap_shader::make_framebuffer() -> Framebuffer +auto lightmap_shader::make_framebuffer(Vector2i size) -> Framebuffer { Framebuffer framebuffer; - framebuffer.fb = GL::Framebuffer{{ {}, image_size }}; + framebuffer.fb = GL::Framebuffer{{ {}, size }}; framebuffer.color = GL::Texture2D{}; - framebuffer.color.setStorage(1, GL::TextureFormat::RGB8, image_size); + framebuffer.color.setStorage(1, GL::TextureFormat::RGB8, size); //framebuffer.depth = GL::Renderbuffer{}; //framebuffer.depth.setStorage(GL::RenderbufferFormat::DepthComponent32F, fb_size); @@ -53,7 +53,8 @@ GL::Mesh lightmap_shader::make_mesh() } lightmap_shader::lightmap_shader() : - framebuffer { make_framebuffer() }, + framebuffer { make_framebuffer(image_size) }, + accum { make_framebuffer(image_size) }, _quads { ValueInit, buffer_size }, _indexes { ValueInit, buffer_size }, _vertex_buf { _quads }, @@ -116,10 +117,8 @@ void lightmap_shader::add_light(Vector2i neighbor_offset, const light_s& light) I = 1; break; case light_falloff::linear: - I = light.dist * tile_size / 5; - break; case light_falloff::quadratic: - I = light.dist * tile_size * 100; + I = light.dist * tile_size; break; } diff --git a/shaders/lightmap.frag b/shaders/lightmap.frag index db1cf895..61bf10ff 100644 --- a/shaders/lightmap.frag +++ b/shaders/lightmap.frag @@ -9,14 +9,18 @@ out vec4 color; void main() { vec2 pos = gl_FragCoord.xy; - float I = color_intensity.w; + float L = color_intensity.w; float dist = distance(pos, center); //float dist = sqrt(tmp.x*tmp.x + tmp.y*tmp.y); float A = 1; + //attenuation = ((light_dist - dist)**exponent) / (light_dist**exponent) if (falloff == 0) // linear - A = I/(I + dist); + A = max(0, (L - dist) / L); else if (falloff == 2) // quadratic - A = I/(I + dist*dist); + { + float tmp = max(0, L - dist); + A = max(0, tmp*tmp / (L*L)); + } //I = sqrt(color_intensity.w*1.5)*16; //dist = sqrt(tmp.x*tmp.x + tmp.y*tmp.y); //float alpha = 1 - min(1, dist / I); diff --git a/shaders/lightmap.hpp b/shaders/lightmap.hpp index e458cadc..4ae6cb5c 100644 --- a/shaders/lightmap.hpp +++ b/shaders/lightmap.hpp @@ -43,12 +43,11 @@ struct lightmap_shader final : GL::AbstractShaderProgram void begin(Vector2i neighbor_offset, const light_s& light); void add_chunk(Vector2i neighbor_offset, const chunk& ch); - void add_vertex(Vector2i neighbor_offset, const std::array& obj); void end(); GL::Texture2D& texture(); private: - static Framebuffer make_framebuffer(); + static Framebuffer make_framebuffer(Vector2i size); GL::Mesh make_mesh(); void add_light(Vector2i neighbor_offset, const light_s& light); void flush_vertexes(); @@ -64,12 +63,10 @@ private: //DepthUniform = 4, }; - Framebuffer framebuffer; + Framebuffer framebuffer, accum; Array> _quads; Array> _indexes; size_t _count = 0; - //light_u _light_uniform; - //light_s _light; GL::Buffer _vertex_buf{NoCreate}, _index_buf{NoCreate}; GL::Mesh _mesh{NoCreate}; }; -- cgit v1.2.3