diff --git a/CMakeLists.txt b/CMakeLists.txt index 53996ad7..25d7ff50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,7 +214,9 @@ FetchContent_Declare(spdlog EXCLUDE_FROM_ALL SYSTEM URL https://github.co FetchContent_Declare(stb EXCLUDE_FROM_ALL SYSTEM URL https://github.com/nothings/stb/archive/013ac3beddff3dbffafd5177e7972067cd2b5083.zip) FetchContent_Declare(libktx EXCLUDE_FROM_ALL SYSTEM URL https://github.com/KhronosGroup/KTX-Software/archive/refs/tags/v4.3.0.tar.gz) FetchContent_Declare(implot EXCLUDE_FROM_ALL SYSTEM URL https://github.com/epezent/implot/archive/refs/tags/v0.16.tar.gz) -FetchContent_Declare(imgui EXCLUDE_FROM_ALL SYSTEM URL https://github.com/ocornut/imgui/archive/refs/tags/v1.90.1.tar.gz) +FetchContent_Declare(imgui EXCLUDE_FROM_ALL SYSTEM URL https://github.com/ocornut/imgui/archive/refs/tags/v1.91.3.tar.gz + PATCH_COMMAND ${CMAKE_CURRENT_LIST_DIR}/patches/apply.sh ${CMAKE_CURRENT_LIST_DIR}/patches/imgui/ +) # Use the docking branch of imgui to use multi-viewport # FetchContent_Declare(imgui EXCLUDE_FROM_ALL URL https://github.com/ocornut/imgui/archive/ce0d0ac8298ce164b5d862577e8b087d92f6e90e.zip) diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 1ebbfde3..858103ee 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -35,8 +35,6 @@ #include #include #include -#include -#include #include #include #include @@ -120,41 +118,59 @@ static void check_vk_result(VkResult result) } } -std::optional> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const +std::vector> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const { if (!in.active) return {}; - auto M = glm::transpose(glm::mat3_cast(orientation_)); // world-to-plane transform + std::vector> intersections; - glm::vec3 controller_direction = glm::column(glm::mat3_cast(in.aim_orientation), 2); - - // Compute all vectors in the reference frame of the GUI plane - glm::vec3 ray_start = M * (in.aim_position - position_); - glm::vec3 ray_dir = M * controller_direction; - - if (ray_dir.z > 0.0001f) + for (const auto & i: layers_) { - glm::vec2 coord; + auto M = glm::transpose(glm::mat3_cast(i.orientation)); // world-to-plane transform - // ray_start + lambda × ray_dir ∈ imgui plane - // => ray_start.z + lambda × ray_dir.z = 0 - float lambda = -ray_start.z / ray_dir.z; + glm::quat q; + if (std::abs(glm::length(in.aim_orientation) - 1) < 0.01) + q = in.aim_orientation; + else + q = i.orientation; - coord.x = ray_start.x + lambda * ray_dir.x; - coord.y = ray_start.y + lambda * ray_dir.y; + glm::vec3 controller_direction = glm::column(glm::mat3_cast(q), 2); - // Convert from mesh coordinates to imgui coordinates - coord = coord / scale_; + // Compute all vectors in the reference frame of the GUI plane + glm::vec3 ray_start = M * (in.aim_position - i.position); + glm::vec3 ray_dir = M * controller_direction; - if (fabs(coord.x) <= 0.5 && fabs(coord.y) <= 0.5) - return std::make_pair(ImVec2( - (0.5 + coord.x) * size.width, - (0.5 - coord.y) * size.height), - -lambda); + if (ray_dir.z > 0.0001f) + { + glm::vec2 coord; + + // ray_start - distance × ray_dir ∈ imgui plane + // => ray_start.z - distance × ray_dir.z = 0 + float distance = ray_start.z / ray_dir.z; + + if (distance < 0) + continue; + + coord.x = ray_start.x - distance * ray_dir.x; + coord.y = ray_start.y - distance * ray_dir.y; + + // Convert from mesh coordinates to imgui coordinates + coord = coord / i.size; + + if (fabs(coord.x) > 0.5 or fabs(coord.y) > 0.5) + continue; + + intersections.emplace_back(ImVec2( + (0.5 + coord.x) * i.vp_size.x + i.vp_origin.x, + (0.5 - coord.y) * i.vp_size.y + i.vp_origin.y), + distance); + } } - return {}; + std::ranges::sort(intersections, [](auto & a, auto & b) { return a.second < b.second; }); + + return intersections; } imgui_context::imgui_frame & imgui_context::get_frame(vk::Image destination) @@ -212,7 +228,7 @@ static const vk::DescriptorSetLayoutBinding layout_bindings{ .descriptorCount = 1, .stageFlags = vk::ShaderStageFlagBits::eFragment}; -imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, XrSpace world, std::span controllers_, xr::swapchain & swapchain, glm::vec2 size) : +imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, std::span controllers_, xr::swapchain & swapchain, std::vector layers) : physical_device(physical_device), device(device), queue_family_index(queue_family_index), @@ -234,12 +250,12 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii: command_buffers(swapchain.images().size()), size(swapchain.extent().width, swapchain.extent().height), format(swapchain.format()), - scale_(size.x, size.y), + layers_(std::move(layers)), swapchain(swapchain), context(ImGui::CreateContext()), plot_context(ImPlot::CreateContext()), io((ImGui::SetCurrentContext(context), ImGui::GetIO())), - world(world) + world(application::space(xr::spaces::world)) { controllers.reserve(controllers_.size()); for (const auto & i: controllers_) @@ -263,19 +279,20 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii: .Device = *application::get_device(), .QueueFamily = application::queue_family_index(), .Queue = *application::get_queue(), - .PipelineCache = *application::get_pipeline_cache(), .DescriptorPool = *descriptor_pool, - .Subpass = 0, + .RenderPass = *renderpass, .MinImageCount = 2, .ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData .MSAASamples = VK_SAMPLE_COUNT_1_BIT, + .PipelineCache = *application::get_pipeline_cache(), + .Subpass = 0, .Allocator = nullptr, .CheckVkResultFn = check_vk_result, }; ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); - ImGui_ImplVulkan_Init(&init_info, *renderpass); + ImGui_ImplVulkan_Init(&init_info); initialize_fonts(); @@ -287,6 +304,8 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii: ImGuiStyle & style = ImGui::GetStyle(); style.WindowBorderSize = 0; + style.DisabledAlpha = 0.2; + style.Colors[ImGuiCol_ModalWindowDimBg] = {0, 0, 0, 0}; } void imgui_context::add_chars(std::string_view sv) @@ -521,6 +540,34 @@ void imgui_context::initialize_fonts() ImGui_ImplVulkan_CreateFontsTexture(); } +std::optional imgui_context::get_pointer_position_in_imgui_frame() +{ + if (pointer_position.empty()) + return {}; + + for (auto [pos, distance]: pointer_position) + { + for (ImGuiWindow * window: context->Windows) + { + if (window->Hidden or not window->Active) + continue; + + if (window->Pos.x > pos.x) + continue; + if (window->Pos.y > pos.y) + continue; + if (window->Pos.x + window->Size.x < pos.x) + continue; + if (window->Pos.y + window->Size.y < pos.y) + continue; + + return pos; + } + } + + return pointer_position.back().first; +} + void imgui_context::new_frame(XrTime display_time) { ImGui::SetCurrentContext(context); @@ -554,22 +601,27 @@ void imgui_context::new_frame(XrTime display_time) index_tip.pose.position.y, index_tip.pose.position.z}; - new_state.aim_orientation = orientation_; + // Use the GUI plane as orientation to have the finger point perpendicularly + // to the plane instead of following the finger direction. + // ray_plane_intersection() interprets a null quaternion as a request to use the layer + // orientation. + new_state.aim_orientation = glm::quat(0, 0, 0, 0); new_state.active = true; + new_state.source = ImGuiMouseSource_VRHandTracking; auto position_distance = ray_plane_intersection(new_state); - if (position_distance) + if (not position_distance.empty()) { - new_state.hover_distance = std::abs(position_distance->second); + new_state.hover_distance = position_distance.front().second; - if (std::abs(position_distance->second) < 0.1f) + if (std::abs(position_distance.front().second) < 0.1f) new_state.fingertip_hovered = true; else new_state.active = false; - if (new_state.hover_distance < 0.02) - new_state.fingertip_touched = true; + if (new_state.fingertip_hovered && (new_state.hover_distance < 0.02)) + new_state.fingertip_touching = true; } else new_state.hover_distance = 1e10; @@ -578,6 +630,8 @@ void imgui_context::new_frame(XrTime display_time) continue; } + new_state.source = ImGuiMouseSource_VRController; + if (auto location = application::locate_controller(ctrl.aim, world, display_time)) { new_state.active = true; @@ -618,19 +672,13 @@ void imgui_context::new_frame(XrTime display_time) } } - for (auto [new_state, controller]: std::views::zip(new_states, controllers)) - { - if (new_state.hover_distance < 0.02 && controller.second.hover_distance >= 0.02) - new_state.fingertip_touched = true; - } - float closest_hover_distance = 1e10; for (auto && [index, new_state]: utils::enumerate(new_states)) { - if (new_state.hover_distance < closest_hover_distance && new_state.fingertip_hovered) + if (std::abs(new_state.hover_distance) < closest_hover_distance && new_state.fingertip_hovered) { new_focused_controller = index; - closest_hover_distance = new_state.hover_distance; + closest_hover_distance = std::abs(new_state.hover_distance); } else if (new_state.squeeze_clicked || new_state.trigger_clicked || glm::length(new_state.scroll_value) > 0.01f) { @@ -640,33 +688,36 @@ void imgui_context::new_frame(XrTime display_time) bool focused_change = new_focused_controller != focused_controller && focused_controller != (size_t)-1; - // Simulate a pen for the following events - io.AddMouseSourceEvent(ImGuiMouseSource_Pen); + // Set the source for the following events + if (focused_controller != (size_t)-1) + io.AddMouseSourceEvent(new_states[focused_controller].source); + if (focused_change && controllers[focused_controller].second.trigger_clicked) { // Focused controller changed: end the current click io.AddMouseButtonEvent(0, false); button_pressed = false; + fingertip_touching = false; } - std::optional> position_distance; - if (new_focused_controller != (size_t)-1) { - position_distance = ray_plane_intersection(new_states[new_focused_controller]); + pointer_position = ray_plane_intersection(new_states[new_focused_controller]); auto scroll = new_states[new_focused_controller].scroll_value; - bool last_trigger = controllers[new_focused_controller].second.trigger_clicked || controllers[new_focused_controller].second.fingertip_touched; - button_pressed = new_states[new_focused_controller].trigger_clicked || - (new_states[new_focused_controller].fingertip_touched && !controllers[new_focused_controller].second.fingertip_touched); + bool last_trigger = controllers[new_focused_controller].second.trigger_clicked; + button_pressed = new_states[new_focused_controller].trigger_clicked; - if (position_distance) + bool last_touching = controllers[new_focused_controller].second.fingertip_touching; + fingertip_touching = new_states[new_focused_controller].fingertip_touching; + + if (auto position = get_pointer_position_in_imgui_frame()) { - io.AddMousePosEvent(position_distance->first.x, position_distance->first.y); + io.AddMousePosEvent(position->x, position->y); - if (focused_change || last_trigger != button_pressed) + if (focused_change || (last_trigger || last_touching) != (button_pressed || fingertip_touching)) { - io.AddMouseButtonEvent(0, button_pressed); + io.AddMouseButtonEvent(0, button_pressed || fingertip_touching); } if (glm::length(scroll) > 0.01f) @@ -674,15 +725,15 @@ void imgui_context::new_frame(XrTime display_time) } else { - if (last_trigger && !button_pressed) + if ((last_trigger || last_touching) && !(button_pressed || fingertip_touching)) { - io.AddMouseButtonEvent(0, button_pressed); + io.AddMouseButtonEvent(0, button_pressed || fingertip_touching); } } } else { - position_distance = {}; + pointer_position.clear(); } focused_controller = new_focused_controller; @@ -719,14 +770,50 @@ void imgui_context::new_frame(XrTime display_time) ImGui::NewFrame(); - ImDrawList * draw_list = ImGui::GetForegroundDrawList(); +#if WIVRN_SHOW_IMGUI_DEMO_WINDOW + if (show_demo_window) + ImGui::ShowDemoWindow(&show_demo_window); +#endif - if (position_distance) + image_index = swapchain.acquire(); + swapchain.wait(); +} + +std::vector imgui_context::end_frame() +{ + vk::Image destination = swapchain.images()[image_index].image; + + ImGui::SetCurrentContext(context); + ImPlot::SetCurrentContext(plot_context); + + if (auto position = get_pointer_position_in_imgui_frame()) { - float distance_to_border = std::min({position_distance->first.x, - size.width - position_distance->first.x, - position_distance->first.y, - size.height - position_distance->first.y}); + // Clip in the right plane + ImVec2 clip_rect_min(0, 0); + ImVec2 clip_rect_max(size.width, size.height); + + for (auto & i: layers_) + { + if (position->x < i.vp_origin.x) + continue; + if (position->y < i.vp_origin.y) + continue; + if (position->x > i.vp_origin.x + i.vp_size.x) + continue; + if (position->y > i.vp_origin.y + i.vp_size.y) + continue; + + clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); + clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); + + break; + } + + // TODO fix according to layers + float distance_to_border = std::min({position->x, + size.width - position->x, + position->y, + size.height - position->y}); float radius = 10; // std::clamp(distance_to_border / 4, 0, 10); float alpha = std::clamp((distance_to_border - 10) / 50, 0, 0.8); @@ -734,28 +821,19 @@ void imgui_context::new_frame(XrTime display_time) ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha)); ImU32 color_unpressed = ImGui::GetColorU32(ImVec4(1, 1, 1, alpha)); - bool pressed = button_pressed || new_states[new_focused_controller].fingertip_touched; + bool pressed = button_pressed || fingertip_touching; - draw_list->AddCircleFilled(position_distance->first, radius, pressed ? color_pressed : color_unpressed); - draw_list->AddCircle(position_distance->first, radius * 1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4); + ImDrawList * draw_list = ImGui::GetForegroundDrawList(); + draw_list->PushClipRect(clip_rect_min, clip_rect_max); + draw_list->AddCircleFilled(*position, radius, pressed ? color_pressed : color_unpressed); + draw_list->AddCircle(*position, radius * 1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4); + draw_list->PopClipRect(); } - -#if WIVRN_SHOW_IMGUI_DEMO_WINDOW - if (show_demo_window) - ImGui::ShowDemoWindow(&show_demo_window); -#endif - - image_index = swapchain.acquire(); - swapchain.wait(); -} - -XrCompositionLayerQuad imgui_context::end_frame() -{ - vk::Image destination = swapchain.images()[image_index].image; - - ImGui::SetCurrentContext(context); - ImPlot::SetCurrentContext(plot_context); + if (not context->OpenPopupStack.empty()) + { + // TODO dim main window + } ImGui::Render(); @@ -797,20 +875,49 @@ XrCompositionLayerQuad imgui_context::end_frame() swapchain.release(); - return XrCompositionLayerQuad{ - .type = XR_TYPE_COMPOSITION_LAYER_QUAD, - .layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, - .space = world, - .eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH, - .subImage = { - .swapchain = swapchain, - .imageRect = { - .offset = {0, 0}, - .extent = {(int32_t)size.width, (int32_t)size.height}}, - }, - .pose = pose(), - .size = scale(), - }; + std::vector quads; + quads.reserve(layers_.size()); + + for (auto & i: layers_) + { + quads.push_back(XrCompositionLayerQuad{ + .type = XR_TYPE_COMPOSITION_LAYER_QUAD, + .layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, + .space = world, + .eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH, + .subImage = { + .swapchain = swapchain, + .imageRect = { + .offset = { + .x = i.vp_origin.x, + .y = i.vp_origin.y, + }, + .extent = { + .width = i.vp_size.x, + .height = i.vp_size.y, + }}, + }, + .pose = { + .orientation = { + .x = i.orientation.x, + .y = i.orientation.y, + .z = i.orientation.z, + .w = i.orientation.w, + }, + .position = { + .x = i.position.x, + .y = i.position.y, + .z = i.position.z, + }, + }, + .size = { + .width = i.size.x, + .height = i.size.y, + }, + }); + } + + return quads; } imgui_context::~imgui_context() diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index e1bf9af0..f26aeefa 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -18,9 +18,9 @@ #pragma once +#include "wivrn_config.h" #include "xr/hand_tracker.h" #include "xr/swapchain.h" -#include "wivrn_config.h" #include #include #include @@ -83,7 +83,20 @@ public: bool squeeze_clicked = false; bool trigger_clicked = false; bool fingertip_hovered = false; - bool fingertip_touched = false; + bool fingertip_touching = false; + ImGuiMouseSource source = ImGuiMouseSource_Mouse; + }; + + struct viewport + { + // Position of this viewport in the world + glm::vec3 position; + glm::quat orientation; + glm::vec2 size; + + // Position of this viewport in the swapchain image + glm::ivec2 vp_origin; + glm::ivec2 vp_size; }; private: @@ -114,9 +127,7 @@ private: vk::Format format; vk::ClearValue clear_value = {vk::ClearColorValue{0, 0, 0, 0}}; - glm::vec3 position_ = {0, 1, -1.5}; - glm::quat orientation_ = {1, 0, 0, 0}; - glm::vec2 scale_; + std::vector layers_; xr::swapchain & swapchain; int image_index; @@ -131,16 +142,20 @@ private: XrTime last_display_time = 0; bool button_pressed = false; + bool fingertip_touching = false; ImFontGlyphRangesBuilder glyph_range_builder; ImVector glyph_ranges; bool glyph_range_dirty = true; + std::vector> pointer_position; // first: coordinates from imgui pov, second: distance from the plane, sorted by increasing distance + #if WIVRN_SHOW_IMGUI_DEMO_WINDOW bool show_demo_window = true; #endif void initialize_fonts(); + std::optional get_pointer_position_in_imgui_frame(); public: imgui_context( @@ -148,66 +163,19 @@ public: vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, - XrSpace world, std::span controllers, xr::swapchain & swapchain, - glm::vec2 size); + std::vector layers); ~imgui_context(); - void set_position(glm::vec3 position, glm::quat orientation) + std::span layers() { - position_ = position; - orientation_ = orientation; - } - - XrPosef pose() const - { - return XrPosef{ - .orientation = { - .x = orientation_.x, - .y = orientation_.y, - .z = orientation_.z, - .w = orientation_.w, - }, - .position = { - .x = position_.x, - .y = position_.y, - .z = position_.z, - }, - }; - } - - glm::vec3 & position() - { - return position_; - } - - glm::quat & orientation() - { - return orientation_; - } - - glm::vec3 position() const - { - return position_; - } - - glm::quat orientation() const - { - return orientation_; - } - - XrExtent2Df scale() const - { - return { - scale_.x, - scale_.y, - }; + return layers_; } void new_frame(XrTime display_time); - XrCompositionLayerQuad end_frame(); + std::vector end_frame(); ImFont * large_font; size_t get_focused_controller() const @@ -215,7 +183,7 @@ public: return focused_controller; } - std::optional> ray_plane_intersection(const imgui_context::controller_state & in) const; + std::vector> ray_plane_intersection(const imgui_context::controller_state & in) const; ImTextureID load_texture(const std::string & filename, vk::raii::Sampler && sampler); ImTextureID load_texture(const std::string & filename); diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 4be51071..63ee16b5 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -103,12 +103,25 @@ static const std::array supported_depth_formats{ void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position) { const float gui_pitch = -0.2; + const float keyboard_pitch = -0.6; glm::vec3 gui_direction = new_gui_position - head_position; float gui_yaw = atan2(gui_direction.x, gui_direction.z) + M_PI; - imgui_ctx->position() = new_gui_position; - imgui_ctx->orientation() = glm::quat(cos(gui_yaw / 2), 0, sin(gui_yaw / 2), 0) * glm::quat(cos(gui_pitch / 2), sin(gui_pitch / 2), 0, 0); + auto q = glm::quat(cos(gui_yaw / 2), 0, sin(gui_yaw / 2), 0) * glm::quat(cos(gui_pitch / 2), sin(gui_pitch / 2), 0, 0); + auto M = glm::mat3_cast(q); // plane-to-world transform + + // Main window + imgui_ctx->layers()[0].position = new_gui_position; + imgui_ctx->layers()[0].orientation = q; + + // Popup + imgui_ctx->layers()[1].position = new_gui_position + M * glm::vec3(0, 0, 0.05); + imgui_ctx->layers()[1].orientation = q; + + // Keyboard + imgui_ctx->layers()[2].position = new_gui_position + M * glm::vec3(0, -0.3, 0.1); + imgui_ctx->layers()[2].orientation = q * glm::quat(cos(keyboard_pitch / 2), sin(keyboard_pitch / 2), 0, 0); } scenes::lobby::lobby() @@ -422,10 +435,11 @@ std::optional scenes::lobby::check_recenter_action(XrTime predicted_d .aim_position = aim->first, .aim_orientation = aim->second, }); - if (intersection) + + if (not intersection.empty()) { - gui_recenter_distance = std::clamp(intersection->second, 0.05f, 1.f); - spdlog::info("recentering at {}m", intersection->second); + gui_recenter_distance = std::clamp(intersection.front().second, 0.05f, 1.f); + spdlog::info("recentering at {}m", intersection.front().second); } else { @@ -660,7 +674,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) move_gui(head_position->first, *new_gui_position); } - XrCompositionLayerQuad imgui_layer = draw_gui(frame_state.predictedDisplayTime); + std::vector imgui_layers = draw_gui(frame_state.predictedDisplayTime); assert(renderer); renderer->start_frame(); @@ -754,7 +768,9 @@ void scenes::lobby::render(const XrFrameState & frame_state) if (composition_layer_depth_test_supported) { layers_base.push_back(reinterpret_cast(&lobby_layer)); - layers_base.push_back(reinterpret_cast(&imgui_layer)); + + for (auto & layer: imgui_layers) + layers_base.push_back(reinterpret_cast(&layer)); // Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer // The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be: @@ -767,7 +783,8 @@ void scenes::lobby::render(const XrFrameState & frame_state) .compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB}; lobby_layer.next = &layer_depth_test; - imgui_layer.next = &layer_depth_test; + for (auto & layer: imgui_layers) + layer.next = &layer_depth_test; session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); } @@ -776,7 +793,8 @@ void scenes::lobby::render(const XrFrameState & frame_state) if (not application::get_config().passthrough_enabled) layers_base.push_back(reinterpret_cast(&lobby_layer)); - layers_base.push_back(reinterpret_cast(&imgui_layer)); + for (auto & layer: imgui_layers) + layers_base.push_back(reinterpret_cast(&layer)); if (not composition_layer_depth_test_supported) layers_base.push_back(reinterpret_cast(&controllers_layer)); @@ -870,8 +888,30 @@ void scenes::lobby::on_focused() }); } - swapchain_imgui = xr::swapchain(session, device, swapchain_format, 1500, 1000); - imgui_ctx.emplace(physical_device, device, queue_family_index, queue, application::space(xr::spaces::world), imgui_inputs, swapchain_imgui, glm::vec2{0.6, 0.4}); + std::vector vps{ + { + // Main window + .size = {0.6, 0.4}, + .vp_origin = {0, 0}, + .vp_size = {1500, 1000}, + }, + { + // Pop up window + .size = {0.6, 0.2}, + .vp_origin = {1500, 0}, + .vp_size = {1500, 500}, + }, + { + // Virtual keyboard + .size = {0.6, 0.2}, + .vp_origin = {1500, 500}, + .vp_size = {1500, 500}, + }, + }; + + swapchain_imgui = xr::swapchain(session, device, swapchain_format, 3000, 1000); + + imgui_ctx.emplace(physical_device, device, queue_family_index, queue, imgui_inputs, swapchain_imgui, vps); try { diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index a027d426..cdf1c0ef 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -87,7 +87,7 @@ class lobby : public scene_impl void update_server_list(); - XrCompositionLayerQuad draw_gui(XrTime predicted_display_time); + std::vector draw_gui(XrTime predicted_display_time); XrAction recenter_left_action = XR_NULL_HANDLE; XrAction recenter_right_action = XR_NULL_HANDLE; diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 22a26e9e..7db56a15 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -319,6 +319,9 @@ void scenes::lobby::gui_server_list() if ((async_session.valid() || next_scene) && !ImGui::IsPopupOpen("connecting")) ImGui::OpenPopup("connecting"); + const auto & popup_layer = imgui_ctx->layers()[1]; + const glm::vec2 popup_layer_center = popup_layer.vp_origin + popup_layer.vp_size / 2; + ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(20, 20)); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 10); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 2); @@ -347,7 +350,7 @@ void scenes::lobby::gui_settings() { for (float rate: refresh_rates) { - if (ImGui::Selectable(fmt::format("{}", rate).c_str(), rate == current) and rate != current) + if (ImGui::Selectable(fmt::format("{}", rate).c_str(), rate == current, ImGuiSelectableFlags_SelectOnRelease) and rate != current) { session.set_refresh_rate(rate); config.preferred_refresh_rate = rate; @@ -369,7 +372,7 @@ void scenes::lobby::gui_settings() { for (float scale: available_scales) { - if (ImGui::Selectable(fmt::format(_F("{} - {}x{} per eye"), scale, (int)(width * scale), (int)(height * scale)).c_str(), scale == current) and scale != current) + if (ImGui::Selectable(fmt::format(_F("{} - {}x{} per eye"), scale, (int)(width * scale), (int)(height * scale)).c_str(), scale == current, ImGuiSelectableFlags_SelectOnRelease) and scale != current) { config.resolution_scale = scale; config.save(); @@ -535,7 +538,7 @@ void scenes::lobby::gui_licenses() try { auto current = std::make_unique(std::filesystem::path("licenses") / component); - if (ImGui::Selectable(component, component == selected_item)) + if (ImGui::Selectable(component, component == selected_item, ImGuiSelectableFlags_SelectOnRelease)) { selected_item = component; license = std::move(current); @@ -909,7 +912,7 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) } } -XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time) +std::vector scenes::lobby::draw_gui(XrTime predicted_display_time) { for (const auto & [key, server]: application::get_config().servers) { @@ -929,9 +932,7 @@ XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time) ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32(8, 8, 8, 224)); ImGui::SetNextWindowPos({50, 50}); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - ImVec2(100, 100)); - // ImGui::SetNextWindowPos({0, 0}); - // ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size); + ImGui::SetNextWindowSize({1400, 900}); ImGui::Begin("WiVRn", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index d1354b2a..8934e12a 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -206,16 +206,23 @@ void scenes::stream::on_focused() 1500, 1000); + imgui_context::viewport vp{ + .space = xr::spaces::view, + .position = {0, 0, -1}, + .orientation = {1, 0, 0, 0}, + .size = {1.0, 0.6666}, + .vp_origin = {0, 0}, + .vp_size = {1500, 1000}, + }; + imgui_ctx.emplace(physical_device, device, queue_family_index, queue, - application::space(xr::spaces::view), std::span{}, swapchain_imgui, - glm::vec2{1.0, 0.6666}); + std::vector{vp}); - imgui_ctx->set_position({0, 0, -1}, {1, 0, 0, 0}); plots_toggle_1 = get_action("plots_toggle_1").first; plots_toggle_2 = get_action("plots_toggle_2").first; } @@ -740,17 +747,20 @@ void scenes::stream::render(const XrFrameState & frame_state) .views = layer_view.data(), }; - XrCompositionLayerQuad imgui_layer; + std::vector imgui_layers; if (imgui_ctx and plots_visible) { accumulate_metrics(frame_state.predictedDisplayTime, blit_handles, timestamps); - imgui_layer = plot_performance_metrics(frame_state.predictedDisplayTime); + imgui_layers = plot_performance_metrics(frame_state.predictedDisplayTime); } layers_base.push_back(reinterpret_cast(&layer)); if (imgui_ctx and plots_visible) - layers_base.push_back(reinterpret_cast(&imgui_layer)); + { + for (auto & layer: imgui_layers) + layers_base.push_back(reinterpret_cast(&layer)); + } try { diff --git a/client/scenes/stream.h b/client/scenes/stream.h index c46914b4..2906d874 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -232,7 +232,7 @@ private: int metrics_offset = 0; void accumulate_metrics(XrTime predicted_display_time, const std::vector> & blit_handles, const gpu_timestamps & timestamps); - XrCompositionLayerQuad plot_performance_metrics(XrTime predicted_display_time); + std::vector plot_performance_metrics(XrTime predicted_display_time); void on_reference_space_changed(XrReferenceSpaceType space, XrTime) override; }; } // namespace scenes diff --git a/client/scenes/stream_stats.cpp b/client/scenes/stream_stats.cpp index b4136fc2..8140e83e 100644 --- a/client/scenes/stream_stats.cpp +++ b/client/scenes/stream_stats.cpp @@ -132,7 +132,7 @@ void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std metrics_offset = (metrics_offset + 1) % global_metrics.size(); } -XrCompositionLayerQuad scenes::stream::plot_performance_metrics(XrTime predicted_display_time) +std::vector scenes::stream::plot_performance_metrics(XrTime predicted_display_time) { imgui_ctx->new_frame(predicted_display_time); const ImGuiStyle & style = ImGui::GetStyle(); diff --git a/patches/imgui/0001-Change-ImGui-ButtonBehavior-default-flags-when-using.patch b/patches/imgui/0001-Change-ImGui-ButtonBehavior-default-flags-when-using.patch new file mode 100644 index 00000000..6f52b801 --- /dev/null +++ b/patches/imgui/0001-Change-ImGui-ButtonBehavior-default-flags-when-using.patch @@ -0,0 +1,76 @@ +From ebf48c25bdd98cbc7bb0ec01ca19539b7a841b0d Mon Sep 17 00:00:00 2001 +From: Guillaume Meunier +Date: Wed, 16 Oct 2024 20:51:52 +0200 +Subject: [PATCH] Change ImGui::ButtonBehavior default flags when using hand + tracking + +--- + imgui.cpp | 2 +- + imgui.h | 2 ++ + imgui_demo.cpp | 5 +++++ + imgui_widgets.cpp | 7 ++++++- + 4 files changed, 14 insertions(+), 2 deletions(-) + +diff --git a/imgui.cpp b/imgui.cpp +index 50c8d362e..7d67538dc 100644 +--- a/imgui.cpp ++++ b/imgui.cpp +@@ -9969,7 +9969,7 @@ static const char* GetInputSourceName(ImGuiInputSource source) + } + static const char* GetMouseSourceName(ImGuiMouseSource source) + { +- const char* mouse_source_names[] = { "Mouse", "TouchScreen", "Pen" }; ++ const char* mouse_source_names[] = { "Mouse", "TouchScreen", "Pen", "VRHandTracking", "VRController" }; + IM_ASSERT(IM_ARRAYSIZE(mouse_source_names) == ImGuiMouseSource_COUNT && source >= 0 && source < ImGuiMouseSource_COUNT); + return mouse_source_names[source]; + } +diff --git a/imgui.h b/imgui.h +index dc6ac6e59..1b31e56b4 100644 +--- a/imgui.h ++++ b/imgui.h +@@ -1838,6 +1838,8 @@ enum ImGuiMouseSource : int + ImGuiMouseSource_Mouse = 0, // Input is coming from an actual mouse. + ImGuiMouseSource_TouchScreen, // Input is coming from a touch screen (no hovering prior to initial press, less precise initial press aiming, dual-axis wheeling possible). + ImGuiMouseSource_Pen, // Input is coming from a pressure/magnetic pen (often used in conjunction with high-sampling rates). ++ ImGuiMouseSource_VRHandTracking, // Input is coming from a tracked hand in virtual reality ++ ImGuiMouseSource_VRController, // Input is coming from a controller in virtual reality + ImGuiMouseSource_COUNT + }; + +diff --git a/imgui_demo.cpp b/imgui_demo.cpp +index 7ed778b57..580a662ef 100644 +--- a/imgui_demo.cpp ++++ b/imgui_demo.cpp +@@ -7350,6 +7350,11 @@ static void ShowDemoWindowInputs() + for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } + ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); + ++ const char* mouse_source_names[] = { "Mouse", "TouchScreen", "Pen", "VRHandTracking", "VRController" }; ++ IM_ASSERT(IM_ARRAYSIZE(mouse_source_names) == ImGuiMouseSource_COUNT && io.MouseSource >= 0 && io.MouseSource < ImGuiMouseSource_COUNT); ++ ++ ImGui::Text("Mouse source: %s", mouse_source_names[io.MouseSource]); ++ + // We iterate both legacy native range and named ImGuiKey ranges. This is a little unusual/odd but this allows + // displaying the data for old/new backends. + // User code should never have to go through such hoops! +diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp +index 837d1c64d..7df2429fc 100644 +--- a/imgui_widgets.cpp ++++ b/imgui_widgets.cpp +@@ -501,7 +501,12 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool + + // Default behavior requires click + release inside bounding box + if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0) +- flags |= ImGuiButtonFlags_PressedOnDefault_; ++ { ++ if (g.IO.MouseSource == ImGuiMouseSource_VRHandTracking) ++ flags |= ImGuiButtonFlags_PressedOnClick; ++ else ++ flags |= ImGuiButtonFlags_PressedOnDefault_; ++ } + + // Default behavior inherited from item flags + // Note that _both_ ButtonFlags and ItemFlags are valid sources, so copy one into the item_flags and only check that. +-- +2.47.0 +