I'm using MSVC++ and freeglut in order to use openGL. Now, I have a class called Camera
, which is pretty simple, but it also holds the function for the reshaping of my window.
My question is: how can I set glutReshapeFunc(void (*callback)(int,int))
to my function in my camera?
I have the following code, which won't work, because of a compiler error:
int main(int argc, char **argv)
{
Camera *camera = new Camera();
glutReshapeFunc(camera->ReshapeCamera);
}
and my Camera class looks like this in Camera.h:
class Camera
{
public:
Camera(void);
~Camera(void);
void ReshapeCamera(int width, int height);
};
Maybe this is just a more general callback question, but the only thing I found on the internet was creating some wrapper class around the callback. But it doesn't look like this should be so hard. Thanks in advance.
You can't connect it directly. ReshapeCamera is a member function, it needs a camera instance in order to run. The glut callback is a C function, which don't have an instance of camera to call your method. You need to create a function that call the camera reshape method.
The following demonstrates how to register a c callback function from c++ Its generally useful, not specific to glut.
Here is your client c++ program
}
Here is the Camera.cpp
and its header Camera.h
Notice the use of a c++ global class pointer ptr_global_instance
It has nothing to do with a "C vs C++" thing. You just need to understand what a member function call entails and basically what it compiles down to.
A member function is a fancy description of a function that simply takes the object as a first parameter. It's just invisible in the actual parameter list.
From there, C++ adds special semantics to make it easy to work in an object-oriented fashion. So, even though your object has a member function of the format void(int, int), it actually has the format void(Camera*, int, int), and that does not match the desired format! This is why static function members can be used for such function pointers. Ever notice that static member functions cannot access the members of "this"? That is because static member functions do not pass an instance of the object representing "this".
In effect, you could emulate much of object-oriented programming in plain C. If you make a set of functions that take a struct pointer as the first parameter, you gain many of the same advantages!
There's no clean way to do this. C does not know about objects, so pointers-to-members don't work. C does not do templates, so functors don't work. The API does not even allow you to pass an arbitrary
void *userData
to your callback, so you can't pass the object around in that way either.So, indeed, you'll have to create a wrapper function that is not a member function, and you'll have to give it access to the
Camera
object instance somehow, through a static and/or global variable.If there can be multiple objects listening for this event, you could create a singleton class that allows you to register arbitrary listeners.