-1

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.

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I've already verified that the locations are set in my shader objects as 0, 1, 2 like my shader instructs. Changing from sizeof(float) for the size to actual values like 3 doesn't help either.
It doesn't matter what you set them to. The attribuites are not active, so they won't have a location. No way around that. Note that, since you assigned the locations in the shader, you don't need glGetAttribLocation at all. YOu can simply use 0, 1 and 2 - and no error will occur, as all those numbers are valid, while (GLuint)-1 isn't.
Turns out I had stale glErrors I wasn't checking until after the calls to set the interleaved data. I moved the check further up the pipeline and caught an old glEnableClientState call which isn't supported on Mac. It's actually not even necessary since I use a full shader approach. But I'm going to go ahead and say this question is answered as you are indeed correct about the active attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.