OpenGL:Tutorials:Extensions Basics

From GPWiki
Jump to: navigation, search

OpenGL Extensions


A pixel going through the render pipeline. Fragments may overwrite or modify other fragments so "fragment" is not the same as "pixel".

What's an OpenGL Extension?

Extensions are features that were added to OpenGL after its initial release, many are supported by virtually every implementation available (multitexturing for example), some are so bleeding edge that only the newest implementations (i.e. graphics cards/drivers) support them (fragment shaders for example).

What do I need to use an Extension?

The official extension registry maintained by Silicon Graphics, Inc. is available on the web at Here you will find definitive specifications for all known OpenGL extensions, including what new constants they define, how they are intended to operate and possibly some suggested uses. Equally important you will find a header file called glext.h which defines all the new constants defined for all extensions, and platform specific files for Windows and X.

What's an ARB?

ARB stands for Architecture Review Board. This is the group that decides what features will and won't be added to each official OpenGL version, and generally define in what direction OpenGL develops. This board is composed of industry heavyweights: 3dLabs, Apple, ATI, Dell, IBM, NVidia, SGI, and Sun Microsystems. Many of these companies also have vendor specific OpenGL extensions listed on the same page.

What can I do with an extension?

An extraordinary number of things. GL_ARB_occlusion_query allows you to know how many pixels on screen were affected by drawing a particular object. It might not seem particularly useful, but suppose you have a 20,000 triangle model and you want to know if it needs to be drawn this frame. Using this extension you can draw a very simple box that would encase the model (AKA bounding box), if no pixels are effected, you know the model doesn't need to be drawn. GL_ARB_fragment_program defines the interface to Fragment Shaders, the most powerful extension yet. Using this extension you can actually define small programs to be run on the graphics hardware on every fragment your application draws. The language these programs are written in is call OpenGL Shader Language (GLSL or GLSlang for short) and is very similar to C++. You can affect every aspect of the fragment from its depth value to its fog and color.

How do I use an extension?

  1. First you need the header files listed in the above extension list. You'll need glext.h and the appropriate platform header file.
  2. Second make sure your run-time environment supports it. To do that, check the return value of glGetString(GL_EXTENSIONS) for the substring matching the name of the extension you want to use.
  3. Third, unless the extension doesn't require any additional functions, you'll need to get some function pointers because the extensions aren't in the static linked .lib files. The most convenient way to do this is to use the function pointer types defined in glext.h for the extension. They begin with "PFN" and end in "PROC", between is the function name in all caps. So if you wanted to use, for instance glMultiTexCoord2f you'd define the function pointer as PFNGLMULTITEXCOORD2FPROC glMultiTexCoord2f. You'll also need to put the right address into that function pointer and to do that you use a platform specific call, in Windows it's wglGetProcAddress("name").
PFNGLMULTITEXCOORD2FPROC glMultiTexCoord2f = wglGetProcAddress("glMultiTexCoord2f");

Cross platform using SDL:

typedef void (*GL_ActiveTextureARB_Func)(unsigned int);
GL_ActiveTextureARB_Func glActiveTextureARB = 0;
/* Get function pointer */
glActiveTextureARB = (GL_ActiveTextureARB_Func) SDL_GL_GetProcAddress("glActiveTextureARB");

You may want to make the pointer global so you can access it from anywhere within your program.

That's it! You can now use that function, repeat for any additional functions you require.

External Links

  • GLee - The OpenGL Easy Extension library
  • | glew - The OpenGL Extension Wrangler Library