Whenever I make semantic or syntax errors in OpenGL, either I get a black screen or the program crashes. I looked up on internet on how to do error handling in OpenGL and in the documentations I found glGetError()
. To my understanding, glGetError()
will return one or more error code(s) if I call it after calling a "normal" OpenGL function, provided I have made some error. Also I have to call it in while loop to get all the errors.
I have multiple problems with this. One I can't find the line number on which the error has occurred, second it doesn't tell me properly why the error occurred (not a big problem though) and lastly I have to include a while loop after each OpenGL function call.
Here's an example,
unsigned int buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW);
Now if I write it with glGetError()
, it would look something like
unsigned int err = 0;
glGenBuffers(1, &buffer);
while( !(err = glGetError()) ){
std::cout << err;
}
glBindBuffer(GL_ARRAY_BUFFER, buffer);
while( !(err = glGetError()) ){
std::cout << err;
}
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), vertices, GL_STATIC_DRAW);
while( !(err = glGetError()) ){
std::cout << err;
}
Surely this isn't the way to go, right ? The source code looks so ugly.
How do I do proper error handling with OpenGL ?
My graphics card supports OpenGL 4.4 and I am using C++ .
while ( err = glGetError() ) )
$\endgroup$GL_NO_ERROR
which I assumed to have value 0, when there is no error, that's why I had!=
there. In any case it should bewhile((err = glGetError()) != GL_NO_ERROR)
. $\endgroup$