Passing member functions for oversaturation or any other library is quite simple. GLUT is looking for a function pointer.
Let the controller be a class with the OnKeyPress member function that we want to send to glutKeyboardFunc. At first, you may be tempted to try something like
glutKeyboardFunc(&Controller::OnKeyPress);
Here we pass a pointer to a function, however this is not true since you want to send a member function of this class object. In C ++ 11, you can use the new std :: bind , or if you are using an older compiler, I would recommend boost :: bind . In any case, the syntax is about the same.
using namespace std::placeholders;
From the documentation, it looks like glutKeyboardFunc requires 3 parameters. First, we fix the first memory address of the argument of your object, since its member function, and then we supply 3 placeholders.
For those new to std :: bind, this seems weird, but for those who made object-oriented code in C, it is obvious. The function is actually just a C function and needs a "this" pointer for the class. Binding would not be necessary if the callback was a simple function.
Atifm source share