I can't seem to find this information on the OpenGL online documentation and it's been bugging me for a while. The gist of the question is "how/when is state saved".
For instance let's consider this fairly standard VBO initialization:
GLuint vaoID, vboID; glGenVertexArrays(1, &vaoID); // Create our Vertex Array Object glBindVertexArray(vaoID); glGenBuffers(1, &vboID); // Generate our Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, vboID); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices.front(), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer glEnableVertexAttribArray(0);
and Render hook (assume indexCount is initialize properly when the mesh is loaded and there is an associated index buffer object, yada yada)
glBindVertexArray( renderInfo.vbos.vaoID ); glDrawElements(GL_TRIANGLES, renderInfo.indexCount, GL_UNSIGNED_INT, (GLvoid*)0); glBindVertexArray( 0 );
So this creates a Vertex Array Object with an associated Vertex Buffer Object that holds my vertex data. This works and I'm happy.
My question is, what if I want to change the data that is passed in the Vertex Buffer at runtime (say maybe I have several lists of vertices that are pre-transformed to specific animation keyframe positions or something; I'm not doing that but it's an example that's easily digestible). Can I do that? How do I do that? Is the state set in glVertexAttribPointer in the above invocation somehow "saved" to the vertex array object?
Would the "re-binding" look something like the following (assuming that vboID_2 is created just like vboID but with different data)
glBindVertexArray(vaoID); //does this work as re-binding?? glBindBuffer(GL_ARRAY_BUFFER, vboID_2); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); glDrawElements(GL_TRIANGLES, renderInfo.indexCount, GL_UNSIGNED_INT, (GLvoid*)0); glBindVertexArray( 0 );
Hopefully this question is clear, if long-winded. Let me know if I need to clarify anything...