opengl-tutorials/ogl

All tutorials use VAOs in a very inefficient way

Closed this issue · 1 comments

In all tutorials, the VAO setup code (glEnableVertexAttribArray, glVertexAttribPointer) is located inside the main loop although the bindings never change. Even worth, this kind of setup code with enabling/disabling attributes in each frame goes completely against what VAOs are made for and teaches people to issue a lot of unnecessary OpenGL calls.

The better way would be to setup the VAO before the main loop and only use glBindVertexArray with the correct VAO in each frame. (And technically even that would be unnecessary unless there are multiple VAOs/DrawCalls involved).

The problem is not that the solution shown here would be wrong (it actually is valid code) but a lot of people learn writing OpenGL code from your tutorial due to the prominent placement on google. They then later use that code in their own projects and when having a larger number of objects, doing the setup code in each frame will definitely be a problem.

Skipping VAOs entirely was a deliberate decision.

First, I have yet to see a bench that makes VAOs more efficient (it may have changed since the tutorials were written).

Secondly, it makes the rendering loop more intuitive - there is one less abstraction layer to understand.

But most importantly, the tutorials were designed to be portable across many OpenGL versions - including OpenGL 2, which still has a important user base on old hardware. It's also not supported in ES until 3.0.

I also chose to re-bind everything each frame because that's closer to what needs to be done in real application, where you can't just assume that only one objects gets drawn.

So, when I finally ditch the 2.1 port (still useful to many people), or if there's a compelling reason to change all the tutorials, I'll update them, but in the meantime, I think it's a good compromise.