The Unholy Metal Machine
The Unholy Metal Machine

Reputation: 1103

INVALID_ENUM error from glGetError(). glGetError with glu, glew, glfw?

I'm adding glGetError() in my code everytime just right after I call an OpenGL function.

Actually I don't call glGetError() but a function I wrote ( DisplayGlErrors() ) to print all errors (trough a loop) in the console. So now I guess, everytime I call my function right after (for example) gluLookAt() I should be able to get all errors caused by openGL trough that function.

Now let speak about my problem. From this piece of code :

GL_engine::GL_engine(Application* appli):engine(appli), width(get_parent()->getWidth()), height(get_parent()->getHeight())
{
  if (GLEW_OK != glewInit()) // glew needs to be initialised, otherwise we get an error, AFTER a windows has been created BUT BEFORE using buffers
  {
    std::cout << "glewInit() failed" << std::endl;
    exit(EXIT_FAILURE);
  }
  DisplayGlErrors(__FILE__, __LINE__);
  glGetIntegerv( GL_MAJOR_VERSION, &contextMajor );                     DisplayGlErrors(__FILE__, __LINE__);
  glGetIntegerv( GL_MINOR_VERSION, &contextMinor );                     DisplayGlErrors(__FILE__, __LINE__);
  std::cout << "Created OpenGL " << contextMajor <<"."<< contextMinor << " context" << std::endl;
  glClearColor(0.25f, 0.25f, 0.25f, 1.0f);                              DisplayGlErrors(__FILE__, __LINE__);
  cam = Camera();                                                       DisplayGlErrors(__FILE__, __LINE__);
  worldAxis.initialise(); DisplayGlErrors(__FILE__, __LINE__);          DisplayGlErrors(__FILE__, __LINE__);
  worldGrid.initialise(); DisplayGlErrors(__FILE__, __LINE__);          DisplayGlErrors(__FILE__, __LINE__);
}

I got (in the console) :

OpenGL error #1: INVALID_ENUM(/WelcomeToYourPersonalComputerInferno/666/src/GL_engine.cc, 40)
OpenGL error #1: INVALID_ENUM(/WelcomeToYourPersonalComputerInferno/666/src/GL_engine.cc, 41)
Created OpenGL 0.0 context

n.b. : contextMajor and contextMinor are GLint variables.

I don't have any idea what means these INVALID_ENUM... I even think OpenGL doesn't know either...

if for any reason you want to look inside my code, I just updated my git

To finish, since I'm using GLFW, GLU, GLEW in my program. I would like to know if it make sense to call glGetError() (still trough DisplayGlErrors()) after I called a function from these libraries.

Upvotes: 0

Views: 573

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54592

You have somewhat of a chicken and egg problem here. The arguments GL_MAJOR_VERSION and GL_MINOR_VERSION for glGetIntegerv() were only introduced in OpenGL 3.x (some spec information suggests 3.0, some 3.1). It looks like your context does not have at least this version, so you can't use this API to check the version.

If you need at least 3.x for your code to run, you should specify that on context creation. It looks like the glfwWindowHint() call is used for this purpose in GLFW.

To get the supported version across all OpenGL versions, you can use glGetString(GL_VERSION). This call has been available since OpenGL 1.0, so it will work in every possible context.

On when to call glGetError(): It can't really hurt to call it too much during development. You'll just want to make sure that you disable/remove the calls for release builds if you care about performance of your software. For the specific libraries you mention:

  • GLEW: I don't think you normally call anything from GLEW after glewInit(). Except maybe glewIsSupported(). In any case, GLEW just provides access to OpenGL entry points, I don't believe it makes an GL calls itself. So I don't think calling glGetError() after GLEW calls is useful.

  • GLU: These calls definitely make OpenGL calls, so calling glGetError() after them makes sense. Note that GLU is deprecated, and not available anymore with the OpenGL Core Profile.

  • GLFW: This provides an abstraction of the window system interface, so I wouldn't expect it to make OpenGL calls. In this case, calling glGetError() does not seem necessary. It has its own error handling (http://www.glfw.org/docs/latest/group__error.html).

This is partly a question of preference. I personally don't think that calling glGetError() after each call is necessary. Since the errors are sticky, you can always detect when an error happened, even if it was from an earlier call, and search back if necessary. I mostly just put a check like this at the end of the main draw function:

assert(glGetError() == GL_NO_ERROR);

Then if this triggers, I start spreading more of these checks across the code until I narrowed it down to a specific call. Once I found and fixed the errors, I remove those extra calls again.

Having the checks in place after each call all the time obviously tells you much more quickly where exactly the error happened. But I would find having the checks all over the place distracting when reading and maintaining the code. You really have to figure out what works best for you.

Upvotes: 1

Related Questions