Sunday, January 3, 2010

Getting glGetString To Return Something Useful

Here's a small-but-useful factoid.

In OpenGL, glGetString() is the API to query the configuration of the system your code is running on, like the OpenGL version, or which OpenGL extensions are available.

However, if you call glGetString() before you have a current GL connection, no matter which configuration string you're querying, it will just return a NULL (nil) pointer.

If you're working in GLX, the solution is to call glXMakeCurrent() before calling glGetString(). That will open a current GL connection and you'll start getting strings back.

Unfortunately, most GLX tutorials and sample code either assume you know this, or use a utility library like GLUT that solves the problem for you without telling you how. After reading the man pages, this solution seems pretty obvious in retrospect. But as far as I can tell, it's only clearly spelled out in one place on the Net - until now. (That page also tells what to do on Windows.)