Do I have to use glGenVertexArray in opengl

1.5k views Asked by At
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

As you can see from the above code, some tutorials use this before using opengl. But a tutorial that I am following, directly uses glGenBuffers method and it works just fine. Hence I am totally confused what glGenVertexArrays. It is sad that it defines how buffer is used, but noone says in what way. Can you help me understand ?

3

There are 3 answers

0
datenwolf On

It depends on the OpenGL version. Beginning with the OpenGL-3 core profiles having a VAO is mandatory. To stay compatible with older code most developers just throw in the snippet you found there at the beginning to have a VAO lying around to catch all the VBO state.

0
Andon M. Coleman On

Vertex Array Objects (VAOs) are state containers.

In older versions of GL, things like glVertexAttribPointer (...) were global state. However, beginning with GL3 they became tied to VAOs.

Compatibility profiles still allow you to treat vertex array states as global state, but 3.1 (without the GL_ARB_compatibility pseudo-extension) and 3.2+ core profiles require you create and have a non-zero VAO bound any time you make a call like glVertexAttribPointer (...) or glDrawArrays (...). Otherwise, you wind up with a GL_INVALID_OPERATION error.

0
ratchet freak On

From openGL 3.2+ using a VAO is mandatory unless you use a compatibility profile.

Each VAO stores the index buffer (ELEMENT_ARRAY_BUFFER) and per attribute bindings.

In one of my other answer I provided a portion of the state stored in each VAO.