[SOLVED] VBO error: array vertex_buffer_object must be disabled

I keep getting this error trying to get a basic VBO to work. Is this a common error?


Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: array vertex_buffer_object must be disabled to call this method


      gl.glGenBuffersARB(1, vertexVBO, 0)
      gl.glGenBuffersARB(1, indexVBO, 0)

      // bind VBOs for vertex array and index array
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexVBO(0))        // for vertex coordinates
      gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, triangleCount * 3 * 3 *
                         BufferUtil.SIZEOF_FLOAT, vertices, GL.GL_STATIC_DRAW_ARB)
      gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, indexVBO(0)) // for indices
      gl.glBufferDataARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, triangleCount * 3 *
                         BufferUtil.SIZEOF_SHORT, indices, GL.GL_STATIC_DRAW_ARB)


      

      // number of coordinates per vertex, the type of each coordinate, stride, and ptr offset
      gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices)

      ... fill in with data ...

      gl.glEnableClientState(GL.GL_VERTEX_ARRAY)             // activate vertex coords array
      gl.glDrawElements(GL.GL_TRIANGLES, triangleCount, GL.GL_UNSIGNED_SHORT, 0)
      gl.glDisableClientState(GL.GL_VERTEX_ARRAY)

      // bind with 0, so, switch back to normal pointer operation
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, 0)
      gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, 0)

When in trouble, simplify your problem. Don’t debug something in the middle of a complex app.

As a reference, use:
http://sscce.org/

Regarding your problem: you cannot use VBOs and VAs (vertex arrays) at the same time in OpenGL. To use one, you need to disable the other, like glBindBufferARB(…, 0).

Both JOGL and LWJGL are checking this for you, so it doesn’t trip the driver and crash your app with a segfault.

[quote]When in trouble, simplify your problem. Don’t debug something in the middle of a complex app.
[/quote]
This is easier said than done with OpenGL 3, and I wouldn’t call this a complex app. One has to go through a bit of code just to get something on the screen. I couldn’t find many JOGL VBO examples, so I grabbed a C++ one, which was apparently wrong. I’m going to try getting it to work with JOGL 2.0 (was using 1.1.1).

Based on some other example code, I have the following so far. It’s in scala, but should be easy enough to read. Do you see anything wrong with this?


    def display(drawable:GLAutoDrawable) {
      val gl = drawable.getGL().getGL3()
      gl.glClear(GL.GL_COLOR_BUFFER_BIT)
      gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4)
    }

      val gl = drawable.getGL().getGL3()

      gl.glGenBuffers(1, bufferObject, 0)
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferObject(0))
      gl.glBufferData(GL.GL_ARRAY_BUFFER, 4 * 2 * BufferUtil.SIZEOF_FLOAT, buffer, GL.GL_STATIC_DRAW)
      val data = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL.GL_WRITE_ONLY).asFloatBuffer()
      data.put(-0.75f)
      data.put(-0.75f)
      data.put(-0.75f)
      data.put(0.75f)
      data.put(0.75f)
      data.put(0.75f)
      data.put(0.75f)
      data.put(-0.75f)
      gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER)

      gl.glVertexAttribPointer(0, 2, GL.GL_FLOAT, false, 0, 0)
      val vs = gl.glCreateShader(GL2ES2.GL_VERTEX_SHADER)
      gl.glShaderSource(vs, 1, (const GLchar **) &vertex_shader_code, NULL) // still need to port this
      gl.glCompileShader(vs)
      val fs = gl.glCreateShader(GL2ES2.GL_FRAGMENT_SHADER)
      gl.glShaderSource(fs, 1, (const GLchar **) &fragment_shader_code, NULL) // still need to port this
      gl.glCompileShader(fs)
      val program = gl.glCreateProgram()
      gl.glAttachShader(program, vs)
      gl.glAttachShader(program, fs)
      gl.glLinkProgram(program)
      gl.glUseProgram(program)
    }

Also, you can see I use GL2ES2 to get the vertex and fragment values. For some reason GL.GL_VERTEX_SHADER and GL.GL_FRAGMENT_SHADER weren’t contained in GL or GL3 despite what the documentation said.

I don’t get it. You have trouble with VBO/VA and still you are dealing with shaders.

Try to get a single triangle to render with VAs, and then with VBOs. Work from there, in small steps.

I’m using OpenGL core, so I have to use shaders. I don’t think that’s the issue here. There’s a certain amount of setup for these VBOs and I was just wondering if an experienced VBO user could tell me if it “looked” correct.

So… on which line does the Exception happen?

My guess is that it happens on:

gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices)

Why are you specifying a VA when you already put the data into a VBO?

You should use:

glVertexPointer(size, type, stride, pntr_offset);

Alright, I cleaned up my code to look like Stu Pomerantz’s VBO example (http://stupomerantz.com/public/opengl/). It certainly looks cleaner, but I’m still not getting anything drawn to my screen. I still need to check the output of my shaders to make sure they’re working, but still I’m perplexed why nothing is showing up.


    val bufferObject = new Array[int](1)
    //val buffer = BufferUtil.newFloatBuffer(8)
    def init(drawable:GLAutoDrawable) {
      val gl = drawable.getGL().getGL3()

      gl.glClearColor(.3f, .3f, .3f, 0)

      val triangleData = BufferUtil.newFloatBuffer(9)
      triangleData.put(-0.9f)
      triangleData.put(-0.9f)
      triangleData.put(-1f)

      triangleData.put(0.9f)
      triangleData.put(-0.9f)
      triangleData.put(-1f)

      triangleData.put(0.9f)
      triangleData.put(0.9f)
      triangleData.put(-1f)

      gl.glGenBuffers(1, bufferObject, 0)
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferObject(0))
      gl.glBufferData(GL.GL_ARRAY_BUFFER, 9*BufferUtil.SIZEOF_FLOAT, triangleData, GL.GL_STATIC_DRAW)
    }


    def display(drawable:GLAutoDrawable) {
      val gl = drawable.getGL().getGL3()
      gl.glClear(GL.GL_COLOR_BUFFER_BIT)

      dummyShader.bind(gl)
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferObject(0))
      val vertexLocation = dummyShader.attribLocation(gl, "vertex")
      gl.glVertexAttribPointer(vertexLocation, 3, GL.GL_FLOAT, false, 0, 0)
      gl.glEnableVertexAttribArray(vertexLocation)
      //gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4)
      gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
      dummyShader.release(gl)
   }


in vec3 vertex;

void main() {
    gl_Position = vec4(vertex,1.0);
}


void main() {
    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

after the last put()
triangleData.put(-1f)

call
triangleData.flip()

Maybe it’s fixed on OpenGL ES, but at least on my desktop I found that not using gl_Vertex in a vertex shader can cause a factor 10 drop of performance.

I would rather call rewind().

rewind() doesn’t set the limit for the buffer thus leaving its valid data length something of a mystery…

Cas :slight_smile:

The limit is set at the creation of the buffer:

[quote]allocateDirect

public static ByteBuffer allocateDirect(int capacity)

Allocates a new direct byte buffer.

The new buffer's position will be zero,[b] its limit will be its capacity[/b]

[/quote]
Calling rewind() does not change the limit and it is what we want as the limit is already set:

[quote]rewind() makes a buffer ready for re-reading the data that it already contains: It leaves the limit unchanged and sets the position to zero.
[/quote]

::slight_smile:

Who cares. Both work, in this specific case. This is a thread where somebody tries to get something to work.

Gouessej, what you said it correct, in this specific case.

What Cas and I said, is correct, in the general case.

I was actually wondering if I needed to rewind the buffer, but was already on my way to work. Rewinding the buffer after pushing all the data did the trick. Thanks.

:smiley: you’re welcome.