I have created a virtual class with a basic draw() method which doesn't do anything. The purpose of this is that other classes, shapes and other stuff, which will be able to draw themselves in OpenGL, will inherit this virtual class, allowing me to create an array of pointers to many different classes. The idea behind this was that I was hoping I would be able to pass a pointer to this array into my glutDisplayFunc callback. (This happens to be named drawScene(). Unfortunately I don't seem to be able to pass anything to it, as glutDisplayFunc is designed to take a method which takes no parameters and return nothing.
Is there any way of passing arguments to callback functions, and then any way of passing a pointer into my drawScene function?
(TLDR? See below.)
Essentially I want to be able to do this:
class a{ ... };
void drawScene( a** a_array_pointer){ ... }
glutDisplayFunc(drawScene); // <-- How do I pass an argument into this?
Your must parametrize your drawScene with the pointer.
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.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.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?