Gabriel Reiser
Gabriel Reiser

Reputation: 402

OpenGL Invalid Operation

I'm having an issue loading/assigning interleaved vertex data in OpenGL.

I keep getting an INVALID_OPERATION when setting the second attribute.

EDIT Turns out this only happens on Mac. On Windows, I don't get an INVALID_OPERATION error. But I have modified the below with what it looks like now. Still errors out on 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();

Any idea why? Others seem to do it and it works great, but I can't seem to get it to work.

Here is my GLSL for this:

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;

}

and the frag:

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

EDIT

Turns out I had stale glErrors in the queue from earlier in the pipeline. I checked earlier and had a bum call to glEnableClientState which isn't supported on Mac using the 4.2 context. I removed it as it wasn't necessary anymore with a full shader approach. This fixed the error and my glorious white mesh was displayed.

Upvotes: 0

Views: 5711

Answers (1)

derhass
derhass

Reputation: 45352

Only active attributes have a location. Your normal attribute is not active, as it is not used (the fact that you forward it to out_normal is irrelevant, as out_normal is not used). glGetAttributeLocation will return -1 for that, but the attribute index for glVertexAttribPointer is a GLuint, and (GLuint)-1 is way out of the range for allowed attribute indices. You should get the same error for texcoord too.

Please also note that using sizeof(float) as the size parameter for glVertexAttribPointer is wrong too. That parameter determines the number of components for the attribute vector, 1 (scalar), 2d, 3d or 4d, not some number of bytes.

Upvotes: 4

Related Questions