From f8fa4b87a81e8a3fe8b3f6bdc7c68c049750abad Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sun, 27 Oct 2024 21:05:25 +0100 Subject: [PATCH] Put magic constants in a single file --- client/constants.h | 120 +++++++++++++++++++++++++++++++++++ client/render/imgui_impl.cpp | 45 ++++++------- client/scenes/lobby.cpp | 55 +++++++--------- client/scenes/lobby_gui.cpp | 22 ++++--- 4 files changed, 179 insertions(+), 63 deletions(-) create mode 100644 client/constants.h diff --git a/client/constants.h b/client/constants.h new file mode 100644 index 00000000..0548886b --- /dev/null +++ b/client/constants.h @@ -0,0 +1,120 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace constants::gui +{ +// Minimum distance between a GUI layer and a fingertip/controller to register a click +constexpr float min_pointer_distance = -0.1; + +// Number of combined image samplers in ImGui descriptor pool +constexpr unsigned int nb_combined_image_samplers = 100; + +// Font to use (desktop only) +constexpr const char font_name[] = "Noto Sans"; + +// Font sizes +constexpr int font_size_small = 30; +constexpr int font_size_large = 75; + +// Ratio between joystick position and scroll distance/s +constexpr float scroll_ratio = 3; + +// Threshold on the trigger value to register a click +constexpr float trigger_click_thd = 0.8; + +// Thresholds on the distance between the fingertip and GUI layers +constexpr float fingertip_distance_hovering_thd = 0.15; +constexpr float fingertip_distance_touching_thd = 0.05; + +// Minimum scroll value to enable a controller +constexpr float scroll_value_thd = 0.01; + +// Pointer radius +constexpr float pointer_radius_in = 10; +constexpr float pointer_radius_out = 12; +constexpr float pointer_thickness = 4; + +// Pointer transparency +constexpr float pointer_alpha = 0.8; +constexpr float pointer_alpha_disabled = 0.25; +constexpr float pointer_fading_distance = 40; + +// Pointer color +constexpr uint32_t pointer_color_pressed = 0xffff3300; +constexpr uint32_t pointer_color_unpressed = 0xffffffff; +constexpr uint32_t pointer_color_border = 0xff000000; +} // namespace constants::gui + +namespace constants::lobby +{ +// Position and orientation of the GUI layers +constexpr float gui_pitch = -0.2; +constexpr float keyboard_pitch = -0.6; +constexpr glm::vec3 popup_position = {0, 0, 0.05}; +constexpr glm::vec3 keyboard_position = {0, -0.3, 0.1}; + +// Position of the near plane +constexpr float near_plane = 0.02; + +// Recenter gesture thresholds +constexpr float recenter_cosangle_min = 0.8; +constexpr float recenter_distance_up = 0.3; +constexpr float recenter_distance_front = 0.2; + +// Recenter distance when using the controller, if the controller doesn't point at the GUI when the button is pressed +constexpr float recenter_action_distance = 0.3; + +// Default distance between the headset and the GUI, when the GUI is first shown, when the session state changes, when the lobby is refocused +constexpr float initial_gui_distance = 0.5; + +// Skybox color +constexpr std::array sky_color = {0, 0.25, 0.5, 1}; + +// Dimming scale/bias when a popup window is shown +constexpr XrColor4f dimming_scale = {0.5, 0.5, 0.5, 1}; +constexpr XrColor4f dimming_bias = {0.25, 0.25, 0.25, 0}; + +// Z-indices of composition layers +constexpr int zindex_passthrough = -2; +constexpr int zindex_lobby = -1; +constexpr int zindex_gui = 0; +constexpr int zindex_controllers = 1; +constexpr int zindex_recenter_tip = 2; +} // namespace constants::lobby + +namespace constants::style +{ +constexpr ImVec2 window_padding = {20, 20}; +constexpr float window_border_size = 2; +constexpr float window_rounding = 10; + +constexpr ImVec2 tooltip_padding = {5, 5}; +constexpr float tooltip_distance = 10; + +constexpr ImVec2 button_size = {220, 80}; +constexpr float connection_popup_width = 1000; + +} // namespace constants::style diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 59cccc38..cd528879 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -21,6 +21,7 @@ #include "application.h" #include "asset.h" +#include "constants.h" #include "image_loader.h" #include "openxr/openxr.h" #include "utils/ranges.h" @@ -206,7 +207,7 @@ std::vector> imgui_context::ray_plane_intersection(cons // => ray_start.z + distance × ray_dir.z = 0 float distance = -ray_start.z / ray_dir.z; - if (distance < -0.1) + if (distance < constants::gui::min_pointer_distance) continue; coord.x = ray_start.x + distance * ray_dir.x; @@ -292,7 +293,7 @@ static const std::array pool_sizes = { vk::DescriptorPoolSize{ .type = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 100, + .descriptorCount = constants::gui::nb_combined_image_samplers, }}; static const vk::DescriptorSetLayoutBinding layout_bindings{ @@ -456,7 +457,7 @@ static std::vector find_font(ImFontGlyphRangesBuilder glyph_range_b static FcConfig * config = FcInitLoadConfigAndFonts(); - FcPattern * pattern = FcNameParse((const FcChar8 *)"Noto Sans"); + FcPattern * pattern = FcNameParse((const FcChar8 *)constants::gui::font_name); if (!pattern) throw std::runtime_error("Failed to create Fontconfig pattern"); @@ -587,15 +588,15 @@ void imgui_context::initialize_fonts() for (auto & font: fonts) { spdlog::info("Using font {}", font, application::get_messages_info().language); - io.Fonts->AddFontFromFileTTF(font.c_str(), 30, &config, glyph_ranges.Data); + io.Fonts->AddFontFromFileTTF(font.c_str(), constants::gui::font_size_small, &config, glyph_ranges.Data); config.MergeMode = true; } config.MergeMode = true; config.GlyphMinAdvanceX = 40; // Use if you want to make the icon monospaced static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0}; - io.Fonts->AddFontFromMemoryTTF(const_cast(font_awesome_regular.data()), font_awesome_regular.size(), 30, &config, icon_ranges); - io.Fonts->AddFontFromMemoryTTF(const_cast(font_awesome_solid.data()), font_awesome_solid.size(), 30, &config, icon_ranges); + io.Fonts->AddFontFromMemoryTTF(const_cast(font_awesome_regular.data()), font_awesome_regular.size(), constants::gui::font_size_small, &config, icon_ranges); + io.Fonts->AddFontFromMemoryTTF(const_cast(font_awesome_solid.data()), font_awesome_solid.size(), constants::gui::font_size_small, &config, icon_ranges); } { @@ -603,7 +604,7 @@ void imgui_context::initialize_fonts() config.FontDataOwnedByAtlas = false; for (auto & font: fonts) { - large_font = io.Fonts->AddFontFromFileTTF(font.c_str(), 75, &config, glyph_ranges.Data); + large_font = io.Fonts->AddFontFromFileTTF(font.c_str(), constants::gui::font_size_large, &config, glyph_ranges.Data); config.MergeMode = true; } } @@ -615,7 +616,7 @@ void imgui_context::initialize_fonts() std::vector imgui_context::read_controllers_state(XrTime display_time) { - float scroll_scale = io.DeltaTime * 3; + float scroll_scale = io.DeltaTime * constants::gui::scroll_ratio; size_t new_focused_controller = focused_controller; std::vector new_states; @@ -665,7 +666,7 @@ std::vector imgui_context::read_controllers_sta /*if (new_state.trigger_value < 0.5) new_state.trigger_clicked = false; else */ - if (new_state.trigger_value > 0.8) + if (new_state.trigger_value > constants::gui::trigger_click_thd) new_state.trigger_clicked = true; } @@ -686,11 +687,10 @@ std::vector imgui_context::read_controllers_sta if (state.source == ImGuiMouseSource_VRHandTracking) { - // TODO tunable - if (state.hover_distance < 0.15) + if (state.hover_distance < constants::gui::fingertip_distance_hovering_thd) state.fingertip_hovering = true; - if (state.hover_distance < 0.05) + if (state.hover_distance < constants::gui::fingertip_distance_touching_thd) state.fingertip_touching = true; } } @@ -777,7 +777,7 @@ void imgui_context::new_frame(XrTime display_time) { if (state.source == ImGuiMouseSource_VRController) { - if (state.trigger_clicked or glm::length(state.scroll_value) > 0.01f) + if (state.trigger_clicked or glm::length(state.scroll_value) > constants::gui::scroll_value_thd) { new_focused_controller = index; } @@ -810,7 +810,7 @@ void imgui_context::new_frame(XrTime display_time) { io.AddMousePosEvent(position->x, position->y); - if (glm::length(scroll) > 0.01f) + if (glm::length(scroll) > constants::gui::scroll_value_thd) io.AddMouseWheelEvent(scroll.x, scroll.y); } } @@ -930,21 +930,22 @@ std::vector> imgui_context::end_frame() distance = std::min(distance, distance_to_window(window, *position)); } - float radius = 10; - float alpha = std::clamp((40 - distance) / 50, 0, 0.8); + float alpha = std::clamp(1 - distance / constants::gui::pointer_fading_distance, 0, 1); - if (&controller != &controllers[focused_controller]) - alpha /= 3; + if (&controller == &controllers[focused_controller]) + alpha *= constants::gui::pointer_alpha; + else + alpha *= constants::gui::pointer_alpha_disabled; - ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha)); - ImU32 color_unpressed = ImGui::GetColorU32(ImVec4(1, 1, 1, alpha)); + ImU32 color_pressed = ImGui::GetColorU32(constants::gui::pointer_color_pressed, alpha); + ImU32 color_unpressed = ImGui::GetColorU32(constants::gui::pointer_color_unpressed, alpha); bool pressed = controller.second.trigger_clicked || controller.second.fingertip_touching; 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->AddCircleFilled(*position, constants::gui::pointer_radius_in, pressed ? color_pressed : color_unpressed); + draw_list->AddCircle(*position, constants::gui::pointer_radius_out, ImGui::GetColorU32(constants::gui::pointer_color_border, alpha), 0, constants::gui::pointer_thickness); draw_list->PopClipRect(); } } diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index a922efd3..984e15b9 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -19,6 +19,7 @@ #include "lobby.h" #include "application.h" +#include "constants.h" #include "glm/geometric.hpp" #include "hardware.h" #include "imgui.h" @@ -73,7 +74,8 @@ static const std::array supported_depth_formats{ static glm::quat compute_gui_orientation(glm::vec3 head_position, glm::vec3 new_gui_position) { - const float gui_pitch = -0.2; + using constants::lobby::gui_pitch; + glm::vec3 gui_direction = new_gui_position - head_position; float gui_yaw = atan2(gui_direction.x, gui_direction.z) + M_PI; @@ -83,7 +85,9 @@ static glm::quat compute_gui_orientation(glm::vec3 head_position, glm::vec3 new_ void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position) { - const float keyboard_pitch = -0.6; + using constants::lobby::keyboard_pitch; + using constants::lobby::keyboard_position; + using constants::lobby::popup_position; auto q = compute_gui_orientation(head_position, new_gui_position); auto M = glm::mat3_cast(q); // plane-to-world transform @@ -93,11 +97,11 @@ void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 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].position = new_gui_position + M * popup_position; 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].position = new_gui_position + M * keyboard_position; imgui_ctx->layers()[2].orientation = q * glm::quat(cos(keyboard_pitch / 2), sin(keyboard_pitch / 2), 0, 0); } @@ -394,9 +398,9 @@ std::optional scenes::lobby::check_recenter_gesture(const std::array< glm::quat q{o.w, o.x, o.y, o.z}; glm::vec3 v{p.x, p.y, p.z}; - if (glm::dot(q * glm::vec3(0, 1, 0), glm::vec3(0, -1, 0)) > 0.8) + if (glm::dot(q * glm::vec3(0, 1, 0), glm::vec3(0, -1, 0)) > constants::lobby::recenter_cosangle_min) { - return v + glm::vec3(0, 0.3, 0) + q * glm::vec3(0, 0, -0.2); + return v + glm::vec3(0, constants::lobby::recenter_distance_up, 0) + q * glm::vec3(0, 0, -constants::lobby::recenter_distance_front); } return std::nullopt; @@ -443,7 +447,7 @@ std::optional scenes::lobby::check_recenter_action(XrTime predicted_d else { gui_recenter_position = glm::vec3(0, 0, 0); - gui_recenter_distance = 0.3; + gui_recenter_distance = constants::lobby::recenter_action_distance; } } else @@ -488,14 +492,12 @@ std::optional scenes::lobby::check_recenter_action(XrTime predicted_d std::optional scenes::lobby::check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation) { - const float gui_distance = 0.50; - glm::vec3 head_direction = -glm::column(glm::mat3_cast(head_orientation), 2); if (recenter_gui) { recenter_gui = false; - glm::vec3 new_gui_position = head_position + gui_distance * head_direction; + glm::vec3 new_gui_position = head_position + constants::lobby::initial_gui_distance * head_direction; new_gui_position.y = head_position.y - 0.1; return new_gui_position; } @@ -528,12 +530,10 @@ static std::pair, std::vector, std::vector::infinity(), - .farZ = zn, + .farZ = constants::lobby::near_plane, }); } @@ -789,7 +789,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) if (application::get_config().passthrough_enabled) clear_color = {0, 0, 0, 0}; else - clear_color = {0, 0.25, 0.5, 1}; + clear_color = constants::lobby::sky_color; if (composition_layer_depth_test_supported) { @@ -884,7 +884,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND; }, [&](auto & p) { - layers_with_z_index.emplace_back(-2, p.layer()); + layers_with_z_index.emplace_back(constants::lobby::zindex_passthrough, p.layer()); }}, passthrough); } @@ -892,18 +892,8 @@ void scenes::lobby::render(const XrFrameState & frame_state) // Dimming settings if a popup window is displayed XrCompositionLayerColorScaleBiasKHR color_scale_bias{ .type = XR_TYPE_COMPOSITION_LAYER_COLOR_SCALE_BIAS_KHR, - .colorScale = { - .r = 0.5, - .g = 0.5, - .b = 0.5, - .a = 1, - }, - .colorBias = { - .r = 0.25, - .g = 0.25, - .b = 0.25, - .a = 0, - }, + .colorScale = constants::lobby::dimming_scale, + .colorBias = constants::lobby::dimming_bias, }; // Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer @@ -918,12 +908,12 @@ void scenes::lobby::render(const XrFrameState & frame_state) }; // if (composition_layer_depth_test_supported or not application::get_config().passthrough_enabled) - layers_with_z_index.emplace_back(-1, reinterpret_cast(&lobby_layer)); + layers_with_z_index.emplace_back(constants::lobby::zindex_lobby, reinterpret_cast(&lobby_layer)); for (auto & [z_index, layer]: imgui_layers) layers_with_z_index.emplace_back(z_index, reinterpret_cast(&layer)); - layers_with_z_index.emplace_back(1000, reinterpret_cast(&controllers_layer)); + layers_with_z_index.emplace_back(constants::lobby::zindex_controllers, reinterpret_cast(&controllers_layer)); if (composition_layer_depth_test_supported) { @@ -1083,6 +1073,7 @@ void scenes::lobby::on_focused() .size = {0.6, 0.4}, .vp_origin = {0, 0}, .vp_size = {1500, 1000}, + .z_index = constants::lobby::zindex_gui, }, { // Pop up window @@ -1090,6 +1081,7 @@ void scenes::lobby::on_focused() .size = {0.6, 0.24}, .vp_origin = {1500, 0}, .vp_size = {1500, 600}, + .z_index = constants::lobby::zindex_gui, }, { // Virtual keyboard @@ -1098,6 +1090,7 @@ void scenes::lobby::on_focused() .vp_origin = {1500, 600}, .vp_size = {1500, 500}, .always_show_cursor = true, + .z_index = constants::lobby::zindex_gui, }, { // Recenter tip @@ -1107,7 +1100,7 @@ void scenes::lobby::on_focused() .size = {0.6, 0.12}, .vp_origin = {0, 1000}, .vp_size = {1500, 300}, - .z_index = 2000, + .z_index = constants::lobby::zindex_recenter_tip, }}; swapchain_imgui = xr::swapchain(session, device, swapchain_format, 3000, 1300); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 797f8ff7..4102c64c 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -22,6 +22,7 @@ #include "application.h" #include "asset.h" #include "configuration.h" +#include "constants.h" #include "imgui.h" #include "imgui_internal.h" #include "implot.h" @@ -69,7 +70,7 @@ static void CenterTextHV(const std::string & text) static void display_recentering_tip(imgui_context & ctx, const std::string & tip) { ImGui::PushFont(ctx.large_font); - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 20}); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::window_padding); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 2); ImGui::SetNextWindowPos(ctx.layers()[3].vp_center(), ImGuiCond_Always, {0.5, 0.5}); ImGui::Begin("Recentering tip", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize); @@ -108,10 +109,10 @@ void scenes::lobby::tooltip(std::string_view text) ImVec2 pos{ (ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2, - ImGui::GetItemRectMin().y - 10, + ImGui::GetItemRectMin().y - constants::style::tooltip_distance, }; - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {5, 5}); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::tooltip_padding); // Clamp position to avoid overflowing on the right auto & style = ImGui::GetStyle(); @@ -142,7 +143,7 @@ void scenes::lobby::vibrate_on_hover() void scenes::lobby::gui_connecting() { - const ImVec2 button_size(220, 80); + using constants::style::button_size; std::string status; if (next_scene) @@ -198,14 +199,14 @@ void scenes::lobby::gui_connecting() void scenes::lobby::gui_new_server() { + using constants::style::button_size; + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20}); ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, {10, 10}); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10}); ImGui::Dummy({1000, 1}); - const ImVec2 button_size(220, 80); - ImGui::BeginTable("table", 2); ImGui::TableSetupColumn("Field name", ImGuiTableColumnFlags_WidthFixed); @@ -293,6 +294,8 @@ void scenes::lobby::gui_new_server() void scenes::lobby::gui_server_list() { + using constants::style::button_size; + auto & config = application::get_config(); // Build an index of the cookies sorted by server name std::multimap sorted_cookies; @@ -301,7 +304,6 @@ void scenes::lobby::gui_server_list() sorted_cookies.emplace(data.service.name, cookie); } - const ImVec2 button_size(220, 80); const float list_item_height = 100; auto & style = ImGui::GetStyle(); @@ -418,9 +420,9 @@ void scenes::lobby::gui_server_list() 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); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::window_padding); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::window_rounding); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, constants::style::window_border_size); if (ImGui::BeginPopupModal("connecting", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) { gui_connecting();