diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-29 22:15:27 +0100 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-29 22:32:46 +0100 |
| commit | 9c5027215e1052adb0131362207b78ec89822985 (patch) | |
| tree | ad928799caedab4fa497802e8981db9dc66fcc76 /shaders | |
| parent | 2659fae8c838601282b9b90d5e0dc8b1da3f3f2b (diff) | |
fix z value discontinuity on chunk boundaries
Diffstat (limited to 'shaders')
| -rw-r--r-- | shaders/tile.cpp | 14 | ||||
| -rw-r--r-- | shaders/tile.hpp | 9 | ||||
| -rw-r--r-- | shaders/tile.vert | 4 |
3 files changed, 15 insertions, 12 deletions
diff --git a/shaders/tile.cpp b/shaders/tile.cpp index ff08de2e..9df05f8e 100644 --- a/shaders/tile.cpp +++ b/shaders/tile.cpp @@ -31,7 +31,7 @@ tile_shader::tile_shader() set_scale({640, 480}); set_tint({1, 1, 1, 1}); - setUniform(OffsetUniform, Vector2{}); + setUniform(OffsetUniform, Vector3{}); } tile_shader::~tile_shader() = default; @@ -43,9 +43,10 @@ tile_shader& tile_shader::set_scale(const Vector2& scale) return *this; } -tile_shader& tile_shader::set_camera_offset(Vector2d camera_offset) +tile_shader& tile_shader::set_camera_offset(const Vector2d& camera_offset, float depth_offset) { _camera_offset = camera_offset; + _depth_offset = depth_offset; return *this; } @@ -58,11 +59,12 @@ tile_shader& tile_shader::set_tint(const Vector4& tint) void tile_shader::_draw() { fm_assert(std::fabs(_camera_offset[0]) < 1 << 24 && std::fabs(_camera_offset[1]) < 1 << 24); + fm_assert(std::fabs(_depth_offset) < 1 << 24); if (_tint != _real_tint) setUniform(TintUniform, _real_tint = _tint); - if (const auto offset = Vector2{(float)_camera_offset[0], (float)_camera_offset[1]}; + if (const auto offset = Vector3((float)_camera_offset[0], (float)_camera_offset[1], _depth_offset); offset != _real_camera_offset) { _real_camera_offset = offset; @@ -72,11 +74,7 @@ void tile_shader::_draw() float tile_shader::depth_value(const local_coords& xy, float offset) noexcept { - constexpr float max = (TILE_MAX_DIM+1)*(TILE_MAX_DIM+1) * .5f; - constexpr float min = -1 + 1.f/256; - float value = min + (xy.to_index() + offset)/max; - fm_assert(value > -1 && value < 1); - return value; + return (xy.to_index() + offset)*depth_tile_size; } } // namespace floormat diff --git a/shaders/tile.hpp b/shaders/tile.hpp index 72f36c40..d6eb7cca 100644 --- a/shaders/tile.hpp +++ b/shaders/tile.hpp @@ -1,5 +1,6 @@ #pragma once #include "compat/defs.hpp" +#include "tile-defs.hpp" #include <Magnum/GL/AbstractShaderProgram.h> #include <Magnum/Math/Vector2.h> #include <Magnum/Math/Vector3.h> @@ -24,9 +25,10 @@ struct tile_shader : GL::AbstractShaderProgram Vector2 scale() const { return _scale; } tile_shader& set_scale(const Vector2& scale); Vector2d camera_offset() const { return _camera_offset; } - tile_shader& set_camera_offset(Vector2d camera_offset); + tile_shader& set_camera_offset(const Vector2d& camera_offset, float depth_offset); Vector4 tint() const { return _tint; } tile_shader& set_tint(const Vector4& tint); + float depth_offset() const { return _depth_offset; } static float depth_value(const local_coords& xy, float offset = 0) noexcept; template<typename T = float> static constexpr Math::Vector2<T> project(const Math::Vector3<T>& pt); @@ -35,13 +37,16 @@ struct tile_shader : GL::AbstractShaderProgram template<typename T, typename... Xs> decltype(auto) draw(T&& mesh, Xs&&... xs); + static constexpr float depth_tile_size = 1.f/(256 * TILE_COUNT); + private: void _draw(); Vector2d _camera_offset; Vector4 _tint, _real_tint; Vector2 _scale; - Vector2 _real_camera_offset; + Vector3 _real_camera_offset; + float _depth_offset = 0; enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, }; }; diff --git a/shaders/tile.vert b/shaders/tile.vert index ad038e6d..a539ed6b 100644 --- a/shaders/tile.vert +++ b/shaders/tile.vert @@ -1,7 +1,7 @@ precision highp float; layout (location = 0) uniform vec2 scale; -layout (location = 1) uniform vec2 offset; +layout (location = 1) uniform vec3 offset; layout (location = 0) in vec4 position; layout (location = 1) in vec2 texcoords; @@ -10,6 +10,6 @@ noperspective out vec2 frag_texcoords; void main() { float x = -position.y, y = -position.x, z = position.z; - gl_Position = vec4((x-y+offset.x)*scale.x, ((x+y+z*2)*.59-offset.y)*scale.y, depth, 1); + gl_Position = vec4((x-y+offset.x)*scale.x, ((x+y+z*2)*.59-offset.y)*scale.y, offset.z + depth, 1); frag_texcoords = texcoords; } |
