34 Buffer(GLenum target = GL_ARRAY_BUFFER) {
35 this->target = target;
40 glDeleteBuffers(1, &
buffer);
49 glBindBuffer(target,
buffer);
57 this->target = target;
58 glBindBuffer(target,
buffer);
70 void bind(GLenum target,
unsigned int index) {
71 this->target = target;
73 case GL_ATOMIC_COUNTER_BUFFER:
74 case GL_TRANSFORM_FEEDBACK_BUFFER:
75 case GL_UNIFORM_BUFFER:
76 case GL_SHADER_STORAGE_BUFFER:
77 glBindBufferBase(target, index,
buffer);
80 glBindBuffer(target,
buffer);
92 glBindBuffer(target, 0);
101 template <
typename T>
106 glBufferData(target,
size,
nullptr, usage);
116 template <
typename T>
117 void load(
const std::vector<T> &data, GLenum usage) {
120 size = data.size() *
sizeof(T);
123 glBufferData(target,
size, data.data(), usage);
134 template <
typename T>
140 glBufferData(target,
size, data, usage);
154 bool store(
unsigned int offset,
unsigned int size,
void *storage) {
155 if(offset >= this->size)
164 bind(GL_COPY_READ_BUFFER);
165 glGetBufferSubData(GL_COPY_READ_BUFFER, offset,
size, storage);
180 unsigned int write_offset,
unsigned int size) {
182 if(read_offset >= source.
size ||
186 if(write_offset >= this->size ||
187 write_offset +
size >= this->size)
190 source.
bind(GL_COPY_READ_BUFFER);
191 bind(GL_COPY_WRITE_BUFFER);
192 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
193 read_offset, write_offset,
size);
208 unsigned int write_offset,
unsigned int size) {
213 if(read_offset >= source->
size ||
217 if(write_offset >= this->size ||
218 write_offset +
size >= this->size)
221 source->
bind(GL_COPY_READ_BUFFER);
222 bind(GL_COPY_WRITE_BUFFER);
223 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
224 read_offset, write_offset,
size);
239 void *
map(GLenum access) {
240 bind(GL_ARRAY_BUFFER);
241 void *ptr = glMapBuffer(target, access);
251 bind(GL_ARRAY_BUFFER);
252 glUnmapBuffer(target);
260 template <
typename T>
264 unsigned int new_size = data.size() *
sizeof(T);
266 if(
size == new_size) {
268 glBufferSubData(target, 0,
size, data.data());
272 glBufferData(target, new_size, data.data(), usage);
282 template <
typename T>
286 unsigned int new_size = number_elements *
sizeof(T);
288 if(
size == new_size) {
290 glBufferSubData(target, 0,
size, data);
294 glBufferData(target, new_size, data, usage);
304 template <
typename T>
307 unsigned int data_size = data.size() *
sizeof(T);
309 if(offset + data_size >
size) {
314 glBufferSubData(target, offset, data_size, data.data());
325 template <
typename T>
328 unsigned int number_elements) {
330 unsigned int data_size = number_elements *
sizeof(T);
332 if(offset + data_size >
size) {
337 glBufferSubData(target, offset, data_size, data);
GLuint buffer
The name of the buffer object.
void unbind()
Unbinds the buffer object from the target it was bound to.
void load(unsigned int num_elements, const T *data, GLenum usage)
Loads data into the buffer.
void create_empty(unsigned int num_elements, GLenum usage)
Creates an empty buffer.
bool store(unsigned int offset, unsigned int size, void *storage)
Writes the contents of the buffer object into the storage.
void bind()
Binds the buffer object to the target it was previously bound to or GL_ARRAY_BUFFER if the buffer has...
bool replace_partial_data(unsigned int offset, const std::vector< T > &data)
Overwrites data in the buffer starting at the specified offset.
void load(const std::vector< T > &data, GLenum usage)
Loads data into the buffer.
void unmap()
Releases the mapping of a buffer object's data store into the client's address space.
void replace_data(const std::vector< T > &data)
Overwrites all data in a vertex buffer.
bool copy(Buffer *source, unsigned int read_offset, unsigned int write_offset, unsigned int size)
Copies data from another buffer object.
void * map(GLenum access)
Maps all of a buffer object's data into the client's address space.
void replace_data(const T *data, unsigned int number_elements)
Overwrites all data in the buffer.
void bind(GLenum target)
Binds the buffer object to a target.
bool copy(Buffer &source, unsigned int read_offset, unsigned int write_offset, unsigned int size)
Copies data from another buffer object.
bool replace_partial_data(unsigned int offset, const T *data, unsigned int number_elements)
Overwrites data in the buffer starting at the specified offset.
void bind(GLenum target, unsigned int index)
Binds the buffer object to an indexed buffer target.
unsigned int size
Size of the buffer in bytes.
Buffer(GLenum target=GL_ARRAY_BUFFER)
unsigned int num_elements
Number of elements contained in the buffer.