Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Glut callback functions & Wrappers

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    2

    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 :

    Code :
    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

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

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

    Code :
    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 ..
    Last edited by malikcpp; 06-13-2012 at 04:14 AM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    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)?

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,220
    Quote Originally Posted by malikcpp View Post
    ...now what i want is implementing multiple MyClass objects in multiple windows..
    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:

    * http://linux.die.net/man/3/glutgetwindow

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    2
    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.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    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 `glutSetWindow`with 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.

  6. #6
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,220
    Quote Originally Posted by malikcpp View Post
    ...i do not see how your solutions should be implemented. Or maybe i did not understand how Glut is managing multiple windows.
    Code :
    #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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •