Using the GLEW Library to access the latest OpenGL functionality
The OpenGL ABI (application binary interface) is frozen to OpenGL version 1.1 on Windows. Unfortunately for Windows developers, that means that it is not possible to link directly to functions that are provided in newer versions of OpenGL. Instead, one must get access to these functions by acquiring a function pointer at runtime. Getting access to the function pointers requires somewhat tedious work, and has a tendency to clutter your code. Additionally, Windows typically comes with a standard OpenGL header file that conforms to OpenGL 1.1. The OpenGL wiki states that Microsoft has no plans to update the gl.h
and opengl32.lib
that comes with their compilers. Thankfully, others have provided libraries that manage all of this for us by probing your OpenGL libraries and transparently providing the necessary function pointers, while also exposing the necessary functionality in its header files. One such library is called GLEW (OpenGL Extension Wrangler).
Getting ready
Download the GLEW distribution from http://glew.sourceforge.net. There are binaries available for Windows, but it is also a relatively simple matter to compile GLEW from source (see the instructions on the website: http://glew.sourceforge.net).
Place the header files glew.h
and wglew.h
from the GLEW distribution into a proper location for your compiler. If you are using Windows, copy the glew32.lib
to the appropriate library directory for your compiler, and place the glew32.dll
into a system-wide location, or the same directory as your program's executable. Full installation instructions for all operating systems and common compilers are available on the GLEW website.
How to do it...
To start using GLEW in your project, use the following steps:
Make sure that, at the top of your code, you include the
glew.h
header before you include the OpenGL header files:#include <GL/glew.h> #include <GL/gl.h> #include <GL/glu.h>
In your program code, somewhere just after the GL context is created (typically in an initialization function), and before any OpenGL functions are called, include the following code:
GLenum err = glewInit(); if( GLEW_OK != err ) { fprintf(stderr, "Error initializing GLEW: %s\n", glewGetErrorString(err) ); }
That's all there is to it!
How it works...
Including the glew.h
header file provides declarations for the OpenGL functions as function pointers, so all function entry points are available at compile time. At run time, the glewInit()
function will scan the OpenGL library, and initialize all available function pointers. If a function is not available, the code will compile, but the function pointer will not be initialized.
There's more...
GLEW includes a few additional features and utilities that are quite useful.
GLEW visualinfo
The command line utility visualinfo
can be used to get a list of all available extensions and "visuals" (pixel formats, pbuffer availability, and so on). When executed, it creates a file called visualinfo.txt
, which contains a list of all the available OpenGL, WGL, and GLU extensions, including a table of available visuals (pixel formats, pbuffer availability, and the like).
GLEW glewinfo
The command line utility glewinfo
lists all available functions supported by your driver. When executed, the results are printed to stdout
.
Checking for extension availability at runtime
You can also check for the availability of extensions by checking the status of some GLEW global variables that use a particular naming convention. For example, to check for the availability of ARB_vertex_program
, use something like the following:
if ( ! GLEW_ARB_vertex_program ) { fprintf(stderr, "ARB_vertex_program is missing!\n"); ... }
See also
Another option for managing OpenGL extensions is the GLee library (GL Easy Extension). It is available from http://www.elf-stone.com/glee.php and is open source under the modified BSD license. It works in a similar manner to GLEW, but does not require runtime initialization.