Advertisement

Index Buffer & Vertex Array Binding

Started by September 16, 2019 12:29 PM
1 comment, last by Green_Baron 5 years, 3 months ago

My question is regarding element array buffer (IBO) binding. 

I have heard it said that (providing a VAO is already bound) an IBO binding will be saved to the current state of the VAO. Does this mean that instead of binding the IBO henceforth, we can just bind the VAO instead? i.e. as long as the VAO is bound before calling glDrawElements(), the draw call will always work?

I have provided an annotated code example below, for clarity:


// ...

// VBO:
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glNamedBufferData(VBO, sizeof(data), &data, draw_method)

// VAO:
glGenVertexArrays(1, VAO);
glBindVertexArray(VAO);

// Enable and input attributes etc... 

// IBO:
glGenBuffers(1, IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glNamedBufferData(IBO, sizeof(data), &data, draw_method)
/* index buffer binding saved to VAO state?
   as long as VAO is bound prior to draw call, 
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO)
   does not need to be called again? */
  
// ...
  
while (!display.WindowShouldClose())
{
	display.ClearBuffers();
  
  	// ...
  
  	glBindVertexArray(VAO); 
  	// index buffer binding not necessary? Only VAO binding required?
    	glDrawElements(GL_TRIANGLES, elements, GL_UNSIGNED_SHORT, nullptr);
  
    	// ...
  
 	display.SwapBuffers();
}

 

Depends.

As long as there is only one vertex array and a corresponding index buffer like in most of the simple examples we can find out there, you can just leave them bound in your loop.

Sooner or later your scenes will be more complex and you will have many buffers and their corresponding pipelines (shader programs with their states), your drawable objects. Then you will have to bind vertex arrays and index buffers separately depending on what you're up to draw and what the pipelines expect.

In your example, if you had different objects with different indices, let's say a sphere and a box, you'd bind the sphere array and indices and draw the sphere, then you'd bind the box data and draw that (arbitrary sequence, if there were many objects spheres you'd build a spacial structure and sort by type and from front to back, and/or use indirect buffers or instancing or so).

Red and blue book explain draw calls and their buffer objects quite well. In the end, it depends on your application. There is no shortfall of possibilities ?

This topic is closed to new replies.

Advertisement