From a2804d3da268951b471f6a0ed2a7e1d52a44427f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 7 Jan 2026 13:46:01 +0100 Subject: [PATCH] feat(VBOs): rework vbo functions and add comments --- inc/core/vbo.hpp | 20 +++++++++++++++----- src/core/vbo.cpp | 34 +++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/inc/core/vbo.hpp b/inc/core/vbo.hpp index cd9c5bb..95398ca 100755 --- a/inc/core/vbo.hpp +++ b/inc/core/vbo.hpp @@ -5,19 +5,29 @@ #include "glad/glad.h" +// A VBO (Vertex Buffer Object) is a contiguous block of GPU memory that stores +// vertex data as raw bytes. class VBO { private: - unsigned int id; + GLuint id; // OpenGL handle for this buffer public: - VBO(); + // VBO constructor + // It uploads raw vertex data into GPU memory (VRAM). + // The interpretation of this data is defined later by the VAO. + VBO(const void *data, size_t size); + + // VBO destructor + // Deletes the GPU buffer when the VBO object goes out of scope. Frees the + // VRAM associated with this VBO. ~VBO(); - void setData(const GLfloat* vertices, size_t size); - + // Binds this VBO to the current GL_ARRAY_BUFFER target void bind(); - void unbind(); + + // unbinds current GL_ARRAY_BUFFER target (it's replaced by null id) + static void unbind(); }; #endif \ No newline at end of file diff --git a/src/core/vbo.cpp b/src/core/vbo.cpp index 996b532..43ab9d4 100755 --- a/src/core/vbo.cpp +++ b/src/core/vbo.cpp @@ -1,16 +1,32 @@ -#include "vbo.hpp" +#include "core/vbo.hpp" -VBO::VBO() : id(0) {} +#include -void VBO::setData(const GLfloat* vertices, size_t size) +VBO::VBO(const void *data, std::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); + // Generate a unique OpenGL buffer handle (ID). + glGenBuffers(1, &id); + // Binding the VBO is mandatory before allocating GPU memory + bind(); + // Allocate GPU memory and upload data. + glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); } -VBO::~VBO() { glDeleteBuffers(1, &this->id); } +VBO::~VBO() +{ + // delete the OpenGL buffer handle + glDeleteBuffers(1, &this->id); +} -void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, this->id); } +void VBO::bind() +{ + // Bind this buffer to the GL_ARRAY_BUFFER target. + // GL_ARRAY_BUFFER tells opengl this buffer contains vertices + glBindBuffer(GL_ARRAY_BUFFER, this->id); +} -void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); } \ No newline at end of file +void VBO::unbind() +{ + // bind the buffer to NULL handle (remove old id) + glBindBuffer(GL_ARRAY_BUFFER, 0); +} \ No newline at end of file