I was certain that if you bind a buffer via glBindBuffer()
, you can safely assume that it stays bound, until the target is rebound through another call to glBindBuffer()
. I was therefore quite surprised when I discovered that calling glBindVertexArray()
sets the buffer bound to the GL_ELEMENT_ARRAY target to 0.
Here's the minimal C++ sample code:
GLuint buff;
glGenBuffers(1, &buff);
std::cout << "Buffer is " << buff << "\n";
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buff);
GLuint vao;
glGenVertexArrays(1, &vao);
GLint bound_buff;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &bound_buff);
std::cout << "Bound before glBindVertexArray: " << bound_buff << "\n";
glBindVertexArray(vao);
// ^- an implicit glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); ?
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &bound_buff);
std::cout << "Bound after glBindVertexArray: " << bound_buff << "\n";
I run this code immediately after initializing an OpenGL 3.2 device context and get the following output:
Buffer is 1
Bound before glBindVertexArray: 1
Bound after glBindVertexArray: 0
The GL_ARRAY_BUFFER on the other hand is not changed by the call. I checked the OpenGL 3.2 spec (2.10) for glBindVertexArray
and found no mention of that unexpected side effect.
- Is this behavior compliant with the Spec?
- If so, what other side effects can be expected from a call to
glBindVertexArray
? - What is the rationale behind this?
I tested this on an nvidia card on a Win XPx64 machine with the 296.10 WHQL driver. A quick test on OS X Lion with an nvidia GT330M gave the same results.