diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-01 18:01:03 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-01 18:01:03 +0100 |
commit | 92585b90be4b0be8953618d68f866711c9d15c7a (patch) | |
tree | 219f6742b9fb5bcd8411290cb14b24fd5e61a749 /editor/tests | |
parent | 595081292307808fcd5140ae92594efea899d5dc (diff) |
w
Diffstat (limited to 'editor/tests')
-rw-r--r-- | editor/tests/path-test.cpp | 14 | ||||
-rw-r--r-- | editor/tests/raycast-test.cpp | 53 |
2 files changed, 48 insertions, 19 deletions
diff --git a/editor/tests/path-test.cpp b/editor/tests/path-test.cpp index c03ef990..42404983 100644 --- a/editor/tests/path-test.cpp +++ b/editor/tests/path-test.cpp @@ -107,27 +107,17 @@ void path_test::draw_overlay(app& a) if (!has_result) return; - const auto win_size = a.main().window_size(); const auto line_color = ImGui::ColorConvertFloat4ToU32({0, 0, 1, 1}); const auto dot_color = ImGui::ColorConvertFloat4ToU32({1, 0, 0, 1}); constexpr float line_thickness = 3, dot_radius = 5; - auto& shader = a.main().shader(); ImDrawList& draw = *ImGui::GetForegroundDrawList(); - constexpr auto get_screen_pos = [](tile_shader& shader, point pt, Vector2i win_size) { - auto c3 = pt.chunk3(); - auto c2 = pt.chunk(); - with_shifted_camera_offset co{shader, c3, c2, c2 }; - 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); - }; - - auto last = get_screen_pos(shader, result.from, win_size); + auto last = a.point_screen_pos(result.from); draw.AddCircleFilled({last.x(), last.y()}, dot_radius, dot_color); for (auto pt : result.path) { - auto pos = get_screen_pos(shader, pt, win_size); + auto pos = a.point_screen_pos(pt); draw.AddLine({pos.x(), pos.y()}, {last.x(), last.y()}, line_color, line_thickness); draw.AddCircleFilled({pos.x(), pos.y()}, dot_radius, dot_color); last = pos; diff --git a/editor/tests/raycast-test.cpp b/editor/tests/raycast-test.cpp index 2953ae69..50e06063 100644 --- a/editor/tests/raycast-test.cpp +++ b/editor/tests/raycast-test.cpp @@ -3,6 +3,7 @@ #include "floormat/main.hpp" #include "compat/shared-ptr-wrapper.hpp" #include "src/critter.hpp" +#include "../imgui-raii.hpp" #include <memory> #include <array> #include <vector> @@ -13,6 +14,8 @@ namespace floormat::tests { namespace { +using namespace imgui; + struct aabb_result { Vector2 ts; @@ -59,7 +62,7 @@ aabb_result ray_aabb_intersection(Vector2 ray_origin, Vector2 ray_dir_inv_norm, struct bbox { point center; - Vector2ub size; + Vector2ui size; }; struct result_s @@ -115,7 +118,19 @@ struct raycast_test : base_test void draw_overlay(app& a) override { + if (!result.has_result) + return; + const auto color = ImGui::ColorConvertFloat4ToU32({1, 0, 0, 1}); + ImDrawList& draw = *ImGui::GetForegroundDrawList(); + + for (const auto& p : result.path) + { + auto p0 = object::normalize_coords(p.center, -Vector2i(p.size/2)); + auto p1 = object::normalize_coords(p.center, Vector2i(p.size)); + auto r0 = a.point_screen_pos(p0), r1 = a.point_screen_pos(p1); + draw.AddRect({r0.x(), r0.y()}, {r1.x(), r1.y()}, color); + } } void draw_ui(app& a, float width) override @@ -144,18 +159,21 @@ struct raycast_test : base_test return; } - do_raycasting(pending.from, pending.to); + do_raycasting(a, pending.from, pending.to); } } - void do_raycasting(point from, point to) + void do_raycasting(app& a, point from, point to) { constexpr auto inv_tile_size = 1. / Vector2d(iTILE_SIZE2); constexpr auto chunk_size = Vector2d{TILE_MAX_DIM}; - constexpr double eps = 1e-8; + constexpr double eps = 1e-6; + constexpr double inv_eps = 1/eps; constexpr double sqrt_2 = Math::sqrt(2.); constexpr double inv_sqrt_2 = 1. / sqrt_2; + result.has_result = false; + auto vec = Vector2d{}; vec += (Vector2d(to.chunk()) - Vector2d(from.chunk())) * chunk_size; vec += Vector2d(to.local()) - Vector2d(from.local()); @@ -188,15 +206,36 @@ struct raycast_test : base_test else { step = Math::abs(inv_sqrt_2 / dir[short_axis]); + step = Math::clamp(step, 1., TILE_MAX_DIM*.5); Debug{} << "step" << step; - step = Math::clamp(step, 1., chunk_size[short_axis] * .5); } + auto nsteps = (uint32_t)Math::ceil(vec.length() / step); + + result.path.clear(); + result.path.reserve(nsteps); + result.has_result = true; + + { + auto center = object::normalize_coords(from, Vector2i(dir * .5)); + Vector2d size; + size[short_axis] = (double)iTILE_SIZE2[short_axis]; + size[long_axis] = step * (double)iTILE_SIZE2[long_axis]; + Debug{} << "size" << size; + result.path.push_back(bbox{center, Vector2ui(size)}); + } + + auto dir_inv_norm = Vector2d{ - Math::abs(dir.x()) < eps ? 0. : 1. / dir.x(), - Math::abs(dir.y()) < eps ? 0. : 1. / dir.y(), + Math::abs(dir.x()) < eps ? std::copysign(inv_eps, dir.x()) : 1. / dir.x(), + Math::abs(dir.y()) < eps ? std::copysign(inv_eps, dir.y()) : 1. / dir.y(), }; auto signs = ray_aabb_signs(dir_inv_norm); + + for (auto i = 1u; i < nsteps; i++) + { + + } } }; |