问题
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.
回答1:
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.
回答2:
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.
回答3:
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.
myObject.memberFunction(); // This is what the programmer sees.
memberFunction(&myObject); // This is what the compiler sees.
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.
void MyClass::memberFunction() // This is the declaration the programmer sees.
void memberFunction(MyClass* this) // This is what the compiler sees.
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!
回答4:
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
int main(int argc, char *argv[]) {
std::cout << "launching Camera ..." << std::endl;
Camera * camera = new Camera();
// ------ glut new window boilerplate ------- //
int WindowHandle = 0;
glutInit(&argc, argv);
WindowHandle = glutCreateWindow("hello there");
if(WindowHandle < 1) {
std::cerr << "ERROR: Could not create a new rendering window" << std::endl;
exit(EXIT_FAILURE);
}
// ------------------------------------------ //
camera->setup_callback();
glutMainLoop();
return 0;
}
Here is the Camera.cpp
Camera * ptr_global_instance = NULL;
extern "C" void ReshapeCamera_callback(int width, int height) {
// c function call which calls your c++ class method
ptr_global_instance->ReshapeCamera_cb(width, height);
}
void Camera::ReshapeCamera_cb(int width, int height) {
std::cout << "width " << width << " height " << height << std::endl;
}
void Camera::setup_callback() {
// c++ method which registers c function callback
::ptr_global_instance = this;
::glutReshapeFunc(::ReshapeCamera_callback);
}
and its header Camera.h
class Camera {
public:
void ReshapeCamera_cb(int width, int height);
void setup_callback();
};
Notice the use of a c++ global class pointer ptr_global_instance
来源:https://stackoverflow.com/questions/5008266/callback-function-in-freeglut-from-object