diff --git a/assets/shaders/grid.frag.glsl b/assets/shaders/grid.frag.glsl new file mode 100644 index 0000000..faae2b2 --- /dev/null +++ b/assets/shaders/grid.frag.glsl @@ -0,0 +1,9 @@ +#version 460 core + +out vec4 FragColor; // Fragment color output (single-precision) +uniform dvec3 u_Color; // Double-precision uniform + +void main() { + // Convert double-precision to single-precision for output + FragColor = vec4(u_Color, 1.0); +} \ No newline at end of file diff --git a/assets/shaders/grid.vert.glsl b/assets/shaders/grid.vert.glsl new file mode 100644 index 0000000..01186e2 --- /dev/null +++ b/assets/shaders/grid.vert.glsl @@ -0,0 +1,10 @@ +#version 460 core + +layout (location = 0) in dvec3 a_Position; // Input double-precision data +uniform dmat4 u_Projection; // Double-precision uniform +uniform dmat4 u_View; // Double-precision uniform + +void main() { + // Convert double-precision to single-precision for gl_Position + gl_Position = mat4(u_Projection) * mat4(u_View) * vec4(a_Position, 1.0); +} \ No newline at end of file diff --git a/assets/shaders/grid_fragment_shader.glsl b/assets/shaders/grid_fragment_shader.glsl deleted file mode 100644 index 48218e7..0000000 --- a/assets/shaders/grid_fragment_shader.glsl +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core - -out vec4 FragColor; - -uniform vec3 u_Color; - -void main() { - FragColor = vec4(u_Color, 1.0); -} diff --git a/assets/shaders/grid_vertex_shader.glsl b/assets/shaders/grid_vertex_shader.glsl deleted file mode 100644 index 353f18e..0000000 --- a/assets/shaders/grid_vertex_shader.glsl +++ /dev/null @@ -1,10 +0,0 @@ -#version 330 core - -layout (location = 0) in vec3 a_Position; - -uniform mat4 u_Projection; // Projection matrix -uniform mat4 u_View; // View matrix - -void main() { - gl_Position = u_Projection * u_View * vec4(a_Position, 1.0); -} \ No newline at end of file diff --git a/include/imgui_layer.h b/include/imgui_layer.h index fc8c595..11cfa9f 100644 --- a/include/imgui_layer.h +++ b/include/imgui_layer.h @@ -13,9 +13,14 @@ class ImGuiLayer { void renderUI(); void updateSimulationData(double position, double velocity); + void updateCameraData(const glm::dvec3 &position, const glm::dvec3 &orientation); + private: GLFWwindow* m_Window; + glm::dvec3 m_CameraPosition{}; + glm::dvec3 m_CameraOrientation{}; + // Circular buffer for storing simulation data static constexpr int historySize = 100; std::vector m_PositionHistory = std::vector(historySize, 0.0); @@ -25,9 +30,10 @@ class ImGuiLayer { static void showMainMenu(); static void showSimulationControls(); static void showRenderingOptions(); - static void showDebugWindow(); + void showDebugWindow(); void showSimulationData(); + }; diff --git a/include/renderer.h b/include/renderer.h index fea77a9..24cad09 100644 --- a/include/renderer.h +++ b/include/renderer.h @@ -16,12 +16,16 @@ class Renderer { void handleMouseMovement(double xpos, double ypos); void handleKeyboardInput(GLFWwindow* window, double deltaTime); + glm::dvec3 getCameraPosition() const; + + glm::dvec3 getCameraOrientation() const; + private: GLFWwindow* m_Window; GLuint m_GridShaderProgram; - glm::dvec3 m_CameraPos = glm::dvec3(0.0, 0.0, 3.0); - glm::dvec3 m_CameraFront = glm::dvec3(0.0, 0.0, -1.0); + glm::dvec3 m_CameraPos = glm::dvec3(0.0, 5.2, 10.5); + glm::dvec3 m_CameraFront = glm::dvec3(0.0, -0.5, -1.0); glm::dvec3 m_CameraUp = glm::dvec3(0.0, 1.0, 0.0); double m_Yaw = -90.0; double m_Pitch = 0.0; @@ -39,6 +43,9 @@ class Renderer { static GLuint compileShader(GLenum type, const char *source); static GLuint createShaderProgram(const char *vertexSource, const char *fragmentSource); + + static std::string loadShaderFromFile(const std::string &filepath); + }; diff --git a/src/core/application.cpp b/src/core/application.cpp index f71e7df..6ec4e2d 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -107,14 +107,22 @@ void Application::update(double stepTime) { velocity += acceleration * stepTime; position += velocity * stepTime; + // Pass simulation data to ImGui if (m_ImGuiLayer) { m_ImGuiLayer->updateSimulationData(position, velocity); } + + // Pass camera data to ImGui + if (m_Renderer && m_ImGuiLayer) { + glm::dvec3 cameraPos = m_Renderer->getCameraPosition(); + glm::dvec3 cameraOrientation = m_Renderer->getCameraOrientation(); + m_ImGuiLayer->updateCameraData(cameraPos, cameraOrientation); + } } void Application::render() { - Renderer::clearScreen({0.1, 0.1, 0.1, 1.0}); + m_Renderer->clearScreen({0.1, 0.1, 0.1, 1.0}); m_Renderer->draw(); m_ImGuiLayer->renderUI(); } diff --git a/src/graphics/renderer.cpp b/src/graphics/renderer.cpp index 4f038b7..9256859 100644 --- a/src/graphics/renderer.cpp +++ b/src/graphics/renderer.cpp @@ -7,6 +7,8 @@ #include "glm/glm.hpp" #include #include +#include +#include #include "renderer.h" @@ -24,30 +26,10 @@ Renderer::Renderer(GLFWwindow* window) initOpenGL(); - const char* vertexShaderSource = R"( - #version 460 core - layout (location = 0) in dvec3 a_Position; // Input double-precision data - uniform dmat4 u_Projection; // Double-precision uniform - uniform dmat4 u_View; // Double-precision uniform + std::string vertexShaderSource = loadShaderFromFile("C:/Users/devkon/CLionProjects/DynamicsLab/assets/shaders/grid.vert.glsl"); + std::string fragmentShaderSource = loadShaderFromFile("C:/Users/devkon/CLionProjects/DynamicsLab/assets/shaders/grid.frag.glsl"); - void main() { - // Convert double-precision to single-precision for gl_Position - gl_Position = mat4(u_Projection) * mat4(u_View) * vec4(a_Position, 1.0); - } - )"; - - const char* fragmentShaderSource = R"( - #version 460 core - out vec4 FragColor; // Fragment color output (single-precision) - uniform dvec3 u_Color; // Double-precision uniform - - void main() { - // Convert double-precision to single-precision for output - FragColor = vec4(u_Color, 1.0); - } - )"; - - m_GridShaderProgram = createShaderProgram(vertexShaderSource, fragmentShaderSource); + m_GridShaderProgram = createShaderProgram(vertexShaderSource.c_str(), fragmentShaderSource.c_str()); } @@ -57,6 +39,19 @@ Renderer::~Renderer() { } +std::string Renderer::loadShaderFromFile(const std::string& filepath) { + std::ifstream file(filepath, std::ios::in); + if (!file.is_open()) { + throw std::runtime_error("Failed to open shader file: " + filepath); + } + + std::stringstream buffer; + buffer << file.rdbuf(); + file.close(); + return buffer.str(); +} + + void Renderer::initOpenGL() { GLenum err = glGetError(); if (err != GL_NO_ERROR) { @@ -260,3 +255,11 @@ GLuint Renderer::createShaderProgram(const char* vertexSource, const char* fragm return program; } + +glm::dvec3 Renderer::getCameraPosition() const { + return m_CameraPos; +} + +glm::dvec3 Renderer::getCameraOrientation() const { + return m_CameraFront; +} diff --git a/src/ui/imgui_layer.cpp b/src/ui/imgui_layer.cpp index d1ee19d..56903be 100644 --- a/src/ui/imgui_layer.cpp +++ b/src/ui/imgui_layer.cpp @@ -43,6 +43,11 @@ void ImGuiLayer::updateSimulationData(double position, double velocity) { m_CurrentIndex = (m_CurrentIndex + 1) % historySize; } +void ImGuiLayer::updateCameraData(const glm::dvec3& position, const glm::dvec3& orientation) { + m_CameraPosition = position; + m_CameraOrientation = orientation; +} + // Individual UI components void ImGuiLayer::showMainMenu() { @@ -102,10 +107,17 @@ void ImGuiLayer::showDebugWindow() { ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate); ImGui::Text("Frame Time: %.3f ms", 1000.0f / ImGui::GetIO().Framerate); + // Camera information + ImGui::Separator(); + ImGui::Text("Camera Info:"); + ImGui::Text("Position: (%.2f, %.2f, %.2f)", m_CameraPosition.x, m_CameraPosition.y, m_CameraPosition.z); + ImGui::Text("Orientation: (%.2f, %.2f, %.2f)", m_CameraOrientation.x, m_CameraOrientation.y, m_CameraOrientation.z); + ImGui::End(); } + void ImGuiLayer::showSimulationData() { ImGui::Begin("Simulation Data");