aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/application.cpp422
-rw-r--r--src/core/application.h64
-rw-r--r--src/core/hdri_manager.h6
-rw-r--r--src/core/settings_manager.cpp1
-rw-r--r--src/core/state.h19
-rw-r--r--src/core/state_manager.cpp88
-rw-r--r--src/core/state_manager.h41
-rw-r--r--src/core/window.cpp105
-rw-r--r--src/core/window.h12
9 files changed, 315 insertions, 443 deletions
diff --git a/src/core/application.cpp b/src/core/application.cpp
index ae8a5d9..6239c36 100644
--- a/src/core/application.cpp
+++ b/src/core/application.cpp
@@ -1,36 +1,41 @@
#include "application.h"
-#include "rendering/renderer.h"
+#include "rendering/render_api.h" // RendererAPI
#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 "platform/opengl/opengl_device.h"
+#include "platform/vulkan/vulkan_device.h"
#include <GLFW/glfw3.h>
#include <imgui.h>
-#include <ImGuizmo.h>
-#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
+#include <glm/gtc/matrix_transform.hpp>
+#include <algorithm>
+#include <cmath>
+#include <numbers>
namespace Donut
{
Application* Application::s_instance = nullptr;
+ // Scroll is delivered through a GLFW callback; accumulate it here and drain it
+ // once per frame in process_input (chaining ImGui's own scroll handler).
+ static double g_scroll_accum = 0.0;
+ static GLFWscrollfun g_prev_scroll = nullptr;
+ static void donut_scroll_callback(GLFWwindow* w, double x, double y)
+ {
+ if (g_prev_scroll) g_prev_scroll(w, x, y);
+ g_scroll_accum += y;
+ }
+
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.
+ // 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();
{
@@ -40,13 +45,10 @@ namespace Donut
}
if (RendererAPI::get_api() == RendererAPI::API::Vulkan)
- vulkan_prepare_glfw(); // must precede glfwInit() inside the Window ctor
+ RHI::vulkan_prepare_glfw(); // must precede glfwInit() inside the Window ctor
m_window = create_scope<Window>(name, width, height);
- m_window->set_event_callback([this](Event& event)
- {
- on_event(event);
- });
+ m_window->set_event_callback([this](Event& event) { on_event(event); });
on_init();
}
@@ -57,27 +59,225 @@ namespace Donut
s_instance = nullptr;
}
+ auto Application::on_init() -> void
+ {
+ int w = 0, h = 0;
+ glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h);
+
+ if (RendererAPI::get_api() == RendererAPI::API::Vulkan)
+ m_device = RHI::create_vulkan_device();
+ else
+ m_device = RHI::create_opengl_device();
+
+ if (!m_device->init(m_window->get_native_window(), w, h))
+ {
+ DONUT_ERROR("RHI device initialization failed");
+ return;
+ }
+
+ m_hdri_path = "assets/hdri/hdr_blue_nebulae_1.hdr";
+ m_cubemap = m_device->create_cubemap_from_hdri(m_hdri_path);
+
+ m_scene_renderer = create_scope<SceneRenderer>();
+ m_scene_renderer->init(*m_device);
+ m_black_hole_renderer = create_scope<BlackHoleRenderer>();
+ m_black_hole_renderer->init(*m_device);
+
+ // Black-hole camera: large-scale orbital viewer outside the disk.
+ m_camera.set_camera_mode(CameraMode::Orbital);
+ m_camera.set_orbital_target(glm::vec3(0.0f));
+ m_camera.set_orbital_radius(4e11); // ~31 r_s: outside the 12 r_s disk
+ m_camera.set_orbital_limits(2.2e11, 1.5e12);
+ m_camera.set_orbital_speed(0.01f);
+ m_camera.set_zoom_speed(3e10);
+ m_camera.set_azimuth(0.0f);
+ m_camera.set_elevation(1.25f);
+
+ // Scene camera: normal-scale orbital world-builder viewer.
+ m_scene_camera.set_camera_mode(CameraMode::Orbital);
+ m_scene_camera.set_orbital_target(glm::vec3(0.0f));
+ m_scene_camera.set_orbital_radius(15.0);
+ m_scene_camera.set_orbital_limits(2.0, 200.0);
+ m_scene_camera.set_orbital_speed(0.01f);
+ m_scene_camera.set_zoom_speed(2.0);
+ m_scene_camera.set_azimuth(0.0f);
+ m_scene_camera.set_elevation((float)std::numbers::pi / 3.0f);
+ m_scene_camera.update_orbital();
+
+ m_device->init_imgui();
+ // Load the UI font into the device-created ImGui context (backend-agnostic;
+ // the backend rebuilds the atlas on the next frame).
+ ImGuiIO& io = ImGui::GetIO();
+ if (!io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Regular.ttf", 16.0f))
+ io.Fonts->AddFontDefault();
+
+ g_prev_scroll = glfwSetScrollCallback((GLFWwindow*)m_window->get_native_window(), donut_scroll_callback);
+
+ m_start_time = glfwGetTime();
+ DONUT_INFO("Application ready on {} backend", m_device->device_name());
+ }
+
auto Application::run() -> void
{
while (m_running)
{
- if (m_renderer)
+ glfwPollEvents();
+ if (!m_minimized && m_device)
+ render_frame();
+ }
+ }
+
+ auto Application::render_frame() -> void
+ {
+ RHI::CommandList* cmd = m_device->begin_frame(glm::vec4(0.05f, 0.06f, 0.10f, 1.0f));
+ if (!cmd) return; // frame skipped (e.g. swapchain recreation)
+
+ m_device->imgui_new_frame();
+ build_ui();
+ process_input();
+
+ const glm::vec4 clear(0.05f, 0.06f, 0.10f, 1.0f);
+ int w = 0, h = 0;
+ glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h);
+
+ if (m_scene_mode)
+ {
+ float aspect = (float)w / (float)std::max(h, 1);
+ m_scene_camera.set_projection(45.0f, aspect, 0.1f, 1000.0f);
+ CameraView cv;
+ cv.view = m_scene_camera.get_view_matrix();
+ cv.projection = m_scene_camera.get_projection_matrix();
+ cv.position = m_scene_camera.get_orbital_position();
+ cv.fb_width = w; cv.fb_height = h;
+
+ cmd->begin_render_pass(nullptr, clear);
+ m_scene_renderer->render(*cmd, cv, m_scene_objects, m_selected_object, m_cubemap.get());
+ m_device->imgui_render(*cmd);
+ cmd->end_render_pass();
+ }
+ else
+ {
+ GeodesicView gv;
+ glm::vec3 pos, fwd;
+ if (m_camera.get_camera_mode() == CameraMode::FPS)
{
- 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(); });
+ pos = m_camera.get_position();
+ fwd = m_camera.get_forward_direction();
}
else
{
- if (!m_minimized)
- {
- on_update();
- on_render();
- }
- m_window->on_update(); // polls events + swaps buffers (OpenGL)
+ pos = m_camera.get_orbital_position();
+ fwd = glm::normalize(m_camera.get_orbital_target() - pos);
}
+ glm::vec3 right = glm::normalize(glm::cross(fwd, glm::vec3(0, 1, 0)));
+ gv.position = pos; gv.right = right; gv.up = glm::cross(right, fwd); gv.forward = fwd;
+ gv.tan_half_fov = (float)tan(glm::radians(m_bh_params.fov_degrees * 0.5f));
+ gv.aspect = (float)BlackHoleRenderer::GEO_HI_W / (float)BlackHoleRenderer::GEO_HI_H;
+ gv.moving = m_user_moving;
+ gv.time = (float)(glfwGetTime() - m_start_time);
+
+ m_black_hole_renderer->render_geodesic(*cmd, gv, m_bh_params, m_cubemap.get());
+ cmd->begin_render_pass(nullptr, clear);
+ m_black_hole_renderer->blit(*cmd, w, h);
+ m_device->imgui_render(*cmd);
+ cmd->end_render_pass();
}
+
+ m_device->end_frame();
+ }
+
+ auto Application::process_input() -> void
+ {
+ GLFWwindow* window = (GLFWwindow*)m_window->get_native_window();
+ bool over_ui = ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureMouse;
+
+ double now = glfwGetTime();
+ float dt = m_last_frame_time > 0.0 ? (float)(now - m_last_frame_time) : 0.0f;
+ m_last_frame_time = now;
+
+ double mx = 0, my = 0;
+ glfwGetCursorPos(window, &mx, &my);
+ bool left_down = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
+
+ if (!m_scene_mode && m_camera.get_camera_mode() == CameraMode::FPS)
+ {
+ if (left_down && !m_left_was_down) { m_fps_last_x = mx; m_fps_last_y = my; }
+ if (left_down && !over_ui)
+ m_camera.on_mouse_move(float(mx - m_fps_last_x), float(m_fps_last_y - my));
+ m_fps_last_x = mx; m_fps_last_y = my;
+ m_left_was_down = left_down;
+
+ bool over_kb = ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard;
+ bool moved = false;
+ if (!over_kb && dt > 0.0f)
+ {
+ float mdt = dt * (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS ? 4.0f : 1.0f);
+ if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { m_camera.move_forward(mdt); moved = true; }
+ if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { m_camera.move_backward(mdt); moved = true; }
+ if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { m_camera.move_left(mdt); moved = true; }
+ if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { m_camera.move_right(mdt); moved = true; }
+ if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) { m_camera.move_up(mdt); moved = true; }
+ if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) { m_camera.move_down(mdt); moved = true; }
+ }
+ g_scroll_accum = 0.0;
+ m_user_moving = moved || (left_down && !over_ui);
+ }
+ else
+ {
+ Camera& cam = m_scene_mode ? m_scene_camera : m_camera;
+ if (left_down && !m_left_was_down && !over_ui)
+ cam.process_orbital_mouse_button(GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, 0);
+ else if (!left_down && m_left_was_down)
+ cam.process_orbital_mouse_button(GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, 0);
+ m_left_was_down = left_down;
+
+ cam.process_orbital_mouse_move(mx, my);
+
+ double scroll = g_scroll_accum; g_scroll_accum = 0.0;
+ if (scroll != 0.0 && !over_ui)
+ cam.process_orbital_scroll(0.0, scroll);
+
+ m_user_moving = cam.is_dragging() || cam.is_panning();
+ }
+ }
+
+ auto Application::set_free_fly(bool enabled) -> void
+ {
+ const bool is_fps = m_camera.get_camera_mode() == CameraMode::FPS;
+ if (enabled == is_fps) return;
+ if (enabled)
+ {
+ // Seed the fly pose from the current orbital framing so the view is continuous.
+ glm::vec3 pos = m_camera.get_orbital_position();
+ glm::vec3 fwd = glm::normalize(m_camera.get_orbital_target() - pos);
+ float pitch = glm::degrees(asin(glm::clamp(fwd.y, -1.0f, 1.0f)));
+ float yaw = glm::degrees(atan2(fwd.z, fwd.x));
+ m_camera.set_camera_mode(CameraMode::FPS);
+ m_camera.set_movement_speed(2.0e10f);
+ m_camera.set_mouse_sensitivity(0.15f);
+ m_camera.set_position(pos);
+ m_camera.set_rotation(glm::vec3(pitch, yaw, 0.0f));
+ }
+ else
+ {
+ m_camera.set_camera_mode(CameraMode::Orbital); // orbital state was left intact
+ }
+ }
+
+ auto Application::reset_camera() -> void
+ {
+ m_camera.set_camera_mode(CameraMode::Orbital);
+ m_camera.set_orbital_radius(4e11);
+ m_camera.set_azimuth(0.0f);
+ m_camera.set_elevation(1.25f);
+ }
+
+ auto Application::set_hdri(const std::string& path) -> void
+ {
+ if (m_hdri_path == path || !m_device) return;
+ m_device->wait_idle();
+ m_cubemap = m_device->create_cubemap_from_hdri(path); // old cube freed after idle
+ m_hdri_path = path;
}
// One-time ImGui theme: rounded, roomy, dark with a warm accretion-disk accent.
@@ -120,8 +320,7 @@ namespace Donut
{
static bool styled = false;
if (!styled) { apply_donut_style(); styled = true; }
- if (!m_renderer) return;
- RendererBackend& r = *m_renderer;
+ if (!m_device) return;
// 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.
@@ -130,9 +329,8 @@ namespace Donut
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::TextDisabled("%s", m_device->device_name().empty() ? "GPU" : m_device->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();
@@ -155,29 +353,28 @@ namespace Donut
}
ImGui::Spacing();
- bool scene = r.is_scene_mode();
ImGui::TextDisabled("VIEW");
- if (ImGui::RadioButton("Black hole", !scene)) r.set_scene_mode(false);
+ if (ImGui::RadioButton("Black hole", !m_scene_mode)) m_scene_mode = false;
ImGui::SameLine();
- if (ImGui::RadioButton("Scene", scene)) r.set_scene_mode(true);
+ if (ImGui::RadioButton("Scene", m_scene_mode)) m_scene_mode = true;
ImGui::Separator();
- if (scene)
+ if (m_scene_mode)
{
build_scene_ui();
}
else
{
- BlackHoleParams& bh = r.black_hole_params();
+ BlackHoleParams& bh = m_bh_params;
if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen))
{
- bool ff = r.is_free_fly();
- if (ImGui::RadioButton("Orbital", !ff)) r.set_free_fly(false);
+ bool ff = m_camera.get_camera_mode() == CameraMode::FPS;
+ if (ImGui::RadioButton("Orbital", !ff)) set_free_fly(false);
ImGui::SameLine();
- if (ImGui::RadioButton("Free-fly", ff)) r.set_free_fly(true);
+ if (ImGui::RadioButton("Free-fly", ff)) set_free_fly(true);
ImGui::SameLine();
- if (ImGui::Button("Reset view")) r.reset_camera();
+ if (ImGui::Button("Reset view")) 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");
@@ -203,15 +400,14 @@ namespace Donut
if (ImGui::CollapsingHeader("Environment", ImGuiTreeNodeFlags_DefaultOpen))
{
auto& hdri = HDRIManager::get();
- std::string current = r.current_hdri();
- std::string preview = hdri.get_hdri_name(current);
+ std::string preview = hdri.get_hdri_name(m_hdri_path);
if (ImGui::BeginCombo("Starfield", preview.c_str()))
{
for (const auto& path : hdri.get_available_hdri())
{
- bool selected = (path == current);
+ bool selected = (path == m_hdri_path);
if (ImGui::Selectable(hdri.get_hdri_name(path).c_str(), selected))
- r.set_hdri(path);
+ set_hdri(path);
if (selected) ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
@@ -225,14 +421,12 @@ namespace Donut
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;
+ auto& objs = m_scene_objects;
+ if (m_selected_object >= (int)objs.size()) m_selected_object = (int)objs.size() - 1;
ImGui::Text("Spheres (%d)", (int)objs.size());
if (ImGui::Button("Add") && objs.size() < 64)
@@ -242,27 +436,26 @@ namespace Donut
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;
+ m_selected_object = (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;
+ objs.erase(objs.begin() + m_selected_object);
+ if (m_selected_object >= (int)objs.size()) m_selected_object = (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;
+ if (ImGui::Selectable(label.c_str(), m_selected_object == i)) m_selected_object = i;
}
ImGui::EndChild();
- r.set_selected_object(sel);
- if (sel >= 0 && sel < (int)objs.size())
+ if (m_selected_object >= 0 && m_selected_object < (int)objs.size())
{
- SceneObject& o = objs[sel];
+ SceneObject& o = objs[m_selected_object];
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);
@@ -286,115 +479,30 @@ namespace Donut
dispatcher.dispatch<WindowResizeEvent>([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());
- }
+ m_minimized = (e.get_width() == 0 || e.get_height() == 0);
+ if (m_device) m_device->resize(e.get_width(), e.get_height());
event.handled = true;
return true;
});
-
- dispatcher.dispatch<KeyPressedEvent>([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<VulkanRenderer>();
- else
- m_renderer = create_scope<OpenGLRenderer>();
-
- 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)
+ if (m_device)
{
- m_renderer->shutdown();
- m_renderer.reset();
- }
- else
- {
- if (m_state_manager)
- m_state_manager->shutdown();
- Renderer::shutdown();
+ m_device->wait_idle();
+ m_scene_renderer.reset();
+ m_black_hole_renderer.reset();
+ m_cubemap.reset();
+ // Free HDRIManager's cached GPU textures now, while the render context
+ // is still alive. On OpenGL these are GL textures owned by a static
+ // singleton; letting them destruct at program exit would call
+ // glDeleteTextures after the GL context is gone and crash on close.
+ HDRIManager::get().clear_cache();
+ m_device->shutdown();
+ m_device.reset();
}
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);
- }
};
diff --git a/src/core/application.h b/src/core/application.h
index 4c377f0..4b67c84 100644
--- a/src/core/application.h
+++ b/src/core/application.h
@@ -1,16 +1,25 @@
#pragma once
-#include "state_manager.h"
#include "memory.h"
#include "window.h"
#include "event.h"
#include "log.h"
+#include "camera.h"
-#include "engine/engine.h"
-#include "rendering/renderer_backend.h"
+#include "rendering/rhi.h"
+#include "rendering/scene_renderer.h"
+#include "rendering/black_hole_renderer.h"
+
+#include <string>
+#include <vector>
namespace Donut
{
+ // The application owns ONE RHI::Device (OpenGL or Vulkan, chosen at startup)
+ // and the two portable renderers written on top of it, plus all the shared
+ // state (cameras, scene objects, disk parameters, HDRI). Because the rendering
+ // lives in the RHI and the shared renderers, the app behaves identically no
+ // matter which backend is active.
class Application
{
public:
@@ -21,33 +30,52 @@ namespace Donut
auto run() -> void;
auto close() -> void;
- auto get_window() -> Window& { return *m_window; }
- auto get_state_manager() -> StateManager& { return *m_state_manager; }
- auto get_engine() -> Engine& { return *m_engine; }
- static auto get() -> Application& { return *s_instance; }
+ auto get_window() -> Window& { return *m_window; }
+ static auto get() -> Application& { return *s_instance; }
private:
auto on_init() -> void;
auto on_shutdown() -> void;
- auto on_update() -> void;
- auto on_render() -> void;
auto on_event(Event& event) -> void;
- auto setup_docking_layout() -> void;
- auto build_ui() -> void; // shared ImGui panel, drives the active RendererBackend
- auto build_scene_ui() -> void; // scene-view (object list) section of the UI
+
+ auto build_ui() -> void; // shared ImGui panel
+ auto build_scene_ui() -> void; // scene-view (object list) section
+
+ auto process_input() -> void; // orbit / free-fly / zoom, was per-backend
+ auto set_free_fly(bool enabled) -> void;
+ auto reset_camera() -> void;
+ auto set_hdri(const std::string& path) -> void;
+
+ auto render_frame() -> void;
private:
- Scope<StateManager> m_state_manager;
Scope<Window> m_window;
- Scope<Engine> m_engine;
- Scope<RendererBackend> m_renderer; // active backend (non-null once a backend runs the app)
+
+ // The unified renderer: one device + the shared renderers on top of it.
+ Scope<RHI::Device> m_device;
+ Scope<SceneRenderer> m_scene_renderer;
+ Scope<BlackHoleRenderer> m_black_hole_renderer;
+ Ref<RHI::Texture> m_cubemap; // shared HDRI environment
+
+ Camera m_camera{ 60.0f, 16.0f / 9.0f, 0.1f, 100.0f }; // black-hole view
+ Camera m_scene_camera{ 45.0f, 16.0f / 9.0f, 0.1f, 1000.0f }; // world-builder view
+
+ BlackHoleParams m_bh_params;
+ std::vector<SceneObject> m_scene_objects{ SceneObject{} };
+ int m_selected_object = 0;
+ bool m_scene_mode = false;
+ std::string m_hdri_path;
+
+ // input tracking (mirrors the old per-backend process_input)
+ bool m_left_was_down = false;
+ double m_fps_last_x = 0.0, m_fps_last_y = 0.0;
+ double m_last_frame_time = 0.0;
+ double m_start_time = 0.0;
+ bool m_user_moving = false;
bool m_running;
bool m_minimized;
- float m_delta_time = 0.0f;
- float m_last_frame = 0.0f;
-
static Application* s_instance;
};
}
diff --git a/src/core/hdri_manager.h b/src/core/hdri_manager.h
index fa05330..d98830f 100644
--- a/src/core/hdri_manager.h
+++ b/src/core/hdri_manager.h
@@ -39,9 +39,9 @@ namespace Donut
std::vector<std::string> m_available_hdri =
{
- "assets/hdri/HDR_blue_nebulae-1.hdr",
- "assets/hdri/HDR_subdued_blue_nebulae.hdr",
- "assets/hdri/HDR_subdued_multi_nebulae.hdr",
+ "assets/hdri/hdr_blue_nebulae_1.hdr",
+ "assets/hdri/hdr_subdued_blue_nebulae.hdr",
+ "assets/hdri/hdr_subdued_multi_nebulae.hdr",
"assets/hdri/night_sky.hdr"
};
};
diff --git a/src/core/settings_manager.cpp b/src/core/settings_manager.cpp
index ca4cac5..6909c60 100644
--- a/src/core/settings_manager.cpp
+++ b/src/core/settings_manager.cpp
@@ -1,7 +1,6 @@
#include "settings_manager.h"
#include "log.h"
#include "theme_manager.h"
-#include "rendering/renderer.h"
#include <fstream>
#include <filesystem>
diff --git a/src/core/state.h b/src/core/state.h
deleted file mode 100644
index 623f1c2..0000000
--- a/src/core/state.h
+++ /dev/null
@@ -1,19 +0,0 @@
-#pragma once
-
-#include "event.h"
-
-namespace Donut
-{
- class State
- {
- public:
- virtual ~State() = default;
-
- virtual auto on_enter() -> void = 0;
- virtual auto on_exit() -> void = 0;
- virtual auto on_update(float delta_time) -> void = 0;
- virtual auto on_render() -> void = 0;
- virtual auto on_im_ui_render() -> void = 0;
- virtual auto on_event(Event& event) -> void = 0;
- };
-};
diff --git a/src/core/state_manager.cpp b/src/core/state_manager.cpp
deleted file mode 100644
index 8018401..0000000
--- a/src/core/state_manager.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-#include "state_manager.h"
-#include "log.h"
-
-namespace Donut
-{
- auto StateManager::shutdown() -> void
- {
- if (m_current_state)
- m_current_state->on_exit();
-
- destroy_states();
- DONUT_INFO("StateManager shutdown");
- }
-
- auto StateManager::destroy_states() -> void
- {
- m_states.clear();
- m_current_state = nullptr;
- m_current_state_name = "";
- }
-
- auto StateManager::register_state(const std::string& state_name, Scope<State> state) -> void
- {
- if (m_states.find(state_name) != m_states.end())
- DONUT_WARN("State '{}' already exists, overwriting", state_name);
- m_states[state_name] = std::move(state);
- DONUT_INFO("Registered state: {}", state_name);
- }
-
- auto StateManager::switch_to_state(const std::string& state_name) -> void
- {
- auto it = m_states.find(state_name);
- if (it == m_states.end())
- {
- DONUT_ERROR("Attempted to switch to unknown state: {}", state_name);
- return;
- }
-
- State* new_state = it->second.get();
- if (new_state == m_current_state)
- return;
-
- if (m_current_state)
- m_current_state->on_exit();
-
- m_current_state = new_state;
- m_current_state_name = state_name;
- m_current_state->on_enter();
- DONUT_INFO("Switched to state: {}", state_name);
- }
-
- auto StateManager::get_state(const std::string& state_name) -> State*
- {
- auto it = m_states.find(state_name);
- if (it != m_states.end())
- return it->second.get();
- return nullptr;
- }
-
- auto StateManager::has_state(const std::string& state_name) const -> bool
- {
- return m_states.find(state_name) != m_states.end();
- }
-
- auto StateManager::update(float delta_time) -> void
- {
- if (m_current_state)
- m_current_state->on_update(delta_time);
- }
-
- auto StateManager::render() -> void
- {
- if (m_current_state)
- m_current_state->on_render();
- }
-
- auto StateManager::on_im_ui_render() -> void
- {
- if (m_current_state)
- m_current_state->on_im_ui_render();
- }
-
- auto StateManager::on_event(Event& event) -> void
- {
- if (m_current_state)
- m_current_state->on_event(event);
- }
-}
diff --git a/src/core/state_manager.h b/src/core/state_manager.h
deleted file mode 100644
index 446841e..0000000
--- a/src/core/state_manager.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-
-#include "memory.h"
-#include "state.h"
-
-#include <string>
-#include <unordered_map>
-
-namespace Donut
-{
- class StateManager
- {
- public:
- ~StateManager() = default;
- auto shutdown() -> void;
-
- auto update(float delta_time) -> void;
- auto render() -> void;
- auto on_im_ui_render() -> void;
- auto on_event(Event& event) -> void;
-
- auto register_state(const std::string& state_name, Scope<State> state) -> void;
- auto switch_to_state(const std::string& state_name) -> void;
-
- auto get_current_state_name() const -> std::string { return m_current_state_name; }
- auto get_current_state() const -> State* { return m_current_state; }
-
- auto get_state(const std::string& state_name) -> State*;
- auto has_state(const std::string& state_name) const -> bool;
-
- private:
- auto create_states() -> void;
- auto destroy_states() -> void;
-
- private:
- State* m_current_state = nullptr;
- std::string m_current_state_name = "";
-
- std::unordered_map<std::string, Scope<State>> m_states;
- };
-}
diff --git a/src/core/window.cpp b/src/core/window.cpp
index 047ef0d..dd5882c 100644
--- a/src/core/window.cpp
+++ b/src/core/window.cpp
@@ -1,14 +1,8 @@
-#include <glad/glad.h>
#include "window.h"
-#include "theme_manager.h"
-#include "rendering/renderer.h" // RendererAPI::get_api()
+#include "rendering/render_api.h" // RendererAPI::get_api()
#include <cstdint>
-#include <imgui.h>
-#include <imgui_impl_glfw.h>
-#include <imgui_impl_opengl3.h>
-
namespace Donut
{
static bool s_glfw_initialized = false;
@@ -94,8 +88,6 @@ namespace Donut
{
DONUT_INFO("Shutting down window: ", m_title);
- shutdown_im_gui();
-
glfwDestroyWindow(m_window);
s_glfw_window_count--;
@@ -133,101 +125,6 @@ namespace Donut
GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN);
}
- auto Window::init_im_gui() -> void
- {
- IMGUI_CHECKVERSION();
- ImGui::CreateContext();
- ImGuiIO& io = ImGui::GetIO();
- io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
- io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
-#ifndef __APPLE__
- // Multi-viewport (dragging ImGui panels out as separate OS windows)
- // relies on a populated platform-monitor list and is unreliable on
- // macOS, where it intermittently asserts (Monitors.Size > 0) and
- // aborts. Docking stays enabled; panels just remain inside the window.
- io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
-#endif
-
- setup_im_gui_fonts();
- ThemeManager::set_theme(Theme::Dark);
-
- ImGuiStyle& style = ImGui::GetStyle();
- if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
- {
- style.WindowRounding = 0.0f;
- style.Colors[ImGuiCol_WindowBg].w = 1.0f;
- }
-
- ImGui_ImplGlfw_InitForOpenGL(m_window, true);
-#ifdef __APPLE__
- // macOS uses a core-profile context, which rejects the legacy
- // "#version 130" GLSL the ImGui backend defaults to. 150 is the
- // minimum core-profile GLSL that macOS's OpenGL 4.1 accepts.
- ImGui_ImplOpenGL3_Init("#version 150");
-#else
- ImGui_ImplOpenGL3_Init("#version 130");
-#endif
-
- DONUT_INFO("ImGUI initialized successfully");
- }
-
- auto Window::setup_im_gui_fonts() -> void
- {
- ImGuiIO& io = ImGui::GetIO();
- io.Fonts->Clear();
-
- m_main_font = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Regular.ttf", 16.0f);
- if (!m_main_font)
- {
- DONUT_WARN("Failed to load Inter font, falling back to default");
- m_main_font = io.Fonts->AddFontDefault();
- }
- else
- DONUT_INFO("Successfully loaded Inter font");
-
- m_large_font = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Bold.ttf", 20.0f);
- if (!m_large_font)
- m_large_font = m_main_font;
-
- m_small_font = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Light.ttf", 12.0f);
- if (!m_small_font)
- m_small_font = m_main_font;
-
- io.FontDefault = m_main_font;
- DONUT_INFO("ImGUI fonts loaded successfully");
- }
-
- auto Window::shutdown_im_gui() -> void
- {
- ImGui_ImplOpenGL3_Shutdown();
- ImGui_ImplGlfw_Shutdown();
- ImGui::DestroyContext();
-
- DONUT_INFO("ImGUI shutdown");
- }
-
- auto Window::begin_im_gui_frame() -> void
- {
- ImGui_ImplOpenGL3_NewFrame();
- ImGui_ImplGlfw_NewFrame();
- ImGui::NewFrame();
- }
-
- auto Window::end_im_gui_frame() -> void
- {
- ImGui::Render();
- ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
-
- ImGuiIO& io = ImGui::GetIO();
- if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
- {
- GLFWwindow* backup_current_context = glfwGetCurrentContext();
- ImGui::UpdatePlatformWindows();
- ImGui::RenderPlatformWindowsDefault();
- glfwMakeContextCurrent(backup_current_context);
- }
- }
-
auto Window::glfw_error_callback(int error, const char* description) -> void
{
DONUT_ERROR("GLFW Error ({}): {}", error, description ? description : "");
diff --git a/src/core/window.h b/src/core/window.h
index 9b7d7b9..b8b59c5 100644
--- a/src/core/window.h
+++ b/src/core/window.h
@@ -4,7 +4,6 @@
#include "log.h"
#include <GLFW/glfw3.h>
-#include <imgui.h>
#include <string>
@@ -38,17 +37,10 @@ namespace Donut
auto is_cursor_locked() const -> bool { return m_cursor_locked; }
auto is_cursor_visible() const -> bool { return m_cursor_visible; }
- auto init_im_gui() -> void;
- auto begin_im_gui_frame() -> void;
- auto end_im_gui_frame() -> void;
-
private:
auto init() -> void;
auto shutdown() -> void;
- auto setup_im_gui_fonts() -> void;
- auto shutdown_im_gui() -> void;
-
static auto glfw_error_callback(int error, const char* description) -> void;
static auto glfw_window_close_callback(GLFWwindow* window) -> void;
static auto glfw_window_size_callback(GLFWwindow* window, int width, int height) -> void;
@@ -70,9 +62,5 @@ namespace Donut
bool m_is_closed;
bool m_cursor_locked = false;
bool m_cursor_visible = true;
-
- ImFont* m_main_font = nullptr;
- ImFont* m_small_font = nullptr;
- ImFont* m_large_font = nullptr;
};
}