5

I need help to see the trade-offs between them.

  • It looks to me that glDrawElements() needs to get the index-data "live" as a parameter.
  • On the other side if I use VAOs then during startup I buffer the data and the driver might decide to put it on the GPU, then during rendering I only bind the VAO and call glDrawArrays().

Is there no way to combine the advantages? Can we buffer the index-data too?

And how would that look in the vertex shader? Can it use the index and look it up in the vertex positions array?

ben
  • 452
  • 3
  • 10

1 Answers1

5

This information is really a bit hard to find, but one can use glDrawElements also in combination with a VAO. The index data can then (but doesn't have to) be supplied by a ELEMENT_ARRAY_BUFFER. Indexing works then as usual, one does not have to do anything special in the vertex shader. OpenGL ensures already that the indices are used in the correct way during primitiv assembly.

The spec states to this in section 10.3.10:

DrawElements, DrawRangeElements, and DrawElementsInstanced source their indices from the buffer object whose name is bound to ELEMENT_- ARRAY_BUFFER, using their indices parameters as offsets into the buffer object

This basically means, that whenever a ELEMENT_ARRAY_BUFFER is bound, the indices parameter is used as an offset into this buffer (0 means start from the beginning). When no such buffer is bound, the indices pointer specifies the address of a index array.

BDL
  • 18,169
  • 14
  • 45
  • 47
  • 3
    Since the question is about VAOs, it's also worth mentioning that the `GL_ELEMENT_ARRAY_BUFFER` binding is part of the VAO state. So you can bind the buffer once during VAO setup, and don't have to explicitly bind it again before each draw call. That will then happen as part of binding the VAO. – Reto Koradi Jun 23 '16 at 15:27