#include "application.h" #include "rendering/renderer.h" #include "settings_manager.h" #include "hdri_manager.h" #include "states/simulation_state.h" #include "states/config_state.h" #include "states/world_builder_state.h" #include #include #include #include "platform/vulkan/vulkan_renderer.h" #include "platform/opengl/opengl_renderer.h" #ifdef __APPLE__ #include "platform/metal/metal_context.h" #include "platform/vulkan/vulkan_context.h" #endif namespace Donut { Application* Application::s_instance = nullptr; Application::Application(const std::string& name, int width, int height) : m_running(true), m_minimized(false) { s_instance = this; // Select the render API before the window is created: the window is // built differently for Vulkan (GLFW_NO_API) than for OpenGL. Logger::init(); SettingsManager::initialize(); { const auto& settings = SettingsManager::get_settings_const(); RendererAPI::set_api(settings.graphics.render_api == "Vulkan" ? RendererAPI::API::Vulkan : RendererAPI::API::OpenGL); } if (RendererAPI::get_api() == RendererAPI::API::Vulkan) vulkan_prepare_glfw(); // must precede glfwInit() inside the Window ctor m_window = create_scope(name, width, height); m_window->set_event_callback([this](Event& event) { on_event(event); }); on_init(); } Application::~Application() { on_shutdown(); s_instance = nullptr; } auto Application::run() -> void { while (m_running) { if (m_renderer) { glfwPollEvents(); // before the ImGui frame so input is current if (!m_minimized) m_renderer->draw_frame(glm::vec4(0.05f, 0.06f, 0.10f, 1.0f), [this] { build_ui(); }); } else { if (!m_minimized) { on_update(); on_render(); } m_window->on_update(); // polls events + swaps buffers (OpenGL) } } } // One-time ImGui theme: rounded, roomy, dark with a warm accretion-disk accent. static auto apply_donut_style() -> void { ImGuiStyle& s = ImGui::GetStyle(); s.WindowRounding = 7.0f; s.ChildRounding = 5.0f; s.FrameRounding = 4.0f; s.GrabRounding = 4.0f; s.PopupRounding = 4.0f; s.ScrollbarRounding = 5.0f; s.TabRounding = 4.0f; s.WindowPadding = ImVec2(12, 12); s.FramePadding = ImVec2(9, 5); s.ItemSpacing = ImVec2(9, 8); s.ItemInnerSpacing = ImVec2(7, 5); s.WindowBorderSize = 0.0f; s.FrameBorderSize = 0.0f; s.WindowTitleAlign = ImVec2(0.02f, 0.5f); const ImVec4 amber = ImVec4(0.98f, 0.62f, 0.20f, 1.00f); const ImVec4 amberHi = ImVec4(1.00f, 0.73f, 0.36f, 1.00f); ImVec4* c = s.Colors; c[ImGuiCol_WindowBg] = ImVec4(0.07f, 0.08f, 0.10f, 0.97f); c[ImGuiCol_ChildBg] = ImVec4(0.10f, 0.11f, 0.13f, 0.55f); c[ImGuiCol_PopupBg] = ImVec4(0.09f, 0.10f, 0.12f, 0.98f); c[ImGuiCol_TitleBg] = ImVec4(0.09f, 0.10f, 0.12f, 1.00f); c[ImGuiCol_TitleBgActive] = ImVec4(0.13f, 0.14f, 0.17f, 1.00f); c[ImGuiCol_Text] = ImVec4(0.90f, 0.91f, 0.93f, 1.00f); c[ImGuiCol_TextDisabled] = ImVec4(0.48f, 0.50f, 0.54f, 1.00f); c[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.17f, 0.20f, 1.00f); c[ImGuiCol_FrameBgHovered] = ImVec4(0.22f, 0.24f, 0.28f, 1.00f); c[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.28f, 0.33f, 1.00f); c[ImGuiCol_Header] = ImVec4(0.17f, 0.19f, 0.23f, 1.00f); c[ImGuiCol_HeaderHovered] = ImVec4(0.24f, 0.27f, 0.32f, 1.00f); c[ImGuiCol_HeaderActive] = ImVec4(0.28f, 0.31f, 0.37f, 1.00f); c[ImGuiCol_Button] = ImVec4(0.20f, 0.22f, 0.26f, 1.00f); c[ImGuiCol_ButtonHovered] = ImVec4(0.27f, 0.30f, 0.35f, 1.00f); c[ImGuiCol_ButtonActive] = amber; c[ImGuiCol_SliderGrab] = amber; c[ImGuiCol_SliderGrabActive]= amberHi; c[ImGuiCol_CheckMark] = amberHi; c[ImGuiCol_Separator] = ImVec4(0.20f, 0.22f, 0.26f, 1.00f); c[ImGuiCol_ScrollbarGrab] = ImVec4(0.24f, 0.26f, 0.30f, 1.00f); } auto Application::build_ui() -> void { static bool styled = false; if (!styled) { apply_donut_style(); styled = true; } if (!m_renderer) return; RendererBackend& r = *m_renderer; // Full-viewport dock space with a pass-through centre, so the panel can be // docked to any edge while the render shows through the middle. ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode); ImGui::SetNextWindowSize(ImVec2(340, 580), ImGuiCond_FirstUseEver); ImGui::Begin("Donut \xc2\xb7 Black Hole"); // --- header: device + performance ----------------------------------- ImGuiIO& io = ImGui::GetIO(); ImGui::TextDisabled("%s", r.device_name().empty() ? "GPU" : r.device_name().c_str()); ImGui::Text("%.0f FPS", io.Framerate); ImGui::SameLine(); ImGui::TextDisabled("%.1f ms/frame", io.Framerate > 0 ? 1000.0f / io.Framerate : 0.0f); ImGui::Spacing(); if (ImGui::CollapsingHeader("Renderer", ImGuiTreeNodeFlags_DefaultOpen)) { auto& s = SettingsManager::get_settings(); const char* apis[] = { "OpenGL", "Vulkan" }; int cur = (s.graphics.render_api == "Vulkan") ? 1 : 0; if (ImGui::Combo("Graphics API", &cur, apis, 2)) { s.graphics.render_api = apis[cur]; SettingsManager::save_settings(); } const char* running = (RendererAPI::get_api() == RendererAPI::API::Vulkan) ? "Vulkan" : "OpenGL"; if (s.graphics.render_api != running) ImGui::TextColored(ImVec4(0.98f, 0.62f, 0.20f, 1.0f), "Restart to apply (%s running)", running); else ImGui::TextDisabled("Active backend: %s", running); } ImGui::Spacing(); bool scene = r.is_scene_mode(); ImGui::TextDisabled("VIEW"); if (ImGui::RadioButton("Black hole", !scene)) r.set_scene_mode(false); ImGui::SameLine(); if (ImGui::RadioButton("Scene", scene)) r.set_scene_mode(true); ImGui::Separator(); if (scene) { build_scene_ui(); } else { BlackHoleParams& bh = r.black_hole_params(); if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen)) { bool ff = r.is_free_fly(); if (ImGui::RadioButton("Orbital", !ff)) r.set_free_fly(false); ImGui::SameLine(); if (ImGui::RadioButton("Free-fly", ff)) r.set_free_fly(true); ImGui::SameLine(); if (ImGui::Button("Reset view")) r.reset_camera(); ImGui::SliderFloat("FOV", &bh.fov_degrees, 20.0f, 90.0f, "%.0f\xc2\xb0"); ImGui::TextDisabled(ff ? "WASD move | Q/E down-up | Shift boost | drag look" : "Drag to orbit | scroll to zoom"); } if (ImGui::CollapsingHeader("Accretion disk", ImGuiTreeNodeFlags_DefaultOpen)) { ImGui::SliderFloat("Temperature", &bh.temperature, 2000.0f, 15000.0f, "%.0f K"); ImGui::SliderFloat("Brightness", &bh.brightness, 0.0f, 3.0f, "%.2f"); ImGui::SliderFloat("Inner radius", &bh.disk_inner_rs, 3.0f, 12.0f, "%.1f r_s"); if (bh.disk_outer_rs < bh.disk_inner_rs + 0.5f) bh.disk_outer_rs = bh.disk_inner_rs + 0.5f; ImGui::SliderFloat("Outer radius", &bh.disk_outer_rs, bh.disk_inner_rs + 0.5f, 25.0f, "%.1f r_s"); ImGui::SliderFloat("Turbulence", &bh.turbulence, 0.0f, 2.0f, "%.2f"); ImGui::TextDisabled("Inner edge is the ISCO (3 r_s). Novikov-Thorne profile."); } if (ImGui::CollapsingHeader("Quality")) { ImGui::SliderInt("Integration steps", &bh.quality_steps, 2000, 15000); ImGui::TextDisabled("Deeper lensing at higher cost. Settled frame is 4x supersampled."); } if (ImGui::CollapsingHeader("Environment", ImGuiTreeNodeFlags_DefaultOpen)) { auto& hdri = HDRIManager::get(); std::string current = r.current_hdri(); std::string preview = hdri.get_hdri_name(current); if (ImGui::BeginCombo("Starfield", preview.c_str())) { for (const auto& path : hdri.get_available_hdri()) { bool selected = (path == current); if (ImGui::Selectable(hdri.get_hdri_name(path).c_str(), selected)) r.set_hdri(path); if (selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } ImGui::TextDisabled("Switching rebuilds the cubemap (brief pause)."); } } ImGui::End(); } auto Application::build_scene_ui() -> void { RendererBackend& r = *m_renderer; if (!ImGui::CollapsingHeader("Objects", ImGuiTreeNodeFlags_DefaultOpen)) return; ImGui::TextDisabled("Drag to orbit, scroll to zoom. HDRI skybox behind."); auto& objs = r.scene_objects(); static int sel = 0; if (sel >= (int)objs.size()) sel = (int)objs.size() - 1; ImGui::Text("Spheres (%d)", (int)objs.size()); if (ImGui::Button("Add") && objs.size() < 64) { SceneObject o; o.position = glm::vec3(((int)objs.size() % 5) * 5.0f - 10.0f, 2.0f, ((int)objs.size() / 5) * 5.0f); o.radius = 1.5f; o.color = glm::vec3(0.35f + 0.12f * (objs.size() % 5), 0.55f, 0.9f - 0.12f * (objs.size() % 4)); objs.push_back(o); sel = (int)objs.size() - 1; } ImGui::SameLine(); if (ImGui::Button("Delete") && !objs.empty()) { objs.erase(objs.begin() + sel); if (sel >= (int)objs.size()) sel = (int)objs.size() - 1; } ImGui::BeginChild("obj_list", ImVec2(0, 90), true); for (int i = 0; i < (int)objs.size(); ++i) { std::string label = "Sphere " + std::to_string(i); if (ImGui::Selectable(label.c_str(), sel == i)) sel = i; } ImGui::EndChild(); r.set_selected_object(sel); if (sel >= 0 && sel < (int)objs.size()) { SceneObject& o = objs[sel]; ImGui::DragFloat3("Position", &o.position.x, 0.1f); ImGui::DragFloat("Radius", &o.radius, 0.05f, 0.1f, 20.0f); ImGui::ColorEdit3("Color", &o.color.x); } } auto Application::close() -> void { m_running = false; } auto Application::on_event(Event& event) -> void { EventDispatcher dispatcher(event); dispatcher.dispatch([this, &event](WindowCloseEvent& e) { m_running = false; event.handled = true; return true; }); dispatcher.dispatch([this, &event](WindowResizeEvent& e) { if (e.get_width() == 0 || e.get_height() == 0) m_minimized = true; else m_minimized = false; if (m_renderer) { m_renderer->resize(e.get_width(), e.get_height()); } else { Renderer::on_window_resize(e.get_width(), e.get_height()); if (m_engine) m_engine->set_window_dimensions(e.get_width(), e.get_height()); } event.handled = true; return true; }); dispatcher.dispatch([this, &event](KeyPressedEvent& e) { if (!m_state_manager) return true; if (e.get_key_code() == GLFW_KEY_1) { m_state_manager->switch_to_state("Config"); event.handled = true; return true; } else if (e.get_key_code() == GLFW_KEY_2) { m_state_manager->switch_to_state("Simulation"); event.handled = true; return true; } else if (e.get_key_code() == GLFW_KEY_3) { m_state_manager->switch_to_state("WorldBuilder"); event.handled = true; return true; } return true; }); if (m_state_manager) m_state_manager->on_event(event); } auto Application::on_init() -> void { // (Logger, settings and API selection happen in the constructor.) Both // backends run the SAME application through the RendererBackend interface, // so the app behaves identically regardless of the active graphics API. int w = 0, h = 0; glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h); if (RendererAPI::get_api() == RendererAPI::API::Vulkan) m_renderer = create_scope(); else m_renderer = create_scope(); if (!m_renderer->init(m_window->get_native_window(), w, h)) DONUT_ERROR("Renderer initialization failed"); else m_renderer->init_ui(); } auto Application::on_shutdown() -> void { if (m_renderer) { m_renderer->shutdown(); m_renderer.reset(); } else { if (m_state_manager) m_state_manager->shutdown(); Renderer::shutdown(); } SettingsManager::shutdown(); Logger::shutdown(); } auto Application::on_update() -> void { float current_frame = (float)glfwGetTime(); m_delta_time = current_frame - m_last_frame; m_last_frame = current_frame; m_state_manager->update(m_delta_time); } auto Application::on_render() -> void { m_state_manager->render(); m_window->begin_im_gui_frame(); ImGuizmo::BeginFrame(); ImGuizmo::SetOrthographic(false); setup_docking_layout(); m_state_manager->on_im_ui_render(); m_window->end_im_gui_frame(); } auto Application::setup_docking_layout() -> void { ImGuiViewport* viewport = ImGui::GetMainViewport(); ImGui::DockSpaceOverViewport(0, viewport, ImGuiDockNodeFlags_PassthruCentralNode); } };