我有以下非常简单的顶点着色器,当我用它渲染时,我得到一个空白屏幕:
#version 110
layout(location = 1) attribute vec3 position;
uniform mat4 modelview_matrix;
uniform mat4 projection_matrix;
void main() {
vec4 eye = modelview_matrix * vec4(position, 1.0);
gl_Position = projection_matrix * eye;
}
然而,改变
layout(location = 1) attribute vec3 position;
至
layout(location = 0) attribute vec3 position;
允许我正确渲染。这是我的渲染功能:
glUseProgram(program);
GLenum error;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUniformMatrix4fv(
modelview_uniform, 1, GL_FALSE, glm::value_ptr(modelview));
glUniformMatrix4fv(
projection_uniform, 1, GL_FALSE, glm::value_ptr(projection));
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(
position_attribute,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(position_attribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer);
glDrawElements(
GL_TRIANGLES,
monkey_mesh.indices.size(),
GL_UNSIGNED_INT,
(void*)0);
glDisableVertexAttribArray(position_attribute);
glutSwapBuffers();
我position_attribute
通过调用获得glGetAttribLocation(program, "position");
。它在两种情况下都包含正确的值(第一种情况下为 1,第二种情况下为 0)。
有什么我做错了吗?我确信我能够渲染location == 0
只是因为我很幸运,而且数据是靠运气写在那里的,但我一生都无法弄清楚我错过了哪一步。