Glut callback functions & Wrappers

Hello everyone, i’m really disturbed with a issue that i’m working on since two or three days.
Here is the context :


class MyClass {
  ...
};

MyClass::display(void) {
  ...
}

I’v a class MyClass, with a method display(). Then i’m declaring everything for my object that i call object. And what i want to do is some glutDisplayFunc(). So i’m using some wrappers to avoid errors of kind

void (*) () expected instead of void (MyClass::) ()

… here is a solution for people who were stucked there :


struct MyWrapper {
  static void display(void) {
     u->display();
  }
  static MyClass * u;
};

MyClass MyWrapper::u = 0;

...

int main ( .. ) {

  ...
  
  MyWrapper::u = &object;
  glutDisplayFunc(MyWrapper::display);

  ...

}

Fortunately i’v passed this issue, but now what i want is implementing multiple MyClass objects in multiple windows…
But modifying the structure by simply trying to make arrays of object or something like that will stuck because of we need a reference to the concerned object. (here is this version)

I’v thought of functions pointers but i’v telled that it will not work.

Thank you for your help.

For the moment i’m using a limited amount of object wich should work until the amount of objects is not dynamically set …

Why do you want to use a member-function pointer of a variable number of objects when GLUT only allows one function being the active callback at a time anyway?

Why not implement a single update function which selects thew object dynamically based on some criterion(criteria)?

As thokra said, you can do what you want with a shared “display” function. At the top of this function, just lookup your class instance pointer from something unique to the window you’re rendering to, such as the window ID (e.g. glutGetWindow). In fact, what you’re trying to do is a specific use case listed in the glutGetWindow man page:

What i know is that each created window should have it’s own glut callback functions, so i do not see how your solutions should be implemented.
Or maybe i did not understand how Glut is managing multiple windows.

But thank you for your answers and i will try to implement these tonight, and i will post a feedback.

What i know is that each created window should have it’s own glut callback functions, so i do not see how your solutions should be implemented.

The callback setting functions (generally) work with the current window. If you have a GLUT window, and you want to set its display callback, you must first call glutSetWindowwith that window, then call glutDisplayFunc. A lot like regular OpenGL.

You still can’t use member pointers though. So you’ll have to give each window a global function, each of which calls a specific window instance’s display func.


#include <map>

std::map< int, Obj * > Window_obj;

void display()
{
  Obj *obj = Window_obj[ glutGetWindow() ];
  ...
}

void createWindows()
{
  Window_obj[ glutCreateWindow() ] = obj1;
  Window_obj[ glutCreateWindow() ] = obj2;
  ...
}

Tweak to taste.