GlDrawArray如何知道要画什么?

6jjcrrmo  于 2022-10-18  发布在  其他
关注(0)|答案(3)|浏览(110)

我正在学习一些乞讨的OpenGL教程,并且对这段代码有点困惑:

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); //Bind GL_ARRAY_BUFFER to our handle
glEnableVertexAttribArray(0); //?
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); //Information about the array, 3 points for each vertex, using the float type, don't normalize, no stepping, and an offset of 0. I don't know what the first parameter does however, and how does this function know which array to deal with (does it always assume we're talking about GL_ARRAY_BUFFER?

glDrawArrays(GL_POINTS, 0, 1); //Draw the vertices, once again how does this know which vertices to draw? (Does it always use the ones in GL_ARRAY_BUFFER)

glDisableVertexAttribArray(0); //?
glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbind

我不明白glDrawArrays怎么知道要画哪些顶点,以及glEnableVertexAttribArray要做的所有事情。有没有人能说明一下情况?

aiazj4mn

aiazj4mn1#

glBindBuffer的调用告诉OpenGL在需要GL_ARRAY_BUFFER时使用vertexBufferObject
glEnableVertexAttribArray表示您希望OpenGL使用顶点属性数组;如果没有此调用,您提供的数据将被忽略。
正如您所说,glVertexAttribPointer告诉OpenGL如何处理提供的数组数据,因为OpenGL本身并不知道这些数据将是什么格式。
glDrawArrays使用上述所有数据来绘制点。
请记住,OpenGL是一个大型状态机。大多数对OpenGL函数的调用都会修改您不能直接访问的全局状态。这就是代码以glDisableVertexAttribArrayglBindBuffer(..., 0)结尾的原因:您必须在使用完全局状态后将其放回原处。

nnsrf1az

nnsrf1az2#

DrawArray从ARRAY_BUFFER获取数据。
数据根据你在glVertexAttribPointer中的设置进行Map,glVertexAttribPointer告诉你顶点的定义是什么。
在您的示例中,位置0处有一个顶点属性(GlEnableVertex AttribArray)(通常可以有16个顶点属性,每个顶点属性4个浮点)。然后,从位置0开始从缓冲区读取3个GL_FLOATS,即可获得每个属性。

ny6fqffe

ny6fqffe3#

作为对其他答案的补充,这里有一些指向OpenGL文档的指针。根据维基百科[1],OpenGL的开发已经在2016年停止,转而支持后续的API“Vulkan”[2,3]。最新的OpenGL规范是2017年的4.6,但比3.2[1]增加的很少。
原始问题中的代码片段不需要完整的OpenGL API,只需要编码为OpenGL ES(最初用于嵌入式系统)的子集[4]。例如,广泛使用的图形用户界面开发平台Qt使用OpenGL ES 3.x[5]。
OpenGL的维护者是Khronos联盟[1,6]。最新的OpenGL版本的参考位于[7],但有一些不一致之处(4.6页链接到4.5页)。如果有疑问,请使用[8]中的3.2参考。
教程的集合在[9]。
1.https://en.wikipedia.org/wiki/OpenGL
1.https://en.wikipedia.org/wiki/Vulkan
1.https://vulkan.org
1.https://en.wikipedia.org/wiki/OpenGL_ES
1.请参阅函数引用(如https://doc.qt.io/qt-6/qopenglfunctions.html#glVertexAttribPointer)中的链接
1.https://registry.khronos.org
1.https://www.khronos.org/opengl
1.https://registry.khronos.org/OpenGL-Refpages/es3
1.http://www.opengl-tutorial.org

相关问题