OpenGL extension initialization

Started by
13 comments, last by Rhiakath 14 years, 11 months ago
Hi there. I'm trying to keep my GL framework simple, and i'm currently facing a design doubt. How to initialize the extensions? Like, the framework is made up of several cpp files, so when i try to declare the extensions in an .h file, it just gives me linker errors, saying that those functions are already declared on the previous cpp file. All those files should have access to the extensions. For example, I have a couple of files for shaders, one file for general opengl operations, among other things. the shader files should have access to the function pointers, just as the general opengl file. How to do this? I tried using "external " keyword on the declaration, and then redeclaring them on the main cpp, and initialize them there. I'd assume all the cpp files would have access to the now initialized func pointers. Didn't work. Also tried creating a singleton class, only for extension pointers, with static members. Compiler borked on me. This is g++/linux. Any ideas?
Advertisement
Why don't you just use GLee or GLEW?

Otherwise, check for the GL_VERSION_[mayor]_[minor] macro before declaring your functions, for example:

#ifndef GL_VERSION_2_0// define GL 2.0 extensions here#endif

Quote:Original post by Rhiakath
Hi there. I'm trying to keep my GL framework simple, and i'm currently facing a design doubt. How to initialize the extensions? Like, the framework is made up of several cpp files, so when i try to declare the extensions in an .h file, it just gives me linker errors, saying that those functions are already declared on the previous cpp file. All those files should have access to the extensions. For example, I have a couple of files for shaders, one file for general opengl operations, among other things. the shader files should have access to the function pointers, just as the general opengl file. How to do this? I tried using "external " keyword on the declaration, and then redeclaring them on the main cpp, and initialize them there. I'd assume all the cpp files would have access to the now initialized func pointers.


Yes, it will work. Declare as extern in a single .h file. include that .h in all your .cpp files. Keep the definition in a single .cpp file.
That's what I use to do. Then I moved to glew.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Yes, I was using glew too, but i wanted to do the initialization myself, because i don't understand some minor stuff about glew.
Also, if I have the glCreateShaderARB extension, and also the glCreateShader core function, should I use the core, or the extension? Why are the pointers different? Since it's now part of core, shouldn't the pointers to the functions point to the same place?
Quote:Original post by Rhiakath
Yes, I was using glew too, but i wanted to do the initialization myself, because i don't understand some minor stuff about glew.
Also, if I have the glCreateShaderARB extension, and also the glCreateShader core function, should I use the core, or the extension? Why are the pointers different? Since it's now part of core, shouldn't the pointers to the functions point to the same place?


Because one returns a GLhandleARB, and the core version uses GLuint's as handles.

I prefer core versions, but is just me :)
rhiakath@Gentoo /usr/include/GL $ grep GLhandleARB *

glext.h:typedef unsigned int GLhandleARB; /* shader object handle */


So, they're both the same. So, the only difference is really the function pointer. Supposedly they both do the same. Do you guys know any reason for the pointer not be the same?

Also, the reason because I wanted to do my own initializing was just because I was planning to do something like, if both funcs are available, then both pointers point to the core func. That way, I would always use the call to the core func, and let the pointers do the rest durint init.
I would not have to have a gazillion if's spread across the code to see if the function is available as core, instead of extension.
If you really want to do your own initialisation (I'm doing that because I find Glee's mere code size and parse overhead unacceptable), you're probably off best writing a spec parser and automatising the process.
You can grab all the extensions in 3-4 minutes off opengl.org with wget. When a new extension that you want to use 4comes out, download that additional spec and re-run your tool for a new header/source pair.

What you need for the extension loading are the names of functions. You don't have to care about constants or everything else, all that is already in glext.h.
In every spec file, you find the names of the functions in the section labelled New\s+(Functions|Procedures)+ (it may be "functions and procedures" or "procedures and functions", sometimes it's "functions, procedures, and something else" too).
The typedefs for the function pointers in glext.h are named PFNGL(NAME-OF-FUNCTION)PROC, which is easy enough to automate.
You'll want to declare extern global variables for the function pointers and have a source file that defines each of them and that has a "init" function of some sort which retrieves the addresses. From your point of view, they'll work like plain ordinary functions later.

I'm doing the spec parsing using a PHP script, as PHP nicely supports reading files into an array, running a regex over them, and building the output header/source, eleminating duplicate elements etc. You'd probably take half a year coding that in C whereas it's a mere afternoon and under 500 lines of code (including formatting) in PHP. Other people may prefer perl instead (which I'm uncomfortable with), it sure does the job just fine, too -- use what you like.

Two things to watch for:
1. Some functions may be defined identically in two (or possibly more) extension specs, so you must make sure you don't break the one-definition-rule. PHP's array_unique() is your friend here.
2. glext.h may be erroneous. This is currently (revision of march 27, 2009) the case with ARB_instanced_arrays, which lacks the ARB suffix in its function pointer names. Unluckily, there is no easy way to deal with this in an automated manner.
GLee implements this extension wrong (in accordance with glext.h) which of course works fine. You might do the same or you could edit glext.h, or you could wait and hope that they fix it eventually (and you might not need that particular extension at all).
Quote:Original post by Rhiakath
rhiakath@Gentoo /usr/include/GL $ grep GLhandleARB *

glext.h:typedef unsigned int GLhandleARB; /* shader object handle */


So, they're both the same.

Purely an implementation detail. As far as OpenGL and the interface is concerned, the types are different; one is GLuint and one is GLhandleARB.

Quote:Original post by Rhiakath
So, the only difference is really the function pointer. Supposedly they both do the same. Do you guys know any reason for the pointer not be the same?

Supposedly they both do the same? If you cannot guarantee that they are, in fact, doing exactly the same thing, then...

Quote:Original post by Rhiakath
Also, the reason because I wanted to do my own initializing was just because I was planning to do something like, if both funcs are available, then both pointers point to the core func. That way, I would always use the call to the core func, and let the pointers do the rest durint init.
I would not have to have a gazillion if's spread across the code to see if the function is available as core, instead of extension.

... really is a bad idea. Given that you say the function pointers returned are different suggest that you should be very careful.

Even though the documentation suggests they behave the same, there could be subtle differences as allocating names from different pools, accepting slightly different parameters (maybe the ARB-variant accept some additional parameter for some other extension, like geometry shaders which is not a part of the core), or otherwise behaving different internally. Subtle differences like that may not be obvious from the documentation. The API and interface makes a difference between the function (the name), the driver exposes different entry points, so you better treat them as different.
I use GLEW in my engine, found it extremely easy to install and use and grants me instant access to the extensions I need. However, even if it says that a core function is supported as core, I still use the ARB one, simply because 1) it's easy than programming two different ways for different users based on whether it's supported or not 2) I had issues with FBO's not working when using the Core functions exposed through GLEW.
I assumed they do the same thing, not that they do it the same way.
And, of course, I'd only do this with functions that had similar signatures, right?

Now, is it really needed for me to have a thousand if's, just checking if the core function is available, and if not use the extension?
Is there no other way?

This topic is closed to new replies.

Advertisement