#include "UI.hpp" #include #include #include #include "../state.h" #include "../contouring/Abstract.hpp" #include "../world/materials.hpp" void UI::setup(GLFWwindow* window) { // Setup Dear ImGui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGui::StyleColorsDark(); // Setup Platform/Renderer bindings ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init("#version 130"); } void UI::unload() { ImGui_ImplGlfw_Shutdown(); ImGui_ImplOpenGL3_Shutdown(); ImGui::DestroyContext(); } UI::Actions UI::draw(options &options, state &state, const reports &reports, GLuint aim) { auto actions = Actions::None; ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); const ImGuiIO &io = ImGui::GetIO(); if(state.capture_mouse) { ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x / 2, io.DisplaySize.y / 2), ImGuiCond_Always, ImVec2(.5f, .5f)); ImGui::Begin("Aim", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoInputs); ImGui::Image((void *)(intptr_t)aim, ImVec2(32, 32)); ImGui::End(); } if (options.show_debug_menu) { ImGui::BeginMainMenuBar(); if (ImGui::BeginMenu("Debug")) { ImGui::Checkbox("Render", &options.show_debug_render); ImGui::Checkbox("World", &options.show_debug_world); ImGui::Checkbox("Contouring", &options.show_debug_contouring); ImGui::Checkbox("Controls", &options.show_debug_controls); ImGui::EndMenu(); } ImGui::Checkbox("Editor", &options.editor_show); if(ImGui::MenuItem("Close")) options.show_debug_menu = false; ImGui::EndMainMenuBar(); } if (options.show_debug_render) { ImGui::Begin("Debug: Render", &options.show_debug_render, ImGuiWindowFlags_AlwaysAutoResize); ImGui::Text("Tris: %ld (%ld models)", reports.tris_count, reports.models_count); ImGui::Separator(); ImGui::Checkbox("Overlay", &options.overlay_show); if (ImGui::SliderInt("FPS", &options.target_fps, MIN_FPS-1, MAX_FPS+1, options.target_fps > MIN_FPS ? (options.target_fps < MAX_FPS ? "%d" : "UNLIMITED") : "VSYNC")){ actions |= Actions::FPS; } ImGui::Text("Sampling %d (requires restart)", options.samples); if (ImGui::Checkbox("Fullscreen", &options.fullscreen)){ actions |= Actions::FullScreen; } { bool changeRenderer = false; changeRenderer |= ImGui::Checkbox("PBR", &options.renderer.main.pbr); ImGui::SameLine(); changeRenderer |= ImGui::Checkbox("Triplanar", &options.renderer.main.triplanar); changeRenderer |= ImGui::Checkbox("Geometry", &options.renderer.main.geometry); ImGui::SameLine(); if(options.renderer.main.geometry) { changeRenderer |= ImGui::Checkbox("Blend", &options.renderer.main.blend); } else { ImGui::TextDisabled("Blend"); } changeRenderer |= ImGui::Checkbox("Fog", &options.renderer.main.fog); ImGui::SameLine(); ImGui::Checkbox("Skybox", &options.renderer.skybox); if (changeRenderer) { actions |= Actions::RendererSharders; } } if (ImGui::ColorEdit3("Fog color", &options.renderer.clear_color[0])) { actions |= Actions::ClearColor; } if (ImGui::Checkbox("Wireframe", &options.renderer.wireframe)) glPolygonMode(GL_FRONT_AND_BACK, options.renderer.wireframe ? GL_LINE : GL_FILL); ImGui::Text("Textures '%s'", options.renderer.textures.c_str()); // MAYBE: select if (ImGui::SliderFloat("LOD", &options.renderer.mipMapLOD, -1, 1)) { actions |= Actions::RendererTextures; } ImGui::End(); } if (options.show_debug_world) { ImGui::Begin("Debug: World", &options.show_debug_world, ImGuiWindowFlags_AlwaysAutoResize); ImGui::Text("Path: %s", options.world.folderPath.c_str()); if (ImGui::SliderInt("Load distance", &options.world.loadDistance, 1, options.world.keepDistance) | ImGui::SliderInt("Keep distance", &options.world.keepDistance, options.world.loadDistance + 1, 21)) { actions |= Actions::World; } if(ImGui::SliderInt("Voxel density", &options.voxel_density, 1, CHUNK_LENGTH * REGION_LENGTH)) { options.voxel_density = pow(2, ceil(log(options.voxel_density) / log(2))); } ImGui::End(); } if (options.show_debug_contouring) { ImGui::Begin("Debug: Contouring", &options.show_debug_contouring, ImGuiWindowFlags_AlwaysAutoResize); if (ImGui::BeginCombo("Contouring", contouring::names[options.contouring_idx].c_str())) { for (size_t i = 0; i < contouring::names.size(); i++) { const bool is_selected = (options.contouring_idx == (int)i); if (ImGui::Selectable(contouring::names[i].c_str(), is_selected)) { actions |= Actions::ChangeContouring; contouring::save(options.contouring_idx, state.contouring, options.contouring_data); options.contouring_idx = i; } if (is_selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } ImGui::Checkbox("Culling", &options.culling); state.contouring->onGui(); ImGui::End(); } { const auto farRange = state.contouring->getFarRange(); if (options.show_debug_controls) { ImGui::Begin("Debug: Controls", &options.show_debug_controls, ImGuiWindowFlags_AlwaysAutoResize); const auto p = state.position.as_voxel(options.voxel_density); ImGui::Text("Position: (%lld, %lld, %lld)", p.x, p.y, p.z); ImGui::Separator(); { if (ImGui::SliderFloat("Move speed", &options.control.speed, 0.1, 50) | ImGui::SliderInt("Sensibility", &options.control.sensibility, 1, 100, "%d%%")) { actions |= Actions::Control; } ImGui::Checkbox("Collide", &options.control.collide); } ImGui::Separator(); { bool changePerspective = false; changePerspective |= ImGui::SliderAngle("FoV", &options.camera.fov, 30, 110); changePerspective |= ImGui::SliderFloat("Near", &options.camera.near, 0.01, 10); changePerspective |= ImGui::SliderFloat("Far", &options.camera.far, farRange.first / options.voxel_density, farRange.second / options.voxel_density); if(changePerspective) { actions |= Actions::Camera; } } ImGui::End(); } const auto far = std::clamp(options.camera.far, farRange.first / options.voxel_density, farRange.second / options.voxel_density); if(far != options.camera.far) { options.camera.far = far; actions |= Actions::Camera; } } if (options.editor_show) { ImGui::Begin("Editor", &options.editor_show, ImGuiWindowFlags_AlwaysAutoResize); if (state.look_at.has_value()) { const auto &look = state.look_at.value(); ImGui::Text("Look at: (%ld: %lld, %lld, %lld) (%s, %.1f)", look.pos.first.index, look.pos.second.x, look.pos.second.y, look.pos.second.z, world::materials::textures[look.value.material()].c_str(), look.value.density() * 1. / world::Voxel::DENSITY_MAX); const auto w_pos = look.pos.second + look.offset; ImGui::Text("(%.3f, %.3f, %.3f)", w_pos.x * 1. / options.voxel_density, w_pos.y * 1. / options.voxel_density, w_pos.z * 1. / options.voxel_density); } else { ImGui::Text("Look at: none"); } ImGui::Separator(); if (ImGui::BeginCombo("Material", world::materials::textures[options.tool.material].c_str())) { for (size_t i = 0; i < world::materials::textures.size(); i++) { const bool is_selected = (options.tool.material == i); if (ImGui::Selectable(world::materials::textures[i].c_str(), is_selected)) options.tool.material = i; if (is_selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } ImGui::SliderInt("Radius", &options.tool.radius, 0, 10); ImGui::End(); } /*if (show_console) { ImGui::SetNextWindowPos(ImVec2(UI_MARGIN, 500), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(200, 100), ImGuiCond_FirstUseEver); ImGui::Begin("Console", &show_console, ImGuiWindowFlags_MenuBar); ImGui::BeginMenuBar(); ImGui::MenuItem("Auto-scrool", NULL, &console_scrool); ImGui::EndMenuBar(); // TODO: text ImGui::Separator(); // Command-line bool reclaim_focus = false; // TODO: completion callback if (ImGui::InputText("Input", console_buffer, IM_ARRAYSIZE(console_buffer), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory)) { char *s = console_buffer; //Strtrim(s); //if (s[0]) // ExecCommand(s); strcpy(s, ""); reclaim_focus = true; } // Auto-focus on window apparition ImGui::SetItemDefaultFocus(); if (reclaim_focus) ImGui::SetKeyboardFocusHere(-1); // Auto focus previous widget ImGui::End(); }*/ if (options.overlay_show) { if (options.overlay_corner != -1) { ImVec2 window_pos = ImVec2((options.overlay_corner & 1) ? io.DisplaySize.x - UI_MARGIN : UI_MARGIN, (options.overlay_corner & 2) ? io.DisplaySize.y - UI_MARGIN : UI_MARGIN); ImVec2 window_pos_pivot = ImVec2((options.overlay_corner & 1) ? 1.0f : 0.0f, (options.overlay_corner & 2) ? 1.0f : 0.0f); ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); } ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background ImGui::Begin("Overlay", &options.overlay_show, (options.overlay_corner != -1 ? ImGuiWindowFlags_NoMove : 0) | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav); ImGui::Text("%.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::Text("%ld tris(%ld models)", reports.tris_count, reports.models_count); if (ImGui::BeginPopupContextWindow()) { if (ImGui::MenuItem("Custom", NULL, options.overlay_corner == -1)) options.overlay_corner = -1; if (ImGui::MenuItem("Top-left", NULL, options.overlay_corner == 0)) options.overlay_corner = 0; if (ImGui::MenuItem("Top-right", NULL, options.overlay_corner == 1)) options.overlay_corner = 1; if (ImGui::MenuItem("Bottom-left", NULL, options.overlay_corner == 2)) options.overlay_corner = 2; if (ImGui::MenuItem("Bottom-right", NULL, options.overlay_corner == 3)) options.overlay_corner = 3; if (options.overlay_show && ImGui::MenuItem("Close")) options.overlay_show = false; ImGui::EndPopup(); } ImGui::End(); } ImGui::Render(); return actions; } void UI::render() { ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } bool UI::isFocus() { return ImGui::IsAnyItemActive(); }