Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to check error in OpenGL

📅 2015-Sep-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ error, glgeterror, mesa, opengl ⬩ 📚 Archive

In OpenGL, multiple types of errors might have occurred by the time you check for them. The errors are stored using the data type GLenum. GL_NO_ERROR always has the value 0 and you need to call glGetError() multiple times (one for each error that has occurred) until it returns GL_NO_ERROR. A general rule of thumb is to check for error at least once in a rendering cycle.

The standard OpenGL errors I found defined on my system:

#define GL_NO_ERROR                0
#define GL_INVALID_ENUM                0x0500
#define GL_INVALID_VALUE           0x0501
#define GL_INVALID_OPERATION           0x0502
#define GL_STACK_OVERFLOW          0x0503
#define GL_STACK_UNDERFLOW         0x0504
#define GL_OUT_OF_MEMORY           0x0505

In addition to these standard values, OpenGL extensions could introduce their own error types.

Putting all this together, we can write a simple function named CheckGLError that checks for errors and if any, then prints them out:

const char * GetGLErrorStr(GLenum err)
{
    switch (err)
    {
    case GL_NO_ERROR:          return "No error";
    case GL_INVALID_ENUM:      return "Invalid enum";
    case GL_INVALID_VALUE:     return "Invalid value";
    case GL_INVALID_OPERATION: return "Invalid operation";
    case GL_STACK_OVERFLOW:    return "Stack overflow";
    case GL_STACK_UNDERFLOW:   return "Stack underflow";
    case GL_OUT_OF_MEMORY:     return "Out of memory";
    default:                   return "Unknown error";
    }
}

void CheckGLError()
{
    while (true)
    {
        const GLenum err = glGetError();
        if (GL_NO_ERROR == err)
            break;

        std::cout << "GL Error: " << GetGLErrorStr(err) << std::endl;
    }
}

Note: If you are using GLU, then the gluErrorString function can be used to convert OpenGL error to a string.

Tried with: Mesa 10.1.3 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧