7

In OpenGL (OpenGL ES 2.0 in particular), is glVertexAttribPointer required to be called each time a new VBO is bound?

For example:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer1);

glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,position));
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,color);
glEnableVertexAttribArray(ATTRIB_COLOR);

glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

// If vertexBuffer2 has the exact same attributes as vertexBuffer1
// is this legal or do the calls to glVertexAttribPointer (and glEnableVertexAttribArray)
// required again?

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer2);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

I know that there are VAOs that address not having to call glVertexAttribPointer but I'm wondering if this is ok to do in OpenGL?

OpenUserX03
  • 1,440
  • 1
  • 13
  • 20

1 Answers1

9

If you did this:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer1);

glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,position));
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,color);
glEnableVertexAttribArray(ATTRIB_COLOR);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

Your code would work just fine. What all of the gl*Pointer calls do is look at what is stored in GL_ARRAY_BUFFER at the time the function is called, and create an association between that buffer and that attribute. So the only way to change what buffer gets used for an attribute is to call gl*Pointer.

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829