Search code examples
c++openglgccglx

Manually calling OpenGL functions


I've been working on creating an OpenGL function loading library which will help me call OpenGL functions when I need them.

I have a getProcAddress function which uses glX.

void* getProcAddress(const char *name)
{
    auto pr = reinterpret_cast<void*>(glXGetProcAddress(
            reinterpret_cast<const unsigned char*>(name)));

    return pr;
}

This returns the address of an OpenGL function. I get weird compiler errors if I don't use reinterpret_casts so that's why they're there.

I then define a gl* function prototype in a header file:

typedef void _GLACTIVETEXTURE(GLenum texture);

Where GLenum is defined in another header file as an enum. I then declare the function pointer in a class:

_GLACTIVETEXTURE glActiveTexture;

And then in a function called init I do:

void GLFunctions::init()
{
    glActiveTexture = (_GLACTIVETEXTURE)getProcAddress("glActiveTexture");
}

The getProcAddress function compiles by itself fine, but the above line of code won't compile. GCC throws this compiler error:

error: invalid cast to function type ‘_GLACTIVETEXTURE {aka void(GLenum)}’

And I don't know how to deal with this kind of compiler error. It makes no sense because this is a function pointer, not a function itself unless I use (). I'm not really sure what the problem here is; whether it's from my side or GCC. It's not clear. I've tried fiddling around with the pointers and voids, but all in vain and the same error message comes up. Does anyone know what's going on here and how I can properly call OpenGL functions?


Solution

  • Your typedef is wrong. You are creating an alias to a function type, not to a function pointer. This is how it is done correctly:

    typedef void (*_GLACTIVETEXTURE)(GLenum texture);