Skip to content

Commit

Permalink
"Remove redundant precision macro and add consistent formatting updat…
Browse files Browse the repository at this point in the history
…es across codebase."
  • Loading branch information
devk0n committed Jan 28, 2025
1 parent 4323ae5 commit 7735993
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
8 changes: 7 additions & 1 deletion src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "renderer.h"
#include "imgui_layer.h"


Application::Application(int width, int height, const char* title)
: m_Window(nullptr, glfwDestroyWindow) {

Expand Down Expand Up @@ -51,6 +52,7 @@ Application::Application(int width, int height, const char* title)
m_ImGuiLayer = std::make_unique<ImGuiLayer>(m_Window.get());
}


Application::~Application() {
try {
m_ImGuiLayer.reset();
Expand All @@ -68,6 +70,7 @@ Application::~Application() {
}
}


void Application::run() {
while (!glfwWindowShouldClose(m_Window.get())) {
processInput();
Expand All @@ -79,6 +82,7 @@ void Application::run() {
}
}


void Application::processInput() {
static double lastFrameTime = 0.0;
double currentFrameTime = glfwGetTime();
Expand All @@ -94,6 +98,7 @@ void Application::processInput() {
}
}


void Application::update(double stepTime) {
static double position = 0.0;
static double velocity = 0.0;
Expand All @@ -107,8 +112,9 @@ void Application::update(double stepTime) {
}
}


void Application::render() {
m_Renderer->clearScreen({0.1, 0.1, 0.1, 1.0});
Renderer::clearScreen({0.1, 0.1, 0.1, 1.0});
m_Renderer->draw();
m_ImGuiLayer->renderUI();
}
Expand Down
20 changes: 12 additions & 8 deletions src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#define GLM_FORCE_PRECISION GLM_PRECISION_HIGHP_DOUBLE

#include <iostream>
#include <vector>
#include <stdexcept>
Expand All @@ -12,6 +10,7 @@

#include "renderer.h"


Renderer::Renderer(GLFWwindow* window)
: m_Window(window) {
if (!m_Window) {
Expand Down Expand Up @@ -48,15 +47,16 @@ Renderer::Renderer(GLFWwindow* window)
}
)";


m_GridShaderProgram = createShaderProgram(vertexShaderSource, fragmentShaderSource);

}


Renderer::~Renderer() {
std::cout << "Renderer destroyed." << std::endl;
}


void Renderer::initOpenGL() {
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
Expand All @@ -71,15 +71,16 @@ void Renderer::initOpenGL() {
std::cout << "OpenGL Initialized" << std::endl;
}


void Renderer::clearScreen(const glm::dvec4& color) {
// Convert double to float before passing to glClearColor
glClearColor(static_cast<float>(color.r),
static_cast<float>(color.g),
static_cast<float>(color.b),
static_cast<float>(color.a));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}


void Renderer::draw() {
clearScreen(glm::dvec4(0.1, 0.1, 0.1, 1.0));

Expand Down Expand Up @@ -117,6 +118,7 @@ void Renderer::drawGrid(double size, int divisions, const glm::dvec3& color) con
if (bufferSize > static_cast<std::size_t>(std::numeric_limits<GLsizeiptr>::max())) {
throw std::runtime_error("Buffer size exceeds maximum allowable GLsizeiptr value.");
}

glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(bufferSize), vertices.data(), GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 3 * sizeof(double), nullptr);
Expand All @@ -125,21 +127,20 @@ void Renderer::drawGrid(double size, int divisions, const glm::dvec3& color) con
glUseProgram(m_GridShaderProgram);
glUniform3dv(glGetUniformLocation(m_GridShaderProgram, "u_Color"), 1, glm::value_ptr(color));
glBindVertexArray(VAO);

std::size_t count = vertices.size() / 3;
if (count > static_cast<std::size_t>(std::numeric_limits<GLsizei>::max())) {
throw std::runtime_error("Too many vertices to draw; exceeds maximum GLsizei value.");
}

glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(count));


glBindVertexArray(0);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
}



void Renderer::handleMouseMovement(double xpos, double ypos) {
if (!m_RightMouseHeld) return;

Expand Down Expand Up @@ -187,6 +188,7 @@ void Renderer::handleMouseButton(int button, int action) {
}
}


void Renderer::handleKeyboardInput(GLFWwindow* window, double deltaTime) {
double velocity = m_CameraSpeed * deltaTime;

Expand All @@ -204,18 +206,21 @@ void Renderer::handleKeyboardInput(GLFWwindow* window, double deltaTime) {
}
}


void Renderer::updateViewMatrix() {
glm::dmat4 view = glm::lookAt(m_CameraPos, m_CameraPos + m_CameraFront, m_CameraUp);
glUseProgram(m_GridShaderProgram);
glUniformMatrix4dv(glGetUniformLocation(m_GridShaderProgram, "u_View"), 1, GL_FALSE, glm::value_ptr(view));
}


void Renderer::updateProjectionMatrix(double aspectRatio) const {
glm::dmat4 projection = glm::perspective(glm::radians(45.0), aspectRatio, 0.1, 100.0);
glUseProgram(m_GridShaderProgram);
glUniformMatrix4dv(glGetUniformLocation(m_GridShaderProgram, "u_Projection"), 1, GL_FALSE, glm::value_ptr(projection));
}


GLuint Renderer::compileShader(GLenum type, const char* source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, nullptr);
Expand All @@ -232,6 +237,7 @@ GLuint Renderer::compileShader(GLenum type, const char* source) {
return shader;
}


GLuint Renderer::createShaderProgram(const char* vertexSource, const char* fragmentSource) {
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSource);
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentSource);
Expand All @@ -254,5 +260,3 @@ GLuint Renderer::createShaderProgram(const char* vertexSource, const char* fragm

return program;
}


3 changes: 3 additions & 0 deletions src/simulation/quaternion_math.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "quaternion_math.h"


Eigen::Matrix4d skewNegativeMatrix(Eigen::Vector3d a) {
Eigen::Matrix4d sN = Eigen::Matrix4d::Zero();

Expand All @@ -12,6 +13,7 @@ Eigen::Matrix4d skewNegativeMatrix(Eigen::Vector3d a) {
return sN;
}


Eigen::Matrix<double, 3, 4> gMatrix(Eigen::Vector4d p) {
Eigen::Matrix<double, 3, 4> G;

Expand All @@ -22,6 +24,7 @@ Eigen::Matrix<double, 3, 4> gMatrix(Eigen::Vector4d p) {
return G;
}


Eigen::Matrix<double, 3, 4> lMatrix(Eigen::Vector4d p) {
Eigen::Matrix<double, 3, 4> L;

Expand Down
4 changes: 3 additions & 1 deletion src/simulation/rigid_body.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "rigid_body.h"


RigidBody::RigidBody(double mass, const Eigen::Matrix3d& inertia, const Eigen::Vector3d& position, const Eigen::Vector4d& orientation)
: m_Mass(mass),
m_Inertia(inertia),
Expand All @@ -13,6 +14,7 @@ Eigen::Vector3d RigidBody::getPosition() const {
return m_Position;
}


Eigen::Vector4d RigidBody::getOrientation() const {
return m_Orientation;
}
}
8 changes: 4 additions & 4 deletions src/simulation/simulation.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//
// Created by devkon on 28/01/2025.
//

#include "simulation.h"


Simulation::Simulation(double timeStep) {};


void Simulation::addRigidBody(const RigidBody& body){

}


void Simulation::update(){

}


const std::vector<RigidBody>& Simulation::getRigidBodies() const {
return m_RigidBodies;
}
10 changes: 9 additions & 1 deletion src/ui/imgui_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"


ImGuiLayer::ImGuiLayer(GLFWwindow* window) : m_Window(window) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
Expand All @@ -11,12 +12,14 @@ ImGuiLayer::ImGuiLayer(GLFWwindow* window) : m_Window(window) {
ImGui_ImplOpenGL3_Init("#version 460 core");
}


ImGuiLayer::~ImGuiLayer() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}


void ImGuiLayer::renderUI() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
Expand All @@ -33,14 +36,15 @@ void ImGuiLayer::renderUI() {
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}


void ImGuiLayer::updateSimulationData(double position, double velocity) {
m_PositionHistory[m_CurrentIndex] = position;
m_VelocityHistory[m_CurrentIndex] = velocity;
m_CurrentIndex = (m_CurrentIndex + 1) % historySize;
}

// Individual UI components

// Individual UI components
void ImGuiLayer::showMainMenu() {
ImGui::Begin("Main Menu");
if (ImGui::Button("Start Simulation")) {
Expand All @@ -53,6 +57,7 @@ void ImGuiLayer::showMainMenu() {
ImGui::End();
}


void ImGuiLayer::showSimulationControls() {
ImGui::Begin("Simulation Controls");

Expand All @@ -67,6 +72,7 @@ void ImGuiLayer::showSimulationControls() {
ImGui::End();
}


void ImGuiLayer::showRenderingOptions() {
ImGui::Begin("Rendering Options");

Expand All @@ -85,6 +91,7 @@ void ImGuiLayer::showRenderingOptions() {
ImGui::End();
}


void ImGuiLayer::showDebugWindow() {
ImGui::Begin("Debug Info");

Expand All @@ -98,6 +105,7 @@ void ImGuiLayer::showDebugWindow() {
ImGui::End();
}


void ImGuiLayer::showSimulationData() {
ImGui::Begin("Simulation Data");

Expand Down

0 comments on commit 7735993

Please sign in to comment.