Problems with using GL/GLUT in OO program

Hey all,

I’m a newbie to OpenGL, but I’ve run into a fairly esoteric problem. I am trying to render an orrery (animated Solar System model), which I’ve written a class for. This Orrery class basically uses the GL and GLUT functions within its own member functions to initialize and draw itself.

My problem is: in my main OpenGL initialization function, I can’t manage to get my own display function registered via glutDisplayFunc(). The problem is that the display function (it’s called doDisplay() ) is a member function of the Orrery class, so it’s not getting passed to glutDisplayFunc() in the same way that a normal global function would.

I’m using the following code in Visual C++ 6.0:

void ( Orrery::*memPtr ) () = doDisplay;

(which sets memPtr to the address of the doDisplay() function) and:

glutDisplayFunc(memPtr );

which should set the display function correctly.

The error message I keep getting is “error C2664: ‘glutDisplayFunc’ : cannot convert parameter 1 from ‘void (__thiscall Orrery::*)(void)’ to ‘void (__cdecl *)(void)’”. In other words, glutDisplayFunc() doesn’t like the fact that it’s being passed a member function of Orrery.

I would greatly appreciate any help!

In C++, member functions have one extra paramater passed to them: the “this” pointer. In other words, when you say:

class Foo
int bar(void);

That function (Foo::bar) actually takes one argument… a Foo*.

The solution is to declare your drawing function static. A static member function does not get this implicit paramater.

The draw back, of course, is that you can’t access member variables, since there is no ‘this’ pointer.

Hope this helps.

Thanks for your idea. It would require adding a great deal more complexity, though, to get doDisplay() to work as a static. (It really needs access to member variables.)

Is there any way to get it to work otherwise, do you think?


Keep around a global pointer to your Orrey class that you can access from doDisplay().

Thanks for all the help. I’ve decided to go back to the non-OO approach for the time being, since it works better with the way things are organized. I’ll know what to do when I try again, thanks to you!