# Error when trying to use VBO "array vertex_buffer_object must be disabled to call this method"

EDIT

I have effectivley re-wrote this question in order to greatly imrpove its quality - see revision logs if you must

I have narrowed down my problem to the initialisation phase of my program, when I am trying to create my vertex buffer. The code that I am currently using is...

vaoID = new int[1];
gl.glGenVertexArrays(1, vaoID, 0);
gl.glBindVertexArray(vaoID[0]);
vboID = new int[1];
gl.glGenBuffers(1, vboID, 0);
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboID[0]);
FloatBuffer vData = FloatBuffer.allocate(vertexData.length);
vData.put(vertexData);
vData.rewind();
gl.glBufferData(GL4.GL_ARRAY_BUFFER, vData.capacity() * (Float.SIZE / 8), vData, GL4.GL_STATIC_DRAW);
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, 0); // With out this line my code throws the exception
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 4, GL.GL_FLOAT, false, 0, null); // This is the line where the exception is thrown
gl.glBindVertexArray(0);


So yes, at present my code is able to run with out crashing, but it must have a mistake some where as I am not able to get this to render. For the buffering the index data, I use this code, which does complete successfully when it runs

gl.glGenBuffers(1, bufferIDs, 0);
gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, bufferIDs[0]);
ShortBuffer iData = ShortBuffer.allocate(indexData.length);
iData.put(indexData);
iData.rewind();
indexCount = iData.capacity();
gl.glBufferData(GL4.GL_ELEMENT_ARRAY_BUFFER, indexCount * (Short.SIZE / 8), iData, GL4.GL_STATIC_DRAW);
gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, 0);


Then when it comes to drawing, I perform the following steps, and I don't think this really matters, as I am sure I am still not buffer correctly in the first place

// first I bind my VAO
gl.glBindVertexArray(vaoID[0]);
// then bind, draw, unbind the index buffer
gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, bufferIDs[0]);
gl.glDrawElements(GL4.GL_POINT, indexCount, GL4.GL_UNSIGNED_SHORT, 0);
gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, 0);
// finally I unbind the VAO
gl.glBindVertexArray(0);


I am fairly sure that this is the correct process for rendering buffered geometry. I have confirmed that my shader code is correct and valid by use of some immediate drawing commands, so I do I have full pipeline, it just seems I am not feeding data into it. As a point of interest, I also tried editing my vertex shader to set all points to (0,0,0,1) so that if any points where being passed in, I should see them, but this did not work, which I took to mean that I am not passing data into my shader in the first place.

Thanks for all the help so far.

• bufferID and bufferIDs? That is surely confusing... As for redundant code pasting, just put it on pastie.org, and if it becomes relevant to the question, you can inline it in your original post. Dec 5, 2011 at 18:24
• well, I have made a few changes to the code... so I will work on updating the question. Dec 6, 2011 at 9:48
• You only need to set up the vertex attributes once, and when the VAO is bound. The draw step should only consist of binding the VAO, and calling glDrawElements with the indices count (no other bindings). Dec 6, 2011 at 20:11
• GL_ELEMENT_ARRAY_BUFFER is part of the VAO. You should your element array in the VAO setup code, not in your rendering code. The VAO contains everything you need (vertex data-wise) to render with. Feb 5, 2012 at 16:54
• VAO's are kinda living their own life in terms of what buffers get bound: stackoverflow.com/questions/8973690/…
– arul
Feb 6, 2012 at 3:12

gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, 0); // With out this line my code throws the exception


That's unbinding your buffer object before the glVertexAttribPointer call. You need to have a buffer bound in order for that to be a reasonable call. It's the combination of having a buffer bound to GL_ARRAY_BUFFER and calling glVertexAttribPointer that sets up an association between a buffer object and a vertex attribute.

Once you've called glVertexAttribPointer, then you can unbind the buffer. But not until then.

gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, bufferIDs[0]);


GL_ELEMENT_ARRAY_BUFFER is part of the VAO's state (unlike GL_ARRAY_BUFFER). So you need to do it while your VAO is still bound to the context, not after.

Are you using a vertex buffer somewhere else in the code? Or using a third-party library that does?

I would try calling gl.glDisableClientState(GL2.GL_VERTEX_ARRAY); at each frame, just before gl.glEnableVertexAttribArray(0);.

• I have tried disabling GL_VERTEX_ARRAY but it makes no difference Dec 7, 2011 at 8:37

I am facing the same problem with VBO myself, so cannot help you much with it but I have one basic suggestion

    gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, 0);
// With out this line my code throws the exception


The above line is de-allocating your buffer so you are right. No data is being copied to the vbo. The drawing process needs to be bracketed within the calls

    gl.glEnableCLientState(GL2.GL_VERTEX_ARRAY);
//TODO Drawing Commands
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);


If someone can shade some more light on this issue it will be very helpful as it is very difficult for java users to get clear answers on VBO issues.

• -1: Welcome to GameDev.SE. This is not a discussion forum. The "Post Your Answer" button is used for answers, not commentary on the question. Your answer isn't really an answer, but a request for more information. Also, "The above line is de-allocating your buffer" No it isn't. It is merely unbinding the buffer object. Oh, and "The drawing process needs to be bracketed within the calls" No it does not. That's when you're using old glVertexPointer arrays; he's using generic vertex attributes. Jan 28, 2013 at 7:02