How to use CLEW

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.

  • Get the CLEW code from Github:
$ git clone https://github.com/martijnberger/clew.git
  • Compile CLEW:
$ mkdir build
$ cd build
$ cmake ..
$ make
  • Test if CLEW works:
$ clewTest/clewTest
num platforms: 1

If you have an OpenCL device and OpenCL library installed, then the number of platforms reported will be greater than zero.

  • For your own code, include the clew.h header file and link with the libclew.so library file. Call clewInit at the beginning. After that you can follow with any OpenCL calls.

Tried with: NVIDIA 352 drivers, NVIDIA GeForce GTX 750 Ti and Ubuntu 14.04

How to check error in GLEW

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:

const GLenum err = glewInit();

if (GLEW_OK != err)
{
    std::cout << "GLEW Error: " << glewGetErrorString(err) << std::endl;
    exit(1);
}

Tried with: GLEW 1.10 and Ubuntu 14.04

Segmentation fault at glCreateShader in wxWidgets

Problem

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.

Solution

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.

Segmentation fault on glGenVertexArrays

Problem

I was trying to compile the code from the book Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL (6th Edition). The code compiled fine, but execution failed with SIGSEGV segmentation fault at the line:

GLuint vao;
glGenVertexArrays( 1, &vao );

Solution

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:

glewExperimental = GL_TRUE; 
glewInit();

Tried with: NVIDIA GeForce 9600 GT, NVIDIA drivers 331 and Ubuntu 14.04

Instant GLEW

Instant GLEW
Instant GLEW

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 PacktPubAmazon, 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! 🙂