我创建了一个 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;
}
}
传递给glutDisplayFunc()
的显示函数是该类的另一个成员函数:
void myPixmap::draw(void)
{
glDrawPixels( m,n,GL_RGB,GL_UNSIGNED_BYTE, pixel );
}
但是,Mac OS X 10.6.4 上的 gcc 4.2.1 拒绝编译此代码,声称:
“void (myPixmap::)()”类型的参数与“void (*)()”不匹配
有没有办法在类的成员函数中使用 GLUT 工具包,或者我必须在程序的 main
函数中调用所有 GLUT 函数?
最佳答案
问题是指向实例绑定(bind)成员函数的指针必须包含 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);
}
};
C 链接与 C++ 链接也可能存在问题,在这种情况下,您将不得不使用 extern "C"
。如果是这样,您可能必须使用全局函数而不是静态成员函数作为回调,并让 that 调用 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);
}
有了这一切,尽量少做些改动,因为在处理 OpenGL 作为 C API 的情况下,这确实是一种杂乱无章的事情。
如果您想要多个实例(我认为大多数使用 GLUT 的人不会创建多个实例,但也许您是),您必须找出使用 std::map 来检索实例的解决方案:
static std::map<int, myPixmap> instanceMap;
你会在哪里得到 int
来解析 which 实例,我不确定:)
仅供引用,您应该以这种方式定义不带参数的函数:
void some_function() { }
不是
void some_function(void) { }
关于c++ - 在类中使用 OpenGL glutDisplayFunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3589422/