PDA

View Full Version : Problems with GL/GLUT used with OO



daviem01
04-19-2001, 04:40 PM
Hey all,

I'm a newbie to OpenGL, but I've run into what seems to be 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 (a member of the Orrery class), 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 itself 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!

rts
04-19-2001, 04:47 PM
See my reply in the beginner's forum.

Korval
04-19-2001, 04:49 PM
The problem has nothing to do with OpenGL or even GLUT; it's C++. You cannot register a callback like this that is inside of a class; it has something to do with C++ changing the function prototype and adding a 'this' pointer to it.

You can either make the callback function a global function, or you can make it a static member function of the class. Either way, you will have to find some way to give it access to the classes data members (static functions can only access other static methods and data of their class directly). A globally avaliable pointer to the object would work.