每次调用glBindBuffer时都需要glVertexAttribPointer吗?

时间:2011-10-01 02:00:09

标签: opengl opengl-es opengl-es-2.0

在OpenGL(特别是OpenGL ES 2.0)中,每次绑定一个新的VBO时都需要调用glVertexAttribPointer吗?

例如:

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);

我知道有些VAO可以解决不必调用glVertexAttribPointer但是我想知道在OpenGL中这样做是否可行?

1 个答案:

答案 0 :(得分:9)

如果你这样做了:

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);

您的代码可以正常运行。所有gl*Pointer调用的作用是查看函数调用时GL_ARRAY_BUFFER 中存储的内容,并在该缓冲区与该属性之间创建关联。因此,更改用于属性的缓冲区的唯一方法是调用gl*Pointer