Yeah, OpenGL extensions sucks, but we have to live with it.
To get extensions work on different platforms often means various mechanisms to get the extensions' function pointer, deal with different calling conventions, etc.
The easiest way to deal with such mess is use a library like GLEW and forget about it. But sometimes when I code my own little stuff, I only use a very small set of extensions(often times I just want to test the extensions functionality) , GLEW's mega-header is often very browse/search unfriendly, and if the GLEW's version is out of date, I have to download it again.
I used to code every single extension manually, and it's really boring and repeated work. I don't know if there exist an automatic extensions generator(I know it exist, most extensions lib is generated by such generator, I just have to find an excuse to make my own), give it a file which contain the function definition, and it output the .h and .cpp. Then I put the two files in my project and voila.
I think this is an interesting little project and recently I have been learning Python. So I give it a try using Python.
The gl extension source file looks like this
[core]
void glEnable (GLenum capability)
void glDisable (GLenum capability)
void glBlendFunc (GLenum sfactor, GLenum dfactor)
...
[GL_EXT_texture_compression_s3tc]
...
The generator read each line of the file, then using Python's regular expression module re to parse the line to see if it's an [...], which means a group of extensions. [core] group is special, which contains functions in core GL spec . Other [...] must contains it's extension's name string. And if the line is parsed as function declaration, it is split into three pieces - return type, function name and args list, then it's stored in a Python dictionary using function name as key for latter use.
When parse complete, the generator output .h and .cpp. The .h look like this(with a lot of hassle omitted),
//core
extern void(JGL_API_ENTRY jglEnable) (GLenum capability);
extern void(JGL_API_ENTRY jglDisable) (GLenum capability);
extern void(JGL_API_ENTRY jglBlendFunc) (GLenum sfactor, GLenum dfactor);
...
//GL_EXT_texture_compression_s3tc
...
namespace joker
{
struct glCaps{
bool support_core;
bool support_GL_EXT_texture_compression_s3tc;
};
void InitGL(glCaps** caps);
}
then in my project, I just call InitGL(&caps), init the extensions and get the caps, if the extension is supported, I can call it's jglXXX function.
the generator is halfway done, the caps check is not implemented yet, and I also want to add log version of gl functions. And it only support Mac/Windows.
The source can be found here. and the extension def file here.
Yeah, many stuff looks like Quake 3's qgl, because I steal most ideas from it :) .
And of course this is only a toy, any production code should use GLEW instead.
P.S. Now I am really confused, what's the difference between core/extension? OpenGL sucks...