StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
staplegl::vertex_buffer Class Reference

Vertex Buffer Object (VBO) wrapper. More...

#include <vertex_buffer.hpp>

Inheritance diagram for staplegl::vertex_buffer:
Collaboration diagram for staplegl::vertex_buffer:

Public Member Functions

 vertex_buffer (std::span< const float > vertices) noexcept
 Construct a new vertex buffer object.
 
 vertex_buffer (std::span< const float > vertices, driver_draw_hint hint) noexcept
 
 vertex_buffer (std::span< const float > vertices, vertex_buffer_layout &&layout) noexcept
 
 vertex_buffer (std::span< const float > vertices, vertex_buffer_layout &&layout, driver_draw_hint hint) noexcept
 
 ~vertex_buffer ()
 
 vertex_buffer (const vertex_buffer &)=delete
 
auto operator= (const vertex_buffer &) -> vertex_buffer &=delete
 
 vertex_buffer (vertex_buffer &&other) noexcept
 
auto operator= (vertex_buffer &&other) noexcept -> vertex_buffer &
 
void bind () const
 Bind the vertex buffer object.
 
void set_layout (const vertex_buffer_layout &layout)
 Set the layout object.
 
constexpr auto layout () const -> const vertex_buffer_layout &
 
void set_data (std::span< const float > vertices) const noexcept
 Give new data to the vertex buffer object, overwriting the old one.
 
void set_data (std::span< const float > vertices, driver_draw_hint hint) const noexcept
 Give new data to the vertex buffer object, overwriting the old one. Also re-specify the hint.
 
constexpr auto id () const noexcept -> std::uint32_t
 Get the id of the vertex buffer object.
 
constexpr auto size () const noexcept -> std::size_t
 Get the number of vertices in the vertex buffer object.
 
constexpr auto draw_hint () const noexcept -> driver_draw_hint
 Return the driver draw hint of the vertex buffer object.
 
constexpr auto size_bytes () const noexcept -> std::size_t
 Get the size of the vertex buffer object in bytes.
 
template<plain_old_data T>
void apply (const std::function< void(std::span< T > vertices)> &func, driver_access_specifier access_specifier=staplegl::READ_WRITE) noexcept
 Applies a function to the vertices of the vertex buffer object.
 

Static Public Member Functions

static void unbind ()
 Unbind the vertex buffer object.
 

Protected Attributes

std::uint32_t m_id {}
 
staplegl::driver_draw_hint m_hint {}
 
vertex_buffer_layout m_layout
 
std::size_t m_size {}
 

Detailed Description

Vertex Buffer Object (VBO) wrapper.

Vertex Buffer Objects are OpenGL objects that store an array of data in the GPU's memory, the data is passed as a pointer and is then accessed through a user-specified layout.

See also
https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Buffer_Object
vertex_buffer_layout.hpp
Examples
batches.cpp, sandbox.cpp, and teapot.cpp.

Definition at line 79 of file vertex_buffer.hpp.

Constructor & Destructor Documentation

◆ vertex_buffer() [1/6]

staplegl::vertex_buffer::vertex_buffer ( std::span< const float >  vertices)
inlinenoexcept

Construct a new vertex buffer object.

Note
The vertices array is copied into the GPU's memory, so it can be safely deleted after the call.
By passing an empty std::span, the VBO will be initialized with no data.
Parameters
verticesa pointer to the vertices array, can be any contiguous container of floats.
sizethe size of the vertices array in bytes.

Definition at line 232 of file vertex_buffer.hpp.

◆ vertex_buffer() [2/6]

staplegl::vertex_buffer::vertex_buffer ( std::span< const float >  vertices,
driver_draw_hint  hint 
)
inlinenoexcept

Definition at line 222 of file vertex_buffer.hpp.

◆ vertex_buffer() [3/6]

staplegl::vertex_buffer::vertex_buffer ( std::span< const float >  vertices,
vertex_buffer_layout &&  layout 
)
inlinenoexcept

Definition at line 227 of file vertex_buffer.hpp.

◆ vertex_buffer() [4/6]

staplegl::vertex_buffer::vertex_buffer ( std::span< const float >  vertices,
vertex_buffer_layout &&  layout,
driver_draw_hint  hint 
)
inlinenoexcept

Definition at line 211 of file vertex_buffer.hpp.

◆ ~vertex_buffer()

staplegl::vertex_buffer::~vertex_buffer ( )
inline

Definition at line 237 of file vertex_buffer.hpp.

◆ vertex_buffer() [5/6]

staplegl::vertex_buffer::vertex_buffer ( const vertex_buffer )
delete

◆ vertex_buffer() [6/6]

staplegl::vertex_buffer::vertex_buffer ( vertex_buffer &&  other)
inlinenoexcept

Definition at line 244 of file vertex_buffer.hpp.

Member Function Documentation

◆ apply()

template<plain_old_data T>
void staplegl::vertex_buffer::apply ( const std::function< void(std::span< T > vertices)> &  func,
driver_access_specifier  access_specifier = staplegl::READ_WRITE 
)
noexcept

Applies a function to the vertices of the vertex buffer object.

Note
Ensure that T is tightly packed (no padding), as the vertices of the vertex buffer object are tightly packed.

This function provides an API for low-level manipulation of the vertices of the vertex buffer object, this can be useful to perform a number of modifications to the vertices without issuing multiple API calls, for example in the case of an instanced vertex buffer, one can update the whole buffer with a single call.

Internally, this works by obtaining a pointer to the vertices of the vertex buffer object and reinterpreting it as a user-provided type

Parameters
functhe function to be applied to the vertices of the vertex buffer object.
access_specifierthe access mode of the buffer, defaults to READ_WRITE, take care not to violate the specifier (reading a write-only buffer, writing a read-only buffer, etc.) as it results in undefined behaviour. If unsure, use READ_WRITE.
Template Parameters
Ta type that represents a vertex of the vertex buffer object.
See also
plain_old_data

Definition at line 301 of file vertex_buffer.hpp.

◆ bind()

void staplegl::vertex_buffer::bind ( ) const
inline

Bind the vertex buffer object.

Definition at line 268 of file vertex_buffer.hpp.

◆ draw_hint()

constexpr auto staplegl::vertex_buffer::draw_hint ( ) const -> driver_draw_hint
inlineconstexprnoexcept

Return the driver draw hint of the vertex buffer object.

Returns
driver_draw_hint the hint of the vertex buffer object.

Definition at line 164 of file vertex_buffer.hpp.

◆ id()

constexpr auto staplegl::vertex_buffer::id ( ) const -> std::uint32_t
inlineconstexprnoexcept

Get the id of the vertex buffer object.

Returns
std::uint32_t the id of the vertex buffer object.

Definition at line 149 of file vertex_buffer.hpp.

◆ layout()

constexpr auto staplegl::vertex_buffer::layout ( ) const -> const vertex_buffer_layout&
constexpr

Definition at line 283 of file vertex_buffer.hpp.

◆ operator=() [1/2]

auto staplegl::vertex_buffer::operator= ( const vertex_buffer ) -> vertex_buffer &=delete
delete

◆ operator=() [2/2]

auto staplegl::vertex_buffer::operator= ( vertex_buffer &&  other) -> vertex_buffer&
inlinenoexcept

Definition at line 253 of file vertex_buffer.hpp.

◆ set_data() [1/2]

void staplegl::vertex_buffer::set_data ( std::span< const float >  vertices) const
inlinenoexcept

Give new data to the vertex buffer object, overwriting the old one.

Parameters
verticesstd::span<const float> the new data to be given to the vertex buffer object.

Definition at line 288 of file vertex_buffer.hpp.

◆ set_data() [2/2]

void staplegl::vertex_buffer::set_data ( std::span< const float >  vertices,
driver_draw_hint  hint 
) const
inlinenoexcept

Give new data to the vertex buffer object, overwriting the old one. Also re-specify the hint.

Parameters
verticesstd::span<const float> the new data to be given to the vertex buffer object.
hintstaplegl::driver_draw_hint the new hint to be given to the vertex buffer object.
See also
driver_draw_hint

Definition at line 294 of file vertex_buffer.hpp.

◆ set_layout()

void staplegl::vertex_buffer::set_layout ( const vertex_buffer_layout layout)
inline

Set the layout object.

Parameters
layoutthe layout to be set.
See also
vertex_buffer_layout.hpp
Examples
batches.cpp, and teapot.cpp.

Definition at line 278 of file vertex_buffer.hpp.

◆ size()

constexpr auto staplegl::vertex_buffer::size ( ) const -> std::size_t
inlineconstexprnoexcept

Get the number of vertices in the vertex buffer object.

Returns
std::size_t the number of vertices in the vertex buffer object.

Definition at line 157 of file vertex_buffer.hpp.

◆ size_bytes()

constexpr auto staplegl::vertex_buffer::size_bytes ( ) const -> std::size_t
inlineconstexprnoexcept

Get the size of the vertex buffer object in bytes.

Returns
std::size_t the number of bytes the underlying OpenGL buffer takes up (assuming packed data).

Definition at line 171 of file vertex_buffer.hpp.

◆ unbind()

void staplegl::vertex_buffer::unbind ( )
inlinestatic

Unbind the vertex buffer object.

Definition at line 273 of file vertex_buffer.hpp.

Member Data Documentation

◆ m_hint

staplegl::driver_draw_hint staplegl::vertex_buffer::m_hint {}
protected

Definition at line 200 of file vertex_buffer.hpp.

◆ m_id

std::uint32_t staplegl::vertex_buffer::m_id {}
protected

Definition at line 199 of file vertex_buffer.hpp.

◆ m_layout

vertex_buffer_layout staplegl::vertex_buffer::m_layout
protected

Definition at line 201 of file vertex_buffer.hpp.

◆ m_size

std::size_t staplegl::vertex_buffer::m_size {}
protected

Definition at line 202 of file vertex_buffer.hpp.


The documentation for this class was generated from the following file: