1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#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 <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
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<Window>(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<WindowCloseEvent>([this, &event](WindowCloseEvent& e)
{
m_running = false;
event.handled = true;
return true;
});
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());
}
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)
{
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);
}
};
|