如何在 vao 中使用 glVertexAttrib3f?

How to use glVertexAttrib3f with vao?

我用vao和vbo画了一个四边形。顶点着色器具有以下输入:

layout (location = 0) in vec3 pos

我想使用 glVertexAttrib3f 为顶点着色器设置一个常量 pos 值。以下代码无效(四边形正在绘制):

glVertexAttrib3f(0, 0.0f, 0.0f, 0.0f);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

怎么了?

OpenGL 4.6 API Core Profile Specification - 10.2 Current Vertex Attribute Values:

The commands in this section are used to specify current attribute values. These values are used by drawing commands to define the attributes transferred for a vertex when a vertex array defining a required attribute is not enabled [...]

请注意,当前顶点属性值未在顶点数组对象中说明。

必须禁用具有指定索引的顶点属性:

glBindVertexArray(VAO);
glDisableVertexAttribArray(0);