32

I am confused with the point in generating/creating a Vertex Array Object (VAO) with:

glGenVertexArrays(GLsizei n, GLuint *arrays);

and

glBindVertexArray(GLuint);

Because I can still create a buffer object, say for vertices, and describe that buffer object with glVertexAttribPointer and glEnableVertexAttribArraywithout ever creating a VAO.

My question is if you do not have to actually create the VAO to describe the data in a buffer object, why would such sources as the OpenGL SuperBible 5ed include a call to create a VAO when creating VBOs? Are they only used for more advanced topics I have yet to discover, am I totally confused?

Also I first encountered this question when reading wikipedias entry on VBOs and their sample code includes no calls to glGenVertexArrays() but they still describe the data with glVertexAttribPointer().Wiki VBO entry -- Example where VAOs are created for what reason?

zython
  • 966
  • 1
  • 19
  • 41
bluth
  • 481
  • 1
  • 5
  • 10

3 Answers3

21

Performance improvements.

In many cases, setting up your attributes on the host require a high number of API calls, and an important amount of validation inside the implementation.

Doing all those once, and using the VAO allow that work to be amortized.

See, for example, Graham Sellers' analysis for actual data.

Bahbar
  • 16,994
  • 38
  • 60
  • They have a flaw though - they can't be shared across contexts, making them pretty useless outside of a gaming scenario. – cmannett85 May 11 '11 at 21:32
  • 2
    And also the use of no VAOs is deprecated style in GL4, I think. So besided their advantages, you will also have to use them if you want to be forward compatible. – Christian Rau May 11 '11 at 23:52
  • Well the default VAO of name 0 can still be used without generating a VAO so you don't necessarily have to in 4.1, explicitly anyways. But, of course now that I understand better, you would. – bluth May 12 '11 at 08:07
  • 4
    @bluth "Well the default VAO of name 0 can still be used without generating a VAO ": No, it cannot. Not according to core OpenGL 3.1 and above. If you're in compatibility mode, then you can do it. But not in core OpenGL. Note that NVIDIA doesn't implement this part of the spec correctly and allows 0 to function as a VAO. But AMD does follow the spec, so if you ask for a core OpenGL context on their hardware, you must create and bind a VAO somewhere in your program. – Nicol Bolas Jun 16 '11 at 10:04
17

Say you are building an object that needs three VBOs (different data for shading, maybe positions, normals and some fancy effect parameter). Each time you would draw this object, you would have to bind all these VBOs and set any additional parameters. If you use a VAO, you only have to use one call (binding the vertex array), this will set up the entire environment for the specific object.

Orka
  • 1,095
  • 1
  • 9
  • 15
7

The answer is not as hard as you imagined. Just think of it as a funny metaphor; VertexArrayObject is the boss and it manages several staff members which are VetexBufferObjects.

Keith Smiley
  • 54,143
  • 12
  • 89
  • 105
ShannonLiu
  • 101
  • 1
  • 1