diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-04-12 15:53:09 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-04-12 16:28:43 +0200 |
commit | 506f87c554153eb0bbc3bc9e4fb0f26792f8bfca (patch) | |
tree | 0b9af737ab98b44b0e59a2b6fb4d2d94d01f460c | |
parent | 903e1644fce5652a621803a6eb3617d605d22434 (diff) |
depth buffer fixes
-rw-r--r-- | main/draw.cpp | 15 | ||||
-rw-r--r-- | shaders/tile.cpp | 3 | ||||
-rw-r--r-- | shaders/tile.hpp | 6 | ||||
-rw-r--r-- | src/camera-offset.cpp | 36 | ||||
-rw-r--r-- | src/chunk-render.cpp | 2 | ||||
-rw-r--r-- | src/global-coords.hpp | 6 |
6 files changed, 45 insertions, 23 deletions
diff --git a/main/draw.cpp b/main/draw.cpp index 2c9d6964..063da14d 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -31,12 +31,12 @@ void main_impl::recalc_viewport(Vector2i fb_size, Vector2i win_size) noexcept framebuffer.color = GL::Texture2D{}; framebuffer.color.setStorage(1, GL::TextureFormat::RGBA8, fb_size); framebuffer.depth = GL::Renderbuffer{}; - framebuffer.depth.setStorage(GL::RenderbufferFormat::Depth32FStencil8, fb_size); + framebuffer.depth.setStorage(GL::RenderbufferFormat::DepthComponent32F, fb_size); framebuffer.fb.attachTexture(GL::Framebuffer::ColorAttachment{0}, framebuffer.color, 0); - framebuffer.fb.attachRenderbuffer(GL::Framebuffer::BufferAttachment::DepthStencil, framebuffer.depth); + framebuffer.fb.attachRenderbuffer(GL::Framebuffer::BufferAttachment::Depth, framebuffer.depth); framebuffer.fb.clearColor(0, Color4{0.f, 0.f, 0.f, 1.f}); - framebuffer.fb.clearDepthStencil(0, 0); + framebuffer.fb.clearDepth(0); framebuffer.fb.bind(); } @@ -102,6 +102,13 @@ auto main_impl::get_draw_bounds() const noexcept -> draw_bounds y0 = std::min(y0, p.y); y1 = std::max(y1, p.y); } + + constexpr int16_t max = 7, min = -max; + x0 = std::clamp(x0, min, max); + x1 = std::clamp(x1, min, max); + y0 = std::clamp(y0, min, max); + y1 = std::clamp(y1, min, max); + return {x0, x1, y0, y1}; } @@ -113,7 +120,7 @@ void main_impl::draw_world() noexcept _clickable_scenery.clear(); #ifdef FM_USE_DEPTH32 - framebuffer.fb.clearDepthStencil(0, 0); + framebuffer.fb.clearDepth(0); #else GL::defaultFramebuffer.clearDepth(0); #endif diff --git a/shaders/tile.cpp b/shaders/tile.cpp index d5f6b7d3..d774ddf0 100644 --- a/shaders/tile.cpp +++ b/shaders/tile.cpp @@ -2,6 +2,7 @@ #include "loader/loader.hpp" #include "compat/assert.hpp" #include "local-coords.hpp" +#include <cmath> #include <Corrade/Containers/Iterable.h> #include <Corrade/Containers/StringStl.h> #include <Magnum/Math/Vector4.h> @@ -74,7 +75,7 @@ void tile_shader::_draw() float tile_shader::depth_value(const local_coords& xy, float offset) noexcept { - return depth_tile_size + (xy.to_index() + offset)*depth_tile_size; + return (float)((xy.to_index() + (double)offset) * depth_tile_size); } } // namespace floormat diff --git a/shaders/tile.hpp b/shaders/tile.hpp index fd3788f3..8415d836 100644 --- a/shaders/tile.hpp +++ b/shaders/tile.hpp @@ -37,9 +37,9 @@ struct tile_shader : GL::AbstractShaderProgram template<typename T, typename... Xs> decltype(auto) draw(T&& mesh, Xs&&... xs); - static constexpr float depth_tile_size = 3.56e-15; // 1 - nextafter(1, 0) * TILE_MAX_DIM / 2 - static constexpr float depth_start = -1 + depth_tile_size; - static constexpr float scenery_depth_offset = .5f, character_depth_offset = .5f; + static constexpr double depth_tile_size = 1/(double)(TILE_COUNT * 16 * 16); + static constexpr double depth_chunk_size = 1/(double)(16 * 16); + static constexpr float scenery_depth_offset = 0.5f, character_depth_offset = 0.5f, wall_depth_offset = 0.25f; private: void _draw(); diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp index 0aab5ea1..de6f584d 100644 --- a/src/camera-offset.cpp +++ b/src/camera-offset.cpp @@ -4,25 +4,35 @@ namespace floormat { -with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chunk_coords_ c, chunk_coords first, chunk_coords last) : +with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chunk_coords_ c_, chunk_coords first_, chunk_coords last_) : _shader{shader}, _camera{shader.camera_offset()} { fm_assert(shader.depth_offset() == 0.f); constexpr auto chunk_size = TILE_MAX_DIM20d*dTILE_SIZE; - const auto offset = _camera + tile_shader::project(Vector3d(c.x, c.y, 0) * chunk_size); - first.x -= 8; first.y -= 8; last.x += 8; last.y += 8; // Z levels - auto len_x = (float)(last.x - first.x), cx = (float)(c.x - first.x), cy = (float)(c.y - first.y); - //cx += c.z; cy += c.z; - float depth_offset = shader.depth_tile_size*(cy*TILE_MAX_DIM*len_x*TILE_MAX_DIM + cx*TILE_MAX_DIM); - const int z = c.z - chunk_z_min; - depth_offset += tile_shader::depth_value(local_coords{z, z}); - - // wip - - if (c.z == chunk_z_max) - depth_offset = 1; + auto offset = _camera + tile_shader::project(Vector3d(c_.x, c_.y, 0) * chunk_size); + auto pos = Vector2d(chunk_coords(c_) - first_); + auto len = Vector2d(last_ - first_) + Vector2d(1, 1); + auto pos1 = pos.y() * len.x() + pos.x(); + auto z = c_.z - chunk_z_min; + constexpr auto depth_start = -1 + 1.111e-16; + + double chunk_offset, tile_offset; + + if (c_.z < chunk_z_max) + { + chunk_offset = depth_start + tile_shader::depth_chunk_size * pos1; + tile_offset = (double)tile_shader::depth_value({z, z}); + } + else + { + chunk_offset = 1; + tile_offset = 0; + } + + double depth_offset_ = chunk_offset + tile_offset; + auto depth_offset = (float)depth_offset_; _shader.set_camera_offset(offset, depth_offset); } diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index 802cb11a..28bdc759 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -71,7 +71,7 @@ GL::Mesh chunk::make_wall_mesh(size_t count) const local_coords pos{i / 2u}; const auto center = Vector3(pos) * TILE_SIZE; const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); - const float depth = tile_shader::depth_value(pos); + const float depth = tile_shader::depth_value(pos, tile_shader::wall_depth_offset); const auto texcoords = atlas->texcoords_for_id(variant); auto& v = vertexes[k]; for (auto j = 0uz; j < 4; j++) diff --git a/src/global-coords.hpp b/src/global-coords.hpp index a5f9dff0..737fc21b 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -20,6 +20,8 @@ struct chunk_coords final { template<typename T> requires (std::is_floating_point_v<T> || std::is_integral_v<T> && (sizeof(T) > sizeof(x) || std::is_same_v<T, std::decay_t<decltype(x)>>)) explicit constexpr operator Math::Vector3<T>() const noexcept { return Math::Vector3<T>(T(x), T(y), T(0)); } + + constexpr Vector2i operator-(chunk_coords other) const noexcept { return Vector2i{x - other.x, y - other.y }; } }; struct chunk_coords_ final { @@ -55,7 +57,9 @@ struct chunk_coords_ final { x = int16_t(x - int{off.x()}); y = int16_t(y - int{off.y()}); z = int8_t(z - off.z()); return *this; } - Vector3i operator-(chunk_coords_ other) const noexcept { return Vector3i{x - other.x, y - other.y, z - other.z}; } + constexpr Vector3i operator-(chunk_coords_ other) const noexcept { return Vector3i{x - other.x, y - other.y, z - other.z}; } + + explicit operator chunk_coords() const noexcept { return chunk_coords{x, y}; } }; constexpr inline int8_t chunk_z_min = -1, chunk_z_max = 14; |