我正在尝试使用 lwjgl(java OpenGL 绑定(bind))渲染基本模型。我试图尽可能利用我所记得的知识来完成这件事。我创建了一个像这样的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));
我遇到的问题是我不知道将索引缓冲区绑定(bind)到 vao 的方法。对于包含顶点数据的 vbo,我知道使用 GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
但我记得索引缓冲区的行为不同。这是一个学习过程,因此请提出建设性的批评。
最佳答案
您需要做的就是在绑定(bind) VAO 的同时绑定(bind)索引缓冲区。
参见https://www.opengl.org/wiki/Buffer_Object#General_use :
GL_ELEMENT_ARRAY_BUFFER
block 引用>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.
关于java - 如何设置索引缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888404/