Im facing an issue which I believe to be VAO-dependant, but Im not sure..
I am not sure about the correct usage of a VAO, what I used to do during GL initialization was a simple
glGenVertexArrays(1,&vao)
followed by a
glBindVertexArray(vao)
and later, in my drawing pipeline, I just called glBindBuffer(), glVertexAttribPointer(), glEnableVertexAttribArray() and so on.. without caring about the initally bound VAO
is this a correct practice?
VAOs act similarly to VBOs and textures with regard to how they are bound. Having a single VAO bound for the entire length of your program will yield no performance benefits because you might as well just be rendering without VAOs at all. In fact it may be slower depending on how the implementation intercepts vertex attribute settings as they're being drawn.
The point of a VAO is to run all the methods necessary to draw an object once during initialization and cut out all the extra method call overhead during the main loop. The point is to have multiple VAOs and switch between them when drawing.
In terms of best practice, here's how you should organize your code:
This avoids the mess of binding/unbinding buffers and passing all the settings for each vertex attribute and replaces it with just a single method call, binding a VAO.
Robert's answer above worked for me when I tried it. For what it's worth here is the code, in Go, of using multiple Vertex Attribute Objects:
// VAO 1
Then in your main loop you can use them as such:
If you want to see the full source, it is available as a Gist and derived from the examples in go-gl:
https://gist.github.com/mdmarek/0f73890ae2547cdba3a7
Thanks everyone for the original answers, I had the same question as ECrownofFire.
No, that's not how you use VAO. You should use VAO in same way how you are using VBO or textures, or shaders. First set it up. And during rendering only Bind them, without modifying it.
So with VAO you do following:
See also these links:
Yes, this is perfectly legal and valid. Is it good? Well...
There has been some informal performance testing on this sort of thing. And it seems, at least on NVIDIA hardware where this was tested, the "proper" use of VAOs (ie: what everyone else advocated) actually is slower in many cases. This is especially true if changing VAOs does not change which buffers are bound.
No similar performance testing has taken place on AMD hardware, to my knowledge. In general, unless something changes with them, this is an acceptable use of VAOs.