Search code examples
openglgraphicsmeshopengl-3opengl-4

where is the correct place to glEnableVertexAttribArray


Quick question: I've always seen VAO meshes enable theire vertex attribute arrays inside the 'draw' call in such a way:

void draw() {
    bindVAO();
    glEnableVertexAttribArray(0 ... 3);
    glDrawElements(...);
    glDisableVertexAttribArray(0 ... 3);
    unbindVAO();
}

I've also noticed that I could outright omit the second half down from the Draw Elements call as such:

void draw() {
    bindVAO();
    glEnableVertexAttribArray(0 ... 3);
    glDrawElements(...);
}

Similarly I moved the glEnableVertexAttribArray() calls to the mesh generation/loading function, and everything seems to run fine.

I was wondering if all of this was legal, and if there was a reason why many pieces of sample code (or other things of such nature) have the attribute arrays enabled and disabled next to the draw call (surely doing it every frame/draw cycle is a waste of time).


Solution

  • If you're using VAOs and within a single VAO do not change the buffer bindings, then tying glEnableVertexAttribArray to VAO creation and update is perfectly fine, and recommended.

    However if no VAO is used or bindings within a VAO are changed dynamically, then placing glEnableVertexAttribArray close to the binding code (which is usually happening in drawing, then) is a better fit.