OpenGL 3: glBindVertexArray invalidates GL_ELEMENT_ARRAY_BUFFER

試著忘記壹切 提交于 2019-12-03 08:14:40

Vertex Array Objects encapsulate all of the state* necessary to render vertex data. Therefore, they must encapsulate what buffers you associated with attributes (via glVertexAttribPointer), GL_ELEMENT_ARRAY_BUFFER (needed for glDrawElement* calls), and so forth.

However, I still feel a little puzzled by the fact that I couldn't find any mention of this side-effect in the docs.

The specification clearly explains this, though it requires understanding how the spec works to see how.

OpenGL is a collection of state, which means that all OpenGL functions (except those that actually render something) modify OpenGL state. When you call glVertexAttribPointer, this function conceptually modifies some piece of internal OpenGL state.

OpenGL objects are defined by which pieces of OpenGL state they encapsulate. Thus, if a function modifies the state encapsulated by an object, then that function modifies the object itself. Binding an object means replacing the current pieces of state that they encapsulate with the current state of that object.

The ARB_vertex_array_object specification defines VAOs based on what state they encapsulate. It basically points at one of the OpenGL state tables and says, "VAOs are all of that." The core 3.x version of this functionality actually modifies the state tables to make it a bit more clear (same behavior, slightly different explanation thereof):

OpenGL 3.3 specification, section 2.10:

The resulting vertex array object is a new state vector, comprising all the state values listed in tables 6.4 and 6.5.

I'm not going to reprint tables 6.4 and 6.5; you can look them up yourself. But they clearly include GL_ELEMENT_ARRAY_BUFFER_BINDING and the various GL_VERTEX_ATTRIB_ARRAY_BUFFER_BIDNING (which are buffer objects).

* Note: VAOs do not contain the state set by the glVertexAttrib functions. These can affect the rendering if an attribute array is not enabled.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!