OpenGL 无效操作

OpenGL Invalid Operation

我在 OpenGL 中遇到 loading/assigning 交错顶点数据的问题。

我在设置第二个属性时一直收到 INVALID_OPERATION。

EDIT 原来这只发生在 Mac。在 Windows 上,我没有收到 INVALID_OPERATION 错误。但是我已经用现在的样子修改了下面的内容。 Mac.

仍然出错
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
        GL.VertexAttribPointer(shader.GetAttribLocation("position"), 3, VertexAttribPointerType.Float, false, _vertexStride, 0);
        REngine.CheckGLError();
        GL.VertexAttribPointer(shader.GetAttribLocation("normal"), 3, VertexAttribPointerType.Float, false, _vertexStride, 12);
        REngine.CheckGLError();
        GL.VertexAttribPointer(shader.GetAttribLocation("texcoord"), 2, VertexAttribPointerType.Float, false, _vertexStride, 24);
        REngine.CheckGLError();
        GL.EnableVertexAttribArray(shader.GetAttribLocation("position"));
        REngine.CheckGLError();
        GL.EnableVertexAttribArray(shader.GetAttribLocation("normal"));
        REngine.CheckGLError();
        GL.EnableVertexAttribArray(shader.GetAttribLocation("texcoord"));
        REngine.CheckGLError();

知道为什么吗?其他人似乎这样做了,而且效果很好,但我似乎无法让它工作。

这是我的 GLSL:

layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 texcoord;

out vec4 out_position;
out vec4 out_normal;
out vec2 out_texcoord;


void main() {
  out_normal = vec4(normal,1.0f);
  out_position = vec4(position,1.0f);
  out_texcoord = texcoord;

}

和片段:

out vec4 color;
void main()
{
  color = vec4(1.0f,1.0f,1.0f,1.0f);
}

编辑

原来我在管道中的队列中有陈旧的 glErrors。我早些时候检查过,并使用 4.2 上下文对 Mac 不支持的 glEnableClientState 进行了一次错误调用。我删除了它,因为它不再需要使用完整的着色器方法。这修复了错误,并显示了我光彩夺目的白色网格。

只有活动属性才有位置。您的 normal 属性未激活,因为它未被使用(您将其转发到 out_normal 的事实无关紧要,因为 out_normal 未被使用)。 glGetAttributeLocation 会 return -1,但是 glVertexAttribPointer 的属性索引是 GLuint,而 (GLuint)-1 超出允许的属性索引范围. texcoord 你也应该得到同样的错误。

另请注意,使用 sizeof(float) 作为 glVertexAttribPointersize 参数也是错误的。该参数确定属性向量的分量数,1(标量)、2d、3d 或 4d,而不是一些字节数。