OpenGL的VAO-VBO-EBO

本文详细解释了OpenGL中的三个核心概念:VAO(顶点数组对象)、VBO(顶点缓存对象)和EBO(元素缓存对象)。介绍了它们的作用、创建与使用的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考: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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春夜喜雨

稀罕你的喜欢!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值