如何设置索引缓冲区对象

How to setup Index Buffer Object

我正在尝试使用 lwjgl(java OpenGL 绑定)渲染一个基本模型。我正在尝试使用我所记得的尽可能多地利用我自己的知识来做到这一点。我创建了一个这样的 vbo:

    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));

我已经了解了索引缓冲区对象:

    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));

我遇到的问题是我不知道用于将索引缓冲区绑定到 vao 的方法。对于包含我知道使用 GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0); 的顶点数据的 vbo,但我记得索引缓冲区的行为不同。这是一个学习过程,所以请建设性地提出您的批评。

您需要做的就是在绑定 VAO 的同时绑定索引缓冲区。

参见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.