Vertex Buffer Object - Horizon-NTH/HorizonGUI GitHub Wiki
VertexBufferObject
Overview
The VertexBufferObject
class in the hgui::kernel
namespace provides a mechanism for creating and managing
OpenGL Vertex Buffer Objects (VBOs). VBOs are essential for efficiently storing vertex data,
which can be used for rendering in modern OpenGL applications. This class facilitates the creation, binding, updating,
and management of VBOs. The class is designed to work with OpenGL for rendering and requires an active OpenGL context.
This class is defined inside the header file VertexBufferObject.h
.
Constructors
-
VertexBufferObject()
: Constructs aVertexBufferObject
by generating a new VBO ID. -
~VertexBufferObject()
: Destroys theVertexBufferObject
and releases the associated VBO ID.
Member Functions
-
void bind()
: Binds the VBO to the current OpenGL context, making it the active VBO for subsequent data operations. -
void unbind()
: Unbinds the currently bound VBO, restoring the default state for data operations. -
void set_data(const void* data, int size, bool dynamic = false)
: Allocates and sets the VBO's data with the given data and size. Thedynamic
flag specifies whether the data is expected to change frequently. -
void set_sub_data(const void* data, int size, int offset)
: Updates a portion of the VBO's data with the provided data and size, starting from the specified offset.
Example Usage
#include <hgui/header/VertexBufferObject.h>
// Example Usage of VertexBufferObject class
hgui::kernel::VertexBufferObject vbo;
// Bind the VBO
vbo.bind();
// Set data for the VBO
const float vertices[] = {/* your vertex data */};
vbo.set_data(vertices, sizeof(vertices), false);
// Perform rendering operations using the VBO data
// Optionally, update a portion of the VBO's data
const float updatedData[] = {/* new data */};
vbo.set_sub_data(updatedData, sizeof(updatedData), /* offset */);
// Don't forget to unbind the VBO when done
vbo.unbind();
Note: In the "Example Usage" section, you should provide your specific vertex data and make sure to set up the VBO data correctly. Additionally, you can update a portion of the VBO's data using
set_sub_data
if needed. Remember to unbind the VBO to avoid interfering with subsequent data operations.