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 /src | |
parent | 903e1644fce5652a621803a6eb3617d605d22434 (diff) |
depth buffer fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/camera-offset.cpp | 36 | ||||
-rw-r--r-- | src/chunk-render.cpp | 2 | ||||
-rw-r--r-- | src/global-coords.hpp | 6 |
3 files changed, 29 insertions, 15 deletions
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; |