Merge pull request #200 from orange-cpp/feature/aabb_improvemnt

Feature/aabb improvemnt
This commit is contained in:
2026-06-30 01:51:30 +03:00
committed by GitHub
7 changed files with 474 additions and 115 deletions
+301 -72
View File
@@ -16,7 +16,10 @@ namespace
struct frame_context struct frame_context
{ {
ID3D12Resource* render_target = nullptr; ID3D12Resource* render_target = nullptr;
// Each back buffer gets its own allocator because allocators cannot be reset while GPU work uses them.
ID3D12CommandAllocator* command_allocator = nullptr;
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = {}; D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = {};
UINT64 fence_value = 0;
}; };
bool g_initialized = false; bool g_initialized = false;
@@ -28,9 +31,188 @@ namespace
ID3D12DescriptorHeap* g_rtv_heap = nullptr; ID3D12DescriptorHeap* g_rtv_heap = nullptr;
ID3D12DescriptorHeap* g_srv_heap = nullptr; ID3D12DescriptorHeap* g_srv_heap = nullptr;
ID3D12GraphicsCommandList* g_command_list = nullptr; ID3D12GraphicsCommandList* g_command_list = nullptr;
ID3D12CommandAllocator* g_command_allocator = nullptr; ID3D12Fence* g_fence = nullptr;
HANDLE g_fence_event = nullptr;
UINT64 g_fence_value = 0;
std::vector<frame_context> g_frames; std::vector<frame_context> g_frames;
// This fence tracks only the overlay work submitted by this DLL, not the game's whole frame.
bool create_sync_objects()
{
if (g_fence)
return true;
if (FAILED(g_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&g_fence))))
return false;
g_fence_event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (!g_fence_event)
{
g_fence->Release();
g_fence = nullptr;
return false;
}
g_fence_value = 0;
return true;
}
bool wait_for_fence_value(UINT64 fence_value)
{
if (!g_fence || !g_fence_event || fence_value == 0 || g_fence->GetCompletedValue() >= fence_value)
return true;
if (FAILED(g_fence->SetEventOnCompletion(fence_value, g_fence_event)))
return false;
WaitForSingleObject(g_fence_event, INFINITE);
return true;
}
void wait_for_frame(frame_context& fc)
{
// The current back buffer's allocator is safe to reset only after its previous overlay pass completes.
if (wait_for_fence_value(fc.fence_value))
fc.fence_value = 0;
}
void wait_for_gpu()
{
// ResizeBuffers and shutdown must not release back buffers still referenced by queued overlay commands.
if (!g_command_queue || !g_fence || !g_fence_event)
return;
const UINT64 fence_value = ++g_fence_value;
if (FAILED(g_command_queue->Signal(g_fence, fence_value)))
return;
if (wait_for_fence_value(fence_value))
{
for (auto& fc : g_frames)
fc.fence_value = 0;
}
}
bool signal_frame(frame_context& fc)
{
if (!g_command_queue || !g_fence)
return false;
const UINT64 fence_value = ++g_fence_value;
if (FAILED(g_command_queue->Signal(g_fence, fence_value)))
return false;
fc.fence_value = fence_value;
return true;
}
void release_sync_objects()
{
if (g_fence_event)
{
CloseHandle(g_fence_event);
g_fence_event = nullptr;
}
if (g_fence)
{
g_fence->Release();
g_fence = nullptr;
}
g_fence_value = 0;
}
void release_frame_contexts()
{
for (auto& fc : g_frames)
{
if (fc.render_target)
{
fc.render_target->Release();
fc.render_target = nullptr;
}
if (fc.command_allocator)
{
fc.command_allocator->Release();
fc.command_allocator = nullptr;
}
fc.fence_value = 0;
}
g_frames.clear();
if (g_rtv_heap)
{
g_rtv_heap->Release();
g_rtv_heap = nullptr;
}
}
void release_command_objects()
{
if (g_command_list)
{
g_command_list->Release();
g_command_list = nullptr;
}
}
bool create_command_objects()
{
if (g_frames.empty() || !g_frames[0].command_allocator)
return false;
if (FAILED(g_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_frames[0].command_allocator,
nullptr, IID_PPV_ARGS(&g_command_list))))
{
release_command_objects();
return false;
}
g_command_list->Close();
return true;
}
bool create_render_targets(IDXGISwapChain* swap_chain)
{
// These references must be released before IDXGISwapChain::ResizeBuffers reaches the original function.
DXGI_SWAP_CHAIN_DESC desc{};
if (FAILED(swap_chain->GetDesc(&desc)))
return false;
const UINT buffer_count = desc.BufferCount;
D3D12_DESCRIPTOR_HEAP_DESC heap_desc{};
heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
heap_desc.NumDescriptors = buffer_count;
heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
heap_desc.NodeMask = 1;
if (FAILED(g_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&g_rtv_heap))))
return false;
g_frames.resize(buffer_count);
const UINT rtv_size = g_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = g_rtv_heap->GetCPUDescriptorHandleForHeapStart();
for (UINT i = 0; i < buffer_count; ++i)
{
g_frames[i].rtv_handle = rtv_handle;
if (FAILED(g_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&g_frames[i].command_allocator))))
{
release_frame_contexts();
return false;
}
if (FAILED(swap_chain->GetBuffer(i, IID_PPV_ARGS(&g_frames[i].render_target))))
{
release_frame_contexts();
return false;
}
g_device->CreateRenderTargetView(g_frames[i].render_target, nullptr, rtv_handle);
rtv_handle.ptr += rtv_size;
}
return true;
}
void init(IDXGISwapChain* swap_chain) void init(IDXGISwapChain* swap_chain)
{ {
g_init_attempted = true; g_init_attempted = true;
@@ -53,37 +235,15 @@ namespace
if (FAILED(g_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&g_srv_heap)))) if (FAILED(g_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&g_srv_heap))))
return; return;
} }
{
D3D12_DESCRIPTOR_HEAP_DESC heap_desc{};
heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
heap_desc.NumDescriptors = buffer_count;
heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
heap_desc.NodeMask = 1;
if (FAILED(g_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&g_rtv_heap))))
return;
}
if (FAILED(g_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, if (!create_render_targets(swap_chain))
IID_PPV_ARGS(&g_command_allocator))))
return; return;
g_frames.resize(buffer_count); if (!create_command_objects())
const UINT rtv_size = g_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); return;
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = g_rtv_heap->GetCPUDescriptorHandleForHeapStart();
if (!create_sync_objects())
for (UINT i = 0; i < buffer_count; ++i)
{
g_frames[i].rtv_handle = rtv_handle;
if (FAILED(swap_chain->GetBuffer(i, IID_PPV_ARGS(&g_frames[i].render_target))))
return;
g_device->CreateRenderTargetView(g_frames[i].render_target, nullptr, rtv_handle);
rtv_handle.ptr += rtv_size;
}
if (FAILED(g_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_command_allocator, nullptr,
IID_PPV_ARGS(&g_command_list))))
return; return;
g_command_list->Close();
ImGui::CreateContext(); ImGui::CreateContext();
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
@@ -114,27 +274,65 @@ namespace
void on_execute_command_lists(ID3D12CommandQueue* queue, UINT, ID3D12CommandList* const*) void on_execute_command_lists(ID3D12CommandQueue* queue, UINT, ID3D12CommandList* const*)
{ {
// The overlay records DIRECT command lists; executing them on COPY/COMPUTE queues can remove the device.
if (!g_command_queue) if (!g_command_queue)
g_command_queue = queue; {
const D3D12_COMMAND_QUEUE_DESC desc = queue->GetDesc();
if (desc.Type == D3D12_COMMAND_LIST_TYPE_DIRECT)
g_command_queue = queue;
}
} }
void on_present(IDXGISwapChain* swap_chain, UINT, UINT) bool ensure_initialized(IDXGISwapChain* swap_chain)
{ {
if (!g_initialized) if (g_initialized)
{ return true;
if (!g_init_attempted && g_command_queue)
init(swap_chain);
return;
}
if (!g_command_queue) if (!g_init_attempted && g_command_queue)
return; init(swap_chain);
return false;
}
bool ensure_render_targets(IDXGISwapChain* swap_chain)
{
if (!g_frames.empty() && g_rtv_heap)
return true;
return create_render_targets(swap_chain);
}
bool ensure_command_list()
{
if (g_command_list)
return true;
return create_command_objects();
}
bool ensure_sync_ready()
{
if (g_fence)
return true;
return create_sync_objects();
}
bool ensure_present_resources(IDXGISwapChain* swap_chain)
{
if (!ensure_initialized(swap_chain) || !g_command_queue)
return false;
return ensure_render_targets(swap_chain) && ensure_command_list() && ensure_sync_ready();
}
bool begin_imgui_frame()
{
if (GetAsyncKeyState(VK_INSERT) & 1) if (GetAsyncKeyState(VK_INSERT) & 1)
show_menu = !show_menu; show_menu = !show_menu;
if (!show_menu) if (!show_menu)
return; return false;
ImGui_ImplDX12_NewFrame(); ImGui_ImplDX12_NewFrame();
ImGui_ImplWin32_NewFrame(); ImGui_ImplWin32_NewFrame();
@@ -142,67 +340,97 @@ namespace
ImGui::GetIO().MouseDrawCursor = true; ImGui::GetIO().MouseDrawCursor = true;
ImGui::ShowDemoWindow(); ImGui::ShowDemoWindow();
ImGui::EndFrame(); ImGui::EndFrame();
return true;
}
frame_context* current_frame_context()
{
const UINT buf_idx = g_swap_chain->GetCurrentBackBufferIndex(); const UINT buf_idx = g_swap_chain->GetCurrentBackBufferIndex();
auto& fc = g_frames[buf_idx]; if (buf_idx >= g_frames.size())
return nullptr;
g_command_allocator->Reset(); return &g_frames[buf_idx];
g_command_list->Reset(g_command_allocator, nullptr); }
bool reset_overlay_command_list(frame_context& fc)
{
wait_for_frame(fc);
// Both resets depend on wait_for_frame(); otherwise DLSSG/Streamline can observe invalid GPU work.
if (FAILED(fc.command_allocator->Reset()))
return false;
if (FAILED(g_command_list->Reset(fc.command_allocator, nullptr)))
return false;
return true;
}
void transition_render_target(frame_context& fc, D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after)
{
D3D12_RESOURCE_BARRIER barrier{}; D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = fc.render_target; barrier.Transition.pResource = fc.render_target;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT; barrier.Transition.StateBefore = before;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET; barrier.Transition.StateAfter = after;
g_command_list->ResourceBarrier(1, &barrier); g_command_list->ResourceBarrier(1, &barrier);
}
void record_overlay_commands(frame_context& fc)
{
transition_render_target(fc, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
g_command_list->OMSetRenderTargets(1, &fc.rtv_handle, FALSE, nullptr); g_command_list->OMSetRenderTargets(1, &fc.rtv_handle, FALSE, nullptr);
g_command_list->SetDescriptorHeaps(1, &g_srv_heap); g_command_list->SetDescriptorHeaps(1, &g_srv_heap);
ImGui::Render(); ImGui::Render();
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), g_command_list); ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), g_command_list);
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET; transition_render_target(fc, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT; }
g_command_list->ResourceBarrier(1, &barrier);
g_command_list->Close(); bool submit_overlay_commands(frame_context& fc)
{
if (FAILED(g_command_list->Close()))
return false;
ID3D12CommandList* cmd_lists[] = {g_command_list}; ID3D12CommandList* cmd_lists[] = {g_command_list};
g_command_queue->ExecuteCommandLists(1, cmd_lists); g_command_queue->ExecuteCommandLists(1, cmd_lists);
return signal_frame(fc);
}
void on_present(IDXGISwapChain* swap_chain, UINT, UINT)
{
if (!ensure_present_resources(swap_chain) || !begin_imgui_frame())
return;
frame_context* fc = current_frame_context();
if (!fc || !reset_overlay_command_list(*fc))
return;
record_overlay_commands(*fc);
std::ignore = submit_overlay_commands(*fc);
}
void on_resize_buffers(IDXGISwapChain*, UINT, UINT, UINT, DXGI_FORMAT, UINT)
{
wait_for_gpu();
release_command_objects();
release_frame_contexts();
} }
void release_dx12_resources() void release_dx12_resources()
{ {
for (auto& fc : g_frames) wait_for_gpu();
{ release_command_objects();
if (fc.render_target) release_frame_contexts();
{ release_sync_objects();
fc.render_target->Release();
fc.render_target = nullptr;
}
}
g_frames.clear();
if (g_command_allocator)
{
g_command_allocator->Release();
g_command_allocator = nullptr;
}
if (g_command_list)
{
g_command_list->Release();
g_command_list = nullptr;
}
if (g_srv_heap) if (g_srv_heap)
{ {
g_srv_heap->Release(); g_srv_heap->Release();
g_srv_heap = nullptr; g_srv_heap = nullptr;
} }
if (g_rtv_heap)
{
g_rtv_heap->Release();
g_rtv_heap = nullptr;
}
if (g_swap_chain) if (g_swap_chain)
{ {
g_swap_chain->Release(); g_swap_chain->Release();
@@ -230,6 +458,7 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
auto& mgr = omath::hooks::HooksManager::get(); auto& mgr = omath::hooks::HooksManager::get();
mgr.set_on_present(on_present); mgr.set_on_present(on_present);
mgr.set_on_resize_buffers(on_resize_buffers);
mgr.set_on_execute_command_lists(on_execute_command_lists); mgr.set_on_execute_command_lists(on_execute_command_lists);
std::ignore = mgr.hook_dx12(); std::ignore = mgr.hook_dx12();
return 0; return 0;
+26 -6
View File
@@ -4,17 +4,28 @@
#pragma once #pragma once
#include "omath/linear_algebra/vector3.hpp" #include "omath/linear_algebra/vector3.hpp"
#include <array>
namespace omath::primitives namespace omath::primitives
{ {
enum class UpAxis { X, Y, Z }; enum class UpAxis
{
X,
Y,
Z
};
template<class Type> template<class Type, UpAxis Up = UpAxis::Y>
struct Aabb final struct Aabb final
{ {
Vector3<Type> min; Vector3<Type> min;
Vector3<Type> max; Vector3<Type> max;
[[nodiscard]]
static consteval UpAxis get_up_axis()
{
return Up;
}
[[nodiscard]] [[nodiscard]]
constexpr Vector3<Type> center() const noexcept constexpr Vector3<Type> center() const noexcept
{ {
@@ -27,7 +38,6 @@ namespace omath::primitives
return (max - min) / static_cast<Type>(2); return (max - min) / static_cast<Type>(2);
} }
template<UpAxis Up = UpAxis::Y>
[[nodiscard]] [[nodiscard]]
constexpr Vector3<Type> top() const noexcept constexpr Vector3<Type> top() const noexcept
{ {
@@ -42,7 +52,6 @@ namespace omath::primitives
std::unreachable(); std::unreachable();
} }
template<UpAxis Up = UpAxis::Y>
[[nodiscard]] [[nodiscard]]
constexpr Vector3<Type> bottom() const noexcept constexpr Vector3<Type> bottom() const noexcept
{ {
@@ -57,11 +66,22 @@ namespace omath::primitives
std::unreachable(); std::unreachable();
} }
[[nodiscard]]
constexpr std::array<Vector3<Type>, 8> vertices() const noexcept
{
return {
Vector3<Type>{min.x, min.y, min.z}, Vector3<Type>{max.x, min.y, min.z},
Vector3<Type>{min.x, max.y, min.z}, Vector3<Type>{max.x, max.y, min.z},
Vector3<Type>{min.x, min.y, max.z}, Vector3<Type>{max.x, min.y, max.z},
Vector3<Type>{min.x, max.y, max.z}, Vector3<Type>{max.x, max.y, max.z},
};
}
[[nodiscard]] [[nodiscard]]
constexpr bool is_collide(const Aabb& other) const noexcept constexpr bool is_collide(const Aabb& other) const noexcept
{ {
return min.x <= other.max.x && max.x >= other.min.x && return min.x <= other.max.x && max.x >= other.min.x && min.y <= other.max.y && max.y >= other.min.y
min.y <= other.max.y && max.y >= other.min.y &&min.z <= other.max.z && max.z >= other.min.z; && min.z <= other.max.z && max.z >= other.min.z;
} }
}; };
} // namespace omath::primitives } // namespace omath::primitives
+1
View File
@@ -9,6 +9,7 @@ namespace omath::hud
class CanvasBox final class CanvasBox final
{ {
public: public:
CanvasBox() = default;
CanvasBox(Vector2<float> top, Vector2<float> bottom, float ratio = 4.f); CanvasBox(Vector2<float> top, Vector2<float> bottom, float ratio = 4.f);
[[nodiscard("You have to use array")]] [[nodiscard("You have to use array")]]
+69 -13
View File
@@ -5,13 +5,21 @@
#include "canvas_box.hpp" #include "canvas_box.hpp"
#include "entity_overlay_widgets.hpp" #include "entity_overlay_widgets.hpp"
#include "hud_renderer_interface.hpp" #include "hud_renderer_interface.hpp"
#include "omath/3d_primitives/aabb.hpp"
#include "omath/linear_algebra/vector2.hpp" #include "omath/linear_algebra/vector2.hpp"
#include "omath/utility/color.hpp" #include "omath/utility/color.hpp"
#include <expected>
#include <memory> #include <memory>
#include <string_view> #include <string_view>
#include <type_traits>
namespace omath::hud namespace omath::hud
{ {
enum class EntityOverlayError
{
NO_PROJECTED_VERTEX,
CENTER_PROJECTION_FAILED,
};
class EntityOverlay final class EntityOverlay final
{ {
public: public:
@@ -57,13 +65,17 @@ namespace omath::hud
float offset = 5.f); float offset = 5.f);
// ── Labels ─────────────────────────────────────────────────────── // ── Labels ───────────────────────────────────────────────────────
EntityOverlay& add_right_label(const Color& color, float offset, widget::Outlined outlined, const std::string_view& text); EntityOverlay& add_right_label(const Color& color, float offset, widget::Outlined outlined,
const std::string_view& text);
EntityOverlay& add_left_label(const Color& color, float offset, widget::Outlined outlined, const std::string_view& text); EntityOverlay& add_left_label(const Color& color, float offset, widget::Outlined outlined,
const std::string_view& text);
EntityOverlay& add_top_label(const Color& color, float offset, widget::Outlined outlined, std::string_view text); EntityOverlay& add_top_label(const Color& color, float offset, widget::Outlined outlined,
std::string_view text);
EntityOverlay& add_bottom_label(const Color& color, float offset, widget::Outlined outlined, std::string_view text); EntityOverlay& add_bottom_label(const Color& color, float offset, widget::Outlined outlined,
std::string_view text);
EntityOverlay& add_centered_top_label(const Color& color, float offset, widget::Outlined outlined, EntityOverlay& add_centered_top_label(const Color& color, float offset, widget::Outlined outlined,
const std::string_view& text); const std::string_view& text);
@@ -72,24 +84,24 @@ namespace omath::hud
const std::string_view& text); const std::string_view& text);
template<typename... Args> template<typename... Args>
EntityOverlay& add_right_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt, EntityOverlay& add_right_label(const Color& color, const float offset, const widget::Outlined outlined,
Args&&... args) std::format_string<Args...> fmt, Args&&... args)
{ {
return add_right_label(color, offset, outlined, return add_right_label(color, offset, outlined,
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))}); std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
} }
template<typename... Args> template<typename... Args>
EntityOverlay& add_left_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt, EntityOverlay& add_left_label(const Color& color, const float offset, const widget::Outlined outlined,
Args&&... args) std::format_string<Args...> fmt, Args&&... args)
{ {
return add_left_label(color, offset, outlined, return add_left_label(color, offset, outlined,
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))}); std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
} }
template<typename... Args> template<typename... Args>
EntityOverlay& add_top_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt, EntityOverlay& add_top_label(const Color& color, const float offset, const widget::Outlined outlined,
Args&&... args) std::format_string<Args...> fmt, Args&&... args)
{ {
return add_top_label(color, offset, outlined, return add_top_label(color, offset, outlined,
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))}); std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
@@ -112,8 +124,9 @@ namespace omath::hud
} }
template<typename... Args> template<typename... Args>
EntityOverlay& add_centered_bottom_label(const Color& color, const float offset, const widget::Outlined outlined, EntityOverlay& add_centered_bottom_label(const Color& color, const float offset,
std::format_string<Args...> fmt, Args&&... args) const widget::Outlined outlined, std::format_string<Args...> fmt,
Args&&... args)
{ {
return add_centered_bottom_label(color, offset, outlined, return add_centered_bottom_label(color, offset, outlined,
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))}); std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
@@ -164,6 +177,49 @@ namespace omath::hud
return *this; return *this;
} }
template<class Camera, class Aabb>
[[nodiscard]]
static std::expected<EntityOverlay, EntityOverlayError>
from_aabb(const Camera& camera, const Aabb& aabb, const float aspect,
const std::shared_ptr<HudRendererInterface>& renderer)
{
using ProjectedVector = std::invoke_result_t<decltype(&Aabb::center), const Aabb&>;
ProjectedVector top;
ProjectedVector bottom;
bool has_projected_vertex = false;
for (const auto& vertex : aabb.vertices())
{
if (auto projected = camera.world_to_screen_unclipped(vertex))
{
if (!has_projected_vertex)
{
top = *projected;
bottom = *projected;
has_projected_vertex = true;
continue;
}
if (projected->y < top.y)
top = *projected;
if (projected->y > bottom.y)
bottom = *projected;
}
}
if (!has_projected_vertex)
return std::unexpected(EntityOverlayError::NO_PROJECTED_VERTEX);
auto center = camera.world_to_screen_unclipped(aabb.center());
if (!center)
return std::unexpected(EntityOverlayError::CENTER_PROJECTION_FAILED);
const auto center_x = static_cast<float>(center->x);
return EntityOverlay{
{center_x, static_cast<float>(top.y)}, {center_x, static_cast<float>(bottom.y)}, aspect, renderer};
}
private: private:
// optional<W> dispatch — enables when() conditional widgets // optional<W> dispatch — enables when() conditional widgets
template<typename W> template<typename W>
+6 -6
View File
@@ -119,11 +119,11 @@ namespace
switch (axis) switch (axis)
{ {
case omath::primitives::UpAxis::X: case omath::primitives::UpAxis::X:
return aabb.top<omath::primitives::UpAxis::X>(); return omath::primitives::Aabb<float, omath::primitives::UpAxis::X>{aabb.min, aabb.max}.top();
case omath::primitives::UpAxis::Y: case omath::primitives::UpAxis::Y:
return aabb.top<omath::primitives::UpAxis::Y>(); return aabb.top();
case omath::primitives::UpAxis::Z: case omath::primitives::UpAxis::Z:
return aabb.top<omath::primitives::UpAxis::Z>(); return omath::primitives::Aabb<float, omath::primitives::UpAxis::Z>{aabb.min, aabb.max}.top();
} }
std::unreachable(); std::unreachable();
} }
@@ -133,11 +133,11 @@ namespace
switch (axis) switch (axis)
{ {
case omath::primitives::UpAxis::X: case omath::primitives::UpAxis::X:
return aabb.bottom<omath::primitives::UpAxis::X>(); return omath::primitives::Aabb<float, omath::primitives::UpAxis::X>{aabb.min, aabb.max}.bottom();
case omath::primitives::UpAxis::Y: case omath::primitives::UpAxis::Y:
return aabb.bottom<omath::primitives::UpAxis::Y>(); return aabb.bottom();
case omath::primitives::UpAxis::Z: case omath::primitives::UpAxis::Z:
return aabb.bottom<omath::primitives::UpAxis::Z>(); return omath::primitives::Aabb<float, omath::primitives::UpAxis::Z>{aabb.min, aabb.max}.bottom();
} }
std::unreachable(); std::unreachable();
} }
+38 -18
View File
@@ -4,7 +4,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "omath/3d_primitives/aabb.hpp" #include "omath/3d_primitives/aabb.hpp"
using UpAxis = omath::primitives::UpAxis;
using AABB = omath::primitives::Aabb<float>; using AABB = omath::primitives::Aabb<float>;
using AABBZ = omath::primitives::Aabb<float, UpAxis::Z>;
using Vec3 = omath::Vector3<float>; using Vec3 = omath::Vector3<float>;
// --- center() --- // --- center() ---
@@ -65,14 +67,12 @@ TEST(AabbTests, ExtentsOfDegenerateBox)
EXPECT_FLOAT_EQ(e.z, 0.f); EXPECT_FLOAT_EQ(e.z, 0.f);
} }
using UpAxis = omath::primitives::UpAxis;
// --- top() --- // --- top() ---
TEST(AabbTests, TopYUpSymmetricBox) TEST(AabbTests, TopYUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto t = box.top<UpAxis::Y>(); constexpr auto t = box.top();
EXPECT_FLOAT_EQ(t.x, 0.f); EXPECT_FLOAT_EQ(t.x, 0.f);
EXPECT_FLOAT_EQ(t.y, 2.f); EXPECT_FLOAT_EQ(t.y, 2.f);
EXPECT_FLOAT_EQ(t.z, 0.f); EXPECT_FLOAT_EQ(t.z, 0.f);
@@ -81,7 +81,7 @@ TEST(AabbTests, TopYUpSymmetricBox)
TEST(AabbTests, TopYUpOffsetBox) TEST(AabbTests, TopYUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto t = box.top<UpAxis::Y>(); constexpr auto t = box.top();
EXPECT_FLOAT_EQ(t.x, 2.f); EXPECT_FLOAT_EQ(t.x, 2.f);
EXPECT_FLOAT_EQ(t.y, 10.f); EXPECT_FLOAT_EQ(t.y, 10.f);
EXPECT_FLOAT_EQ(t.z, 4.f); EXPECT_FLOAT_EQ(t.z, 4.f);
@@ -89,8 +89,8 @@ TEST(AabbTests, TopYUpOffsetBox)
TEST(AabbTests, TopZUpSymmetricBox) TEST(AabbTests, TopZUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto t = box.top<UpAxis::Z>(); constexpr auto t = box.top();
EXPECT_FLOAT_EQ(t.x, 0.f); EXPECT_FLOAT_EQ(t.x, 0.f);
EXPECT_FLOAT_EQ(t.y, 0.f); EXPECT_FLOAT_EQ(t.y, 0.f);
EXPECT_FLOAT_EQ(t.z, 3.f); EXPECT_FLOAT_EQ(t.z, 3.f);
@@ -98,8 +98,8 @@ TEST(AabbTests, TopZUpSymmetricBox)
TEST(AabbTests, TopZUpOffsetBox) TEST(AabbTests, TopZUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto t = box.top<UpAxis::Z>(); constexpr auto t = box.top();
EXPECT_FLOAT_EQ(t.x, 2.f); EXPECT_FLOAT_EQ(t.x, 2.f);
EXPECT_FLOAT_EQ(t.y, 7.f); EXPECT_FLOAT_EQ(t.y, 7.f);
EXPECT_FLOAT_EQ(t.z, 6.f); EXPECT_FLOAT_EQ(t.z, 6.f);
@@ -108,7 +108,8 @@ TEST(AabbTests, TopZUpOffsetBox)
TEST(AabbTests, TopDefaultIsYUp) TEST(AabbTests, TopDefaultIsYUp)
{ {
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}}; constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}};
EXPECT_EQ(box.top(), box.top<UpAxis::Y>()); EXPECT_EQ(AABB::get_up_axis(), UpAxis::Y);
EXPECT_FLOAT_EQ(box.top().y, 4.f);
} }
// --- bottom() --- // --- bottom() ---
@@ -116,7 +117,7 @@ TEST(AabbTests, TopDefaultIsYUp)
TEST(AabbTests, BottomYUpSymmetricBox) TEST(AabbTests, BottomYUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto b = box.bottom<UpAxis::Y>(); constexpr auto b = box.bottom();
EXPECT_FLOAT_EQ(b.x, 0.f); EXPECT_FLOAT_EQ(b.x, 0.f);
EXPECT_FLOAT_EQ(b.y, -2.f); EXPECT_FLOAT_EQ(b.y, -2.f);
EXPECT_FLOAT_EQ(b.z, 0.f); EXPECT_FLOAT_EQ(b.z, 0.f);
@@ -125,7 +126,7 @@ TEST(AabbTests, BottomYUpSymmetricBox)
TEST(AabbTests, BottomYUpOffsetBox) TEST(AabbTests, BottomYUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto b = box.bottom<UpAxis::Y>(); constexpr auto b = box.bottom();
EXPECT_FLOAT_EQ(b.x, 2.f); EXPECT_FLOAT_EQ(b.x, 2.f);
EXPECT_FLOAT_EQ(b.y, 4.f); EXPECT_FLOAT_EQ(b.y, 4.f);
EXPECT_FLOAT_EQ(b.z, 4.f); EXPECT_FLOAT_EQ(b.z, 4.f);
@@ -133,8 +134,8 @@ TEST(AabbTests, BottomYUpOffsetBox)
TEST(AabbTests, BottomZUpSymmetricBox) TEST(AabbTests, BottomZUpSymmetricBox)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto b = box.bottom<UpAxis::Z>(); constexpr auto b = box.bottom();
EXPECT_FLOAT_EQ(b.x, 0.f); EXPECT_FLOAT_EQ(b.x, 0.f);
EXPECT_FLOAT_EQ(b.y, 0.f); EXPECT_FLOAT_EQ(b.y, 0.f);
EXPECT_FLOAT_EQ(b.z, -3.f); EXPECT_FLOAT_EQ(b.z, -3.f);
@@ -142,8 +143,8 @@ TEST(AabbTests, BottomZUpSymmetricBox)
TEST(AabbTests, BottomZUpOffsetBox) TEST(AabbTests, BottomZUpOffsetBox)
{ {
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}}; constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto b = box.bottom<UpAxis::Z>(); constexpr auto b = box.bottom();
EXPECT_FLOAT_EQ(b.x, 2.f); EXPECT_FLOAT_EQ(b.x, 2.f);
EXPECT_FLOAT_EQ(b.y, 7.f); EXPECT_FLOAT_EQ(b.y, 7.f);
EXPECT_FLOAT_EQ(b.z, 2.f); EXPECT_FLOAT_EQ(b.z, 2.f);
@@ -152,14 +153,33 @@ TEST(AabbTests, BottomZUpOffsetBox)
TEST(AabbTests, BottomDefaultIsYUp) TEST(AabbTests, BottomDefaultIsYUp)
{ {
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}}; constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}};
EXPECT_EQ(box.bottom(), box.bottom<UpAxis::Y>()); EXPECT_EQ(AABB::get_up_axis(), UpAxis::Y);
EXPECT_FLOAT_EQ(box.bottom().y, 0.f);
} }
TEST(AabbTests, TopAndBottomAreSymmetric) TEST(AabbTests, TopAndBottomAreSymmetric)
{ {
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}}; constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
EXPECT_FLOAT_EQ(box.top<UpAxis::Y>().y, -box.bottom<UpAxis::Y>().y); constexpr AABBZ z_up_box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
EXPECT_FLOAT_EQ(box.top<UpAxis::Z>().z, -box.bottom<UpAxis::Z>().z); EXPECT_FLOAT_EQ(box.top().y, -box.bottom().y);
EXPECT_FLOAT_EQ(z_up_box.top().z, -z_up_box.bottom().z);
}
// --- vertices() ---
TEST(AabbTests, VerticesReturnsAllCorners)
{
constexpr AABB box{{1.f, 2.f, 3.f}, {4.f, 6.f, 8.f}};
constexpr auto v = box.vertices();
EXPECT_EQ(v[0], Vec3(1.f, 2.f, 3.f));
EXPECT_EQ(v[1], Vec3(4.f, 2.f, 3.f));
EXPECT_EQ(v[2], Vec3(1.f, 6.f, 3.f));
EXPECT_EQ(v[3], Vec3(4.f, 6.f, 3.f));
EXPECT_EQ(v[4], Vec3(1.f, 2.f, 8.f));
EXPECT_EQ(v[5], Vec3(4.f, 2.f, 8.f));
EXPECT_EQ(v[6], Vec3(1.f, 6.f, 8.f));
EXPECT_EQ(v[7], Vec3(4.f, 6.f, 8.f));
} }
// --- is_collide() --- // --- is_collide() ---
@@ -0,0 +1,33 @@
//
// Created by orange on 30.06.2026.
//
#include <gtest/gtest.h>
#include <memory>
#include <omath/3d_primitives/aabb.hpp>
#include <omath/engines/unreal_engine/camera.hpp>
#include <omath/hud/entity_overlay.hpp>
TEST(EntityOverlayTests, FromAabbSupportsDoubleCameraCoordinates)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const omath::unreal_engine::Camera camera{{0.0, 0.0, 0.0}, {}, {1920.f, 1080.f}, fov, 0.01, 1000.0};
const omath::primitives::Aabb<double> aabb{{90.0, -1.0, -1.0}, {110.0, 1.0, 1.0}};
const auto overlay = omath::hud::EntityOverlay::from_aabb(camera, aabb, 0.5f,
std::shared_ptr<omath::hud::HudRendererInterface>{});
ASSERT_TRUE(overlay.has_value());
}
TEST(EntityOverlayTests, FromAabbReturnsErrorCodeIfNoVertexProjects)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const omath::unreal_engine::Camera camera{{0.0, 0.0, 0.0}, {}, {1920.f, 1080.f}, fov, 0.01, 1000.0};
const omath::primitives::Aabb<double> aabb{{-110.0, -1.0, -1.0}, {-90.0, 1.0, 1.0}};
const auto overlay = omath::hud::EntityOverlay::from_aabb(camera, aabb, 0.5f,
std::shared_ptr<omath::hud::HudRendererInterface>{});
ASSERT_FALSE(overlay.has_value());
EXPECT_EQ(overlay.error(), omath::hud::EntityOverlayError::NO_PROJECTED_VERTEX);
}