OpenCL Extension Wrangler Library (CLEW) does to OpenCL what GLEW does to OpenGL. It takes out all the pain of dealing with OpenCL libraries from different vendors and on different platforms. The best part is that your code will compile and run even on a computer without a OpenCL device or library. Only when it calls a OpenCL call does it fail.
You typically check for error after initializing GLEW by calling glewInit(). It returns an error value of type GLenum. Note that GLEW merely uses this data type, it has defined its own error values based on it.
By looking at GLEW header files, I find that the error values currently supported are:
#define GLEW_OK 0
#define GLEW_NO_ERROR 0
#define GLEW_ERROR_NO_GL_VERSION 1 /* missing GL version */
#define GLEW_ERROR_GL_VERSION_10_ONLY 2 /* Need at least OpenGL 1.1 */
#define GLEW_ERROR_GLX_VERSION_11_ONLY 3 /* Need at least GLX 1.2 */
So, you typically first check if the value returned from glewInit() is GLEW_OK. If not, you further investigate what the error might be.
GLEW provides an utility function glewGetErrorString() that provides an error string given the error value.
Putting all the above together, this is how we could check for error in GLEW:
I had a simple graphics program written using OpenGL and using GLUT for windowing. To get more windowing functionality, I wanted to move it to wxWidgets. I picked an example program that uses OpenGL in wxWidgets. It compiled and worked correctly. When I placed my OpenGL code into this example, I got a segmentation fault at the glCreateShader call in my code.
I had a glewInit in my original code. I had put this in the application class constructor. Apparently, wxWidgets requires that the first OpenGL or GLEW call be placed inside the OnPaint event handler function. Look for EVT_PAINT to find this function. Once I did that, the error disappeared and OpenGL worked.
The system might be using an old version of GLEW, where glGenVertexArrays is still marked as an experimental OpenGL extension. To enable the use of such experimental extensions add these lines during initialization:
I have written a small book titled Instant GLEW about deploying OpenGL extensions in graphics programs by using the GLEW library. The book is published by PacktPub and is available from 26 July 2013. It is available in both print and ebook formats. The book is available at PacktPub, Amazon, Safari, Kobo and AbeBooks. You can rate and review it at Goodreads. Get this book if you are taking your first steps with using OpenGL extensions in your programs.
GLEW is a pretty simple library to use, but students who are studying computer graphics for the first time have a hard time using OpenGL and OpenGL extensions. This problem is all the more exacerbated by the archaic versions of OpenGL that ship with the various versions of Windows. Programmers are forced to use OpenGL extensions for features that are considered basic and essential in graphics programs. This is the problem that GLEW attacks successfully.
In the book, I explain how to install the GLEW library on Windows and customize your Visual Studio solution to use it for compiling and linking your programs. I illustrate the usage of GLEW by using it to apply vertex and fragment shader programs on a teapot in 3D. Using shaders in an OpenGL program is probably the most popular use of OpenGL extensions and that is the reason I picked this example. You do not need to know GLSL or shader programming to understand this example.
In later sections of the book, I explain the other useful APIs of GLEW which help you write clean and portable programs that can fallback on older rendering techniques when advanced methods are not available. I also introduce a few useful utilities that make using OpenGL extensions informative and fun. Finally, there might be times when you need to access a bleeding-edge OpenGL extension that is not yet supported in GLEW. I show how to use such a new extension in your programs. It can get messy, but I show you how to get it done.
All said, this tiny book should be useful to students and programmers to quickly start using GLEW in their programs. If you find yourself using OpenGL extensions, do try Instant GLEW! 🙂