From 687ecb7f7455aad86d436fb6466e77b0b7c80efa Mon Sep 17 00:00:00 2001 From: miaurizius Date: Mon, 27 Apr 2026 14:00:03 +0200 Subject: [PATCH] added project structure --- CMakeLists.txt | 12 ++++--- include/Camera.hpp | 33 ------------------- include/FPS_Camera.hpp | 51 ----------------------------- include/IndexBuffer.hpp | 34 ------------------- include/VertexBuffer.hpp | 41 ----------------------- include/camera/Camera.hpp | 21 ++++++++++++ include/camera/FPS_Camera.hpp | 20 +++++++++++ include/glPipeline/IndexBuffer.hpp | 15 +++++++++ include/glPipeline/VertexBuffer.hpp | 16 +++++++++ src/camera/Camera.cpp | 21 ++++++++++++ src/camera/FPS_Camera.cpp | 38 +++++++++++++++++++++ src/glPipeline/IndexBuffer.cpp | 28 ++++++++++++++++ src/glPipeline/VertexBuffer.cpp | 32 ++++++++++++++++++ src/main.cpp | 8 ++--- 14 files changed, 203 insertions(+), 167 deletions(-) delete mode 100644 include/Camera.hpp delete mode 100644 include/FPS_Camera.hpp delete mode 100644 include/IndexBuffer.hpp delete mode 100644 include/VertexBuffer.hpp create mode 100644 include/camera/Camera.hpp create mode 100644 include/camera/FPS_Camera.hpp create mode 100644 include/glPipeline/IndexBuffer.hpp create mode 100644 include/glPipeline/VertexBuffer.hpp create mode 100644 src/camera/Camera.cpp create mode 100644 src/camera/FPS_Camera.cpp create mode 100644 src/glPipeline/IndexBuffer.cpp create mode 100644 src/glPipeline/VertexBuffer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f20a22c..95c0bc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,13 +8,17 @@ find_package(SDL3 REQUIRED) find_package(GLEW REQUIRED) add_executable(${PROJECT_NAME} src/main.cpp - include/VertexBuffer.hpp + include/glPipeline/VertexBuffer.hpp include/defines.hpp src/Shader.cpp include/Shader.hpp - include/IndexBuffer.hpp - include/Camera.hpp - include/FPS_Camera.hpp) + include/glPipeline/IndexBuffer.hpp + include/camera/Camera.hpp + include/camera/FPS_Camera.hpp + src/camera/Camera.cpp + src/camera/FPS_Camera.cpp + src/glPipeline/IndexBuffer.cpp + src/glPipeline/VertexBuffer.cpp) target_compile_definitions(${PROJECT_NAME} PRIVATE $<$:_DEBUG>) diff --git a/include/Camera.hpp b/include/Camera.hpp deleted file mode 100644 index 58f87d9..0000000 --- a/include/Camera.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "glm/glm.hpp" -#include "glm/gtc/matrix_transform.hpp" - -class Camera { -protected: - glm::vec3 position; - glm::mat4 projection; - glm::mat4 view; - glm::mat4 viewProj; - -public: - Camera(float fov, float width, float height) { - projection = glm::perspective(fov/2.0f, width/height, 0.1f, 1000.f); - view = glm::mat4(1.0f); - position = glm::vec3(0.0f); - update(); - } - - glm::mat4 getViewProj() { - return viewProj; - } - - virtual void update() { - viewProj = projection * view; - } - - virtual void translate(glm::vec3 v) { - position += v; - view = glm::translate(view, v*-1.0f); - } -}; \ No newline at end of file diff --git a/include/FPS_Camera.hpp b/include/FPS_Camera.hpp deleted file mode 100644 index a8cf3bd..0000000 --- a/include/FPS_Camera.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include "Camera.hpp" - -class FPSCamera : public Camera { -protected: - float yaw; // rotation y-axis - float pitch; // rotating x-axis - glm::vec3 lookAt; - const float mouseSensitivity = 0.3f; - glm::vec3 up; - -public: - FPSCamera(float fov, float width, float height) : Camera(fov, width, height) { - up = glm::vec3(0.0f, 1.0f, 0.0f); - yaw = -90.0f; - pitch = 0.0f; - onMouseMoved(0.0f, 0.0f); - } - - void onMouseMoved(float xRel, float yRel) { - yaw += xRel * mouseSensitivity; - pitch -= yRel * mouseSensitivity; - if (pitch > 89.0f) pitch = 89.0f; - else if (pitch < -89.0f) pitch = -89.0f; - - glm::vec3 front; - front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw)); - front.y = sin(glm::radians(pitch)); - front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw)); - lookAt = glm::normalize(front); - - update(); - } - - void update() override { - view = glm::lookAt(position, position+lookAt, up); - viewProj = projection * view; - } - - void moveFront(float amount) { - translate(lookAt * amount); - update(); - } - - void moveSideways(float amount) { - translate(glm::normalize(glm::cross(lookAt, up)) * amount); - update(); - } - -}; \ No newline at end of file diff --git a/include/IndexBuffer.hpp b/include/IndexBuffer.hpp deleted file mode 100644 index 3f9f652..0000000 --- a/include/IndexBuffer.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include - -#include "defines.hpp" - -struct IndexBuffer { - IndexBuffer(void* data, uint32 numIndices, uint8 elementSize) { - glGenBuffers(1, &bufferId); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*elementSize, data, GL_STATIC_DRAW); - - // POSITION - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x)); - // COLOR - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r)); - - glBindVertexArray(0); - }; - virtual ~IndexBuffer() { - glDeleteBuffers(1, &bufferId); - } - - void bind() { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId); - } - void unbind() { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } - -private: - GLuint bufferId; -}; \ No newline at end of file diff --git a/include/VertexBuffer.hpp b/include/VertexBuffer.hpp deleted file mode 100644 index cf0b84b..0000000 --- a/include/VertexBuffer.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include - -#include "defines.hpp" - -struct VertexBuffer { - VertexBuffer(void* data, uint32 numVertices) { - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - glGenBuffers(1, &bufferId); - glBindBuffer(GL_ARRAY_BUFFER, bufferId); - glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Vertex), data, GL_STATIC_DRAW); - - // POSITION - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x)); - // TEXTURE COORDINATES - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,u)); - // COLOR - glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r)); - - glBindVertexArray(0); - }; - virtual ~VertexBuffer() { - glDeleteBuffers(1, &bufferId); - } - - void bind() { - glBindVertexArray(vao); - } - void unbind() { - glBindVertexArray(0); - } - -private: - GLuint bufferId; - GLuint vao; -}; \ No newline at end of file diff --git a/include/camera/Camera.hpp b/include/camera/Camera.hpp new file mode 100644 index 0000000..0cc2263 --- /dev/null +++ b/include/camera/Camera.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" + +class Camera { +protected: + glm::vec3 position; + glm::mat4 projection; + glm::mat4 view; + glm::mat4 viewProj; + +public: + Camera(float fov, float width, float height); + virtual ~Camera() = default; + + glm::mat4 getViewProj(); + + virtual void update(); + virtual void translate(glm::vec3 v); +}; \ No newline at end of file diff --git a/include/camera/FPS_Camera.hpp b/include/camera/FPS_Camera.hpp new file mode 100644 index 0000000..aa78337 --- /dev/null +++ b/include/camera/FPS_Camera.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "Camera.hpp" + +class FPSCamera : public Camera { +protected: + float yaw; // rotation y-axis + float pitch; // rotating x-axis + glm::vec3 lookAt; + const float mouseSensitivity = 0.3f; + glm::vec3 up; + +public: + FPSCamera(float fov, float width, float height); + + void onMouseMoved(float xRel, float yRel); + void update() override; + void moveFront(float amount); + void moveSideways(float amount); +}; \ No newline at end of file diff --git a/include/glPipeline/IndexBuffer.hpp b/include/glPipeline/IndexBuffer.hpp new file mode 100644 index 0000000..d25c438 --- /dev/null +++ b/include/glPipeline/IndexBuffer.hpp @@ -0,0 +1,15 @@ +#pragma once +#include + +#include "../defines.hpp" + +struct IndexBuffer { + IndexBuffer(void* data, uint32 numIndices, uint8 elementSize); + virtual ~IndexBuffer(); + + void bind(); + void unbind(); + +private: + GLuint bufferId; +}; \ No newline at end of file diff --git a/include/glPipeline/VertexBuffer.hpp b/include/glPipeline/VertexBuffer.hpp new file mode 100644 index 0000000..8b12064 --- /dev/null +++ b/include/glPipeline/VertexBuffer.hpp @@ -0,0 +1,16 @@ +#pragma once +#include + +#include "../defines.hpp" + +struct VertexBuffer { + VertexBuffer(void* data, uint32 numVertices); + virtual ~VertexBuffer(); + + void bind(); + void unbind(); + +private: + GLuint bufferId; + GLuint vao; +}; \ No newline at end of file diff --git a/src/camera/Camera.cpp b/src/camera/Camera.cpp new file mode 100644 index 0000000..c8b0e0f --- /dev/null +++ b/src/camera/Camera.cpp @@ -0,0 +1,21 @@ +#include "camera/Camera.hpp" + +Camera::Camera(float fov, float width, float height) { + projection = glm::perspective(fov/2.0f, width/height, 0.1f, 1000.f); + view = glm::mat4(1.0f); + position = glm::vec3(0.0f); + update(); +} + +glm::mat4 Camera::getViewProj() { + return viewProj; +} + +void Camera::update() { + viewProj = projection * view; +} + +void Camera::translate(glm::vec3 v) { + position += v; + view = glm::translate(view, v*-1.0f); +} diff --git a/src/camera/FPS_Camera.cpp b/src/camera/FPS_Camera.cpp new file mode 100644 index 0000000..2d28bba --- /dev/null +++ b/src/camera/FPS_Camera.cpp @@ -0,0 +1,38 @@ +#include "camera/FPS_Camera.hpp" + +FPSCamera::FPSCamera(float fov, float width, float height) : Camera(fov, width, height) { + up = glm::vec3(0.0f, 1.0f, 0.0f); + yaw = -90.0f; + pitch = 0.0f; + onMouseMoved(0.0f, 0.0f); +} + +void FPSCamera::onMouseMoved(float xRel, float yRel) { + yaw += xRel * mouseSensitivity; + pitch -= yRel * mouseSensitivity; + if (pitch > 89.0f) pitch = 89.0f; + else if (pitch < -89.0f) pitch = -89.0f; + + glm::vec3 front; + front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw)); + front.y = sin(glm::radians(pitch)); + front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw)); + lookAt = glm::normalize(front); + + update(); +} + +void FPSCamera::update() { + view = glm::lookAt(position, position+lookAt, up); + viewProj = projection * view; +} + +void FPSCamera::moveFront(float amount) { + translate(glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f) * lookAt) * amount); + update(); +} + +void FPSCamera::moveSideways(float amount) { + translate(glm::normalize(glm::cross(lookAt, up)) * amount); + update(); +} diff --git a/src/glPipeline/IndexBuffer.cpp b/src/glPipeline/IndexBuffer.cpp new file mode 100644 index 0000000..31c31bb --- /dev/null +++ b/src/glPipeline/IndexBuffer.cpp @@ -0,0 +1,28 @@ +#include "glPipeline/IndexBuffer.hpp" + +IndexBuffer::IndexBuffer(void* data, uint32 numIndices, uint8 elementSize) { + glGenBuffers(1, &bufferId); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*elementSize, data, GL_STATIC_DRAW); + + // POSITION + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x)); + // COLOR + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r)); + + glBindVertexArray(0); +} + +IndexBuffer::~IndexBuffer() { + glDeleteBuffers(1, &bufferId); +} + +void IndexBuffer::bind() { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId); +} + +void IndexBuffer::unbind() { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} diff --git a/src/glPipeline/VertexBuffer.cpp b/src/glPipeline/VertexBuffer.cpp new file mode 100644 index 0000000..5673332 --- /dev/null +++ b/src/glPipeline/VertexBuffer.cpp @@ -0,0 +1,32 @@ +#include "glPipeline/VertexBuffer.hpp" + +VertexBuffer::VertexBuffer(void* data, uint32 numVertices) { + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + glGenBuffers(1, &bufferId); + glBindBuffer(GL_ARRAY_BUFFER, bufferId); + glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Vertex), data, GL_STATIC_DRAW); + + // POSITION + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x)); + // TEXTURE COORDINATES + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,u)); + // COLOR + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r)); + + glBindVertexArray(0); +} +VertexBuffer::~VertexBuffer() { + glDeleteBuffers(1, &bufferId); +} + +void VertexBuffer::bind() { + glBindVertexArray(vao); +} +void VertexBuffer::unbind() { + glBindVertexArray(0); +} diff --git a/src/main.cpp b/src/main.cpp index 32ce773..c78a6e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,16 +5,16 @@ #include #include #include "defines.hpp" -#include "IndexBuffer.hpp" -#include "VertexBuffer.hpp" #include "Shader.hpp" #define STB_IMAGE_IMPLEMENTATION -#include "Camera.hpp" -#include "FPS_Camera.hpp" +#include "camera/Camera.hpp" #include "stb_image.hpp" +#include "camera/FPS_Camera.hpp" #include "glm/glm.hpp" #include "glm/ext/matrix_transform.hpp" #include "glm/gtc/matrix_transform.hpp" +#include "glPipeline/IndexBuffer.hpp" +#include "glPipeline/VertexBuffer.hpp" void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { if (severity == GL_DEBUG_SEVERITY_NOTIFICATION) return; //ignore notifications (mostly performance information)