Catching glerrors when they occur

Hey there!

Is there anyway of making the program break when a glError occurs?

When I’m looking for causes for glErrors now Im cluttering the code with “if (glError())” statements.
Needless to say this is very time-consuming, and not that exciting…

Thanks
/Fredrik Olsson

Originally posted by FXO:
Im cluttering the code with “if (glError())” statements.
Needless to say this is very time-consuming, and not that exciting…

I think this is the way, but note that not every GL command is likely to produce such errors. You could test only when setting the pixelformat and textures. I don’t think you ever get an error while drawing. As setting video mode and textures doesn’t happen every frame, you don’t get any penalty perfomance.

Other errors, like a glBegin() without a glEnd() aren’t needed to be checked in the release code, once you have made sure that they are all in pairs.

For my engine, I use this :

Ex :

if (!Init_MultiTexture))
{
Add_Log(“MultiTexturing Error”);
return false;
}
else Add_Log(“MultiTexturing OK”);

For my engine, I use this :

Ex :

if (!Init_MultiTexture))
{
Add_Log(“MultiTexturing Error”);
return false;
}
else Add_Log(“MultiTexturing OK”);

There are lots of errors that can occur at anytime during rendering.
e.g. Stack over/underflow, invalid enums, etc.

Im not worried about performance, Im just looking for a way to see exactly what command caused the error.

Leyder Dylan, I use similar error checking for extensions and other things.

But I there are so many unpredictable places an glError() could occur in my program.

I only use a single call to glGetError(); at the end of my scene
rendering.

If it catches an error, then I trace the error back. But, I often
don’t get any errors, so I only use it as a sanity check every once
in a while.

Originally posted by FXO:
[b]There are lots of errors that can occur at anytime during rendering.
e.g. Stack over/underflow, invalid enums, etc.

Im not worried about performance, [/b]

But that errors can be fixed at compile time, you can get the sizes of the stacks by using glGet(), so you never overflow them. If you pass a invalid enum, this is a typo, once you detect it, you have not to check for errors on that part of the code.

BTW, yes, you talked about performance in your first post. You said glError() was “time-consuming”.

Hi !

Put a glGetError() at the end of the rendering code, if you do get an error there you can track it down by adding checks if you need to, if you would be lucky enough to be running Mesa yoy could set the MESA_DEBUG environment variable that prints error messages to the console.

Mikael

Here’s what I use…

#define GL_ASSERT() {GLenum sci_err; while ((sci_err = glGetError()) != GL_NO_ERROR)
cerr << “OpenGL error: " << (char *)gluErrorString(sci_err) << " at " << FILE <<”:" << LINE << endl;}

careful of the formatting.

This wont tell you which gl* command caused the error but will tell where you caught the error, which is handy if you have 2k glGetError()s sprinkled through your code. Plus it’s less to type.

hejryj, exactly what I was looking for thanks!

Azdo, I meant that finding where in
the code the glError() occured was time-consuming, not the function itself.
>Im cluttering the code with “if (glError())” statements.
>Needless to say this is very time-consuming, and not that exciting…

mikael aronsson, thats the same way I seek for error sources, not very efficient

Thank you all for your replies!
/Fredrik Olsson

Hi !

Just one thing, be careful not to put the GL_ASSERT line between glBegin() glEnd(), if I remeber correct you can’t even call glGetError() inside a glBegin/End block.

Mikael

Ok, thanks!