我已经看到很多程序在初始化阶段(伪代码)执行此操作:
glGenVertexArrays(vao)
glBindVertexArray(vao)
glGenBuffers(vbo)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(into the vbo)
glVertexAttribPointer(instruct the vao on vbo's memory organization)
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBO
glBindVertexArray(0); // Unbind VAO
glGenBuffers(vbo for indices)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo for indices)
glBufferData(populate indices vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Unbind vbo for indices
这是正确的做法吗? VAO是否也存储了对索引vbo的引用,以便在索引的vbo也被初始化之后我可以解除对vao的绑定(并且避免在绘图阶段将vbo绑定到索引)?
答案 0 :(得分:2)
是的,VAO会存储GL_ELEMENT_ARRAY_BUFFER
绑定。
但请注意,它在解除VAO绑定之前始终存储最后一个状态。因此,如果在取消绑定VOA之前调用glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
,则会删除绑定。在您的情况下,您还在绑定索引缓冲区之前取消绑定VAO,因此此处不存储绑定。
正确的代码如下所示:
lGenVertexArrays(vao)
glBindVertexArray(vao)
glGenBuffers(vbo)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(into the vbo)
glVertexAttribPointer(instruct the vao on vbo's memory organization)
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBO
glGenBuffers(vbo for indices)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo for indices)
glBufferData(populate indices vbo)
glBindVertexArray(0); // Unbind VAO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Unbind vbo for indices