参考:https://blog.youkuaiyun.com/xufeng0991/article/details/80813594
参考:http://www.songho.ca/opengl/gl_vbo.html
参考:https://goharsha.com/lwjgl-tutorial-series/element-buffer-objects/
(Owed by: 春夜喜雨 http://blog.youkuaiyun.com/chunyexiyu)
在读OpenGL编程指南的时候, 一开始就会在例子中碰到这几个VAO、VBO简写, 开始也不太清楚,后来就查阅一些资料,有了一些理解。
单词及描述:
首先是这几个单词和大体的描述:
VAO: Vertex Array Object:顶点数组对象,存储了一组绑定信息的数据,例如使用哪个VBO的哪个部分,VBO对象绑定了哪些变量供使用。绘制时,只需要切换VAO,就可以完成VBO和绑定变量的切换,从而直接调用绘制函数。
VBO: Vertex Buffer Object:顶点缓存对象,存储各种数据,像vertex, normal, texture coordinate, color等。
EBO: Element Buffer Object: 是另一种缓存对象,这种缓存对象使用绑定类型GL_ELEMENT_ARRAY_BUFFER。通常我们用来绑定三角面片索引信息。which is just another buffer object that is bound to GL_ELEMENT_ARRAY_BUFFER instead of GL_ARRAY_BUFFER as it’s target.
创建和使用
然后是他们的创建和使用:
VAO的创建和使用:
// create VAO
GLuint vaoId;
glCreateVertexArrays(1,&vaoId);
// bind as the current VAO
glBindVertexArray(vaoId);
…
// unbind
glBindVertexArray(0);
…
// delete VAO when program terminated
glDeleteVertexArray(1, &vaoId);
VBO的创建和使用:
// create VBO
GLuint vboId;
glGenBuffers(1, &vboId);
// bind as the current buffer
glBindBuffer(GL_ARRAY_BUFFER, vboId);
// upload data to VBO
glBufferData(GL_ARRAY_BUFFER, dataSize, vertices, GL_STATIC_DRAW);
// bind attribute to the buffer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, (void*)0);
// it is safe to delete after copying data to VBO
delete [] vertices
…
// delete VBO when program terminated
glDeleteBuffers(1, &vboId);
EBO的创建和使用:
和VBO不同的是,我们不会看到Pointer函数指向该buffer,也不会在shader中使用到它。Unlike the Vertex Buffer Objects, here we don’t see a call to any pointer functions, as we don’t access the indices from the shaders.
OpenGL会自动使用EBO中的indices去选择VBO中的顶点数据, 使用绘制函数时,需要使用glDrawElements。OpenGL can automatically select the vertices from the VBO based on the indices we specify in the element buffer object. However, we have use a special function call called as glDrawElements instead of the glDrawArrays.
// create ebo
GLuint eboId;
glGenBuffers(1, &eboId);
// set as the GL_ELEMENT_ARRAY_BUFFER
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
// upload data to EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
…
// delete EBO when program terminated
glDeleteBuffers(1, &eboId);
(Owed by: 春夜喜雨 http://blog.youkuaiyun.com/chunyexiyu)