feat(p_cube.hpp): add vertice & indice length

This commit is contained in:
2026-01-06 22:13:09 +01:00
parent cc956449c7
commit 92e190c1a4
11 changed files with 175 additions and 169 deletions

136
src/core/camera.cpp Executable file
View File

@@ -0,0 +1,136 @@
#include "camera.hpp"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "glm/ext/matrix_transform.hpp"
#include "glm/ext/matrix_clip_space.hpp"
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
: width(width),
height(height),
window(window),
cameraSensitivity(sensitivity)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
updateCameraVectors();
}
void Camera::update(float deltaTime)
{
processInput(deltaTime);
processMouseMovement();
updateCameraVectors();
}
// TODO: callback management
void Camera::processInput(float deltaTime)
{
float velocity = speed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPosition += cameraFront * velocity;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPosition -= cameraFront * velocity;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPosition += cameraRight * velocity;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPosition -= cameraRight * velocity;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
cameraPosition += cameraUp * velocity;
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
cameraPosition -= cameraUp * velocity;
// Zoom
fov = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
? 35.f
: 45.f;
}
void Camera::processMouseMovement()
{
double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
if (firstMouse)
{
mousePosX = mouseX;
mousePosY = mouseY;
firstMouse = false;
return;
}
float deltaX = static_cast<float>(mouseX - mousePosX);
float deltaY = static_cast<float>(mousePosY - mouseY);
mousePosX = mouseX;
mousePosY = mouseY;
deltaX *= cameraSensitivity;
deltaY *= cameraSensitivity;
cameraYaw += deltaX;
cameraPitch += deltaY;
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
}
void Camera::updateCameraVectors()
{
glm::vec3 front;
front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
front.y = sin(glm::radians(cameraPitch));
front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
cameraFront = glm::normalize(front);
cameraRight = glm::normalize(glm::cross(cameraFront, worldUp));
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
}
glm::mat4 Camera::getViewMatrix() const
{
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
}
glm::mat4 Camera::getProjectionMatrix() const
{
return glm::perspective(
glm::radians(this->fov),
static_cast<float>(this->width) / static_cast<float>(this->height), 0.1f,
100.0f);
}
void Camera::setSpeed(float newSpeed)
{
if (newSpeed > 0.0f)
{
speed = newSpeed;
}
}
void Camera::setCameraSensitivity(float newSensitivity)
{
if (newSensitivity > 0.0f)
{
cameraSensitivity = newSensitivity;
}
}
void Camera::setFov(float newFov)
{
if (newFov > 1.0f && newFov < 179.0f)
{
fov = newFov;
}
}
void Camera::setPosition(const glm::vec3& newPosition)
{
cameraPosition = newPosition;
}

16
src/core/ebo.cpp Executable file
View File

@@ -0,0 +1,16 @@
#include "ebo.hpp"
EBO::EBO() : id(0) {}
void EBO::setData(const unsigned int* indices, size_t size)
{
if (this->id == 0) glGenBuffers(1, &this->id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
}
EBO::~EBO() { glDeleteBuffers(1, &this->id); }
void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id); }
void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }

86
src/core/shader.cpp Executable file
View File

@@ -0,0 +1,86 @@
#include "core/shader.hpp"
#include <iostream>
Shader::Shader() : shaderProgramID(0), vertexShaderID(0), fragmentShaderID(0) {}
void Shader::compile(const char* vertexSource, const char* fragmentSource)
{
if (this->shaderProgramID != 0) glDeleteProgram(this->shaderProgramID);
this->vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
this->fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
this->shaderProgramID = glCreateProgram();
this->addVertShader(vertexSource);
this->addFragShader(fragmentSource);
this->compileInProgram();
}
void Shader::addVertShader(const char* vertexSource)
{
glShaderSource(this->vertexShaderID, 1, &vertexSource, nullptr);
glCompileShader(this->vertexShaderID);
GLint success;
glGetShaderiv(this->vertexShaderID, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[1024];
glGetShaderInfoLog(this->vertexShaderID, 1024, nullptr, infoLog);
std::cerr << "ERROR(SHADER): VERTEX COMPILATION FAILED (ID "
<< this->vertexShaderID << ")\n"
<< infoLog << std::endl;
}
}
void Shader::addFragShader(const char* fragmentSource)
{
glShaderSource(this->fragmentShaderID, 1, &fragmentSource, nullptr);
glCompileShader(this->fragmentShaderID);
GLint success;
glGetShaderiv(this->fragmentShaderID, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[1024];
glGetShaderInfoLog(this->fragmentShaderID, 1024, nullptr, infoLog);
std::cerr << "ERROR(SHADER): FRAGMENT COMPILATION FAILED (ID "
<< this->fragmentShaderID << ")\n"
<< infoLog << std::endl;
}
}
void Shader::compileInProgram()
{
glAttachShader(this->shaderProgramID, this->vertexShaderID);
glAttachShader(this->shaderProgramID, this->fragmentShaderID);
glLinkProgram(this->shaderProgramID);
GLint success;
glGetProgramiv(this->shaderProgramID, GL_LINK_STATUS, &success);
if (!success)
{
GLchar infoLog[1024];
glGetProgramInfoLog(this->shaderProgramID, 1024, nullptr, infoLog);
std::cerr << "ERROR(SHADER): LINKING FAILED (Program ID "
<< this->shaderProgramID << ")\n"
<< infoLog << std::endl;
}
glDetachShader(this->shaderProgramID, this->vertexShaderID);
glDetachShader(this->shaderProgramID, this->fragmentShaderID);
glDeleteShader(this->vertexShaderID);
glDeleteShader(this->fragmentShaderID);
}
Shader::~Shader() { glDeleteProgram(this->shaderProgramID); }
void Shader::use() const { glUseProgram(this->shaderProgramID); }
GLuint Shader::getShaderProgramID() const { return this->shaderProgramID; }

65
src/core/texture.cpp Executable file
View File

@@ -0,0 +1,65 @@
#include "texture.hpp"
#include <filesystem>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace fs = std::filesystem;
Texture::Texture(const std::string& filename)
{
fs::path path = fs::absolute(fs::current_path() / "res" / filename);
int maxTextureSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
int imgW, imgH, numColCh;
stbi_set_flip_vertically_on_load(true);
unsigned char* data =
stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0);
if (!data)
{
std::cerr << "Error loading image: " << path << std::endl;
return;
}
int loadedTextureSize = imgW * imgH;
if (loadedTextureSize >= maxTextureSize)
{
std::cerr << "Error: max texture size is " << maxTextureSize
<< " pixels, but image size = " << loadedTextureSize << std::endl;
stbi_image_free(data);
return;
}
GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
Texture::~Texture() { glDeleteTextures(1, &id); }
void Texture::bind(GLenum textureUnit) const
{
glActiveTexture(textureUnit);
glBindTexture(GL_TEXTURE_2D, id);
}
unsigned int Texture::getID() const { return id; }

27
src/core/vao.cpp Executable file
View File

@@ -0,0 +1,27 @@
#include "vao.hpp"
VAO::VAO() { glGenVertexArrays(1, &id); }
VAO::~VAO() { glDeleteVertexArrays(1, &id); }
void VAO::bind() { glBindVertexArray(id); }
void VAO::unbind() { glBindVertexArray(0); }
void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type,
GLsizei stride, const void* offset)
{
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset);
}
void VAO::drawElement(GLenum mode, GLsizei count, GLenum type,
const void* indices)
{
glDrawElements(mode, count, type, indices);
}
void VAO::drawArray(GLenum mode, GLint first, GLsizei count)
{
glDrawArrays(mode, first, count);
}

16
src/core/vbo.cpp Executable file
View File

@@ -0,0 +1,16 @@
#include "vbo.hpp"
VBO::VBO() : id(0) {}
void VBO::setData(const GLfloat* vertices, size_t size)
{
if (this->id == 0) glGenBuffers(1, &this->id);
glBindBuffer(GL_ARRAY_BUFFER, this->id);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
}
VBO::~VBO() { glDeleteBuffers(1, &this->id); }
void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, this->id); }
void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }