diff --git a/client/application.h b/client/application.h index 82224a61..6908c0ee 100644 --- a/client/application.h +++ b/client/application.h @@ -358,4 +358,15 @@ public: { return instance().pipeline_cache; } + + static const std::filesystem::path& get_config_path() + { + return instance().config_path; + } + + + static const std::filesystem::path& get_cache_path() + { + return instance().cache_path; + } }; diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index c046fcee..6358c809 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -20,6 +20,7 @@ #include "imgui_impl.h" #include "application.h" +#include "openxr/openxr.h" #include "utils/ranges.h" #include "vulkan/vulkan_enums.hpp" #include "vulkan/vulkan_handles.hpp" @@ -28,6 +29,7 @@ #include "vk/allocation.h" #include #include +#include /* Do not use: * @@ -93,36 +95,46 @@ static void check_vk_result(VkResult result) } } -static std::optional ray_plane_intersection(glm::vec3 plane_center, glm::quat plane_orientation, std::pair ray) +static std::optional ray_plane_intersection(const imgui_context::imgui_viewport& vp, const imgui_context::controller_state& in) { - auto M = glm::transpose(glm::mat3_cast(plane_orientation)); // world-to-plane transform + if (!in.active) + return {}; - glm::vec3 ray_start = M * (ray.first - plane_center); - glm::vec3 ray_dir = M * ray.second; + auto M = glm::transpose(glm::mat3_cast(vp.orientation)); // world-to-plane transform - // spdlog::debug("ray_start = {}, {}, {}", ray_start.x, ray_start.y, ray_start.z); - // spdlog::debug("ray_dir = {}, {}, {}", ray_dir.x, ray_dir.y, ray_dir.z); + glm::vec3 controller_direction = glm::column(glm::mat3_cast(in.aim_orientation), 2); - if (ray_dir.z < 0.0001f) + // Compute all vectors in the reference frame of the GUI plane + glm::vec3 ray_start = M * (in.aim_position - vp.position); + glm::vec3 ray_dir = M * controller_direction; + + if (ray_dir.z > 0.0001f) { glm::vec2 coord; - coord.x = ray_start.x + ray_dir.x * ray_start.z / ray_dir.z; - coord.y = ray_start.y + ray_dir.y * ray_start.z / ray_dir.z; + // ray_start + lambda × ray_dir ∈ imgui plane + // => ray_start.z + lambda × ray_dir.z = 0 + float lambda = -ray_start.z / ray_dir.z; - if (fabs(coord.x) <= 0.5 && fabs(coord.y) <= 0.5) - return coord; + if (lambda < 0) + { + coord.x = ray_start.x + lambda * ray_dir.x; + coord.y = ray_start.y + lambda * ray_dir.y; + + // Convert from mesh coordinates to imgui coordinates + coord = coord / vp.scale; + + if (fabs(coord.x) <= 0.5 && fabs(coord.y) <= 0.5) + return ImVec2( + (0.5 + coord.x) * vp.size.width, + (0.5 - coord.y) * vp.size.height); + } } return {}; } -static std::pair compute_ray(glm::vec3 controller_position, glm::quat controller_orientation) -{ - return std::make_pair(controller_position, glm::transpose(glm::mat3_cast(controller_orientation)) * glm::vec3(0, 0, -1)); -} - -imgui_viewport::imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& command_pool, vk::RenderPass renderpass, vk::Extent2D size, vk::Format format) : +imgui_context::imgui_viewport::imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& command_pool, vk::RenderPass renderpass, vk::Extent2D size, vk::Format format) : device(device), size(size) { @@ -182,7 +194,7 @@ imgui_viewport::imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& } } -imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue) : +imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span controllers_) : device(device), queue_family_index(queue_family_index), queue(queue), @@ -199,11 +211,17 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind .queueFamilyIndex = queue_family_index, }), context(ImGui::CreateContext()), - io(ImGui::GetIO()) + io(ImGui::GetIO()), + world(world) { + controllers.reserve(controllers_.size()); + for(const auto& i:controllers_) + controllers.emplace_back(i, controller_state{}); + viewport = std::make_shared(device, command_pool, *renderpass, vk::Extent2D{1000, 1000}, vk::Format::eR8G8B8A8Unorm); - // See example_sdl2_vulkan/main.cpp + ini_filename = application::get_config_path() / "imgui.ini"; + io.IniFilename = ini_filename.c_str(); // Setup Dear ImGui style ImGui::StyleColorsDark(); @@ -247,52 +265,132 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind } -void imgui_context::new_frame(std::span inputs) +static float read_action_bool_or_float(imgui_context::controller::typed_action action) { - // Poll and handle events (inputs, window resize, etc.) - // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. - // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. - // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. - // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. - // SDL_Event event; - // while (SDL_PollEvent(&event)) - // { - // ImGui_ImplSDL2_ProcessEvent(&event); - // if (event.type == SDL_QUIT) - // done = true; - // if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) - // done = true; - // } - // - // // Resize swap chain? - // if (g_SwapChainRebuild) - // { - // int width, height; - // SDL_GetWindowSize(window, &width, &height); - // if (width > 0 && height > 0) - // { - // ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); - // ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); - // g_MainWindowData.FrameIndex = 0; - // g_SwapChainRebuild = false; - // } - // } + assert(action.second == XR_ACTION_TYPE_BOOLEAN_INPUT || action.second == XR_ACTION_TYPE_FLOAT_INPUT); - // Compute the intersection of the controllers' ray and the imgui plane - std::vector ray_pos; - for(auto& i: inputs) + float value = 0; + switch(action.second) { - if (!i.active) - continue; + case XR_ACTION_TYPE_BOOLEAN_INPUT: + if (auto act = application::read_action_bool(action.first); act) + value = act->second; + break; - auto& vp = *viewport; + case XR_ACTION_TYPE_FLOAT_INPUT: + if (auto act = application::read_action_float(action.first); act) + value = act->second; + break; - std::pair ray = compute_ray(i.controller_position, i.controller_orientation); - std::optional pos = ray_plane_intersection(vp.position, vp.orientation, ray); // between -0.5 and 0.5 + default: + __builtin_unreachable(); + } - if (pos) - ray_pos.push_back(*pos + glm::vec2(0.5, 0.5));; + return value; +} +void imgui_context::new_frame(XrTime display_time) +{ + size_t new_focused_controller = focused_controller; + + std::vector new_states; + + for(auto&& [index, controller]: utils::enumerate(controllers)) + { + auto& [ctrl, state] = controller; + + controller_state& new_state = new_states.emplace_back(); + + if (auto location = application::locate_controller(ctrl.aim, world, display_time); location) + { + new_state.active = true; + new_state.aim_position = location->first; + new_state.aim_orientation = location->second; + } + else + { + new_state.active = false; + } + + if (ctrl.squeeze.first) + { + new_state.squeeze_value = read_action_bool_or_float(ctrl.squeeze); + + if (new_state.squeeze_value < 0.5) + new_state.squeeze_clicked = false; + else if (new_state.squeeze_value > 0.8) + new_state.squeeze_clicked = true; + } + + if (ctrl.trigger.first) + { + new_state.trigger_value = read_action_bool_or_float(ctrl.trigger); + + if (new_state.trigger_value < 0.5) + new_state.trigger_clicked = false; + else if (new_state.trigger_value > 0.8) + new_state.trigger_clicked = true; + } + + if (ctrl.scroll.first) + { + assert(ctrl.scroll.second == XR_ACTION_TYPE_VECTOR2F_INPUT); + + if (auto act = application::read_action_vec2(ctrl.scroll.first); act) + new_state.scroll_value = { act->second.x, act->second.y }; + else + new_state.scroll_value = {0, 0}; + } + + if (new_state.squeeze_clicked || new_state.trigger_clicked || glm::length(new_state.scroll_value) > 0.1f) + { + new_focused_controller = index; + } + } + + bool focused_change = new_focused_controller != focused_controller && focused_controller != (size_t)-1; + + // Simulate a pen for the following events + io.AddMouseSourceEvent(ImGuiMouseSource_Pen); + if (focused_change && controllers[focused_controller].second.trigger_clicked) + { + // Focused controller changed: end the current click + io.AddMouseButtonEvent(0, false); + } + + std::optional position; + float trigger; + + if (new_focused_controller != (size_t)-1) + { + position = ray_plane_intersection(*viewport, new_states[new_focused_controller]); + trigger = new_states[new_focused_controller].trigger_value; + auto scroll = new_states[new_focused_controller].scroll_value; + + if (position) + { + io.AddMousePosEvent(position->x, position->y); + + bool last_trigger = controllers[new_focused_controller].second.trigger_clicked; + bool next_trigger = new_states[new_focused_controller].trigger_clicked; + + if (focused_change || last_trigger != next_trigger) + io.AddMouseButtonEvent(0, next_trigger); + + if (glm::length(scroll) > 0.1f) + io.AddMouseWheelEvent(scroll.x, scroll.y); + } + } + else + { + position = {}; + trigger = 0; + } + + focused_controller = new_focused_controller; + for(auto&& [controller, next_state]: utils::zip(controllers, new_states)) + { + controller.second = next_state; } // Start the Dear ImGui frame @@ -302,27 +400,21 @@ void imgui_context::new_frame(std::span inputs) io.DisplayFramebufferScale = ImVec2(1, 1); // See ImGui_ImplSDL2_ProcessEvent - io.DeltaTime = 0.016; // TODO - io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); // TODO - // io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); + if (last_display_time) + io.DeltaTime = std::min((display_time - last_display_time) * 1e-9f, 0.1f); + + last_display_time = display_time; + // io.AddMouseWheelEvent(wheel_x, wheel_y); - // io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); - // io.AddMouseButtonEvent(mouse_button, (event->type == SDL_MOUSEBUTTONDOWN)); - // bd->MouseButtonsDown = (event->type == SDL_MOUSEBUTTONDOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button)); ImGui::NewFrame(); ImDrawList* draw_list = ImGui::GetForegroundDrawList(); - // spdlog::debug("{} intersections", ray_pos.size()); - for(glm::vec2 i: ray_pos) - { - ImVec2 j(i.x * 1000, 1000 - i.y * 1000); - draw_list->AddCircle(j, 30, ImGui::GetColorU32(ImVec4(1, 0, 0, 0.8)), 20, 10); + if (position) + draw_list->AddCircle(*position, 30*(1-0.7*trigger), ImGui::GetColorU32(ImVec4(1, 0, 0, 0.8)), 20, 10); - // spdlog::debug(" - {}, {}", i.x, i.y); - } } std::shared_ptr imgui_context::render() diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index c533540f..26ca2880 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -22,62 +22,67 @@ #include #include #include +#include #include "vk/allocation.h" -struct imgui_frame -{ - image_allocation image; - vk::raii::ImageView image_view = nullptr; - vk::raii::Framebuffer framebuffer = nullptr; - vk::raii::CommandBuffer command_buffer = nullptr; - vk::raii::Fence fence = nullptr; -}; - -struct imgui_viewport -{ - static constexpr int frames_in_flight = 2; - std::array frames; - int frameindex = 0; - - vk::raii::Device& device; - - vk::Extent2D size; - vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}}; - - // TODO: sync with 3d scene - glm::vec3 position = {0, 1, -1.5}; - glm::quat orientation = {1, 0, 0, 0}; - float scale = 1; - - imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& command_pool, vk::RenderPass renderpass, vk::Extent2D size, vk::Format format); -}; - -struct imgui_inputs -{ - bool active = false; - int id; - - glm::vec3 controller_position; - glm::quat controller_orientation; - float squeeze; - float trigger; - glm::vec2 scroll; -}; - -struct imgui_controller_state -{ - bool active = false; - int id; - - float squeeze; - float trigger; - - bool squeeze_hysteresis; - bool trigger_hysteresis; -}; - class imgui_context { +public: + struct imgui_frame + { + image_allocation image; + vk::raii::ImageView image_view = nullptr; + vk::raii::Framebuffer framebuffer = nullptr; + vk::raii::CommandBuffer command_buffer = nullptr; + vk::raii::Fence fence = nullptr; + }; + + struct imgui_viewport + { + static constexpr int frames_in_flight = 2; + std::array frames; + int frameindex = 0; + + vk::raii::Device& device; + + vk::Extent2D size; + vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}}; + + // TODO: sync with 3d scene + glm::vec3 position = {0, 1, -1.5}; + glm::quat orientation = {1, 0, 0, 0}; + glm::vec2 scale = {1, 1}; + + imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& command_pool, vk::RenderPass renderpass, vk::Extent2D size, vk::Format format); + }; + + struct controller + { + using typed_action = std::pair; + XrSpace aim; + typed_action trigger; + typed_action squeeze; + typed_action scroll; + + // TODO: thresholds? + }; + + struct controller_state + { + bool active; + + glm::vec3 aim_position; + glm::quat aim_orientation; + + float trigger_value; + float squeeze_value; + glm::vec2 scroll_value; + + bool squeeze_clicked; + bool trigger_clicked; + }; + +private: static inline const std::array pool_sizes = { vk::DescriptorPoolSize{ @@ -95,16 +100,24 @@ class imgui_context vk::raii::RenderPass renderpass; vk::raii::CommandPool command_pool; + // shared_ptr because it needs to be kept alive until the output textures are not used anymore std::shared_ptr viewport; ImGuiContext * context; ImGuiIO& io; + std::string ini_filename; + + std::vector> controllers; + XrSpace world; + size_t focused_controller = (size_t)-1; + XrTime last_display_time = 0; + public: imgui_context(vk::raii::Device& device, uint32_t queue_family_index, - vk::raii::Queue& queue); + vk::raii::Queue& queue, XrSpace world, std::span controllers); ~imgui_context(); - void new_frame(std::span inputs); + void new_frame(XrTime display_time); std::shared_ptr render(); }; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index acfb8db2..5b6bafbc 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -98,7 +98,22 @@ scenes::lobby::lobby() : spdlog::info("Loaded input profile {}", input->id); - imgui_ctx.emplace(device, queue_family_index, queue); + std::array imgui_inputs{ + imgui_context::controller{ + .aim = application::left_aim(), + .trigger = application::get_action("/user/hand/left/input/select/click"), + .squeeze = application::get_action("/user/hand/left/input/squeeze/value"), + .scroll = application::get_action("/user/hand/left/input/thumbstick"), + }, + imgui_context::controller{ + .aim = application::right_aim(), + .trigger = application::get_action("/user/hand/right/input/select/click"), + .squeeze = application::get_action("/user/hand/right/input/squeeze/value"), + .scroll = application::get_action("/user/hand/right/input/thumbstick"), + }, + }; + + imgui_ctx.emplace(device, queue_family_index, queue, world_space, imgui_inputs); } void scenes::lobby::rasterize_status_string() @@ -268,47 +283,8 @@ void scenes::lobby::render() input->apply(world_space, framestate.predictedDisplayTime); - std::array inputs; - if (auto location = application::locate_controller(application::left_aim(), world_space, framestate.predictedDisplayTime); location) - { - inputs[0].active = true; - inputs[0].controller_position = location->first; - inputs[0].controller_orientation = location->second; - - auto act = application::get_action("/user/hand/left/select/trigger/value").first; - if (act) - inputs[0].trigger = application::read_action_bool(application::get_action("/user/hand/left/select/trigger/value").first).value_or(std::make_pair(0, 0)).second; - - // inputs[0].trigger = application::read_action_float(application::get_action("/user/hand/left/input/trigger/value").first).value_or(std::make_pair(0, 0)).second; - // inputs[0].squeeze = application::read_action_float(application::get_action("/user/hand/left/input/squeeze/value").first).value_or(std::make_pair(0, 0)).second; - inputs[0].scroll = glm::vec2(0,0); - } - else - { - inputs[0].active = false; - } - - if (auto location = application::locate_controller(application::right_aim(), world_space, framestate.predictedDisplayTime); location) - { - inputs[1].active = true; - inputs[1].controller_position = location->first; - inputs[1].controller_orientation = location->second; - - auto act = application::get_action("/user/hand/right/select/trigger/value").first; - if (act) - inputs[1].trigger = application::read_action_bool(act).value_or(std::make_pair(0, 0)).second; - - // inputs[1].trigger = application::read_action_float(application::get_action("/user/hand/right/input/trigger/value").first).value_or(std::make_pair(0, 0)).second; - // inputs[1].squeeze = application::read_action_float(application::get_action("/user/hand/right/input/squeeze/value").first).value_or(std::make_pair(0, 0)).second; - inputs[1].scroll = glm::vec2(1,1); - } - else - { - inputs[1].active = false; - } - - imgui_ctx->new_frame(inputs); + imgui_ctx->new_frame(framestate.predictedDisplayTime); ImGui::ShowDemoWindow(); // Render the GUI to the imgui material