From 3d8d3259e1cf5a7d88e5d1e530cc8c9bf9140734 Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 14 Aug 2025 18:21:00 +0200 Subject: feat: I am going to throw my computer out a window --- src/states/simulation_state.cpp | 93 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) (limited to 'src/states/simulation_state.cpp') diff --git a/src/states/simulation_state.cpp b/src/states/simulation_state.cpp index 7e9cb76..fc235ff 100644 --- a/src/states/simulation_state.cpp +++ b/src/states/simulation_state.cpp @@ -1,14 +1,18 @@ #include "simulation_state.h" #include "rendering/renderer.h" #include "core/application.h" +#include "core/window.h" +#include "core/event.h" #include +#include namespace Donut { void SimulationState::on_enter() { DONUT_INFO("Entering Simulation State"); + m_initialized = true; } void SimulationState::on_exit() @@ -18,23 +22,79 @@ namespace Donut void SimulationState::on_update(float delta_time) { + + m_engine.update_window_dimensions(); + m_engine.update_physics(delta_time); + + if (m_engine.get_camera().is_dragging()) + { + GLFWwindow* window = static_cast(Application::get().get_window().get_native_window()); + double xpos, ypos; + glfwGetCursorPos(window, &xpos, &ypos); + m_engine.get_camera().process_orbital_mouse_move(xpos, ypos); + } } void SimulationState::on_render() { - Renderer::set_clear_color({ 0.0f, 0.0f, 0.0f, 1.0f }); - Renderer::clear(); + if (!m_initialized) return; + + RenderCommand::set_clear_color(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)); + RenderCommand::clear(); + + m_engine.generate_grid(m_engine.get_objects()); + + glm::mat4 view = glm::lookAt(m_engine.get_camera().get_orbital_position(), m_engine.get_camera().get_target(), glm::vec3(0,1,0)); + glm::mat4 proj = glm::perspective(glm::radians(60.0f), float(m_engine.get_width())/m_engine.get_height(), 1e9f, 1e14f); + glm::mat4 view_proj = proj * view; + + m_engine.draw_grid(view_proj); + + RenderCommand::set_viewport(0, 0, m_engine.get_width(), m_engine.get_height()); + m_engine.dispatch_compute(m_engine.get_camera()); + m_engine.draw_full_screen_quad(); } void SimulationState::on_event(Event& event) { + if (!m_initialized) return; + + if (event.get_event_type() == EventType::MouseButtonPressed) + { + MouseButtonPressedEvent& e = (MouseButtonPressedEvent&)event; + int button = e.get_mouse_button(); + + m_engine.get_camera().process_orbital_mouse_button(button, GLFW_PRESS, 0); + } + else if (event.get_event_type() == EventType::MouseButtonReleased) + { + MouseButtonReleasedEvent& e = (MouseButtonReleasedEvent&)event; + int button = e.get_mouse_button(); + m_engine.get_camera().process_orbital_mouse_button(button, GLFW_RELEASE, 0); + } + + if (event.get_event_type() == EventType::MouseScrolled) + { + MouseScrolledEvent& e = (MouseScrolledEvent&)event; + m_engine.get_camera().process_orbital_scroll(e.get_x_offset(), e.get_y_offset()); + } + + if (event.get_event_type() == EventType::KeyPressed) + { + KeyPressedEvent& e = (KeyPressedEvent&)event; + if (e.get_key_code() == GLFW_KEY_G) + { + m_engine.set_gravity(!m_engine.get_gravity()); + DONUT_INFO("Gravity turned {}", m_engine.get_gravity() ? "ON" : "OFF"); + } + } } void SimulationState::on_im_ui_render() { ImGui::Begin("Simulation Controls"); - ImGui::Text("Simulation State"); + ImGui::Text("Black Hole Simulation"); if (ImGui::Button("Back to Config")) Application::get().get_state_manager().switch_to_state("Config"); @@ -43,6 +103,33 @@ namespace Donut ImGui::Text("Simulation Info:"); ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate); ImGui::Text("Frame Time: %.3f ms", 1000.0f / ImGui::GetIO().Framerate); + ImGui::Text("Resolution: %dx%d", m_engine.get_width(), m_engine.get_height()); + ImGui::Text("Compute Resolution: %dx%d", m_engine.get_compute_width(), m_engine.get_compute_height()); + ImGui::Text("Objects: %zu", m_engine.get_objects().size()); + + ImGui::Separator(); + + ImGui::Text("Controls:"); + 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::Separator(); + + ImGui::Text("Physics:"); + bool gravity = m_engine.get_gravity(); + if (ImGui::Checkbox("Gravity Enabled", &gravity)) + m_engine.set_gravity(gravity); + + ImGui::Text("Camera:"); + ImGui::Text("Position: (%.2e, %.2e, %.2e)", + m_engine.get_camera().get_orbital_position().x, + m_engine.get_camera().get_orbital_position().y, + m_engine.get_camera().get_orbital_position().z); + ImGui::Text("Radius: %.2e", m_engine.get_camera().get_radius()); + ImGui::Text("Azimuth: %.2f", m_engine.get_camera().get_azimuth()); + ImGui::Text("Elevation: %.2f", m_engine.get_camera().get_elevation()); ImGui::End(); } -- cgit v1.3