summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-05-03 18:19:33 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-05-05 03:16:22 +0200
commita549fdf835a8548e09084156137cb26e869e8901 (patch)
tree0eb053650b390d6cc1084b4d2967983948c3d08a
parentccb4b508c39e29a7087ba69fe5cca9ef2cd79a25 (diff)
add simplified camera offset formula
-rw-r--r--editor/camera.cpp5
-rw-r--r--src/camera-offset.cpp17
-rw-r--r--src/camera-offset.hpp3
3 files changed, 19 insertions, 6 deletions
diff --git a/editor/camera.cpp b/editor/camera.cpp
index 3fa8d9d3..72b90cc2 100644
--- a/editor/camera.cpp
+++ b/editor/camera.cpp
@@ -89,7 +89,7 @@ object_id app::get_object_colliding_with_cursor()
continue;
auto& c = *cʹ;
c.ensure_passability();
- const with_shifted_camera_offset o{shader, c_pos, {minx, miny}, {maxx, maxy}};
+ const with_shifted_camera_offset o{shader, c_pos};
if (floormat_main::check_chunk_visible(shader.camera_offset(), sz))
{
constexpr auto chunk_size = TILE_SIZE2 * TILE_MAX_DIM;
@@ -144,8 +144,7 @@ Vector2 app::point_screen_pos(point pt)
auto& shader = M->shader();
auto win_size = M->window_size();
auto c3 = pt.chunk3();
- auto c2 = pt.chunk();
- with_shifted_camera_offset co{shader, c3, c2, c2 };
+ with_shifted_camera_offset co{shader, c3};
auto world_pos = TILE_SIZE20 * Vector3(pt.local()) + Vector3(Vector2(pt.offset()), 0);
return Vector2(shader.camera_offset()) + Vector2(win_size)*.5f + shader.project(world_pos);
}
diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp
index 46582539..27899b03 100644
--- a/src/camera-offset.cpp
+++ b/src/camera-offset.cpp
@@ -4,6 +4,18 @@
namespace floormat {
+Vector2d with_shifted_camera_offset::get_offset(chunk_coords_ c)
+{
+ return tile_shader::project(Vector3d(Vector2d(c.x, c.y) * TILE_MAX_DIM, c.z) * dTILE_SIZE);
+}
+
+with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chunk_coords_ c) :
+ _shader{shader}, _camera{shader.camera_offset()}
+{
+ auto offset = _camera + get_offset(c);
+ _shader.set_camera_offset(offset, float{1 << 24});
+}
+
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()}
@@ -11,8 +23,7 @@ with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chun
(void)last_;
fm_assert(shader.depth_offset() == 0.f);
- auto z = int{c_.z};
- auto offset = _camera + tile_shader::project((Vector3d(c_.x, c_.y, 0) * TILE_MAX_DIM20d + Vector3d(0, 0, z)) * dTILE_SIZE);
+ auto offset = _camera + get_offset(c_);
auto pos = chunk_coords(c_) - first_;
constexpr auto depth_start = -1 + 1.111e-16f;
@@ -22,7 +33,7 @@ with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chun
printf("c=(%2hd %2hd %2hhd) pos=(%2d %2d) len=(%d %d) --> %d\n", c_.x, c_.y, c_.z, pos.x(), pos.y(), len.x(), len.y(), depth);
#endif
- auto z_offset = (z-chunk_z_min) * tile_shader::depth_value({}, tile_shader::z_depth_offset);
+ auto z_offset = (int{c_.z}-int{chunk_z_min}) * tile_shader::depth_value({}, tile_shader::z_depth_offset);
auto d = depth * tile_shader::depth_tile_size + depth_start + z_offset;
if (c_.z == chunk_z_max)
diff --git a/src/camera-offset.hpp b/src/camera-offset.hpp
index f3b64177..88a1a08a 100644
--- a/src/camera-offset.hpp
+++ b/src/camera-offset.hpp
@@ -9,8 +9,11 @@ struct tile_shader;
struct with_shifted_camera_offset final
{
+ explicit with_shifted_camera_offset(tile_shader& shader, chunk_coords_ c);
explicit with_shifted_camera_offset(tile_shader& shader, chunk_coords_ c, chunk_coords first, chunk_coords last);
~with_shifted_camera_offset();
+
+ static Vector2d get_offset(chunk_coords_ c);
private:
tile_shader& _shader; // NOLINT
Vector2d _camera;