OpenGL如何知道它应该基于GL_ARRAY_BUFFER绘制顶点的位置?

时间:2015-06-30 05:41:02

标签: c++ opengl buffer vbo vao

例如,我有以下代码:

//Create a vbo and bind it to the GL_ARRAY_BUFFER
glGenBuffers(1, &positionBufferObject);    
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);

//vertexPosition is an array of floats that stores the position of 3 vertices (x, y, z, w)
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);

//Enable the vbo at index 0 of the vao (assuming I have stored it previously at index 0)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

//Finally draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);

我的问题是,glDrawArrays()如何知道绑定到GL_ARRAY_BUFFER的任何内容是指有关位置的信息,而不是关于颜色的信息?

1 个答案:

答案 0 :(得分:2)

如果gl_Vertex可用(即不是核心配置文件),则规范会调用该位置和顶点属性0别名。

来自gl 4.5 compatibility profile specification(第401/1005页):

  

设置通用顶点属性零指定顶点,如   在第10.7.2节中描述。设置任何其他通用顶点   属性更新属性的当前值。没有   顶点属性零的当前值