From c17279960c6ceabd06b694ab08154fb9b67d8e92 Mon Sep 17 00:00:00 2001 From: acetheking987 Date: Mon, 24 Nov 2025 13:25:31 +0000 Subject: [PATCH] camera --- inc/engine/camera.hpp | 32 +++++++++++++++++++++++++++++++ inc/engine/opengl.hpp | 1 + src/engine/camera.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 25 ++++++++++++++---------- 4 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 inc/engine/camera.hpp create mode 100644 src/engine/camera.cpp diff --git a/inc/engine/camera.hpp b/inc/engine/camera.hpp new file mode 100644 index 0000000..4383f96 --- /dev/null +++ b/inc/engine/camera.hpp @@ -0,0 +1,32 @@ +#ifndef CAMERA_HPP +#define CAMERA_HPP + +// Includes +#include +#include + +// Camera Class +class Camera { +public: + Camera(const glm::vec3& position, const glm::vec3& target, const glm::vec3& up); + + glm::mat4 getViewMatrix() const; + glm::mat4 getProjectionMatrix() const; + void setProjectionMatrix(float fov, float aspectRatio, float nearPlane, float farPlane); + void setPosition(const glm::vec3& position); + void setTarget(const glm::vec3& target); + void setUp(const glm::vec3& up); + glm::vec3 getPosition() const; + + float fov = 45.0f; + +private: + glm::vec3 position; + glm::vec3 target; + glm::vec3 up; + float aspectRatio = 4.0f / 3.0f; + float nearPlane = 0.1f; + float farPlane = 100.0f; +}; + +#endif \ No newline at end of file diff --git a/inc/engine/opengl.hpp b/inc/engine/opengl.hpp index 9155c6e..9ee2d33 100644 --- a/inc/engine/opengl.hpp +++ b/inc/engine/opengl.hpp @@ -6,6 +6,7 @@ #include "engine/sdl.hpp" #include "engine/buffers.hpp" #include "engine/shaders.hpp" +#include "engine/camera.hpp" // OpenGL Context Class class OpenGLContext { diff --git a/src/engine/camera.cpp b/src/engine/camera.cpp new file mode 100644 index 0000000..14bf9ea --- /dev/null +++ b/src/engine/camera.cpp @@ -0,0 +1,44 @@ +#include "engine/camera.hpp" + +// Camera Constructor +Camera::Camera(const glm::vec3& position, const glm::vec3& target, const glm::vec3& up) + : position(position), target(target), up(up) { +} + +// Get View Matrix +glm::mat4 Camera::getViewMatrix() const { + return glm::lookAt(position, target, up); +} + +// Set Position +void Camera::setPosition(const glm::vec3& position) { + this->position = position; +} + +// Set Target +void Camera::setTarget(const glm::vec3& target) { + this->target = target; +} + +// Set Up Vector +void Camera::setUp(const glm::vec3& up) { + this->up = up; +} + +// Get Position +glm::vec3 Camera::getPosition() const { + return position; +} + +// Get Projection Matrix +glm::mat4 Camera::getProjectionMatrix() const { + return glm::perspective(glm::radians(fov), aspectRatio, nearPlane, farPlane); +} + +// Set Projection Matrix Parameters +void Camera::setProjectionMatrix(float fov, float aspectRatio, float nearPlane, float farPlane) { + this->fov = fov; + this->aspectRatio = aspectRatio; + this->nearPlane = nearPlane; + this->farPlane = farPlane; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ec4acec..108ac28 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,17 +12,22 @@ int main(int argc, char* argv[]) { OpenGLContext glContext(window); if (!glContext.isInitialized()) { return -1; } - glm::mat4 viewMatrix = glm::lookAt( - glm::vec3(0.0f, 0.0f, 3.0f), // Camera position - glm::vec3(0.0f, 0.0f, 0.0f), // Look at point + + Camera camera( + glm::vec3(0.0f, 0.0f, 3.0f), // Position + glm::vec3(0.0f, 0.0f, 0.0f), // Target glm::vec3(0.0f, 1.0f, 0.0f) // Up vector - ); // View matrix - glm::mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); // Projection matrix - glm::mat4 modelMatrix = glm::mat4(1.0f); // Model matrix + ); + + camera.setProjectionMatrix( + 45.0f, // Field of View + 800.0f / 600.0f, // Aspect Ratio + 0.1f, // Near Plane + 100.0f // Far Plane + ); Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag"); - // test setup // vertex, color float vertexData[] = { 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right @@ -53,9 +58,9 @@ int main(int argc, char* argv[]) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader_program.use(); - shader_program.setMat4("view", viewMatrix); - shader_program.setMat4("projection", projectionMatrix); - shader_program.setMat4("model", modelMatrix); + shader_program.setMat4("view", camera.getViewMatrix()); + shader_program.setMat4("projection", camera.getProjectionMatrix()); + shader_program.setMat4("model", glm::mat4(1.0f)); VAO.draw();