added project structure

This commit is contained in:
2026-04-27 14:00:03 +02:00
parent f5b5ac4d99
commit 687ecb7f74
14 changed files with 203 additions and 167 deletions

View File

@@ -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 $<$<CONFIG:Debug>:_DEBUG>)

View File

@@ -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);
}
};

View File

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

View File

@@ -1,34 +0,0 @@
#pragma once
#include <GL/glew.h>
#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;
};

View File

@@ -1,41 +0,0 @@
#pragma once
#include <GL/glew.h>
#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;
};

21
include/camera/Camera.hpp Normal file
View File

@@ -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);
};

View File

@@ -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);
};

View File

@@ -0,0 +1,15 @@
#pragma once
#include <GL/glew.h>
#include "../defines.hpp"
struct IndexBuffer {
IndexBuffer(void* data, uint32 numIndices, uint8 elementSize);
virtual ~IndexBuffer();
void bind();
void unbind();
private:
GLuint bufferId;
};

View File

@@ -0,0 +1,16 @@
#pragma once
#include <GL/glew.h>
#include "../defines.hpp"
struct VertexBuffer {
VertexBuffer(void* data, uint32 numVertices);
virtual ~VertexBuffer();
void bind();
void unbind();
private:
GLuint bufferId;
GLuint vao;
};

21
src/camera/Camera.cpp Normal file
View File

@@ -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);
}

38
src/camera/FPS_Camera.cpp Normal file
View File

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

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -5,16 +5,16 @@
#include <cmath>
#include <SDL3/SDL.h>
#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)