Multiple vba's, single buffer

Is it possible to bind a single GL_ARRAY_BUFFER to multiple vba’s, and if so what happens if you delete one of the vba’s?

“VBA”?

A call to glVertexAttribPointer() stores the name (handle, ID) of the buffer which is bound to GL_ARRAY_BUFFER as part of the data for that attribute. The buffer can be unbound afterwards. A single buffer may be used as the data source for multiple attributes.

Buffers exist until either they’re deleted explicitly with glDeleteBuffers() or until all of the contexts which contain the buffer are deleted.

I don’t think I made myself clear. My apologies.

Let me elaborate with pseudo-code.

I create a buffer this way:


glGenBuffers(1, &mybuffer_for_mesh_vertices);
glBindBuffer(GL_ARRAY_BUFFER,buffer_for_mesh_vertices);
glBuffersData(GL_ARRAY_BUFFER, number_of_3f_entries*sizeof(Vector_3f), first_3f_address, GL_STATIC_DRAW);

// Now use the data like this...
glGenVertexArray(1, &vba1);
glBindVertexArray(vba1);
glBindBuffer(GL_ARRAY_BUFFER, buffer_for_mesh_vertices);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Repeat for another buffer


glGenVertexArray(1, &vba2);
glBindVertexArray(vba2);
glBindBuffer(GL_ARRAY_BUFFER, buffer_for_mesh_vertices);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Repeat for another buffer

Are vba1 and vba2 now geared up to be used? If so, what happens if I delete vba1? Does this affect the data in buffer_for_mesh_vertices?

I’d like to create the buffers separate, then mix and match them in vertex arrays, but I’m getting a crash point on glDrawElements.

Yes.

No.

Buffers are just blocks of storage. They aren’t “owned” by the VAOs which reference them. You can use the same buffer with multiple VAOs. You can bind it to different attachment points at different times. Or at the same time.

[QUOTE=paradoxresolved;1281523]
I’d like to create the buffers separate, then mix and match them in vertex arrays, but I’m getting a crash point on glDrawElements.[/QUOTE]

Bear in mind that a VAO doesn’t store the current GL_ARRAY_BUFFER binding, only the bindings for individual attributes (i.e. the buffer which was bound to that target at the time of the last glVertexAttribPointer() call for each attribute). However, it does store the current GL_ELEMENT_ARRAY_BUFFER binding.

Bear in mind that a VAO doesn’t store the current GL_ARRAY_BUFFER binding, only the bindings for individual attributes (i.e. the buffer which was bound to that target at the time of the last glVertexAttribPointer() call for each attribute). However, it does store the current GL_ELEMENT_ARRAY_BUFFER binding.

Can you give me an example? I don’t quite follow.

… and thank you for your help so far. I found diff. eq. easier to learn than OpenGL. :frowning:

Edit: I really hope this is the problem I’m facing: Is glDeleteBuffers(1,&vba1) the wrong way to delete a vao?

Edit2: Oh wow. Yes. I definitely is. glDeleteVertexArrays is how you do it. Facepalm There was my bug.

[QUOTE=paradoxresolved;1281527]
Can you give me an example? I don’t quite follow.[/QUOTE]
When you call glBindBuffer(GL_ARRAY_BUFFER, buffer), the buffer is stored as “global” state (stored in the object representing the current OpenGL context). Essentially, each context has one “variable” to store the current GL_ARRAY_BUFFER binding. The same is true of all other buffer bindings except for GL_ELEMENT_ARRAY_BUFFER.

When you call glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer), the buffer is stored as part of the state for the current VAO, rather than as global state as is the case for the other bindings. Binding a different VAO will cause the current GL_ELEMENT_ARRAY_BUFFER binding to be the one in the new VAO. If you call glDrawElements(), the element array buffer stored in the current VAO is used as the source of the vertex indices.

I really hope this is the problem I’m facing: Is glDeleteBuffers(1,&vba1) the wrong way to delete a vao?

glDeleteBuffers() is for buffers. Use glDeleteVertexArrays() to delete VAOs.

Hmmm… Shouldn’t all the buffers be bound using GL_ELEMENT_ARRAY_BUFFER to ensure that the VAO has the proper buffers bound when switching back and forth between VAOs? My current program is only using a single set of buffers so far, so I might not notice if the VAOs weren’t remembering the buffers at this stage.

Buffers containing vertex attributes should be bound to GL_ARRAY_BUFFER prior to the relevant glVertexAttribPointer() call. Buffers containing vertex indices should be bound to GL_ELEMENT_ARRAY_BUFFER.