Wolfgang Skyler
Wolfgang Skyler

Reputation: 1378

glVertexAttribPointer raising impossible GL_INVALID_OPERATION?

I have narrowed down the error to this OpenGL call:

glVertexAttribPointer(var.vertex, 4, GL_FLOAT, GL_FALSE, 0, 0);

there are no errors before it, and there is a GL_INVALID_OPERATION error after it. The output to my console is:

Error in Shader.cpp : bindMesh : 294
        OpenGL Error: Invalid Operation
        (0x502)
        Bound Buffer: 1
        var.vertex = 1

According to this, the only GL_INVALID_OPERATION condition that could apply is a bound buffer ID of 0. But the bound buffer ID is 1.

I'm checking (and printing) the error with:

printErrors();
int i = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &i);
printf("\tBound Buffer: %i\n", i);
printf("\tvar.vertex = %i\n", var.vertex);

where printErrors() is defined as

void PrintErrors(const char* file, const char* func, int line);
#define printErrors() PrintErrors(__BASE_FILE__,__func__,__LINE__)

void PrintErrors(const char* file, const char* func, int line)
{
    const char* errors[] = {
        "OpenGL Error: Invalid Enumeration\n",
        "OpenGL Error: Invalid Value\n",
        "OpenGL Error: Invalid Operation\n",
        "OpenGL Error: Invalid Frame-Buffer Operation\n",
        "OpenGL Error: Out of Memory\n",
        "OpenGL Error: Stack Underflow\n",
        "OpenGL Error: Stack Overflow\n",
    };

    uint32 i = glGetError();
    while (i != GL_NO_ERROR)
    {
        printf("Error in %s : %s : %i\n\t%s\t(0x%x)\n", file, func, line, errors[i - GL_INVALID_ENUM], i);
        i = glGetError();
    }
}

What's going wrong?

Edit:

var.vertex is found with:

var.vertex = glGetAttribLocation(programID, "Vertex");

and there is a check to ensure that var.vertex was found in the bound shader by an if-statement:

if (var.vertex != INVALID_SHADER_VARIABLE) // INVALID_SHADER_VARIABLE = 0xFFFFFFFF (-1 if it where a signed int)
{
    glBindBuffer(...);
    glBuffferData(...);
    glEnableVertexAttribArray(var.vertex);
    glVertexAttribPointer(var.vertex, 4, GL_FLOAT, GL_FALSE, 0, 0);
}

Upvotes: 3

Views: 3732

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43369

You must have a non-zero Vertex Array Object bound in an OpenGL 3.1 context without the extension GL_ARB_compatibility or a core profile context. This is one of the hidden conditions that will generate a GL_INVALID_OPERATION whenever you attempt to do anything related to vertex arrays (e.g. draw, setup vertex pointers, etc.)

The good news is this is a trivial issue to fix.

At minimum, all you need to do is generate (glGenVertexArrays (...)) and bind (glBindVertexArray (...)) a vertex array object when you initialize your program.

Upvotes: 6

Related Questions