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 5 of 5

Thread: GLUI in seperate class?

  1. #1
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    1

    GLUI in seperate class?

    Hello,

    I'm using GLUT windows and a GLUI subwindow for my control elements. No problem so far.

    In GLUI I'm able to give some control elements a callback function. This callback function have to be a static function or must be defined in global shape. I have tried to get everything I need to do with GLUI in a seperate GUI class.

    The problem: I want my callback function also inside of the GUI class. For this to work, I have to declare it static. But from a static function I can't access variables from an object of this class.

    Is there any way to add a normal member function as a callback to one of my GUI objects?

    I'm sorry if it's not very well explained, but I'm from Germany and I didn't find anything on Google.

    Regards
    Dergell

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    26
    Hello Dergell,
    I'm using Glui and Glut now. But I didn't separate them.Do you want to separate the Glui and Glut? As far as I know Glui is built on the top of Glut, so you need Glut for Glui. Another idea, have you tried to separate Glut and Glui from the main function by creating .h and .cpp for them? Or Do you have to use a class for that? Also, what do you mean by saying "This callback function have to be a static function?" which function exactly do you mean? Remember that Glut needs subroutine ( by using glutMainLoop() ).

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2011
    Location
    USA
    Posts
    23
    Quote Originally Posted by Dergell View Post
    The problem: I want my callback function also inside of the GUI class. For this to work, I have to declare it static. But from a static function I can't access variables from an object of this class.
    Since you're using glut, the callback has to be a static function and cannot be a non-static class method. If this poses some limitation for you, you can try a different toolkit or native APIs such as Win32 or GLX or try a namespace with static functions.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    By the nature of GLUT and GLUI, you are going to have to have a global variable somewhere that exposes the stuff you want your functions to operate on. You can develop a framework that hides the need for a global and reduce the global variable down to just one. But you're going to need one global somewhere in your application.

    It should be noted that the new GLFW 3.0 release allows you to stick an arbitrary pointer into it's GLFWWindow instance, which you can later retrieve in your message handler. That way, you can pass through a class instance and have your handler farm the call out to the particular instance of that class.

  5. #5
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    456
    Looking at the docs/source for GLUI, it appears the callback function for controls does include a parameter that can be used to identify the control that triggered the callback, so you should be able to use this information to make your callback function then call a method.

    Code :
    class MyClass
    {
    public:
    ...
      void RealCallback()
      {
        // actual code
      }
     
      // wrapper function that simply uses the id/ctrl passed in to find the class instance to call the real callback for.
      static void Callback(int id)// or static void Callback(GLUI_control* ctrl)
      {
        MyClass *instance = convert_id_to_class_instance(id);
        instance->RealCallback();    
      }
    }

    In general terms, if you have a function call that you want to turn into a method then passing parameters is probably the best way, especially if the function accepts a pointer (or pointer sized variable) so that it can be passed the pointer to the class instance directly. If no parameters are available, then global variables are a fairly easy but untidy way of solving the problem.

    ps. Another method that hasn't been mentioned, and is a fairly extreme solution would be to use thunking, which is well described for Win32 here. It does have its uses but isn't very portable, and is more complex on Win64 than Win32.

Posting Permissions

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