Twitter  Facebook  YouTube  E-Mail  RSS
The One Man MMO Project
The story of a lone developer's quest to build an online world :: MMO programming, design, and industry commentary
The Pains of OpenGL on Windows Eased
By Robert Basler on 2011-03-10 20:36:18
Homepage: email:one at onemanmmo dot com

One of the big pains of using OpenGL on Windows is that the Windows SDK only ships with OpenGL 1.1. There's probably a really good technical (and non-political) reason for this, but the fact remains, if you want to do anything with OpenGL on Windows, you're on your own.

What you need then, is something to bring the Windows SDK up to current standards (OpenGL 4.1 as I write this.) When I was originally trying to solve this problem (around the time of OpenGL 3.0), I came across GLee which is a cool library which makes the full OpenGL spec available easily. I didn't like their license, so I decided to write my own extension loader.

I don't think anyone should ever have to do this again, so I'm making this one public domain, do with it as you will. I'd like to hear if you use it in something, but that's not a requirement.

The neat thing about newer versions of OpenGL, is that they are really just extensions to OpenGL 1.1, so you can get all the functionality you need with a simple loader.

The current version of OpenGlExtensions supports OpenGL 4.1 and all ARB extensions. If you want to add other extensions, it is easy enough to add them to glfunctions.h. Since lots of functionality was deprecated around the time of OpenGL 3.0, it also allows you to choose if you wish to enable deprecated functions or not. I was lucky enough only to be using 2 deprecated functions when I updated.

You'll need to get the latest OpenGL SDK files: glext.h glxext.h wglext.h as well as gl/gl.h from Windows SDK.

OpenGLExtensions mostly works via C preprocessor macro magic. glfunctions.h lists all the functions and their types along with some macros which allow OpenGlExtensions to figure out what the highest OpenGL version supported is. Modify this file to add new OpenGL functions or extensions yourself (or come back here and get a newer one from time to time.)

Initialize the system by calling OpenGlExtensions::Initialize( iWantDeprecatedFunctions ); and indicate if you want deprecated functions from older versions of OpenGL enabled. If you choose to have deprecated functions enabled and call them anyway, the library asserts, which is handy for finding those functions in your code base. Once you have called Initialize, all the OpenGL functions listed in glfunctions.h will work just as if you had an SDK with headers for them. No special handling needed. If you call a function that isn't implemented in your driver, the library will go into the NotInitialized function and assert (See the example with glMatrixModeEnable below for help with this.)

OpenGlExtensions::GetVersion() will return the highest OpenGL version that has all of its functions implemented in your OpenGL driver.

We also define a bool function for each OpenGL function and extension to indicate if it is available on your OpenGL driver. The functions have the format glFunctionNameEnable() and all return true if the function is available. So for example, if we wanted to know if glMatrixMode was supported before we call it, we could call

if ( OpenGlExtensions::glMatrixModeEnable() )
glMatrixMode( GL_MODELVIEW );

You can call OpenGlExtensions::CheckForError() after OpenGL calls to check for errors. I have a macro GlCheckForIndividualFunctionError that compiles these checks out in my FINAL build.

Call OpenGlExtensions::Shutdown() when you are done with OpenGL.

Since this is a teaching hospital, you'll have to do some small amount of massaging to customize the code to either use the standard C library, or your equivalent functionality as I've left all the debugging tools I use in the code:

  • Remove includes for log.h, assert.h, dtypes.h
  • Fix up the FINAL build checks - replace final with whatever you use to check if it is a release build and add this function: inline void noop( void ) {};
  • Add a define for ARRAY_SIZE like so: #define ARRAY_SIZE( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) ) )
  • Replace one call to strcmpa with strcmp
  • Change ASSERT to assert and add the assert.h header from the standard C library
  • Redo Log* lines as prints or whatever you use for debugging output
  • Change uint32 to unsigned int

Here you are:




By Robert Basler on 2012-04-18 09:21:13
Homepage: email:one at onemanmmo dot com
I've updated the glfunctions.h file for OpenGL 4.2 and added all of the extensions. It now supports a whopping 1913 OpenGL functions.
By Anastaziel on 2012-05-11 21:31:08
Homepage: email:
I am not sure how to use this project. It keeps telling me: error: expected constructor, destructor, or type conversion before '(' token| for every function inside the glfunctions.h that is not GLFUNC.
By Anastaziel on 2012-05-11 21:43:22
Homepage: email:
I found a fix at by Alex Edward. You put the ifndef #ifndef OPENGLEXTENSIONS_H inside the file glfunctions.h. I suspect that you are using VS compilers to compile that code as with them you do not need to put this. Codeblocks with gcc needs it.

You have a bug in openglextensions.cpp.
void OpenGLExtensions::Initialize(void)

It should be
void OpenGLExtensions::Initialize(bool includeDeprecated)
and includeDeprecated should be used as well.

Could you fix these and post new files, please?
By Anastaziel on 2012-05-11 21:47:46
Homepage: email:
The only function with code in it is OpenGLExtensions::Initialize and that is wrong as per my post previously. Can you please code the rest of the functions from that class?
By Robert Basler on 2012-05-12 02:06:25
Homepage: email:one at onemanmmo dot com
Hmm, it seems half of openglextensions.cpp got mysteriously eaten. It looks better now. The correct version of Initialize is in there as well.

I think putting #ifndef OPENGLEXTENSIONS_H in glfunctions.h will cause you some other problems, as that file is meant to be included several times both in openglextensions.h and openglextensions.cpp.

Yes, I build this with Visual Studio.
By Robert Basler on 2012-05-12 02:17:42
Homepage: email:one at onemanmmo dot com
The current version includes logchannels.h, remove that line, it is only used by the Log* calls which you also need to massage as per the instructions above.
By on 2012-05-12 05:09:01
Homepage: email:
Thanks for the files.

One more thing. GetVersion is not implemented.
By Robert Basler on 2012-05-12 11:09:42
Homepage: email:one at onemanmmo dot com
Not sure what you mean, its there in the header:

static uint32 GetVersion( void ) { return mVersion; }

New Comment

Cookie Warning

We were unable to retrieve our cookie from your web browser. If pressing F5 once to reload this page does not get rid of this message, please read this to learn more.

You will not be able to post until you resolve this problem.

Comment (You can use HTML, but please double-check web link URLs and HTML tags!)
Your Name
Homepage (optional, don't include http://)
Email (optional, but automatically spam protected so please do)
Cats or dogs? (What's this?)

  Admin Log In

[Home] [Blog] [Video] [Shop] [Press Kit] [About]
Terms Of Use & Privacy Policy