A tip for those newbie OpenGL programmers like me: OpenGL errors.

A tip for those newbie OpenGL programmers like me: OpenGL errors.

Post by <Olaf.Baey.. » Fri, 07 Feb 2003 17:45:25



A tip for those newbie OpenGL programmers like me.

I use this function (see below) to set a breakpoint on an OpenGL error and
use this function almost after every OpenGL command now. OpenGL appears to
keeps an list of occured errors, so you might have more than one error that
can be returned (and cleared) by glGetError()

glGetError() only seems to work if you have an active rendering context! If
your rendering context is not active then glGetError() itself returns with
an error, thus this loop would end with a dead-lock so I introduced this
counter of 20 times. This has helped me a lot to discover OpenGL programming
errors, and this function can be very simply removed from the final  release
version.

------------------------
static void GLClearErrors() {
    GLenum iErrTmp=0;
    int iCount=20;
    do {
        iErrTmp=glGetError();
        #ifdef _DEBUG
            if (iErrTmp!=0) {
                string sErr=(LPCTSTR)gluErrorString(iErrTmp);
                TRACE("CGLScene::ClearErrors() : Error %d
(%s)\n",iErrTmp,sErr.c_str());
                // AfxMessageBox(sErr.c_str());
            }
        #endif
        iCount--;
    } while ((iErrTmp!=0) && (iCount>=0));

Quote:}

Olaf
 
 
 

A tip for those newbie OpenGL programmers like me: OpenGL errors.

Post by Marcin Romaszewic » Sat, 08 Feb 2003 06:11:25


You really shouldn't call any OpenGL functions without a valid context.
Secondly, when you call glGetError, the error is cleared and a second
call will return GL_NO_ERROR.

If you want a function that clears GL errors, it should look like this:

if(haveContext)
    glGetError();

This will do the job. Replace <haveContext> with whatever will tell you
if you have a context bound, ie, wglGetCurrent(), glxGetCurrent(), etc.

-- Marcin


> A tip for those newbie OpenGL programmers like me.

> I use this function (see below) to set a breakpoint on an OpenGL error and
> use this function almost after every OpenGL command now. OpenGL appears to
> keeps an list of occured errors, so you might have more than one error that
> can be returned (and cleared) by glGetError()

> glGetError() only seems to work if you have an active rendering context! If
> your rendering context is not active then glGetError() itself returns with
> an error, thus this loop would end with a dead-lock so I introduced this
> counter of 20 times. This has helped me a lot to discover OpenGL programming
> errors, and this function can be very simply removed from the final  release
> version.

> ------------------------
> static void GLClearErrors() {
>     GLenum iErrTmp=0;
>     int iCount=20;
>     do {
>         iErrTmp=glGetError();
>         #ifdef _DEBUG
>             if (iErrTmp!=0) {
>                 string sErr=(LPCTSTR)gluErrorString(iErrTmp);
>                 TRACE("CGLScene::ClearErrors() : Error %d
> (%s)\n",iErrTmp,sErr.c_str());
>                 // AfxMessageBox(sErr.c_str());
>             }
>         #endif
>         iCount--;
>     } while ((iErrTmp!=0) && (iCount>=0));
> }

> Olaf


 
 
 

A tip for those newbie OpenGL programmers like me: OpenGL errors.

Post by Andrew F. Vespe » Sat, 08 Feb 2003 12:16:55



> You really shouldn't call any OpenGL functions without a valid context.
> Secondly, when you call glGetError, the error is cleared and a second
> call will return GL_NO_ERROR.

> If you want a function that clears GL errors, it should look like this:

> if(haveContext)
>    glGetError();

Sorry, Marcin, but Olaf was correct -- there can be multiple errors
saved up in any particular OpenGL implementation. This is so that
both client side and server side can keep independent error codes,
and glGetError returns the client side one first and the server side
one second. I've also done this with a device-independent / device-
dependent split, with each half having its own error status.

--
Andy V (OpenGL Alpha Geek)
"In order to make progress, one must leave the door to the unknown ajar."
Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.

OpenGL Technical FAQ: http://www.opengl.org/developers/faqs/technical.html

 
 
 

A tip for those newbie OpenGL programmers like me: OpenGL errors.

Post by Marcin Romaszewic » Sun, 09 Feb 2003 04:20:37




>> You really shouldn't call any OpenGL functions without a valid
>> context. Secondly, when you call glGetError, the error is cleared and
>> a second call will return GL_NO_ERROR.

>> If you want a function that clears GL errors, it should look like this:

>> if(haveContext)
>>    glGetError();

> Sorry, Marcin, but Olaf was correct -- there can be multiple errors
> saved up in any particular OpenGL implementation. This is so that
> both client side and server side can keep independent error codes,
> and glGetError returns the client side one first and the server side
> one second. I've also done this with a device-independent / device-
> dependent split, with each half having its own error status.

Cool, thanks. I guess I misunderstood the spec, but the man pages are
perfectly clear on this. Can you think of any particular implementation
that queues errors? Every platform I've worked on to date seems to have
only reported the last error.

-- Marcin

 
 
 

A tip for those newbie OpenGL programmers like me: OpenGL errors.

Post by Andrew F. Vespe » Sun, 09 Feb 2003 06:42:29



> Cool, thanks. I guess I misunderstood the spec, but the man pages are
> perfectly clear on this. Can you think of any particular implementation
> that queues errors? Every platform I've worked on to date seems to have
> only reported the last error.

I worked on an implementation that would have 3 -- a DI status,
a DD status and a hardware status. That didn't see the light
of day, but there were others based on the same DI. That
was back in my Digital days. I don't know if any of the
commodity implementations have this kind of organization.

Also -- it should be the first error, not the most recent one.

--
Andy V (OpenGL Alpha Geek)
"In order to make progress, one must leave the door to the unknown ajar."
Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.

OpenGL Technical FAQ: http://www.opengl.org/developers/faqs/technical.html