glDrawArrays如何知道要画什么?

我正在关注一些begginer的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所做的所有事情是什么。 有人可以解释一下情况吗?

glBindBuffer的调用告诉OpenGL在需要GL_ARRAY_BUFFER时候使用vertexBufferObject

glEnableVertexAttribArray意味着你想让OpenGL使用顶点属性数组; 没有这个调用,你提供的数据将被忽略。

正如你所说的, glVertexAttribPointer告诉OpenGL如何处理提供的数组数据,因为OpenGL本身并不知道数据将以何种格式存在。

glDrawArrays使用以上所有的数据绘制点。

请记住,OpenGL是一个大型的状态机器。 大多数调用OpenGL函数都会修改不能直接访问的全局状态。 这就是为什么代码以glDisableVertexAttribArrayglBindBuffer(..., 0)结尾的原因:在完成使用后,必须将全局状态恢复。

DrawArrays从ARRAY_BUFFER获取数据。

数据根据您在glVertexAttribPointer中的设置进行“映射”,它告诉您顶点的定义是什么。

在你的例子中,你在位置0有一个顶点attrib(glEnableVertexAttribArray)(通常你可以有16个顶点attribs,每个4个浮点数)。 然后你告诉每个属性将通过从位置0开始从缓冲区中读取3个GL_FLOATS来获得。

伟大的教程在这里: http : //www.arcsynthesis.org/gltut/Basics/Tut02%20Vertex%20Attributes.html