Passing 1 argument (pointer) to glutDisplayFunc?

好久不见. 提交于 2019-11-30 21:10:03

Is there any way of passing arguments to callback functions, and then any way of passing a pointer into my drawScene function?

No. glut is really a C library, and the glutDisplayFunc expects a function pointer as it's parameter. The only way to use variables in that callback, is to make them global or static variables of a class.

Your must parametrize your drawScene with the pointer.

#include <GL/glut.h>

template <void *T>
void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if (T == (void *)0) {
        /* handle no array */
    } else {
        /* handle array */
    }
    glutSwapBuffers();
} 

int main(int argc, char **argv)
{
    glutDisplayFunc(drawScene<(void *)0>);

    return 0;
}

This will only work at compiletime. If you need to change the behavior of drawScene at runtime, you must either recompile another module, load it using LoadLibrary/GetProcAddress and change the pointer, or you must use thread local storage and maybe locks.

It's one of the major flaws of glut, since not having an argument for display makes changing behavior of display at runtime on multiple threads very expensive because you need locks or complicated alternatives.

Unfortunately it's not possible. glutDisplayFunc wants a pointer to a function that doesn't have arguments. See the manual page.

aib

This is one of those cases where you should use a global variable.

Also see: How to pass a class method as an argument for another function in C++ and openGL?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!