Skip to content

Commit

Permalink
Refactor shader management to load from external files and add camera…
Browse files Browse the repository at this point in the history
… data integration with ImGui.
  • Loading branch information
devk0n committed Jan 28, 2025
1 parent 7735993 commit f967ad6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 46 deletions.
9 changes: 9 additions & 0 deletions assets/shaders/grid.frag.glsl
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions assets/shaders/grid.vert.glsl
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 0 additions & 9 deletions assets/shaders/grid_fragment_shader.glsl

This file was deleted.

10 changes: 0 additions & 10 deletions assets/shaders/grid_vertex_shader.glsl

This file was deleted.

8 changes: 7 additions & 1 deletion include/imgui_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> m_PositionHistory = std::vector<double>(historySize, 0.0);
Expand All @@ -25,9 +30,10 @@ class ImGuiLayer {
static void showMainMenu();
static void showSimulationControls();
static void showRenderingOptions();
static void showDebugWindow();
void showDebugWindow();
void showSimulationData();


};


Expand Down
11 changes: 9 additions & 2 deletions include/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

};


Expand Down
10 changes: 9 additions & 1 deletion src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
49 changes: 26 additions & 23 deletions src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "glm/glm.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <fstream>
#include <sstream>

#include "renderer.h"

Expand All @@ -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());

}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions src/ui/imgui_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit f967ad6

Please sign in to comment.