Checking for errors in OpenGL
OpenGL will not explicitly notify you when errors occur. Instead,
you can use the
glGetError() and
gluErrorString()
functions to ask OpenGL if errors have occured.
Example C++ code
void checkGLErrors(const char *label) {
GLenum errCode;
const GLubyte *errStr;
if ((errCode = glGetError()) != GL_NO_ERROR) {
errStr = gluErrorString(errCode);
cerr << "OpenGL ERROR: " << (char*)errStr << "; Label: " <<
label << endl;
}
}
checkGLErrors("label") checks if any errors have occurred
since the last call to
checkGLErrors (or since the
program started, on the first call). If so, it prints a message
identifying the error (within OpenGL's somewhat limited set of
error messages), along with the identifying label.
Typical usage
You can break your OpenGL code into logical chunks and surround
each with a pair of labeled
checkGLErrors calls, i.e.:
checkGLErrors("begin foo");
// code using OpenGL to do "foo"
checkGLErrors("end foo");
If you see an error labeled with
end foo, then OpenGL is
unhappy with some call you made within the "foo" chunk. If you
see
begin foo, then an error occurred somewhere else,
before this code ran. If you see no error output, then your
OpenGL calls were all semantically correct (of course, this
doesn't mean the output is right!)
You can do a binary search with this technique, bracketing smaller
and smaller pieces of code until you isolate the error to a single
call.
Common sources of errors
The most common source of OpenGL errors is probably misplaced
GLenum constants. In C/C++, all of OpenGL's symbolic
constants (e.g.,
GL_LIGHTING,
GL_FRONT,
GL_MODELVIEW) are of the same type,
GLenum.
Therefore, the type system will not prevent you from making a
nonsensical call like
glMaterialfv(GL_MODELVIEW, GL_FRONT, 0);
This will cause a
GL_INVALID_ENUM error.