How to setup Index Buffer Object

1.1k views Asked by At

I am trying to render a basic model using lwjgl (java OpenGL binding). I am trying to do this off my own knowledge as much as possible using what I remember. I created a vbo like this:

    int verticesVBO = GL15.glGenBuffers ( );
    vboIDs.add ( verticesVBO );
    FloatBuffer verticesData = bufferFromData ( vertices );// Custom Method
    GL15.glBindBuffer ( GL15.GL_ARRAY_BUFFER , verticesVBO );
    GL15.glBufferData ( GL15.GL_ARRAY_BUFFER , verticesData , GL15.GL_STATIC_DRAW );
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);// Binds the vbo to the bound vao
    if(( error = GL11.glGetError()) != GL11.GL_NO_ERROR) System.err.println(GLU.gluErrorString(error));

I got about this far with the index buffer object:

    int indicesVBO = GL15.glGenBuffers ( );
    vboIDs.add ( verticesVBO );
    IntBuffer indicesData = bufferFromData ( indices );
    GL15.glBindBuffer ( GL15.GL_ELEMENT_ARRAY_BUFFER , indicesVBO );
    GL15.glBufferData ( GL15.GL_ELEMENT_ARRAY_BUFFER , indicesData , GL15.GL_STATIC_DRAW );
    //Problem Here
    if(( error = GL11.glGetError()) != GL11.GL_NO_ERROR) System.err.println(GLU.gluErrorString(error));

The probelm I am having is that I don't know the method to use to bind the index buffer to the vao. For the vbo containing the vertex data I know to use GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0); but I remember index buffers behaving differently. This is a learning process so please be constructive with your criticism.

1

There are 1 answers

16
user253751 On BEST ANSWER

All you need to do is bind the index buffer while the VAO is bound.

See https://www.opengl.org/wiki/Buffer_Object#General_use:

GL_ELEMENT_ARRAY_BUFFER

All rendering functions of the form gl*Draw*Elements*​ will use the pointer field as a byte offset from the beginning of the buffer object bound to this target. The indices used for indexed rendering will be taken from the buffer object. Note that this binding target is part of a Vertex Array Objects state, so a VAO must be bound before binding a buffer here.