Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback function in freeglut from object

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.

like image 307
Marnix Avatar asked Feb 15 '11 19:02

Marnix


People also ask

How callback function is executed?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

Which opengl function is callback function?

There are three types of callbacks: window, menu, and global. Window callbacks indicate when to redisplay or reshape a window, when the visibility of the window changes, and when input is available for the window. The menu callback is set by the glutCreateMenu call described already.

What is a callback object?

What is a Callback? Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name 'call back'. More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.

When callback is executed?

The order in which listeners callback functions execute after the firing of an event is undefined. However, all listener callbacks execute synchronously with the event firing. The handle class notify method calls all listeners before returning execution to the function that called notify .


1 Answers

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.

like image 134
Thomas Avatar answered Sep 27 '22 20:09

Thomas