Advanced Data
Advanced-OpenGL/Advanced-Data
Throughout most chapters we've been extensively using buffers in OpenGL to store data on the GPU. This chapter we'll briefly discuss a few alternative approaches to managing buffers.
A buffer in OpenGL is, at its core, an object that manages a certain piece of GPU memory and nothing more. We give meaning to a buffer when binding it to a specific
So far we've been filling the buffer's memory by calling NULL
as its data argument, the function would only allocate memory and not fill it. This is useful if we first want to reserve a specific amount of memory and later come back to this buffer.
Instead of filling the entire buffer with one function call we can also fill specific regions of the buffer by calling
glBufferSubData (GL_ARRAY_BUFFER, 24, sizeof(data), &data); // Range: [24, 24 + sizeof(data)]
Yet another method for getting data into a buffer is to ask for a pointer to the buffer's memory and directly copy the data in memory yourself. By calling
float data[] = {
0.5f, 1.0f, -0.35f
[...]
};
glBindBuffer (GL_ARRAY_BUFFER, buffer);
// get pointer
void *ptr = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY);
// now copy data into memory
memcpy(ptr, data, sizeof(data));
// make sure to tell OpenGL we're done with the pointer
glUnmapBuffer (GL_ARRAY_BUFFER);
By telling OpenGL we're finished with the pointer operations via
Using
Batching vertex attributes
Using
What we could also do is batch all the vector data into large chunks per attribute type instead of interleaving them. Instead of an interleaved layout 123123123123
we take a batched approach 111122223333
.
When loading vertex data from file you generally retrieve an array of positions, an array of normals and/or an array of texture coordinates. It may cost some effort to combine these arrays into one large array of interleaved data. Taking the batching approach is then an easier solution that we can easily implement using
float positions[] = { ... };
float normals[] = { ... };
float tex[] = { ... };
// fill buffer
glBufferSubData (GL_ARRAY_BUFFER, 0, sizeof(positions), &positions);
glBufferSubData (GL_ARRAY_BUFFER, sizeof(positions), sizeof(normals), &normals);
glBufferSubData (GL_ARRAY_BUFFER, sizeof(positions) + sizeof(normals), sizeof(tex), &tex);
This way we can directly transfer the attribute arrays as a whole into the buffer without first having to process them. We could have also combined them in one large array and fill the buffer right away using
We'll also have to update the vertex attribute pointers to reflect these changes:
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(sizeof(positions)));
glVertexAttribPointer (
2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(sizeof(positions) + sizeof(normals)));
Note that the stride
parameter is equal to the size of the vertex attribute, since the next vertex attribute vector can be found directly after its 3 (or 2) components.
This gives us yet another approach of setting and specifying vertex attributes. Using either approach is feasible, it is mostly a more organized way to set vertex attributes. However, the interleaved approach is still the recommended approach as the vertex attributes for each vertex shader run are then closely aligned in memory.
Copying buffers
Once your buffers are filled with data you may want to share that data with other buffers or perhaps copy the buffer's content into another buffer. The function
void glCopyBufferSubData (GLenum readtarget, GLenum writetarget, GLintptr readoffset,
GLintptr writeoffset, GLsizeiptr size);
The readtarget
and writetarget
parameters expect to give the buffer targets that we want to copy from and to. We could for example copy from a VERTEX_ARRAY_BUFFER buffer to a VERTEX_ELEMENT_ARRAY_BUFFER buffer by specifying those buffer targets as the read and write targets respectively. The buffers currently bound to those buffer targets will then be affected.
But what if we wanted to read and write data into two different buffers that are both vertex array buffers? We can't bind two buffers at the same time to the same buffer target. For this reason, and this reason alone, OpenGL gives us two more buffer targets called GL_COPY_READ_BUFFER and GL_COPY_WRITE_BUFFER. We then bind the buffers of our choice to these new buffer targets and set those targets as the readtarget
and writetarget
argument.
size
from a given readoffset
and writes it into the writetarget
buffer at writeoffset
. An example of copying the content of two vertex array buffers is shown below:
glBindBuffer (GL_COPY_READ_BUFFER, vbo1);
glBindBuffer (GL_COPY_WRITE_BUFFER, vbo2);
glCopyBufferSubData (GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 8 * sizeof(float));
We could've also done this by only binding the writetarget
buffer to one of the new buffer target types:
float vertexData[] = { ... };
glBindBuffer (GL_ARRAY_BUFFER, vbo1);
glBindBuffer (GL_COPY_WRITE_BUFFER, vbo2);
glCopyBufferSubData (GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 8 * sizeof(float));
With some extra knowledge about how to manipulate buffers we can already use them in more interesting ways. The further you get in OpenGL, the more useful these new buffer methods start to become. In the next chapter, where we'll discuss