Using OpenGL glutDisplayFunc within class

后端 未结 4 754
清歌不尽
清歌不尽 2020-12-04 16:13

I\'ve created a C++ class (myPixmap) to encapsulate the work performed by the OpenGL GLUT toolkit. The display() member function of the class cont

相关标签:
4条回答
  • 2020-12-04 16:19

    I had trouble in understanding the correct answer from Merlyn, so here it is the simplified version:

    In myPixmap.h, change display function to static member function. Should work.

    class myPixmap
    {
      //...
      static void display();
      //...
    };
    
    0 讨论(0)
  • 2020-12-04 16:23

    Please check this tutorial, I think this is what you are looking for: http://paulsolt.com/2010/08/glut-object-oriented-framework-on-github/

    0 讨论(0)
  • 2020-12-04 16:23

    Merlyn solution didnt work for me, so I made a little modification by bringing currentInstance outside of class and it worked:

    Class myPixmap;
    static myPixmap* currentInstance;
    Class myPixmap {
    ...
    }
    
    0 讨论(0)
  • 2020-12-04 16:31

    The problem is that a pointer to an instance bound member function has to include the this pointer. OpenGL is a C API, and knows nothing about this pointers. You'll have to use a static member function (which doesn't require an instance, and thus no this), and set some static data members (to access the instance) in order to use glutDisplayFunc.

    class myPixmap
    {
    private:
      static myPixmap* currentInstance;
    
      static void drawCallback()
      {
        currentInstance->draw();
      }
    
      void setupDrawCallback()
      {
        currentInstance = this;
        ::glutDisplayFunc(myPixmap::drawCallback);
      }
    };
    

    You may also have problems with C linkage vs C++ linkage, in which case you'll have to play around with extern "C". If so, you might have to use a global function, rather than a static member function as your callback, and have that call myPixmap::draw. Something like:

    class myPixmap
    {
    public:
      void draw();
    
    private:
      void setupDrawCallback();
    };
    
    myPixmap* g_CurrentInstance;
    
    extern "C"
    void drawCallback()
    {
      g_CurrentInstance->draw();
    }
    
    void myPixmap::setupDrawCallback();
    {
      ::g_CurrentInstance = this;
      ::glutDisplayFunc(::drawCallback);
    }
    

    With all of this, try to make as few changes as possible, since this is really kind of a kludge to deal w/ OpenGL being a C API.

    If you want multiple instances (I don't think most people using GLUT make multiple instances, but maybe you are), you'll have to figure out a solution using a std::map to retrieve the instance:

    static std::map<int, myPixmap> instanceMap;
    

    Where you'd get the int to resolve which instance, I am not sure :)

    FYI, you should define functions that take no parameters this way:

    void some_function() { }
    

    not

    void some_function(void) { }
    
    0 讨论(0)
提交回复
热议问题