使用OpenGL创建三角形
程序运行效果
VAO vertex array object 顶点数组对象
VBO vertex buffer object 顶点缓冲对象
EBO element(index) buffer object 索引缓冲对象
VBO and VAO
Data Flow from CPU to GPU
- Vertex Buffer Object (VBO)
VBOs are buffers of data (bytes) on the GPU
CPU sends all data to the GPU using VBOs before it is rendered - Vertex Array Object (VAO)
VAOs are the link between VBO and shader variables
Describe how to pull the data out of VBOs and feed it into the shader variables
VBO
所以VBO
的作用就是将PC内存中的顶点数据缓冲到显卡的显存中:
CPU -> bytes(PC Memory) -> Display Card Memory -> GPU
CPU为通用的中央处理器,其特点是通用性,缺点是浮点运算能力,计算机的内存好处是CPU操作方便,相比显卡的显存来说,同时期相对来说显存会比内存速度更快,所以VBO
的好处就是将磁盘到内存后的顶点数据缓冲到显卡,让GPU图形处理器去操作显存中的数据,而GPU对比CPU的好处就是图形图像的处理能力,浮点运算能力更强。
所以我们在渲染之前,让CPU使用VBO
将所有数据发送到GPU,这样为我们的OpenGL处理图形带来效率的提升。
VAO
而VAO
则是VBO
和着色器shader
变量之间的链接,描述如何从VBO
中提取数据并将其提供给着色器变量。
(VAO <-> VBO) <-> shader variables
Vertex Attribute Pointer 顶点属性指针
我们要操作VAO
实际上就是使用顶点属性指针,并激活顶点属性数组。
- 将当前
VBO
绑定到当前VAO
- 将着色器变量与当前
VBO
数据链接 - 将布局信息封装在当前
VAO
中,以便稍后检索
将此布局信息封装在当前VAO中,以便稍后检索
// 定义定点数组
GLfloat vertices1[] = {
// 坐标 // 颜色
0.4f, -0.4f, 0.0f, 1.0f, 0.0f, 0.0f, // 右下 红
-0.4f, -0.4f, 0.0f, 0.0f, 1.0f, 0.0f, // 左下 绿
0.0f, 0.4f, 0.0f, 0.0f, 0.0f, 1.0f // 顶部 蓝
};
GLfloat vertices2[] = {
// 坐标 // 颜色
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, // 右上 黄
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, // 右下 品
0.0f, -0.5f, 0.0f, 0.0f,