Fractal Resurgence
Fractal Resurgence

Reputation: 317

Is GLEW mandatory for an up-to-date OpenGL application?

I've been reading on the gl.h header file included with my version of Visual Studio and it seems heavily outdated.

I do not want GLUT or any other middleware/utility library in between to do the dirty work for me, that includes GLEW. Could someone elaborate on why and how does one acquire/query for the modern feature set of the 4.0 specification and what's the idea behind GLEW in general?

Upvotes: 12

Views: 11955

Answers (4)

datenwolf
datenwolf

Reputation: 162164

The gl.h shipping with MSVC++ covers only the functions exported by the opengl32.dll shipping with Windows. This DLL is mostly only a so called "trampoline" into the actual driver. But it only exports a very old version of OpenGL, namely OpenGL-1.1.

Any functionality beyond that must be accessed through the extension mechanism.

I do not want GLUT or any other middleware/utility library in between to do the dirty work for me, that includes GLEW.

GLUT is completely unrelated to GLEW

Could someone elaborate on why and how does one acquire/query for the modern feature set of the 4.0 specification and what's the idea behind GLEW in general?

You aquire the modern feature set through the already mentioned extension system.

There is a function ?glGetProcAddress (exact name depending on the OS environment, in Windows wglGetProcAddress). Using this function you retrieve function pointers to the procedures of extensions for the current OpenGL context (in GLX the function pointers are the same for all contexts, but in Windows they may differ).

Loading a extension goes about this:

typedef (*OGLEXTP_SOMEEXTENSIONFUNCTION)(...)
OGLEXTP_SOMEEXTENSIONFUNCTION oglextp_glSomeExtensionFunction = NULL;
#define glSomeExtensionFunction oglextp_glSomeExtensionFunction;


struct Extensions
{
    bool SomeExtensionFunction;
}

errorcode initGLExtensions(){
    Extensions available;

    GLubyte extensions = glGetStrings(GL_EXTENSIONS);
    parse_extension_string(extensions, available);

    if( available.SomeExtensionFunction ) {
        oglextp_glSomeExtensionFunction = wglGwtProcAddress("glSomeExtensionFunction");
        if( !oglextp_glSomeExtensionFunction )
             return errorcode;
    }
}

And you have to write the bulk code of this for each and every function. Nobody wants to write this. So the developers of GLEW came up with a set of scripts, that fetch the extension specifications from opengl.org and automatically create the whole extension loading and wrap this into a small library, that creates no additional dependencies.

If you want to use higher OpenGL functionality: Use GLEW. Not because it's mandatory, but because it's the most straightforward way to go about this.

Upvotes: 16

TheBuzzSaw
TheBuzzSaw

Reputation: 8826

Don't be afraid of GLEW. First, I've actually seen GLEW used in Nvidia OpenGL SDK examples. It's obviously a reliable tool with a friendly license. Second, you can statically link the library and vaporize it as a dependency. (Personally, I just add the source code to my project and #define GLEW_STATIC to make it work.)

Upvotes: 3

Ben Voigt
Ben Voigt

Reputation: 283684

GLee is another library for loading OpenGL extensions, and it's pretty straightforward and easy to understand the source code (the code generator is a different story). So you can see how loading extension functions works.

Do use the SVN trunk, not the "released" version, since I worked with the author to add support for the OpenGL 4.x extensions.

What I like about GLee is that it only loads the extensions you use, in contrast to GLEW which loads 200+ different extensions, which slows down program startup and especially slows down OpenGL profiling.

Upvotes: -1

xiaoyi
xiaoyi

Reputation: 6741

You want to access OpenGL 4.0 extensions right? You may need to create a OpenGL 4.0 context and ask GLEW to bind corresponding functions.

Here is a sample with GLEW and GLFW copied from my project.

  ...

  if (!glfwInit()) {
    exit(EXIT_FAILURE);
  }

  glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 2);   // I requested OpenGL 2.1 context
  glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 1);

  if(!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW)) {  // get OpenGL context
    glfwTerminate();
    exit(EXIT_FAILURE);
  }

  if (glewInit() != GLEW_OK) {   // init glew, and bind gl* Extension functions
    glfwTerminate();
    exit(EXIT_FAILURE);
  }

  // now gl* extensions is available here

  ....

Upvotes: 0

Related Questions