The infamous 1282 error in OpenGL stands for GL_INVALID_OPERATION, and can be pretty annoying. A friend once described it as "GL_GENERIC_FUCK_YOU_ERROR" since it really doesn't tell you anything except that you are somehow using OpenGL wrong.

The simple method of debugging this is to, line by line, place something like

printf( "%d\n", glGetError() );

and then compile, rerun, and eventually figure out which line is causing the error.

But little did I know that there is a much more elegant solution.

void GLAPIENTRY MessageCallback( GLenum source,
                 GLenum type,
                 GLuint id,
                 GLenum severity,
                 GLsizei length,
                 const GLchar* message,
                 const void* userParam )
  log_warn("OpenGL callback: %s type = %d, severity = %d, message = %s\n",
           ( type == GL_DEBUG_TYPE_ERROR ? "GL_DEBUG_TYPE_ERROR" : "" ),
            type, severity, message );

// Place theese two lines somewhere after initing OpenGL
glEnable              ( GL_DEBUG_OUTPUT );
glDebugMessageCallback( MessageCallback, 0 );

I had no idea that you have the option to register a callback for OpenGL errors.
This produces something like:

14:41:58 WARN  src/main.c:35: OpenGL callback: GL_DEBUF_TYPE_ERROR type = 33356, severity = 37190, message = GL_INVALID_OPERATION in glGetUniformLocation

And voila: now I know I messed up something with glGetUniformLocation.


This seems to be broken on MacOS. Avoid this if you dont seek to have random segfaults :)