From 411b53fd96b3e43960fcd1bf4bfeefe4c5e2f164 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 10 Jun 2023 10:57:53 +0200 Subject: wip --- shaders/lightmap.cpp | 61 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 22 deletions(-) (limited to 'shaders/lightmap.cpp') diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp index 8e8e1884..a599cfc9 100644 --- a/shaders/lightmap.cpp +++ b/shaders/lightmap.cpp @@ -31,7 +31,7 @@ auto lightmap_shader::make_framebuffer() -> Framebuffer framebuffer.fb = GL::Framebuffer{{ {}, image_size }}; framebuffer.color = GL::Texture2D{}; - framebuffer.color.setStorage(1, GL::TextureFormat::RGBA8, image_size); + framebuffer.color.setStorage(1, GL::TextureFormat::RGB8, image_size); //framebuffer.depth = GL::Renderbuffer{}; //framebuffer.depth.setStorage(GL::RenderbufferFormat::DepthComponent32F, fb_size); @@ -80,18 +80,17 @@ lightmap_shader::lightmap_shader() : void lightmap_shader::flush_vertexes() { - // todo -} - -void lightmap_shader::clear() -{ - framebuffer.fb.clearColor(0, Color4{1.f, 0.f, 1.f, 1.f}); - //framebuffer.fb.clearDepth(0); -} - -void lightmap_shader::bind() -{ - framebuffer.fb.bind(); + if (_count > 0) + { + _index_buf.setSubData(0, ArrayView>{_indexes, 1}); + _vertex_buf.setSubData(0, ArrayView>{_quads, 1}); + + GL::MeshView mesh{_mesh}; + mesh.setCount((int)(6 * _count)); + mesh.setIndexRange(0, 0, (uint32_t)(_count * 6 - 1)); + AbstractShaderProgram::draw(mesh); + _count = 0; + } } std::array lightmap_shader::quad_indexes(size_t N) @@ -106,40 +105,51 @@ std::array lightmap_shader::quad_indexes(size_t N) void lightmap_shader::add_light(Vector2i neighbor_offset, const light_s& light) { fm_debug_assert(_count == 0); + fm_debug_assert(_quads.size() > 0); constexpr auto tile_size = TILE_SIZE2.sum()/2; constexpr auto scale = 2/chunk_size; auto dist = std::fmax(0.f, light.dist * tile_size); + auto dist_clip = dist * tile_size; auto center = light.center + chunk_offset + Vector2(neighbor_offset)*chunk_size; auto center_clip = Vector2{center} * scale; // clip coordinate constexpr auto image_size_factor = Vector2(image_size) / Vector2(chunk_size); auto center_fragcoord = center * image_size_factor; // window-relative coordinates - _indexes[0] = quad_indexes(0); - _quads[0] = std::array{{ - { dist + center_clip.x(), -dist + center_clip.y() }, - { dist + center_clip.x(), dist + center_clip.y() }, - { -dist + center_clip.x(), -dist + center_clip.y() }, - { -dist + center_clip.x(), dist + center_clip.y() }, + _indexes[_count] = quad_indexes(0); + _quads[_count] = std::array{{ + { dist_clip + center_clip.x(), -dist_clip + center_clip.y() }, + { dist_clip + center_clip.x(), dist_clip + center_clip.y() }, + { -dist_clip + center_clip.x(), -dist_clip + center_clip.y() }, + { -dist_clip + center_clip.x(), dist_clip + center_clip.y() }, }}; - _index_buf.setSubData(0, ArrayView>{_indexes, 1}); - _vertex_buf.setSubData(0, ArrayView>{_quads, 1}); + _count++; setUniform(ColorIntensityUniform, Vector4{Vector3{light.color}, dist}); setUniform(CenterUniform, center_fragcoord); setUniform(FalloffUniform, (uint32_t)light.falloff); setUniform(SizeUniform, chunk_size); +} +lightmap_shader::~lightmap_shader() = default; +void lightmap_shader::clear() +{ + framebuffer.fb.clearColor(0, Color4{1.f, 0.f, 1.f, 1.f}); + //framebuffer.fb.clearDepth(0); } -lightmap_shader::~lightmap_shader() = default; +void lightmap_shader::bind() +{ + framebuffer.fb.bind(); +} void lightmap_shader::begin(Vector2i neighbor_offset, const light_s& light) { fm_assert(_count == 0); clear(); + bind(); add_light(neighbor_offset, light); } @@ -148,6 +158,13 @@ void lightmap_shader::end() flush_vertexes(); } +GL::Texture2D& lightmap_shader::texture() +{ + fm_assert(_count == 0); + fm_debug_assert(framebuffer.color.id()); + return framebuffer.color; +} + bool light_s::operator==(const light_s&) const noexcept = default; } // namespace floormat -- cgit v1.2.3