Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should glVertexAttribPointer be called?

Tags:

opengl

It's not obvious from the documentation when glVertexAttribPointer should be called. It looks like it's part of VBO initialisation, but I notice example code calling it during rendering.

glVertexAttribPointer(vertexAttributeId, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), reinterpret_cast<const GLvoid*>(offsetof(Vertex2D, m_x)));

Should glVertexAttribPointer be called during initialisation of a GL_ARRAY_BUFFER or should it be called during rendering (after a call to glBindBuffer)?

like image 682
Mark Ingram Avatar asked Jun 17 '13 14:06

Mark Ingram


People also ask

What does glVertexAttribPointer do?

glVertexAttribPointer specifies the location and data format of the array of generic vertex attributes at index index to use when rendering.

What is glEnableVertexAttribArray?

Description. glEnableVertexAttribArray enables the generic vertex attribute array specified by index . glDisableVertexAttribArray disables the generic vertex attribute array specified by index . By default, all client-side capabilities are disabled, including all generic vertex attribute arrays.

What is a VAO?

A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information for a complete rendered object. In our example this is a diamond consisting of four vertices as well as a color for each vertex.

What is a vertex attribute pointer?

pointer. Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target. The initial value is 0.


1 Answers

The function glVertexAttribPointer specifies the format and source buffer (ignoring the deprecated usage of client arrays) of a vertex attribute that is used when rendering something (i.e. the next glDraw... call).

Now there are two scenarios. You either use vertex array objects (VAOs) or you don't (though not using VAOs is deprecated and discouraged/prohibited in modern OpenGL). If you're not using VAOs, then you would usually call glVertexAttribPointer (and the corresponding glEnableVertexAttribArray) right before rendering to setup the state properly. If using VAOs though, you actually call it (and the enable function) inside the VAO creation code (which is usually part of some initialization or object creation), since its settings are stored inside the VAO and all you need to do when rendering is bind the VAO and call a draw function.

But no matter when you call glVertexAttribPointer, you should bind the corresponding buffer right before (no matter when that was actually created and filled), since the glVertexAttribPointer function sets the currently bound GL_ARRAY_BUFFER as source buffer for this attribute (and stores this setting, so afterwards you can freely bind another VBO).

So in modern OpenGL using VAOs (which is recommended), it's usually similar to this workflow:

//initialization glGenVertexArrays glBindVertexArray  glGenBuffers glBindBuffer glBufferData  glVertexAttribPointer glEnableVertexAttribArray  glBindVertexArray(0)  glDeleteBuffers //you can already delete it after the VAO is unbound, since the                 //VAO still references it, keeping it alive (see comments below).  ...  //rendering glBindVertexArray glDrawWhatever 

When not using VAOs it would be something like that:

//initialization glGenBuffers glBindBuffer glBufferData  ...  //rendering glBindBuffer glVertexAttribPointer glEnableVertexAttribArray glDrawWhatever 
like image 113
Christian Rau Avatar answered Sep 23 '22 11:09

Christian Rau