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
|
#include "SimulationState.h"
#include "Rendering/Renderer.h"
#include "Core/Application.h"
#include "Core/HDRIManager.h"
#include "Core/Window.h"
#include "Core/Event.h"
#include "Core/SettingsManager.h"
#include <imgui.h>
#include <GLFW/glfw3.h>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <vector>
namespace Donut
{
void SimulationState::OnEnter()
{
DONUT_INFO("Entering Simulation State");
const auto& settings = SettingsManager::GetSettingsConst();
auto& engine = Application::Get().GetEngine();
engine.SetTargetFPS(settings.simulation.targetFPS);
engine.SetComputeHeight(settings.simulation.computeHeight);
engine.SetMaxStepsMoving(settings.simulation.maxStepsMoving);
engine.SetMaxStepsStatic(settings.simulation.maxStepsStatic);
engine.SetEarlyExitDistance(settings.simulation.earlyExitDistance);
engine.GetGravity() = settings.simulation.gravityEnabled;
engine.SetDiskThickness(settings.simulation.diskThickness);
engine.SetDiskDensity(settings.simulation.diskDensity);
engine.SetRotationSpeed(settings.simulation.rotationSpeed);
engine.SetBlurStrength(settings.simulation.blurStrength);
engine.SetGlowIntensity(settings.simulation.glowIntensity);
engine.UpdateComputeDimensions();
m_Initialized = true;
}
void SimulationState::OnExit()
{
DONUT_INFO("Exiting Simulation State");
}
void SimulationState::OnUpdate(float deltaTime)
{
auto& engine = Application::Get().GetEngine();
engine.UpdatePerformance(deltaTime);
engine.UpdateWindowDimensions();
engine.UpdatePhysics(deltaTime);
if (engine.GetCamera().IsDragging())
{
GLFWwindow* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
engine.GetCamera().ProcessOrbitalMouseMove(xpos, ypos);
}
}
void SimulationState::OnRender()
{
auto& engine = Application::Get().GetEngine();
RenderCommand::SetClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
RenderCommand::Clear();
RenderCommand::SetViewport(0, 0, static_cast<uint32_t>(engine.GetWidth()), static_cast<uint32_t>(engine.GetHeight()));
engine.DispatchCompute(engine.GetCamera());
engine.DrawBlurPass();
}
void SimulationState::OnEvent(Event& event)
{
auto& engine = Application::Get().GetEngine();
if (event.GetEventType() == EventType::MouseButtonPressed)
{
MouseButtonPressedEvent& e = (MouseButtonPressedEvent&)event;
int button = e.GetMouseButton();
GLFWwindow* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow());
Camera& camera = engine.GetCamera();
double lastX = camera.GetLastX();
double lastY = camera.GetLastY();
glfwGetCursorPos(window, &lastX, &lastY);
camera.ProcessOrbitalMouseButton(button, GLFW_PRESS, 0);
}
else if (event.GetEventType() == EventType::MouseButtonReleased)
{
MouseButtonReleasedEvent& e = (MouseButtonReleasedEvent&)event;
int button = e.GetMouseButton();
engine.GetCamera().ProcessOrbitalMouseButton(button, GLFW_RELEASE, 0);
}
if (event.GetEventType() == EventType::MouseScrolled)
{
MouseScrolledEvent& e = (MouseScrolledEvent&)event;
engine.GetCamera().ProcessOrbitalScroll(e.GetXOffset(), e.GetYOffset());
}
if (event.GetEventType() == EventType::KeyPressed)
{
KeyPressedEvent& e = (KeyPressedEvent&)event;
if (e.GetKeyCode() == GLFW_KEY_G)
{
engine.GetGravity() = !engine.GetGravity();
DONUT_INFO("Gravity turned {}", engine.GetGravity() ? "ON" : "OFF");
}
}
}
void SimulationState::OnImUIRender()
{
auto& engine = Application::Get().GetEngine();
ImGui::SetNextWindowSize(ImVec2(400, 600), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x - 420, 20), ImGuiCond_FirstUseEver);
ImGui::Begin("Simulation Controls", nullptr, ImGuiWindowFlags_NoCollapse);
ImGui::TextColored(ImVec4(0.8f, 0.8f, 1.0f, 1.0f), "Black Hole Simulation");
ImGui::SameLine();
if (ImGui::Button("Back to Config"))
Application::Get().GetStateManager().SwitchToState("Config");
ImGui::SameLine();
if (ImGui::Button("Save Settings"))
{
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.targetFPS = engine.GetTargetFPS();
settings.computeHeight = engine.GetComputeHeight();
settings.maxStepsMoving = engine.GetMaxStepsMoving();
settings.maxStepsStatic = engine.GetMaxStepsStatic();
settings.earlyExitDistance = engine.GetEarlyExitDistance();
settings.gravityEnabled = engine.GetGravity();
SettingsManager::SetSimulationSettings(settings);
DONUT_INFO("Simulation settings saved manually");
}
ImGui::Separator();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Performance");
ImGui::Separator();
ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
ImGui::Text("Frame Time: %.3f ms", 1000.0f / ImGui::GetIO().Framerate);
ImGui::Text("Engine FPS: %.1f", engine.GetCurrentFPS());
int targetFPS = engine.GetTargetFPS();
if (ImGui::SliderInt("Target FPS", &targetFPS, 30, 120))
{
engine.SetTargetFPS(targetFPS);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.targetFPS = targetFPS;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Simulation Info");
ImGui::Separator();
ImGui::Text("Resolution: %dx%d", engine.GetWidth(), engine.GetHeight());
ImGui::Text("Compute Resolution: %dx%d", engine.GetComputeWidth(), engine.GetComputeHeight());
ImGui::Text("Objects: %zu", engine.GetObjects().size());
if (ImGui::Button("Print Object Info"))
engine.PrintObjectInfo();
int computeHeight = engine.GetComputeHeight();
if (ImGui::SliderInt("Compute Height", &computeHeight, 64, 2048))
{
engine.SetComputeHeight(computeHeight);
engine.UpdateComputeDimensions();
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.computeHeight = computeHeight;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "Compute Width: %d (auto-calculated)", engine.GetComputeWidth());
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Quality Settings");
ImGui::Separator();
int maxStepsMoving = engine.GetMaxStepsMoving();
if (ImGui::SliderInt("Max Steps (Moving)", &maxStepsMoving, 1000, 60000))
{
engine.SetMaxStepsMoving(maxStepsMoving);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.maxStepsMoving = maxStepsMoving;
SettingsManager::SetSimulationSettings(settings);
}
int maxStepsStatic = engine.GetMaxStepsStatic();
if (ImGui::SliderInt("Max Steps (Static)", &maxStepsStatic, 1000, 30000))
{
engine.SetMaxStepsStatic(maxStepsStatic);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.maxStepsStatic = maxStepsStatic;
SettingsManager::SetSimulationSettings(settings);
}
float earlyExitDistance = engine.GetEarlyExitDistance();
if (ImGui::SliderFloat("Early Exit Distance", &earlyExitDistance, 1e11f, 1e13f, "%.2e"))
{
engine.SetEarlyExitDistance(earlyExitDistance);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.earlyExitDistance = earlyExitDistance;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Physics");
ImGui::Separator();
bool& gravity = engine.GetGravity();
if (ImGui::Checkbox("Gravity Enabled", &gravity))
{
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.gravityEnabled = gravity;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Accretion Disk");
ImGui::Separator();
float diskThickness = engine.GetDiskThickness();
if (ImGui::SliderFloat("Cloud Thickness", &diskThickness, 0.1f, 2.0f, "%.2f"))
{
engine.SetDiskThickness(diskThickness);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.diskThickness = diskThickness;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::TextDisabled("Thickness relative to Schwarzschild radius");
float diskDensity = engine.GetDiskDensity();
if (ImGui::SliderFloat("Cloud Density", &diskDensity, 0.1f, 3.0f, "%.2f"))
{
engine.SetDiskDensity(diskDensity);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.diskDensity = diskDensity;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::TextDisabled("Overall density multiplier");
float rotationSpeed = engine.GetRotationSpeed();
if (ImGui::SliderFloat("Rotation Speed", &rotationSpeed, 0.0f, 3.0f, "%.2f"))
{
engine.SetRotationSpeed(rotationSpeed);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.rotationSpeed = rotationSpeed;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::TextDisabled("Rotation speed multiplier (0 = no rotation)");
float blurStrength = engine.GetBlurStrength();
if (ImGui::SliderFloat("Blur Strength", &blurStrength, 0.5f, 5.0f, "%.2f"))
{
engine.SetBlurStrength(blurStrength);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.blurStrength = blurStrength;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::TextDisabled("Blur radius for glow effect");
float glowIntensity = engine.GetGlowIntensity();
if (ImGui::SliderFloat("Glow Intensity", &glowIntensity, 0.1f, 3.0f, "%.2f"))
{
engine.SetGlowIntensity(glowIntensity);
SimulationSettings settings = SettingsManager::GetSettingsConst().simulation;
settings.glowIntensity = glowIntensity;
SettingsManager::SetSimulationSettings(settings);
}
ImGui::TextDisabled("Intensity of the glow effect");
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "HDRI Environment");
ImGui::Separator();
static int selectedHDRI = 0;
ImGui::PushID("SimulationHDRI");
auto& hdriManager = HDRIManager::Get();
const auto& availableHDRI = hdriManager.GetAvailableHDRI();
static std::vector<std::string> hdriOptionNames;
static std::vector<const char*> hdriOptions;
if (hdriOptionNames.size() != availableHDRI.size())
{
hdriOptionNames.clear();
hdriOptions.clear();
for (const auto& path : availableHDRI)
{
hdriOptionNames.push_back(hdriManager.GetHDRIName(path));
hdriOptions.push_back(hdriOptionNames.back().c_str());
}
}
if (ImGui::Combo("HDRI Environment", &selectedHDRI, hdriOptions.data(), static_cast<int>(hdriOptions.size())))
{
auto& hdriManager = HDRIManager::Get();
hdriManager.SetCurrentHDRI(availableHDRI[selectedHDRI]);
engine.SetHDRIEnvironment(hdriManager.GetCurrentHDRI());
}
ImGui::TextDisabled("HDRI provides background and lighting for the simulation");
ImGui::PopID();
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Camera");
ImGui::Separator();
ImGui::Text("Position: (%.2e, %.2e, %.2e)",
engine.GetCamera().GetOrbitalPosition().x,
engine.GetCamera().GetOrbitalPosition().y,
engine.GetCamera().GetOrbitalPosition().z);
ImGui::Text("Radius: %.2e", engine.GetCamera().GetOrbitalRadius());
ImGui::Text("Azimuth: %.2f", engine.GetCamera().GetAzimuth());
ImGui::Text("Elevation: %.2f", engine.GetCamera().GetElevation());
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Export");
ImGui::Separator();
if (ImGui::Button("Export Frame (1080p)", ImVec2(-1, 30)))
{
auto& engine = Application::Get().GetEngine();
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << "frame_1080p_" << std::put_time(std::localtime(&time_t), "%Y-%m-%d_%H-%M-%S") << ".png";
engine.ExportHighResFrame(ss.str(), 1920, 1080);
}
if (ImGui::Button("Export High-Res Frame (4K)", ImVec2(-1, 30)))
{
auto& engine = Application::Get().GetEngine();
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << "high_res_frame_4k_" << std::put_time(std::localtime(&time_t), "%Y-%m-%d_%H-%M-%S") << ".png";
engine.ExportHighResFrame(ss.str(), 4096, 3072);
}
if (ImGui::Button("Export High-Res Frame (8K)", ImVec2(-1, 30)))
{
auto& engine = Application::Get().GetEngine();
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << "high_res_frame_8k_" << std::put_time(std::localtime(&time_t), "%Y-%m-%d_%H-%M-%S") << ".png";
engine.ExportHighResFrame(ss.str(), 8192, 6144);
}
if (ImGui::Button("Export Ultra High-Res Frame (16K)", ImVec2(-1, 30)))
{
auto& engine = Application::Get().GetEngine();
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << "high_res_frame_16k_" << std::put_time(std::localtime(&time_t), "%Y%m%d_%H%M%S") << ".png";
engine.ExportHighResFrame(ss.str(), 16384, 12288);
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Controls");
ImGui::Separator();
ImGui::Text("Left Mouse: Orbit camera");
ImGui::Text("Scroll: Zoom in/out");
ImGui::Text("G: Toggle gravity");
ImGui::Text("Right Mouse: Enable gravity (hold)");
ImGui::End();
}
};
|