我正在学习一些初级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
与此有什么关系。能否有人对这种情况进行阐述?
glDrawArrays
后,顶点着色器才开始起作用。glDrawArrays
只是将使用glEnableVertexAttribArray
启用的每个活动顶点属性的数据传递到绘图队列中。在顶点着色器中,每个顶点属性数组都有一个绑定,着色器确定哪些元素成为屏幕位置、颜色等。 - datenwolfglVertexAttribPointer(attrib_index, …)
时,由glBindBuffer(GL_ARRAY_BUFFER, …)
绑定的缓冲区将绑定到glVertexAttribPointer
的第一个参数指定的顶点属性。要将另一个缓冲区绑定到另一个属性,需要执行另一个glBindBuffer(GL_ARRAY_BUFFER, …); glVertexAttribPointer(…);
。调用glEnableVertexAttribArray
不是使缓冲区和属性之间绑定的必需操作。但启用顶点属性数组使glDraw…
调用使用已启用的属性。 - datenwolf