Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glVertexAttribPointer raising GL_INVALID_OPERATION

People also ask

What does glVertexAttribPointer do?

Vertex format. The glVertexAttribPointer functions state where an attribute index gets its array data from. But it also defines how OpenGL should interpret that data. Thus, these functions conceptually do two things: set the buffer object information on where the data comes from and define the format of that data.

What is a VAO?

A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information for a complete rendered object. In our example this is a diamond consisting of four vertices as well as a color for each vertex.

What is a vertex attribute?

A vertex attribute is an input variable to a shader that is supplied with per-vertex data. In OpenGL core profile, they are specified as in variables in a vertex shader and are backed by a GL_ARRAY_BUFFER . These variable can contain, for example, positions, normals or texture coordinates.


First, let's get some preliminaries out of the way:

glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

Stop doing this. You already asked for a core OpenGL context. You don't need forward compatibility, and it does nothing for you. This was an old flag when 3.0 had deprecated things but didn't remove them. You don't need it.

That's not causing your problem, though. This is:

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.