How do I create VBOs in JOGL?

I’ve spent last week doing lots of internet seraching, but still can’t find any unambigious descriptions on how to create and use VBOs in my JOGL code.

At the moment, I’m writing a test program that should simply render a rectangle onscreen:

    GL gl = drawable.getGL();
    
    float[] vertex = new float[]{.25f, .25f, 0, .75f, .25f, 0, .75f, .75f, 0, .25f, .75f, 0};
    short[] indicies = new short[]{0, 1, 2, 3};

    FloatBuffer vertexBuf = ByteBuffer.allocateDirect(vertex.length * BufferUtils.SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
    vertexBuf.put(vertex);
    
    ShortBuffer indiciesBuf = ByteBuffer.allocateDirect(indicies.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
    indiciesBuf.put(indicies);
    
    //-----------
    //Loading buffers
    
    int[] retInt = new int[2];
    gl.glGenBuffersARB(2, retInt);
    int vertexBufId = retInt[0];
    int indiciesBufId = retInt[1];
    

    
    //Switch to buffer
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexBufId);
    //Load static data into buffer
    gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, vertexBuf.capacity() * BufferUtils.SIZEOF_FLOAT, vertexBuf, GL.GL_STATIC_DRAW_ARB);
    //Switch back to null buffer
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, 0);
    
    
    gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, indiciesBufId);
    gl.glBufferDataARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, indiciesBuf.capacity() * BufferUtils.SIZEOF_INT, indiciesBuf, GL.GL_STATIC_DRAW_ARB);
    gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
    
    //---------------
    //Using buffers
    
    gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
    
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexBufId);
    gl.glVertexPointer(3, GL.GL_FLOAT, 0, BufferUtils.bufferOffset(0));
    
    gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, indiciesBufId);
    gl.glDrawElements(GL.GL_POLYGON, indicies.length, GL.GL_FLOAT, BufferUtils.bufferOffset(0));
    

    
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, 0);
    gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
    
    //---------------
    // Deallocate buffers
    gl.glDeleteBuffers(2, retInt);

Any idea how I’d fix this code? Also, am I right in thinking that I should run the Prepare bit when my scene loads, the use bit each frame refresh, and the Deallocate bit when I unload my scene?

How would I adjust this vertexBufId was a buffer of dynamically changing data?

Mark McKay

Think I figured it out. Once I changed the datatype I was passing to glDrawElements to GL.GL_UNSIGNED_SHORT, my rectangle started rendering.

What exactly does the BufferUtils.bufferOffset(0) passed to glDrawElements and glVertexPointer represent? Is it an offset into the buffers? If so, is it measured in bytes, or in units of the size of the data type of the buffer?

As in the C API, it’s a byte offset into the buffer object. The VertexBufferObject demo in the jogl-demos workspace shows a non-trivial usage of VBOs through JOGL.