diff options
| author | hachem <im@hachem.wtf> | 2025-08-21 19:08:34 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-21 19:08:34 +0200 |
| commit | 6f72049359651155be62846e7d080d7a5e871096 (patch) | |
| tree | 2d07767c82264c0a126708176c9939806c9a7171 /src | |
| parent | 1bd39d11ff76e61bc3cc8b58286092a84997cc20 (diff) | |
[fix]: proper scene loading
Diffstat (limited to 'src')
| -rw-r--r-- | src/Core/Application.cpp | 4 | ||||
| -rw-r--r-- | src/Core/Application.h | 10 | ||||
| -rw-r--r-- | src/Engine/Engine.cpp | 82 | ||||
| -rw-r--r-- | src/Engine/Engine.h | 7 | ||||
| -rw-r--r-- | src/States/ConfigState.cpp | 9 | ||||
| -rw-r--r-- | src/States/SimulationState.cpp | 110 | ||||
| -rw-r--r-- | src/States/SimulationState.h | 1 | ||||
| -rw-r--r-- | src/States/WorldBuilderState.cpp | 13 |
8 files changed, 166 insertions, 70 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp index 62f1faa..eefa482 100644 --- a/src/Core/Application.cpp +++ b/src/Core/Application.cpp @@ -71,6 +71,7 @@ namespace Donut m_Minimized = false; Renderer::OnWindowResize(e.GetWidth(), e.GetHeight()); + m_Engine->SetWindowDimensions(e.GetWidth(), e.GetHeight()); event.Handled = true; return true; }); @@ -118,6 +119,9 @@ namespace Donut Renderer::OnWindowResize(1280, 720); RenderCommand::SetFaceCulling(false); + m_Engine = CreateScope<Engine>(); + m_Engine->SetWindowDimensions(1280, 720); + m_StateManager = CreateScope<StateManager>(); m_StateManager->RegisterState("Config", CreateScope<ConfigState>()); m_StateManager->RegisterState("Simulation", CreateScope<SimulationState>()); diff --git a/src/Core/Application.h b/src/Core/Application.h index c882bf6..c16b6d9 100644 --- a/src/Core/Application.h +++ b/src/Core/Application.h @@ -1,10 +1,12 @@ #pragma once +#include "StateManager.h" #include "Memory.h" #include "Window.h" #include "Event.h" #include "Log.h" -#include "StateManager.h" + +#include "Engine/Engine.h" namespace Donut { @@ -18,9 +20,10 @@ namespace Donut void Run(); void Close(); - Window& GetWindow() { return *m_Window; } + Window& GetWindow() { return *m_Window; } StateManager& GetStateManager() { return *m_StateManager; } - static Application& Get() { return *s_Instance; } + Engine& GetEngine() { return *m_Engine; } + static Application& Get() { return *s_Instance; } private: void OnInit(); void OnShutdown(); @@ -31,6 +34,7 @@ namespace Donut private: Scope<StateManager> m_StateManager; Scope<Window> m_Window; + Scope<Engine> m_Engine; bool m_Running; bool m_Minimized; diff --git a/src/Engine/Engine.cpp b/src/Engine/Engine.cpp index f6c594c..fa010a7 100644 --- a/src/Engine/Engine.cpp +++ b/src/Engine/Engine.cpp @@ -5,6 +5,7 @@ #include <GLFW/glfw3.h> #include "Engine.h" +#include "Core/Log.h" #include "Rendering/VertexBuffer.h" #include "Rendering/IndexBuffer.h" @@ -13,14 +14,14 @@ namespace Donut Engine::Engine() : m_SagA(glm::vec3(0.0f, 0.0f, 0.0f), 8.54e36f) { - GLFWwindow* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); - glfwGetFramebufferSize(window, &m_Width, &m_Height); + m_Width = 1280; + m_Height = 720; m_Camera.SetCameraMode(CameraMode::Orbital); - m_Camera.SetOrbitalRadius(6.34194e10); - m_Camera.SetOrbitalLimits(1e10, 1e12); + m_Camera.SetOrbitalRadius(1e11); + m_Camera.SetOrbitalLimits(1e9, 1e13); m_Camera.SetOrbitalSpeed(0.01f); - m_Camera.SetZoomSpeed(25e9f); + m_Camera.SetZoomSpeed(1e10f); m_Objects = { @@ -47,12 +48,17 @@ namespace Donut void Engine::UpdateWindowDimensions() { - GLFWwindow* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); + UpdateComputeDimensions(); + } + + void Engine::SetWindowDimensions(int width, int height) + { int oldWidth = m_Width; int oldHeight = m_Height; int oldComputeHeight = m_ComputeHeight; - glfwGetFramebufferSize(window, &m_Width, &m_Height); + m_Width = width; + m_Height = height; if (oldWidth != m_Width || oldHeight != m_Height || @@ -274,4 +280,66 @@ namespace Donut return { vertexArray, texture }; } + + void Engine::LoadObjectsFromScene(const std::vector<Donut::Object>& objects) + { + m_Objects.clear(); + m_Objects.push_back( + { + glm::vec4(0.00f, 0.00f, 0.00f, m_SagA.m_Rs), + glm::vec4(0, 0, 0, 1), + static_cast<float>(m_SagA.m_Mass) + }); + + for (const auto& obj : objects) + { + ObjectData engineObj; + + float scaleFactor = 1e10f; + engineObj.m_PosRadius = glm::vec4 + ( + obj.m_Centre.x * scaleFactor, + obj.m_Centre.y * scaleFactor, + obj.m_Centre.z * scaleFactor, + obj.m_Radius * scaleFactor + ); + + engineObj.m_Color = glm::vec4(obj.m_Material.m_Color, 1.0f); + + float volume = (4.0f / 3.0f) * 3.14159f * engineObj.m_PosRadius.w * engineObj.m_PosRadius.w * engineObj.m_PosRadius.w; + float density = 1e12f; + engineObj.m_Mass = volume * density; + engineObj.m_Velocity = glm::vec3(0.0f, 0.0f, 0.0f); + + m_Objects.push_back(engineObj); + } + + DONUT_INFO("Loaded {} objects from WorldBuilder scene (scaled up by {})", objects.size(), 1e10f); + PrintObjectInfo(); + } + + void Engine::PrintObjectInfo() const + { + DONUT_INFO("=== Object Information ==="); + DONUT_INFO("Total objects: {}", m_Objects.size()); + + for (size_t i = 0; i < m_Objects.size(); ++i) + { + const auto& obj = m_Objects[i]; + DONUT_INFO("Object {}: Pos=({:.2e}, {:.2e}, {:.2e}), Radius={:.2e}, Mass={:.2e}, Color=({:.2f}, {:.2f}, {:.2f})", + i, + obj.m_PosRadius.x, obj.m_PosRadius.y, obj.m_PosRadius.z, + obj.m_PosRadius.w, + obj.m_Mass, + obj.m_Color.x, obj.m_Color.y, obj.m_Color.z + ); + } + DONUT_INFO("Camera position: ({:.2e}, {:.2e}, {:.2e})", + m_Camera.GetOrbitalPosition().x, + m_Camera.GetOrbitalPosition().y, + m_Camera.GetOrbitalPosition().z + ); + DONUT_INFO("Camera radius: {:.2e}", m_Camera.GetOrbitalRadius()); + DONUT_INFO("========================"); + } } diff --git a/src/Engine/Engine.h b/src/Engine/Engine.h index 05bd96f..f3724ff 100644 --- a/src/Engine/Engine.h +++ b/src/Engine/Engine.h @@ -8,8 +8,9 @@ #include <chrono> #include <cmath> -#include "Core/Application.h" #include "Core/Camera.h" +#include "Object.h" + #include "Rendering/Renderer.h" #include "Rendering/Shader.h" #include "Rendering/VertexArray.h" @@ -73,6 +74,7 @@ namespace Donut void RenderScene(); void UpdatePhysics(float deltaTime); void UpdateWindowDimensions(); + void SetWindowDimensions(int width, int height); int GetWidth() const { return m_Width; } int GetHeight() const { return m_Height; } @@ -97,6 +99,9 @@ namespace Donut void SetMaxStepsMoving(int steps) { m_MaxStepsMoving = steps; } void SetMaxStepsStatic(int steps) { m_MaxStepsStatic = steps; } void SetEarlyExitDistance(float distance) { m_EarlyExitDistance = distance; } + + void LoadObjectsFromScene(const std::vector<Donut::Object>& objects); + void PrintObjectInfo() const; private: Ref<Shader> CreateComputeProgram(const char* path); std::pair<Ref<VertexArray>, Ref<Texture2D>> QuadVAO(); diff --git a/src/States/ConfigState.cpp b/src/States/ConfigState.cpp index 69bb2f1..c57bced 100644 --- a/src/States/ConfigState.cpp +++ b/src/States/ConfigState.cpp @@ -216,15 +216,8 @@ namespace Donut ImGui::Separator(); ImGui::Spacing(); - float buttonWidth = (ImGui::GetWindowWidth() - 120) / 6.0f; + float buttonWidth = (ImGui::GetWindowWidth() - 120) / 5.0f; - if (ImGui::Button("Start Simulation", ImVec2(buttonWidth, 35))) - { - ApplySettings(); - Application::Get().GetStateManager().SwitchToState("Simulation"); - } - - ImGui::SameLine(); if (ImGui::Button("World Builder", ImVec2(buttonWidth, 35))) { ApplySettings(); diff --git a/src/States/SimulationState.cpp b/src/States/SimulationState.cpp index b4ed204..fa3c4f5 100644 --- a/src/States/SimulationState.cpp +++ b/src/States/SimulationState.cpp @@ -15,15 +15,16 @@ namespace Donut DONUT_INFO("Entering Simulation State"); const auto& settings = SettingsManager::GetSettingsConst(); + auto& engine = Application::Get().GetEngine(); - m_Engine.SetTargetFPS(settings.simulation.targetFPS); - m_Engine.SetComputeHeight(settings.simulation.computeHeight); - m_Engine.SetMaxStepsMoving(settings.simulation.maxStepsMoving); - m_Engine.SetMaxStepsStatic(settings.simulation.maxStepsStatic); - m_Engine.SetEarlyExitDistance(settings.simulation.earlyExitDistance); - m_Engine.GetGravity() = settings.simulation.gravityEnabled; + 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; - m_Engine.UpdateComputeDimensions(); + engine.UpdateComputeDimensions(); m_Initialized = true; } @@ -34,38 +35,42 @@ namespace Donut void SimulationState::OnUpdate(float deltaTime) { - m_Engine.UpdatePerformance(deltaTime); - m_Engine.UpdateWindowDimensions(); - m_Engine.UpdatePhysics(deltaTime); + auto& engine = Application::Get().GetEngine(); + engine.UpdatePerformance(deltaTime); + engine.UpdateWindowDimensions(); + engine.UpdatePhysics(deltaTime); - if (m_Engine.GetCamera().IsDragging()) + if (engine.GetCamera().IsDragging()) { GLFWwindow* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - m_Engine.GetCamera().ProcessOrbitalMouseMove(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, m_Engine.GetWidth(), m_Engine.GetHeight()); + RenderCommand::SetViewport(0, 0, static_cast<uint32_t>(engine.GetWidth()), static_cast<uint32_t>(engine.GetHeight())); - m_Engine.DispatchCompute(m_Engine.GetCamera()); - m_Engine.DrawFullScreenQuad(); + engine.DispatchCompute(engine.GetCamera()); + engine.DrawFullScreenQuad(); } 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 = m_Engine.GetCamera(); + Camera& camera = engine.GetCamera(); double lastX = camera.GetLastX(); double lastY = camera.GetLastY(); glfwGetCursorPos(window, &lastX, &lastY); @@ -76,13 +81,13 @@ namespace Donut { MouseButtonReleasedEvent& e = (MouseButtonReleasedEvent&)event; int button = e.GetMouseButton(); - m_Engine.GetCamera().ProcessOrbitalMouseButton(button, GLFW_RELEASE, 0); + engine.GetCamera().ProcessOrbitalMouseButton(button, GLFW_RELEASE, 0); } if (event.GetEventType() == EventType::MouseScrolled) { MouseScrolledEvent& e = (MouseScrolledEvent&)event; - m_Engine.GetCamera().ProcessOrbitalScroll(e.GetXOffset(), e.GetYOffset()); + engine.GetCamera().ProcessOrbitalScroll(e.GetXOffset(), e.GetYOffset()); } if (event.GetEventType() == EventType::KeyPressed) @@ -90,14 +95,16 @@ namespace Donut KeyPressedEvent& e = (KeyPressedEvent&)event; if (e.GetKeyCode() == GLFW_KEY_G) { - m_Engine.GetGravity() = !m_Engine.GetGravity(); - DONUT_INFO("Gravity turned {}", m_Engine.GetGravity() ? "ON" : "OFF"); + 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); @@ -111,12 +118,12 @@ namespace Donut if (ImGui::Button("Save Settings")) { SimulationSettings settings = SettingsManager::GetSettingsConst().simulation; - settings.targetFPS = m_Engine.GetTargetFPS(); - settings.computeHeight = m_Engine.GetComputeHeight(); - settings.maxStepsMoving = m_Engine.GetMaxStepsMoving(); - settings.maxStepsStatic = m_Engine.GetMaxStepsStatic(); - settings.earlyExitDistance = m_Engine.GetEarlyExitDistance(); - settings.gravityEnabled = m_Engine.GetGravity(); + 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"); } @@ -128,12 +135,12 @@ namespace Donut ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate); ImGui::Text("Frame Time: %.3f ms", 1000.0f / ImGui::GetIO().Framerate); - ImGui::Text("Engine FPS: %.1f", m_Engine.GetCurrentFPS()); + ImGui::Text("Engine FPS: %.1f", engine.GetCurrentFPS()); - int targetFPS = m_Engine.GetTargetFPS(); + int targetFPS = engine.GetTargetFPS(); if (ImGui::SliderInt("Target FPS", &targetFPS, 30, 120)) { - m_Engine.SetTargetFPS(targetFPS); + engine.SetTargetFPS(targetFPS); SimulationSettings settings = SettingsManager::GetSettingsConst().simulation; settings.targetFPS = targetFPS; SettingsManager::SetSimulationSettings(settings); @@ -144,47 +151,52 @@ namespace Donut ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Simulation Info"); ImGui::Separator(); - ImGui::Text("Resolution: %dx%d", m_Engine.GetWidth(), m_Engine.GetHeight()); - ImGui::Text("Compute Resolution: %dx%d", m_Engine.GetComputeWidth(), m_Engine.GetComputeHeight()); - ImGui::Text("Objects: %zu", m_Engine.GetObjects().size()); + 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 = m_Engine.GetComputeHeight(); + int computeHeight = engine.GetComputeHeight(); if (ImGui::SliderInt("Compute Height", &computeHeight, 64, 2048)) { - m_Engine.SetComputeHeight(computeHeight); - m_Engine.UpdateComputeDimensions(); + 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)", m_Engine.GetComputeWidth()); + 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 = m_Engine.GetMaxStepsMoving(); + int maxStepsMoving = engine.GetMaxStepsMoving(); if (ImGui::SliderInt("Max Steps (Moving)", &maxStepsMoving, 1000, 60000)) { - m_Engine.SetMaxStepsMoving(maxStepsMoving); + engine.SetMaxStepsMoving(maxStepsMoving); SimulationSettings settings = SettingsManager::GetSettingsConst().simulation; settings.maxStepsMoving = maxStepsMoving; SettingsManager::SetSimulationSettings(settings); } - int maxStepsStatic = m_Engine.GetMaxStepsStatic(); + int maxStepsStatic = engine.GetMaxStepsStatic(); if (ImGui::SliderInt("Max Steps (Static)", &maxStepsStatic, 1000, 30000)) { - m_Engine.SetMaxStepsStatic(maxStepsStatic); + engine.SetMaxStepsStatic(maxStepsStatic); SimulationSettings settings = SettingsManager::GetSettingsConst().simulation; settings.maxStepsStatic = maxStepsStatic; SettingsManager::SetSimulationSettings(settings); } - float earlyExitDistance = m_Engine.GetEarlyExitDistance(); + float earlyExitDistance = engine.GetEarlyExitDistance(); if (ImGui::SliderFloat("Early Exit Distance", &earlyExitDistance, 1e11f, 1e13f, "%.2e")) { - m_Engine.SetEarlyExitDistance(earlyExitDistance); + engine.SetEarlyExitDistance(earlyExitDistance); SimulationSettings settings = SettingsManager::GetSettingsConst().simulation; settings.earlyExitDistance = earlyExitDistance; SettingsManager::SetSimulationSettings(settings); @@ -195,7 +207,7 @@ namespace Donut ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, 1.0f), "Physics"); ImGui::Separator(); - bool& gravity = m_Engine.GetGravity(); + bool& gravity = engine.GetGravity(); if (ImGui::Checkbox("Gravity Enabled", &gravity)) { SimulationSettings settings = SettingsManager::GetSettingsConst().simulation; @@ -209,12 +221,12 @@ namespace Donut ImGui::Separator(); ImGui::Text("Position: (%.2e, %.2e, %.2e)", - m_Engine.GetCamera().GetOrbitalPosition().x, - m_Engine.GetCamera().GetOrbitalPosition().y, - m_Engine.GetCamera().GetOrbitalPosition().z); - ImGui::Text("Radius: %.2e", m_Engine.GetCamera().GetOrbitalRadius()); - ImGui::Text("Azimuth: %.2f", m_Engine.GetCamera().GetAzimuth()); - ImGui::Text("Elevation: %.2f", m_Engine.GetCamera().GetElevation()); + 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(); diff --git a/src/States/SimulationState.h b/src/States/SimulationState.h index 2db4738..2589804 100644 --- a/src/States/SimulationState.h +++ b/src/States/SimulationState.h @@ -22,7 +22,6 @@ namespace Donut void OnEvent(Event& event) override; private: - Engine m_Engine; bool m_Initialized = false; }; }; diff --git a/src/States/WorldBuilderState.cpp b/src/States/WorldBuilderState.cpp index 63ef25c..70ef3d4 100644 --- a/src/States/WorldBuilderState.cpp +++ b/src/States/WorldBuilderState.cpp @@ -484,6 +484,18 @@ namespace Donut ImGui::Spacing(); + if (ImGui::Button("Start Simulation", ImVec2(ImGui::GetWindowWidth() - 20, 30))) + { + // Load objects from WorldBuilder scene into Engine + auto& engine = Application::Get().GetEngine(); + engine.LoadObjectsFromScene(m_Scene.objs); + + // Switch to simulation state + Application::Get().GetStateManager().SwitchToState("Simulation"); + } + + ImGui::Spacing(); + if (ImGui::Button("Back to Config", ImVec2(ImGui::GetWindowWidth() - 20, 30))) Application::Get().GetStateManager().SwitchToState("Config"); @@ -550,7 +562,6 @@ namespace Donut Object sphere(m_NewObjectPosition, m_NewObjectRadius, material); m_Scene.objs.push_back(sphere); - // Automatically select the newly created object m_SelectedObjectIndex = static_cast<int>(m_Scene.objs.size() - 1); m_NewObjectPosition = glm::vec3(0.0f, 0.0f, 0.0f); |
