feat(ebo): rework and add comments

This commit is contained in:
2026-01-07 16:38:07 +01:00
parent bf782efa85
commit 8985c7812d
2 changed files with 48 additions and 16 deletions

View File

@@ -1,24 +1,38 @@
#ifndef EBO_HPP #ifndef EBO_HPP
#define EBO_HPP #define EBO_HPP
#include <stddef.h> #include <cstddef>
#include "glad/glad.h" #include "glad/glad.h"
namespace core namespace core
{ {
// An EBO (Element Buffer Object) is a contiguous block of GPU memory that
// stores indices for indexed drawing (used with glDrawElements). Unlike VBOs,
// the EBO binding is stored in the VAO when bound while a VAO is active.
class EBO class EBO
{ {
private: private:
GLuint id; GLuint id; // OpenGL handle for this buffer
public: public:
EBO(); // EBO constructor
// Uploads index data into GPU memory (VRAM) using GL_ELEMENT_ARRAY_BUFFER.
// The indices are used for indexed rendering with glDrawElements.
EBO(const GLuint *indices, std::size_t size);
// EBO destructor
// Deletes the GPU buffer when the EBO object goes out of scope, freeing VRAM.
~EBO(); ~EBO();
void setData(const unsigned int* indices, size_t size); // Binds this EBO to the GL_ELEMENT_ARRAY_BUFFER target.
// If a VAO is bound, this binding is stored in the VAO.
void bind(); void bind();
void unbind();
// Unbinds the current GL_ELEMENT_ARRAY_BUFFER by binding 0.
// This can be useful to avoid accidental modifications to the EBO.
static void unbind();
}; };
}; // namespace core } // namespace core
#endif // EBO_HPP #endif // EBO_HPP

View File

@@ -1,16 +1,34 @@
#include "ebo.hpp" #include "core/ebo.hpp"
EBO::EBO() : id(0) {} core::EBO::EBO(const GLuint* indices, std::size_t size)
void EBO::setData(const unsigned int* indices, size_t size)
{ {
if (this->id == 0) glGenBuffers(1, &this->id); // Generate a unique OpenGL buffer handle (ID)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id); glGenBuffers(1, &id);
// Binding the EBO is mandatory before allocating GPU memory
// This makes OpenGL know which buffer we are uploading data to
bind();
// Allocate GPU memory and upload index data to the buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
} }
EBO::~EBO() { glDeleteBuffers(1, &this->id); } core::EBO::~EBO()
{
// Delete the OpenGL buffer handle and free GPU memory
glDeleteBuffers(1, &id);
}
void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id); } void core::EBO::bind()
{
// Bind this buffer to the GL_ELEMENT_ARRAY_BUFFER target
// OpenGL will use this buffer for indexed drawing (glDrawElements)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void core::EBO::unbind()
{
// Unbind any buffer from GL_ELEMENT_ARRAY_BUFFER
// This prevents accidental modification of the previously bound EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}