Put magic constants in a single file

This commit is contained in:
Guillaume Meunier
2024-11-01 14:13:58 +01:00
parent 8126bb0541
commit f8fa4b87a8
4 changed files with 179 additions and 63 deletions
+24 -31
View File
@@ -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<glm::vec3> 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<glm::vec3> 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<glm::vec3> scenes::lobby::check_recenter_action(XrTime predicted_d
std::optional<glm::vec3> 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<XrCompositionLayerProjectionView>, std::vector<XrCo
if (depth_swapchain)
depth_swapchain.wait();
float zn = 0.02;
frames.push_back({
.destination = color_swapchain.images()[color_image_index].image,
.depth_buffer = depth_swapchain ? depth_swapchain.images()[depth_image_index].image : vk::Image{},
.projection = projection_matrix(view.fov, zn),
.projection = projection_matrix(view.fov, constants::lobby::near_plane),
.view = view_matrix(view.pose),
});
@@ -562,7 +562,7 @@ static std::pair<std::vector<XrCompositionLayerProjectionView>, std::vector<XrCo
.minDepth = 0,
.maxDepth = 1,
.nearZ = std::numeric_limits<float>::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<XrCompositionLayerBaseHeader *>(&lobby_layer));
layers_with_z_index.emplace_back(constants::lobby::zindex_lobby, reinterpret_cast<XrCompositionLayerBaseHeader *>(&lobby_layer));
for (auto & [z_index, layer]: imgui_layers)
layers_with_z_index.emplace_back(z_index, reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer));
layers_with_z_index.emplace_back(1000, reinterpret_cast<XrCompositionLayerBaseHeader *>(&controllers_layer));
layers_with_z_index.emplace_back(constants::lobby::zindex_controllers, reinterpret_cast<XrCompositionLayerBaseHeader *>(&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);
+12 -10
View File
@@ -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<std::string, std::string> 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();