glVertexAttribPointer raising GL_INVALID_OPERATION
Asked Answered
F

1

37

I'm trying to put together a very basic OpenGL 3.2 (core profile) application. In the following code, which is supposed to create a VBO containing the vertex positions for a triangle, the call to glVertexAttribPointer fails and raises the OpenGL error GL_INVALID_OPERATION. What does this mean, and how might I go about fixing it?

GLuint vbo, attribLocation = glGetAttribLocation(...);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
GLfloat vertices[] = { 0, 1, 0, 1, 0, 0, -1, 0, 0 };
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(attribLocation);
// At this point, glGetError() returns GL_NO_ERROR.
glVertexAttribPointer(attribLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
// At this point, glGetError() returns GL_INVALID_OPERATION.
Frier answered 15/11, 2012 at 18:27 Comment(8)
is this all inside of the same function?Distraction
Yes. I'll refactor it when it actually works :PFrier
Are you sure that the GL_INVALID_OPERATION error is coming from glVertexAttribPointer? Remember: OpenGL stores the errors and returns them only when you call glGetError. So if you're not regularly purging the error queue, you may be getting older errors.Wabble
It's definitely coming from glVertexAttribPointer - when I move the error check up to the line above no error is reported.Frier
@robinjam: Please add the specific error checking code to the question, both the latest point where there is you see the error and the earliest point where the error appears. Because I think your error is coming from glEnableVertexAttribArray.Wabble
At the moment, my "error checking" consists of moving cout << glGetError() << endl; around until I find the source of the error (I was planning to refactor once I get the basic demo done). I've added comments to the original question to show exactly where the error shows up.Frier
@robinjam: Are you sure you're getting GL_INVALID_OPERATION? Exactly what enumeration value are you getting? The actual number, not the OpenGL thing. Also, can you provide an [SSCCE](sscce.org)?Wabble
The exact return value from glGetError() is 1282. The code is so short at the moment that I may as well upload it to GitHub - give me a few minutes and I'll do so.Frier
W
71
glEnableVertexAttribArray(program.getAttrib("in_Position"));
// A call to getGLError() at this point prints nothing.
glVertexAttribPointer(program.getAttrib("in_Position"), 3, GL_FLOAT, GL_FALSE, 0, 0);
// A call to getGLError() at this point prints "OpenGL error 1282".

First, there's an obvious driver bug here, because glEnableVertexAttribArray should also have issued a GL_INVALID_OPERATION error. Or you made a mistake when you checked it.

Why should both functions error? Because you didn't use a Vertex Array Object. glEnableVertexAttribArray sets state in the current VAO. There is no current VAO, so... error. Same goes for glVertexAttribPointer. It's even in the list of errors for both on those pages.

You don't need a VAO in a compatibility context, but you do in a core context. Which you asked for. So... you need one:

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

Put that somewhere in your setup and your program will work.


As an aside, this:

glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

is only necessary if you intend your code to run on MacOS's GL 3.2+ implementation. Unless you have that as a goal, it is unneeded and can be disruptive, as a small number of features are available in a core context that are not part of forward compatibility (wide lines, for example).

Wabble answered 15/11, 2012 at 20:0 Comment(8)
Thank you very much for your help. I'm still not getting any output on the screen, but at least I'm not getting any errors now!Frier
With regards to the glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); statement - this project needs to support multiple platforms, and according to the GLFW documentation, OS X only gives you a core context if you ask for a forward-compatible one.Frier
@robinjam: That sounds like a bug in GLFW to me.Wabble
+1 I don't know how it worked in OpenGL 3.3 (which I've been using previously) but that was it - I never generated and bound the vertex array object. Thanks, Nicol!Catharina
*Previously, I've been using AMD drivers. Now it's Nvidia and OpenGL 4.3 core profileCatharina
The GLFW_OPENGL_FORWARD_COMPAT hint is needed on OS X. I keep getting this in output: 65543: NSGL: The targeted version of OS X only supports OpenGL 3.2 and later versions if they are forward-compatibleGuildsman
The line mentioning to stop using GLFW_OPENGL_FORWARD_COMPAT should be removed from this answer, as it is clearly necessary for OpenGL 3.2 core profile support on macOS, as stated both in GLFW documentation and the official OpenGL documentation's API caveats.Tajo
A note to my future self when I see this question for the 110th time: a similar code works with OpenGL v3.1, but fails with the same error as OP (even after defining a VAO) on OpenGL v4.0. This note to my future self is probably good enough for now, but anybody has any thoughts why that's the case?Tick

© 2022 - 2024 — McMap. All rights reserved.