Vertex array object and unexpected behavior

Started by jon291, November 18, 2010, 09:48:59

Previous topic - Next topic


The problem I have is with what the program reports for the currently bound vertex buffer object (vbo) and element array buffer (eab) and whether an error is generated when I call glDrawElements.  To be clear, my code can render correctly, but I'm not sure why I have to make the method calls that I do to get it to render correctly.  The behavior I'm observing just plain doesn't make sense to me, but since this is my first time working with vertex array objects (vao) it could be that I just misunderstand something about how they work.  If it isn't clear what the issue might be from what I described below, I can provide the actual code in a follow up post.

When constructing the vao, I start with nothing bound, then bind the vao and then the vbo and eab.  After doing all the glVertexAttribPointer and glEnableVertexAttribArray calls, I unbind the vao.  Immediately after this initialization, the program enters a rendering loop.  I start by checking which buffers are currently bound with the following line:
System.out.println(GL11.glGetInteger(GL15.GL_ARRAY_BUFFER_BINDING)+", "+GL11.glGetInteger(GL15.GL_ELEMENT_ARRAY_BUFFER_BINDING));

The output of this line gives a non-zero int for the array buffer binding and zero for the eab binding, which I presume means I have a vbo bound but no eab.  This makes sense to me because the specs say the vbo is not affected by binding/unbinding of the vao, but the eab binding is carried with the vao.  I then bind the vao and run the same check and get non-zero ints for both bindings, which is again what I would expect and should indicate I have an eab bound now.

I then immediately call glDrawElements and the program terminates giving the error "org.lwjgl.opengl.OpenGLException: Cannot use offsets when Element Array Buffer Object is disabled," which I don't understand.  Is it ever correct for this error to be thrown if I get a non-zero output from GL11.glGetInteger(GL15.GL_ELEMENT_ARRAY_BUFFER_BINDING)?

What is weird is that the code runs fine if I explicitly bind an arbitrary eab at the top of the rendering loop.  I'm rendering three different "shapes" (triangle, square, and a set of randomly dispersed points if it matters to you) in my test, each with its own vao, vbo, and eab.  I can bind the eab of any given one of the triangles at the top of the loop and all three shapes render correctly.  See my pseudocode below:
bind eab of any one of the shapes (code fails without this)

bind vao of shape 1
call glDrawElements for shape 1
unbind vao of shape 1

bind vao of shape 2
call glDrawElements for shape 2
unbind vao of shape 2

bind vao of shape 3
call glDrawElements for shape 3
unbind vao of shape 3

If I include an explicit unbind of the eab before one of the vao binds, the program terminates in the same manner.  For example, including the call GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,0) before the vao binding for the 3rd shape causes the OpenGLException to be thrown when calling glDrawElements for the 3rd shape even though I have a non-zero element array buffer binding after having bound the vao for the 3rd shape.