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?
Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.
To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.
The new display callback function. Description. glutDisplayFunc sets the display callback for the current window. When GLUT determines that the normal plane for the window needs to be redisplayed, the display callback for the window is called.
Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With