C++ 在类中使用 OpenGL glutDisplayFunc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3589422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:12:58  来源:igfitidea点击:

Using OpenGL glutDisplayFunc within class

c++openglclasscompiler-errors

提问by Nicholas Kinar

I've created a C++ class (myPixmap) to encapsulate the work performed by the OpenGL GLUT toolkit. The display()member function of the class contains most of the code required to set up GLUT.

我创建了一个 C++ 类 ( myPixmap) 来封装由 OpenGL GLUT 工具包执行的工作。display()该类的成员函数包含设置 GLUT 所需的大部分代码。


void myPixmap::display()
{
    // open an OpenGL window if it hasn't already been opened
    if (!openedWindow)
    {
        // command-line arguments to appease glut
        char *argv[] = {"myPixmap"};
        int argc = 1;
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(640, 480);
        glutInitWindowPosition(30, 30);
        glutCreateWindow("Experiment");
        glutDisplayFunc(draw);
        glClearColor(0.9f, 0.9f, 0.9f, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glutMainLoop();

        openedWindow = true;
    }  
}

The display function passed to glutDisplayFunc()is another member function of the class:

传递给的显示函数glutDisplayFunc()是该类的另一个成员函数:


void myPixmap::draw(void)
{
    glDrawPixels( m,n,GL_RGB,GL_UNSIGNED_BYTE, pixel );
}

However, gcc 4.2.1 on Mac OS X 10.6.4 refuses to compile this code, claiming that:

但是,Mac OS X 10.6.4 上的 gcc 4.2.1 拒绝编译此代码,声称:

argument of type 'void (myPixmap::)()' does not match 'void (*)()'

argument of type 'void (myPixmap::)()' does not match 'void (*)()'

Is there a way to use the GLUT toolkit inside a member function of a class, or must I call all GLUT functions within the mainfunction of the program?

有没有办法在类的成员函数中使用 GLUT 工具包,或者我必须main在程序的函数中调用所有的 GLUT 函数?

回答by Merlyn Morgan-Graham

The problem is that a pointer to an instance bound member function has to include the thispointer. OpenGL is a C API, and knows nothing about thispointers. 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.

问题是指向实例绑定成员函数的this指针必须包含该指针。OpenGL 是一个 C API,对this指针一无所知。您必须使用静态成员函数(它不需要实例,因此不需要this),并设置一些静态数据成员(以访问实例)才能使用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 thatcall myPixmap::draw. Something like:

您可能还会遇到 C 链接与 C++ 链接的问题,在这种情况下,您必须使用extern "C". 如果是这样,您可能必须使用全局函数,而不是静态成员函数作为回调,并使用调用myPixmap::draw. 就像是:

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.

有了所有这些,尽量少做一些改变,因为这真的是一种处理 OpenGL 作为 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:

如果您想要多个实例(我认为大多数使用 GLUT 的人不会创建多个实例,但也许您是),您必须找出使用 std::map 检索实例的解决方案:

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

Where you'd get the intto resolve whichinstance, I am not sure :)

你在哪里可以int解决哪个实例,我不确定:)

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

仅供参考,您应该以这种方式定义不带参数的函数:

void some_function() { }

not

不是

void some_function(void) { }

回答by shox_de_coder

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

请查看本教程,我认为这就是您要查找的内容:http: //paulsolt.com/2010/08/glut-object-oriented-framework-on-github/

回答by Tqq

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

Merlyn 解决方案对我不起作用,所以我通过将 currentInstance 带到课堂之外进行了一些修改,它起作用了:

Class myPixmap;
static myPixmap* currentInstance;
Class myPixmap {
...
}

回答by Volodymyr Balytskyy

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

我无法理解 Merlyn 的正确答案,所以这里是简化版:

In myPixmap.h, change display function to staticmember function. Should work.

myPixmap.h 中,将显示函数更改为静态成员函数。应该管用。

class myPixmap
{
  //...
  static void display();
  //...
};