VBOs and glDrawElements()

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

VBOs and glDrawElements()

gmseed
This post was updated on .
Hi

In my render method I have the code below. If I don't use vertex indices (mIsUsingVertexIndices=false) then it works fine. However, if I use vertex indices and glDrawElements() instead I get a runtime exception occuring at the call:

glv.glVertexPointer(3, GL.GL_FLOAT, 0, 0);

of the form:

"element vertex_buffer_object must be disabled to call this method"

I based my code on:

http://www.songho.ca/opengl/gl_vbo.html

What I also don't understand about VBOs in the case of vertex indices is having to bind the integer indices buffer to a handle, mVBOIndices:

        gl2.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER,mVBOIndices);  // bind the buffer
        gl2.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indicesBufferSize, mVertexIndices, GL2.GL_STATIC_DRAW);

but in the render function:

            glv.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, mVBOIndices); // for indices
            glv.glIndexPointer(GL2.GL_UNSIGNED_INT, 0, mVertexIndices);

we call glIndexPointer() passing the vertex indices buffer and not the offset. This is the only version of glIndexPointer() available!!

This differs from the approach using the vertex buffer in which the offset is passed to glVertexPointer().

It's all very confusing and not well documented. If I call the version of glDrawElements() taking the indices buffer:

glv.glDrawElements(GL2.GL_TRIANGLES, numberIndices, GL.GL_UNSIGNED_INT, mVertexIndices);

I get the above exception.

If I call the version of glDrawElements() taking a buffer offset:

glv.glDrawElements(GL2.GL_TRIANGLES, numberIndices, GL.GL_UNSIGNED_INT, 0);

I get a runtime crash.

Thanks

Graham

===

        if (mIsUsingVertexIndices)
        {
            IndexTriMesh3D triMesh = (IndexTriMesh3D)mGeometricObject;
            int numberTriangles = triMesh.numberTriangles();
            int numberIndices = 3 * numberTriangles;
           
            glv.glEnableClientState(GL2.GL_VERTEX_ARRAY);           // enable vertex arrays
            glv.glEnableClientState(GL2.GL_ELEMENT_ARRAY_BUFFER);   // enable vertex indices
            glv.glEnableClientState(GL2.GL_COLOR_ARRAY);            // enable colour arrays

            glv.glBindBuffer(GL2.GL_ARRAY_BUFFER,mVBO);
            glv.glVertexPointer(3, GL.GL_FLOAT, 0, 0);              // set the vertex pointer
           
            glv.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, mVBOIndices); // for indices
            glv.glIndexPointer(GL2.GL_UNSIGNED_INT, 0, mVertexIndices);
           
            glv.glBindBuffer(GL2.GL_ARRAY_BUFFER,mVBOColours);
            glv.glColorPointer(3, GL.GL_FLOAT, 0, 0);
           
            glv.glDrawElements(GL2.GL_TRIANGLES, numberIndices, GL.GL_UNSIGNED_INT, mVertexIndices);
           
            glv.glDisableClientState(GL2.GL_VERTEX_ARRAY);          // disable vertex arrays
            glv.glDisableClientState(GL2.GL_ELEMENT_ARRAY_BUFFER);  // disable vertex colour array
            glv.glDisableClientState(GL2.GL_COLOR_ARRAY);           // disable vertex colour array
            glv.glBindBuffer(GL2.GL_ARRAY_BUFFER,0);                // release VBOs with id 0 after use
            glv.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER,0);        // release VBOs with id 0 after use
            glv.glBindBuffer(GL2.GL_COLOR_ARRAY,0);                 // release VBOs with id 0 after use
        }
        else
        {
            glv.glEnableClientState(GL2.GL_VERTEX_ARRAY);           // enable vertex arrays
            glv.glEnableClientState(GL2.GL_COLOR_ARRAY);            // enable colour arrays

            glv.glBindBuffer(GL2.GL_ARRAY_BUFFER,mVBO);             // bind vertex buffer
            glv.glVertexPointer(3, GL.GL_FLOAT, 0, 0);              // set vertex pointer to the vertex buffer

            glv.glBindBuffer(GL2.GL_ARRAY_BUFFER,mVBOColours);      // bind colour buffer
            glv.glColorPointer( 3, GL.GL_FLOAT, 0, 0);              // set vertex pointer to the vertex buffer

            glv.glDrawArrays(GL.GL_TRIANGLES, 0, mVertexCount);     // draw triangles
   
            glv.glDisableClientState(GL2.GL_VERTEX_ARRAY);          // disable vertex arrays
            glv.glDisableClientState(GL2.GL_COLOR_ARRAY);           // disable colour arrays
            glv.glBindBuffer(GL2.GL_ARRAY_BUFFER,0);                // release VBOs with id 0 after use
            glv.glBindBuffer(GL2.GL_COLOR_ARRAY,0);                 // release VBOs with id 0 after use
        }