PDA

View Full Version : Multiple vba's, single buffer



paradoxresolved
02-15-2016, 10:56 AM
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?

GClements
02-15-2016, 12:22 PM
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.

paradoxresolved
02-15-2016, 12:45 PM
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_verti ces);
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.

GClements
02-15-2016, 01:15 PM
Are vba1 and vba2 now geared up to be used?
Yes.

If so, what happens if I delete vba1? Does this affect the data in buffer_for_mesh_vertices?
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.



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.

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.

paradoxresolved
02-15-2016, 01:28 PM
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. :(

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.

GClements
02-15-2016, 03:53 PM
Can you give me an example? I don't quite follow.
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.

paradoxresolved
02-15-2016, 06:17 PM
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.

GClements
02-15-2016, 10:03 PM
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.