FTLPhysicsGuy
FTLPhysicsGuy

Reputation: 1065

Use one Vertex Buffer Object for glDrawArrays (ignoring IBO) and glDrawElements (with an IBO)

I've got a Vertex Buffer Object (VBO) that I typically use (with an appropriate VAO) to draw points with lines between them via two calls to glDrawArrays (one with GL_POINTS and one with GL_LINE_STRIP).

Now I'm interested in ditching the GL_LINE_STRIP and drawing lines between specific indices in the VBO by defining an Index Buffer Object (IBO) and using glDrawElements.

However, when I create the one VBO and then create and fill the IBO, my call to glDrawArrays(GL_POINTS,...) does nothing! If I simply comment out the line that calls glBufferData(GL_ELEMENT_ARRAY_BUFFER,...), the glDrawArrays call works again. I assumed that adding an IBO would simply be ignored by the glDrawArrays call, but apparently it's not.

So, is it possible to use one VBO to render using a glDrawArrays and (via an IBO) glDrawElements?

One bit of confusion for me is how the IBO is linked to the VBO and/or the VAO and therefore to the current draw command. For example, is it possible to use one VBO and then bind to two different IBOs for different glDrawElements calls? (e.g., what if I want to draw triangles with indices 0, 1, 2 and 1, 2, 3; but I then want to draw lines between indices 0, 1 and 1, 3).

The steps I'm taking in code to connect the IBO with my VAO are as follows:

  1. Create the buffer IDs for both the VBO and IBO
  2. Bind and fill the VBO
  3. Create the ID for the VAO
  4. Bind the VAO (glBindVertexArray) and setup my attribute(s) (using glEnableVertexAttribArray, glVertexAttribFormat, glVertexAttribBinding and glBindVertexBuffer)
  5. Bind and fill the IBO
  6. Un-bind the VAO (glBindVertexArray(0))

Is there something else I need to use (or a different order of actions) to associate the IBO with the VAO?

Thanks.


EDIT - I Found my coding error:

When I generated my buffers, I generated 4 at once to a single array (for use with this and other OpenGL buffers in the code). However, when I assigned the IDs to specific variable names, I used index 0 (zero) for both the VBO and the IBO (then using indices 2 and 3 for other buffers). Basically, I accidentally skipped index 1 and therefore overwrote my VBO when I filled my IBO. As noted in a comment below, this exercise at least helped solidify my understanding of how IBOs are used in the context of VBOs. Thanks.

Upvotes: 1

Views: 626

Answers (1)

Rabbid76
Rabbid76

Reputation: 211278

The Index Buffer (ELEMENT_ARRAY_BUFFER) binding is stored within the Vertex Array Object. When glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO) is called the element buffer object ID is stored in the currently bound Vertex Array Object. Therefore the VAO must be bound before the element buffer with glBindVertexArray(VAO).

In compared to the Index Buffer, the Vertex Buffer binding (ARRAY_BUFFER) is a global state.
Each attribute which is stated in the VAOs state vector may refer to a different ARRAY_BUFFER. When glVertexAttribPointer is called the buffer which is currently bound to the target ARRAY_BUFFER, is associated to the specified attribute index and the ID of the object is stored in the state vector of the currently bound VAO.

Upvotes: 1

Related Questions