mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-08 14:02:06 +00:00
Compare commits
23 Commits
v5.4.0
...
78d92a3c0f
| Author | SHA1 | Date | |
|---|---|---|---|
| 78d92a3c0f | |||
| d2ef40e0bd | |||
| d67d956aca | |||
| 5e3bb0e961 | |||
| 11b429d975 | |||
| b4f4917aec | |||
| 9299ea8960 | |||
| 1982185b71 | |||
| 0a3b10022f | |||
| 935f69426c | |||
| 9eb4dd1155 | |||
| fdcb7845cf | |||
| 3e3d2e5a23 | |||
| 58d3e1904c | |||
| d9a6f6ec91 | |||
| 468a1f4703 | |||
| db99a1ecfc | |||
| 8b4614eb89 | |||
| 0a072b0d56 | |||
| 2c60959e2b | |||
| d7ec571a7a | |||
| 235917008c | |||
| 71e256d033 |
@@ -5,6 +5,8 @@
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_dx11.h>
|
||||
#include <imgui_impl_win32.h>
|
||||
#include <thread>
|
||||
#include <tuple>
|
||||
|
||||
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
@@ -17,13 +19,35 @@ namespace
|
||||
ID3D11DeviceContext* g_context = nullptr;
|
||||
ID3D11RenderTargetView* g_render_target_view = nullptr;
|
||||
|
||||
void create_render_target(IDXGISwapChain* swap_chain)
|
||||
[[nodiscard]]
|
||||
bool create_render_target(IDXGISwapChain* swap_chain)
|
||||
{
|
||||
ID3D11Texture2D* back_buffer = nullptr;
|
||||
if (FAILED(swap_chain->GetBuffer(0, IID_PPV_ARGS(&back_buffer))))
|
||||
return;
|
||||
g_device->CreateRenderTargetView(back_buffer, nullptr, &g_render_target_view);
|
||||
return false;
|
||||
|
||||
D3D11_RENDER_TARGET_VIEW_DESC desc{};
|
||||
desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
|
||||
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
|
||||
const HRESULT result = g_device->CreateRenderTargetView(back_buffer, &desc, &g_render_target_view);
|
||||
back_buffer->Release();
|
||||
return SUCCEEDED(result);
|
||||
}
|
||||
|
||||
void release_render_target()
|
||||
{
|
||||
if (g_context)
|
||||
g_context->OMSetRenderTargets(0, nullptr, nullptr);
|
||||
|
||||
if (g_render_target_view)
|
||||
{
|
||||
g_render_target_view->Release();
|
||||
g_render_target_view = nullptr;
|
||||
}
|
||||
|
||||
if (g_context)
|
||||
g_context->Flush();
|
||||
}
|
||||
|
||||
void init(IDXGISwapChain* swap_chain)
|
||||
@@ -36,9 +60,9 @@ namespace
|
||||
g_device->GetImmediateContext(&g_context);
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC desc{};
|
||||
swap_chain->GetDesc(&desc);
|
||||
std::ignore = swap_chain->GetDesc(&desc);
|
||||
|
||||
create_render_target(swap_chain);
|
||||
std::ignore = create_render_target(swap_chain);
|
||||
|
||||
ImGui::CreateContext();
|
||||
ImGui::StyleColorsDark();
|
||||
@@ -51,7 +75,7 @@ namespace
|
||||
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_wnd_proc(
|
||||
[](HWND h, UINT msg, WPARAM wp, LPARAM lp) -> std::optional<LRESULT>
|
||||
[](const HWND h, const UINT msg, const WPARAM wp, const LPARAM lp) -> std::optional<LRESULT>
|
||||
{
|
||||
if (ImGui_ImplWin32_WndProcHandler(h, msg, wp, lp))
|
||||
return 0;
|
||||
@@ -71,44 +95,49 @@ namespace
|
||||
return;
|
||||
}
|
||||
|
||||
if (!g_render_target_view)
|
||||
create_render_target(swap_chain);
|
||||
if (!g_render_target_view && !create_render_target(swap_chain))
|
||||
return;
|
||||
|
||||
ID3D11RenderTargetView* previous_render_target_views[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT]{};
|
||||
ID3D11DepthStencilView* previous_depth_stencil_view = nullptr;
|
||||
g_context->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, previous_render_target_views,
|
||||
&previous_depth_stencil_view);
|
||||
|
||||
g_context->OMSetRenderTargets(1, &g_render_target_view, nullptr);
|
||||
|
||||
ImGui_ImplDX11_NewFrame();
|
||||
ImGui_ImplWin32_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::SetNextWindowSize({300.f, 80.f}, ImGuiCond_Once);
|
||||
ImGui::SetNextWindowPos({10.f, 10.f}, ImGuiCond_Once);
|
||||
ImGui::Begin("omath | DX11 hook");
|
||||
ImGui::Text("Hook active");
|
||||
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
|
||||
ImGui::End();
|
||||
ImGui::GetIO().MouseDrawCursor = true;
|
||||
ImGui::ShowDemoWindow();
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
g_context->OMSetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, previous_render_target_views,
|
||||
previous_depth_stencil_view);
|
||||
for (ID3D11RenderTargetView* render_target_view : previous_render_target_views)
|
||||
{
|
||||
if (render_target_view)
|
||||
render_target_view->Release();
|
||||
}
|
||||
if (previous_depth_stencil_view)
|
||||
previous_depth_stencil_view->Release();
|
||||
}
|
||||
|
||||
void on_resize_buffers(IDXGISwapChain*, UINT, UINT, UINT, DXGI_FORMAT, UINT)
|
||||
{
|
||||
if (g_render_target_view)
|
||||
{
|
||||
g_render_target_view->Release();
|
||||
g_render_target_view = nullptr;
|
||||
}
|
||||
release_render_target();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
BOOL WINAPI DllMain(const HINSTANCE h_instance, const DWORD reason, LPVOID)
|
||||
{
|
||||
if (reason == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
DisableThreadLibraryCalls(h_instance);
|
||||
CreateThread(
|
||||
nullptr, 0,
|
||||
[](LPVOID) -> DWORD
|
||||
std::thread(
|
||||
[]()
|
||||
{
|
||||
while (!GetModuleHandle("d3d11.dll"))
|
||||
Sleep(100);
|
||||
@@ -116,10 +145,10 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_present(on_present);
|
||||
mgr.set_on_resize_buffers(on_resize_buffers);
|
||||
mgr.hook_dx11();
|
||||
std::ignore = mgr.hook_dx11();
|
||||
return 0;
|
||||
},
|
||||
nullptr, 0, nullptr);
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
else if (reason == DLL_PROCESS_DETACH)
|
||||
{
|
||||
@@ -134,11 +163,7 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
if (g_render_target_view)
|
||||
{
|
||||
g_render_target_view->Release();
|
||||
g_render_target_view = nullptr;
|
||||
}
|
||||
release_render_target();
|
||||
if (g_context)
|
||||
{
|
||||
g_context->Release();
|
||||
|
||||
@@ -16,7 +16,10 @@ namespace
|
||||
struct frame_context
|
||||
{
|
||||
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 = {};
|
||||
UINT64 fence_value = 0;
|
||||
};
|
||||
|
||||
bool g_initialized = false;
|
||||
@@ -28,9 +31,188 @@ namespace
|
||||
ID3D12DescriptorHeap* g_rtv_heap = nullptr;
|
||||
ID3D12DescriptorHeap* g_srv_heap = 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;
|
||||
|
||||
// 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)
|
||||
{
|
||||
g_init_attempted = true;
|
||||
@@ -53,37 +235,15 @@ namespace
|
||||
if (FAILED(g_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&g_srv_heap))))
|
||||
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,
|
||||
IID_PPV_ARGS(&g_command_allocator))))
|
||||
if (!create_render_targets(swap_chain))
|
||||
return;
|
||||
|
||||
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(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))))
|
||||
if (!create_command_objects())
|
||||
return;
|
||||
|
||||
if (!create_sync_objects())
|
||||
return;
|
||||
g_command_list->Close();
|
||||
|
||||
ImGui::CreateContext();
|
||||
ImGui::StyleColorsDark();
|
||||
@@ -114,27 +274,65 @@ namespace
|
||||
|
||||
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)
|
||||
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_init_attempted && g_command_queue)
|
||||
init(swap_chain);
|
||||
return;
|
||||
}
|
||||
if (g_initialized)
|
||||
return true;
|
||||
|
||||
if (!g_command_queue)
|
||||
return;
|
||||
if (!g_init_attempted && g_command_queue)
|
||||
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)
|
||||
show_menu = !show_menu;
|
||||
|
||||
if (!show_menu)
|
||||
return;
|
||||
return false;
|
||||
|
||||
ImGui_ImplDX12_NewFrame();
|
||||
ImGui_ImplWin32_NewFrame();
|
||||
@@ -142,67 +340,97 @@ namespace
|
||||
ImGui::GetIO().MouseDrawCursor = true;
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::EndFrame();
|
||||
return true;
|
||||
}
|
||||
|
||||
frame_context* current_frame_context()
|
||||
{
|
||||
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();
|
||||
g_command_list->Reset(g_command_allocator, nullptr);
|
||||
return &g_frames[buf_idx];
|
||||
}
|
||||
|
||||
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{};
|
||||
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
||||
barrier.Transition.pResource = fc.render_target;
|
||||
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
||||
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
|
||||
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
||||
barrier.Transition.StateBefore = before;
|
||||
barrier.Transition.StateAfter = after;
|
||||
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->SetDescriptorHeaps(1, &g_srv_heap);
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), g_command_list);
|
||||
|
||||
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
||||
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
|
||||
g_command_list->ResourceBarrier(1, &barrier);
|
||||
g_command_list->Close();
|
||||
transition_render_target(fc, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
|
||||
}
|
||||
|
||||
bool submit_overlay_commands(frame_context& fc)
|
||||
{
|
||||
if (FAILED(g_command_list->Close()))
|
||||
return false;
|
||||
|
||||
ID3D12CommandList* cmd_lists[] = {g_command_list};
|
||||
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()
|
||||
{
|
||||
for (auto& fc : g_frames)
|
||||
{
|
||||
if (fc.render_target)
|
||||
{
|
||||
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;
|
||||
}
|
||||
wait_for_gpu();
|
||||
release_command_objects();
|
||||
release_frame_contexts();
|
||||
release_sync_objects();
|
||||
if (g_srv_heap)
|
||||
{
|
||||
g_srv_heap->Release();
|
||||
g_srv_heap = nullptr;
|
||||
}
|
||||
if (g_rtv_heap)
|
||||
{
|
||||
g_rtv_heap->Release();
|
||||
g_rtv_heap = nullptr;
|
||||
}
|
||||
if (g_swap_chain)
|
||||
{
|
||||
g_swap_chain->Release();
|
||||
@@ -221,20 +449,20 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
if (reason == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
DisableThreadLibraryCalls(h_instance);
|
||||
CreateThread(
|
||||
nullptr, 0,
|
||||
[](LPVOID) -> DWORD
|
||||
std::thread(
|
||||
[]()
|
||||
{
|
||||
while (!GetModuleHandle("d3d12.dll"))
|
||||
Sleep(100);
|
||||
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_present(on_present);
|
||||
mgr.set_on_resize_buffers(on_resize_buffers);
|
||||
mgr.set_on_execute_command_lists(on_execute_command_lists);
|
||||
std::ignore = mgr.hook_dx12();
|
||||
return 0;
|
||||
},
|
||||
nullptr, 0, nullptr);
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
else if (reason == DLL_PROCESS_DETACH)
|
||||
{
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
#include "omath/hooks/hooks_manager.hpp"
|
||||
#include <Windows.h>
|
||||
#include <d3d9.h>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_dx9.h>
|
||||
#include <imgui_impl_win32.h>
|
||||
|
||||
#include "omath/hooks/hooks_manager.hpp"
|
||||
|
||||
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
namespace
|
||||
{
|
||||
bool g_initialized = false;
|
||||
bool g_initialized = false;
|
||||
bool g_init_attempted = false;
|
||||
|
||||
void init(IDirect3DDevice9* device)
|
||||
@@ -31,12 +30,14 @@ namespace
|
||||
ImGui_ImplDX9_Init(device);
|
||||
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_wnd_proc([](HWND h, UINT msg, WPARAM wp, LPARAM lp) -> std::optional<LRESULT> {
|
||||
if (ImGui_ImplWin32_WndProcHandler(h, msg, wp, lp))
|
||||
return 0;
|
||||
return std::nullopt;
|
||||
});
|
||||
mgr.hook_wnd_proc(params.hFocusWindow);
|
||||
mgr.set_on_wnd_proc(
|
||||
[](HWND h, UINT msg, WPARAM wp, LPARAM lp) -> std::optional<LRESULT>
|
||||
{
|
||||
if (ImGui_ImplWin32_WndProcHandler(h, msg, wp, lp))
|
||||
return 0;
|
||||
return std::nullopt;
|
||||
});
|
||||
std::ignore = mgr.hook_wnd_proc(params.hFocusWindow);
|
||||
|
||||
g_initialized = true;
|
||||
}
|
||||
@@ -54,11 +55,8 @@ namespace
|
||||
ImGui_ImplWin32_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::SetNextWindowSize({300.f, 80.f}, ImGuiCond_Once);
|
||||
ImGui::SetNextWindowPos({10.f, 10.f}, ImGuiCond_Once);
|
||||
ImGui::Begin("omath | DX9 hook");
|
||||
ImGui::Text("Hook active");
|
||||
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
|
||||
ImGui::GetIO().MouseDrawCursor = true;
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::End();
|
||||
|
||||
ImGui::EndFrame();
|
||||
@@ -78,17 +76,19 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
if (reason == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
DisableThreadLibraryCalls(h_instance);
|
||||
CreateThread(nullptr, 0, [](LPVOID) -> DWORD
|
||||
{
|
||||
while (!GetModuleHandle("d3d9.dll"))
|
||||
Sleep(100);
|
||||
std::thread(
|
||||
[]()
|
||||
{
|
||||
while (!GetModuleHandle("d3d9.dll"))
|
||||
Sleep(100);
|
||||
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_dx9_present(on_present);
|
||||
mgr.set_on_dx9_reset(on_reset);
|
||||
mgr.hook_dx9();
|
||||
return 0;
|
||||
}, nullptr, 0, nullptr);
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_dx9_present(on_present);
|
||||
mgr.set_on_dx9_reset(on_reset);
|
||||
std::ignore = mgr.hook_dx9();
|
||||
return 0;
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
else if (reason == DLL_PROCESS_DETACH)
|
||||
{
|
||||
|
||||
@@ -4,17 +4,28 @@
|
||||
|
||||
#pragma once
|
||||
#include "omath/linear_algebra/vector3.hpp"
|
||||
#include <array>
|
||||
|
||||
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
|
||||
{
|
||||
Vector3<Type> min;
|
||||
Vector3<Type> max;
|
||||
|
||||
[[nodiscard]]
|
||||
static consteval UpAxis get_up_axis()
|
||||
{
|
||||
return Up;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<Type> center() const noexcept
|
||||
{
|
||||
@@ -27,7 +38,6 @@ namespace omath::primitives
|
||||
return (max - min) / static_cast<Type>(2);
|
||||
}
|
||||
|
||||
template<UpAxis Up = UpAxis::Y>
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<Type> top() const noexcept
|
||||
{
|
||||
@@ -42,7 +52,6 @@ namespace omath::primitives
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
template<UpAxis Up = UpAxis::Y>
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<Type> bottom() const noexcept
|
||||
{
|
||||
@@ -57,11 +66,22 @@ namespace omath::primitives
|
||||
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]]
|
||||
constexpr bool is_collide(const Aabb& other) const noexcept
|
||||
{
|
||||
return min.x <= other.max.x && max.x >= other.min.x &&
|
||||
min.y <= other.max.y && max.y >= other.min.y &&min.z <= other.max.z && max.z >= other.min.z;
|
||||
return min.x <= other.max.x && max.x >= other.min.x && min.y <= other.max.y && max.y >= other.min.y
|
||||
&& min.z <= other.max.z && max.z >= other.min.z;
|
||||
}
|
||||
};
|
||||
} // namespace omath::primitives
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by orange on 7/1/2026.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/linear_algebra/vector3.hpp"
|
||||
#include <type_traits>
|
||||
|
||||
namespace omath::algorithm
|
||||
{
|
||||
template<class Camera, class FloatingType>
|
||||
requires std::is_floating_point_v<FloatingType>
|
||||
[[nodiscard]]
|
||||
constexpr Vector2<float> world_to_radar(const Camera& camera, const Vector3<FloatingType>& position, const FloatingType scale)
|
||||
{
|
||||
const auto look_at_angles = camera.calc_look_at_angles(position);
|
||||
const auto current_angles = camera.get_view_angles();
|
||||
|
||||
if consteval
|
||||
{
|
||||
const auto right_yaw = current_angles.yaw
|
||||
- camera.calc_look_at_angles(camera.get_origin() + camera.get_abs_right()).yaw
|
||||
- decltype(current_angles.yaw)::from_degrees(90);
|
||||
const auto sign = right_yaw.cos() < 0 ? -1.f : 1.f;
|
||||
|
||||
const auto yaw = current_angles.yaw - look_at_angles.yaw - decltype(current_angles.yaw)::from_degrees(90);
|
||||
return omath::Vector2<float>(static_cast<float>(yaw.cos()) * sign, static_cast<float>(yaw.sin()))
|
||||
* (camera.get_origin().distance_to(position) * scale);
|
||||
}
|
||||
static const auto sign = [&camera, ¤t_angles]
|
||||
{
|
||||
const auto right_yaw = current_angles.yaw
|
||||
- camera.calc_look_at_angles(camera.get_origin() + camera.get_abs_right()).yaw
|
||||
- decltype(current_angles.yaw)::from_degrees(90);
|
||||
return right_yaw.cos() < 0 ? -1.f : 1.f;
|
||||
}();
|
||||
|
||||
const auto yaw = current_angles.yaw - look_at_angles.yaw - decltype(current_angles.yaw)::from_degrees(90);
|
||||
|
||||
return omath::Vector2<float>(static_cast<float>(yaw.cos()) * sign, static_cast<float>(yaw.sin()))
|
||||
* (camera.get_origin().distance_to(position) * scale);
|
||||
}
|
||||
} // namespace omath::algorithm
|
||||
@@ -9,6 +9,7 @@ namespace omath::hud
|
||||
class CanvasBox final
|
||||
{
|
||||
public:
|
||||
CanvasBox() = default;
|
||||
CanvasBox(Vector2<float> top, Vector2<float> bottom, float ratio = 4.f);
|
||||
|
||||
[[nodiscard("You have to use array")]]
|
||||
|
||||
@@ -5,13 +5,21 @@
|
||||
#include "canvas_box.hpp"
|
||||
#include "entity_overlay_widgets.hpp"
|
||||
#include "hud_renderer_interface.hpp"
|
||||
#include "omath/3d_primitives/aabb.hpp"
|
||||
#include "omath/linear_algebra/vector2.hpp"
|
||||
#include "omath/utility/color.hpp"
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
#include <type_traits>
|
||||
namespace omath::hud
|
||||
{
|
||||
enum class EntityOverlayError
|
||||
{
|
||||
NO_PROJECTED_VERTEX,
|
||||
CENTER_PROJECTION_FAILED,
|
||||
};
|
||||
|
||||
class EntityOverlay final
|
||||
{
|
||||
public:
|
||||
@@ -57,13 +65,17 @@ namespace omath::hud
|
||||
float offset = 5.f);
|
||||
|
||||
// ── 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,
|
||||
const std::string_view& text);
|
||||
@@ -72,24 +84,24 @@ namespace omath::hud
|
||||
const std::string_view& text);
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_right_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_right_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
return add_right_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_left_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_left_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
return add_left_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_top_label(const Color& color, const float offset, const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_top_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
return add_top_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
@@ -112,8 +124,9 @@ namespace omath::hud
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
EntityOverlay& add_centered_bottom_label(const Color& color, const float offset, const widget::Outlined outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_centered_bottom_label(const Color& color, const float offset,
|
||||
const widget::Outlined outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
{
|
||||
return add_centered_bottom_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
@@ -164,6 +177,49 @@ namespace omath::hud
|
||||
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:
|
||||
// optional<W> dispatch — enables when() conditional widgets
|
||||
template<typename W>
|
||||
|
||||
@@ -125,7 +125,7 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle operator+(const Angle& other) noexcept
|
||||
constexpr Angle operator+(const Angle& other) const noexcept
|
||||
{
|
||||
if constexpr (flags == AngleFlags::Normalized)
|
||||
return Angle{angles::wrap_angle(m_angle + other.m_angle, min, max)};
|
||||
@@ -140,7 +140,7 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle operator-(const Angle& other) noexcept
|
||||
constexpr Angle operator-(const Angle& other) const noexcept
|
||||
{
|
||||
return operator+(-other);
|
||||
}
|
||||
|
||||
@@ -119,11 +119,11 @@ namespace
|
||||
switch (axis)
|
||||
{
|
||||
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:
|
||||
return aabb.top<omath::primitives::UpAxis::Y>();
|
||||
return aabb.top();
|
||||
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();
|
||||
}
|
||||
@@ -133,11 +133,11 @@ namespace
|
||||
switch (axis)
|
||||
{
|
||||
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:
|
||||
return aabb.bottom<omath::primitives::UpAxis::Y>();
|
||||
return aabb.bottom();
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "omath/3d_primitives/aabb.hpp"
|
||||
|
||||
using UpAxis = omath::primitives::UpAxis;
|
||||
using AABB = omath::primitives::Aabb<float>;
|
||||
using AABBZ = omath::primitives::Aabb<float, UpAxis::Z>;
|
||||
using Vec3 = omath::Vector3<float>;
|
||||
|
||||
// --- center() ---
|
||||
@@ -65,14 +67,12 @@ TEST(AabbTests, ExtentsOfDegenerateBox)
|
||||
EXPECT_FLOAT_EQ(e.z, 0.f);
|
||||
}
|
||||
|
||||
using UpAxis = omath::primitives::UpAxis;
|
||||
|
||||
// --- top() ---
|
||||
|
||||
TEST(AabbTests, TopYUpSymmetricBox)
|
||||
{
|
||||
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.y, 2.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 0.f);
|
||||
@@ -81,7 +81,7 @@ TEST(AabbTests, TopYUpSymmetricBox)
|
||||
TEST(AabbTests, TopYUpOffsetBox)
|
||||
{
|
||||
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.y, 10.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 4.f);
|
||||
@@ -89,8 +89,8 @@ TEST(AabbTests, TopYUpOffsetBox)
|
||||
|
||||
TEST(AabbTests, TopZUpSymmetricBox)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto t = box.top<UpAxis::Z>();
|
||||
constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto t = box.top();
|
||||
EXPECT_FLOAT_EQ(t.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(t.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 3.f);
|
||||
@@ -98,8 +98,8 @@ TEST(AabbTests, TopZUpSymmetricBox)
|
||||
|
||||
TEST(AabbTests, TopZUpOffsetBox)
|
||||
{
|
||||
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto t = box.top<UpAxis::Z>();
|
||||
constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto t = box.top();
|
||||
EXPECT_FLOAT_EQ(t.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(t.y, 7.f);
|
||||
EXPECT_FLOAT_EQ(t.z, 6.f);
|
||||
@@ -108,7 +108,8 @@ TEST(AabbTests, TopZUpOffsetBox)
|
||||
TEST(AabbTests, TopDefaultIsYUp)
|
||||
{
|
||||
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() ---
|
||||
@@ -116,7 +117,7 @@ TEST(AabbTests, TopDefaultIsYUp)
|
||||
TEST(AabbTests, BottomYUpSymmetricBox)
|
||||
{
|
||||
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.y, -2.f);
|
||||
EXPECT_FLOAT_EQ(b.z, 0.f);
|
||||
@@ -125,7 +126,7 @@ TEST(AabbTests, BottomYUpSymmetricBox)
|
||||
TEST(AabbTests, BottomYUpOffsetBox)
|
||||
{
|
||||
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.y, 4.f);
|
||||
EXPECT_FLOAT_EQ(b.z, 4.f);
|
||||
@@ -133,8 +134,8 @@ TEST(AabbTests, BottomYUpOffsetBox)
|
||||
|
||||
TEST(AabbTests, BottomZUpSymmetricBox)
|
||||
{
|
||||
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto b = box.bottom<UpAxis::Z>();
|
||||
constexpr AABBZ box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
constexpr auto b = box.bottom();
|
||||
EXPECT_FLOAT_EQ(b.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(b.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(b.z, -3.f);
|
||||
@@ -142,8 +143,8 @@ TEST(AabbTests, BottomZUpSymmetricBox)
|
||||
|
||||
TEST(AabbTests, BottomZUpOffsetBox)
|
||||
{
|
||||
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto b = box.bottom<UpAxis::Z>();
|
||||
constexpr AABBZ box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
|
||||
constexpr auto b = box.bottom();
|
||||
EXPECT_FLOAT_EQ(b.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(b.y, 7.f);
|
||||
EXPECT_FLOAT_EQ(b.z, 2.f);
|
||||
@@ -152,14 +153,33 @@ TEST(AabbTests, BottomZUpOffsetBox)
|
||||
TEST(AabbTests, BottomDefaultIsYUp)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
EXPECT_FLOAT_EQ(box.top<UpAxis::Z>().z, -box.bottom<UpAxis::Z>().z);
|
||||
constexpr AABBZ z_up_box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
|
||||
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() ---
|
||||
|
||||
@@ -183,7 +183,7 @@ TEST(UnitTestAngle, Formatter_PrintsDegreesWithSuffix)
|
||||
|
||||
TEST(UnitTestAngle, BinaryPlus_ReturnsWrappedSum)
|
||||
{
|
||||
Angle<> a = Deg::from_degrees(350.0f);
|
||||
const Angle<> a = Deg::from_degrees(350.0f);
|
||||
const Deg b = Deg::from_degrees(20.0f);
|
||||
const Deg c = a + b; // expect 10°
|
||||
EXPECT_FLOAT_EQ(c.as_degrees(), 10.0f);
|
||||
@@ -191,7 +191,7 @@ TEST(UnitTestAngle, BinaryPlus_ReturnsWrappedSum)
|
||||
|
||||
TEST(UnitTestAngle, BinaryMinus_ReturnsWrappedDiff)
|
||||
{
|
||||
Angle<> a = Deg::from_degrees(10.0f);
|
||||
const Angle<> a = Deg::from_degrees(10.0f);
|
||||
const Deg b = Deg::from_degrees(30.0f);
|
||||
const Deg c = a - b; // expect 340°
|
||||
EXPECT_FLOAT_EQ(c.as_degrees(), 340.0f);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//
|
||||
// Created by Vlad on 27.08.2024.
|
||||
//
|
||||
#include "omath/algorithm/radar.hpp"
|
||||
#include "omath/engines/unity_engine/camera.hpp"
|
||||
#include <complex>
|
||||
#include <gtest/gtest.h>
|
||||
@@ -1283,8 +1284,9 @@ TEST(UnitTestProjection, TriangleFarToSideCulled)
|
||||
TEST(UnitTestProjection, TriangleStraddlingFrustumNotCulled)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
constexpr auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
constexpr auto position = omath::algorithm::world_to_radar(cam, {-1.f, 0.f, 0.f}, 0.1f);
|
||||
// Large triangle with vertices on both sides of the frustum — should not be culled
|
||||
const omath::Triangle<omath::Vector3<float>> tri{{100.f, 0.f, 0.f}, {100.f, 5000.f, 0.f}, {100.f, 0.f, 5000.f}};
|
||||
EXPECT_FALSE(cam.is_culled_by_frustum(tri));
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <omath/algorithm/radar.hpp>
|
||||
#include <omath/engines/cry_engine/camera.hpp>
|
||||
#include <omath/engines/frostbite_engine/camera.hpp>
|
||||
#include <omath/engines/iw_engine/camera.hpp>
|
||||
#include <omath/engines/opengl_engine/camera.hpp>
|
||||
#include <omath/engines/rage_engine/camera.hpp>
|
||||
#include <omath/engines/source_engine/camera.hpp>
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
#include <omath/engines/unreal_engine/camera.hpp>
|
||||
|
||||
using namespace omath;
|
||||
|
||||
template<class CameraType, class NumericType>
|
||||
static void verify_world_to_radar_uses_engine_world_axes(const Vector3<NumericType>& world_forward,
|
||||
const Vector3<NumericType>& world_right)
|
||||
{
|
||||
const Vector3<NumericType> origin{NumericType{10}, NumericType{-20}, NumericType{5}};
|
||||
const NumericType world_distance = NumericType{20};
|
||||
const NumericType scale = NumericType{0.5};
|
||||
const auto radar_distance = static_cast<float>(world_distance * scale);
|
||||
|
||||
CameraType camera{
|
||||
origin, {}, {1280.f, 720.f}, projection::FieldOfView::from_degrees(90.f), NumericType{0.01},
|
||||
NumericType{1000}};
|
||||
|
||||
const auto forward_position = origin + world_forward * world_distance;
|
||||
const auto forward_radar = algorithm::world_to_radar(camera, forward_position, scale);
|
||||
|
||||
EXPECT_NEAR(forward_radar.x, 0.f, 1e-4f);
|
||||
EXPECT_NEAR(forward_radar.y, -radar_distance, 1e-4f);
|
||||
|
||||
const auto right_position = origin + world_right * world_distance;
|
||||
const auto right_radar = algorithm::world_to_radar(camera, right_position, scale);
|
||||
|
||||
EXPECT_NEAR(right_radar.x, radar_distance, 1e-4f);
|
||||
EXPECT_NEAR(right_radar.y, 0.f, 1e-4f);
|
||||
}
|
||||
|
||||
template<class CameraType, class NumericType>
|
||||
static void verify_world_to_radar_uses_changed_camera_yaw(const float yaw_degrees,
|
||||
const Vector3<NumericType>& world_forward,
|
||||
const Vector3<NumericType>& world_right)
|
||||
{
|
||||
const Vector3<NumericType> origin{NumericType{10}, NumericType{-20}, NumericType{5}};
|
||||
const NumericType world_distance = NumericType{20};
|
||||
const NumericType scale = NumericType{0.5};
|
||||
const auto radar_distance = static_cast<float>(world_distance * scale);
|
||||
|
||||
CameraType camera{
|
||||
origin, {}, {1280.f, 720.f}, projection::FieldOfView::from_degrees(90.f), NumericType{0.01},
|
||||
NumericType{1000}};
|
||||
|
||||
auto angles = camera.get_view_angles();
|
||||
angles.yaw = decltype(angles.yaw)::from_degrees(yaw_degrees);
|
||||
camera.set_view_angles(angles);
|
||||
|
||||
const auto forward_position = origin + world_right * world_distance;
|
||||
const auto forward_radar = algorithm::world_to_radar(camera, forward_position, scale);
|
||||
|
||||
EXPECT_NEAR(forward_radar.x, 0.f, 1e-4f);
|
||||
EXPECT_NEAR(forward_radar.y, -radar_distance, 1e-4f);
|
||||
|
||||
const auto left_position = origin + world_forward * world_distance;
|
||||
const auto left_radar = algorithm::world_to_radar(camera, left_position, scale);
|
||||
|
||||
EXPECT_NEAR(left_radar.x, -radar_distance, 1e-4f);
|
||||
EXPECT_NEAR(left_radar.y, 0.f, 1e-4f);
|
||||
}
|
||||
|
||||
template<class CameraType, class NumericType>
|
||||
static void verify_world_to_radar_ignores_camera_pitch(const Vector3<NumericType>& world_forward)
|
||||
{
|
||||
const Vector3<NumericType> origin{NumericType{10}, NumericType{-20}, NumericType{5}};
|
||||
const NumericType world_distance = NumericType{20};
|
||||
const NumericType scale = NumericType{0.5};
|
||||
const auto radar_distance = static_cast<float>(world_distance * scale);
|
||||
|
||||
CameraType camera{
|
||||
origin, {}, {1280.f, 720.f}, projection::FieldOfView::from_degrees(90.f), NumericType{0.01},
|
||||
NumericType{1000}};
|
||||
|
||||
auto angles = camera.get_view_angles();
|
||||
angles.pitch = decltype(angles.pitch)::from_degrees(45.f);
|
||||
camera.set_view_angles(angles);
|
||||
|
||||
const auto forward_position = origin + world_forward * world_distance;
|
||||
const auto forward_radar = algorithm::world_to_radar(camera, forward_position, scale);
|
||||
|
||||
EXPECT_NEAR(forward_radar.x, 0.f, 1e-4f);
|
||||
EXPECT_NEAR(forward_radar.y, -radar_distance, 1e-4f);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, SourceEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<source_engine::Camera, float>(source_engine::k_abs_forward,
|
||||
source_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<source_engine::Camera, float>(-90.f, source_engine::k_abs_forward,
|
||||
source_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<source_engine::Camera, float>(source_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, IWEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<iw_engine::Camera, float>(iw_engine::k_abs_forward,
|
||||
iw_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<iw_engine::Camera, float>(-90.f, iw_engine::k_abs_forward,
|
||||
iw_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<iw_engine::Camera, float>(iw_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, FrostbiteEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<frostbite_engine::Camera, float>(frostbite_engine::k_abs_forward,
|
||||
frostbite_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<frostbite_engine::Camera, float>(
|
||||
90.f, frostbite_engine::k_abs_forward, frostbite_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<frostbite_engine::Camera, float>(frostbite_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, OpenGLEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<opengl_engine::Camera, float>(opengl_engine::k_abs_forward,
|
||||
opengl_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<opengl_engine::Camera, float>(-90.f, opengl_engine::k_abs_forward,
|
||||
opengl_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<opengl_engine::Camera, float>(opengl_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, UnityEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<unity_engine::Camera, float>(unity_engine::k_abs_forward,
|
||||
unity_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<unity_engine::Camera, float>(90.f, unity_engine::k_abs_forward,
|
||||
unity_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<unity_engine::Camera, float>(unity_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, CryEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<cry_engine::Camera, float>(cry_engine::k_abs_forward,
|
||||
cry_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<cry_engine::Camera, float>(-90.f, cry_engine::k_abs_forward,
|
||||
cry_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<cry_engine::Camera, float>(cry_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, RageEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<rage_engine::Camera, float>(rage_engine::k_abs_forward,
|
||||
rage_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<rage_engine::Camera, float>(-90.f, rage_engine::k_abs_forward,
|
||||
rage_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<rage_engine::Camera, float>(rage_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(WorldToRadarTests, UnrealEngineCamera)
|
||||
{
|
||||
verify_world_to_radar_uses_engine_world_axes<unreal_engine::Camera, double>(unreal_engine::k_abs_forward,
|
||||
unreal_engine::k_abs_right);
|
||||
verify_world_to_radar_uses_changed_camera_yaw<unreal_engine::Camera, double>(90.f, unreal_engine::k_abs_forward,
|
||||
unreal_engine::k_abs_right);
|
||||
verify_world_to_radar_ignores_camera_pitch<unreal_engine::Camera, double>(unreal_engine::k_abs_forward);
|
||||
}
|
||||
Reference in New Issue
Block a user