Search code examples
c++openglfunction-pointersglutfreeglut

What does the signature of a method have to be to work with glutDisplayFunc()?


So, I am initially trying to do some basic OpenGL (freeglut) stuff in C++, but I've hit a snag. I am getting the following error:

.../TextureMapper.cpp: In member function 'void TextureMapper::run()':
.../TextureMapper.cpp:67:28: error: cannot convert 'TextureMapper::display' from type 'void* (TextureMapper::)()' to type 'void (*)()'  
        glutDisplayFunc(display);
                              ^    

I think that I need display to be a function pointer (or whatever it is called, I'm not terribly familiar with that concept). However, I am doing it wrong. I've never used a function pointer before, and unfortunately the error message means nothing to me, and I can't understand the documentation that I have read. I'm a Java programmer, so it's all very foreign to me.

Here is the declaration for the member function in TextureMapper.h:

void* display(void);

And the instantiation (can't remember what this is called either) in TextureMapper.cpp:

void* TextureMapper::display(void)
{
    ...
}

I noticed that CLion was giving me a warning about this type, but I also tried this first:

void display();

But it gave a similar error.

Here is where I am trying to call glutDisplayFunc:

void TextureMapper::run()
{
    /* Run GL */
    glutDisplayFunc(display);
    glutMainLoop();
}

Solution

  • glutDisplayFunc expects a C function pointer. This pointer cannot come from a non static member function, because, in C++, member functions are mangled.

    So, to be able to make this work, one solution is to define your TextureMapper::Display as a static member function.

    So, try something like this:

    class TextureMapper
    {
    public:
        static void display(void);
        ....
    };
    

    and in texturemapper.cpp

    void TextureMapper::display(void) { \* implementation here *\}