I have a class which has functionality to initialise opengl and run it in separate thread.
My problem is: openGL callbacks such as glutDisplayFunc, glutMotionFunc etc accepts void (*f) void, and I cannot pass class member function.
ways around. 1) I can declare member function as static, but in this case I need to declare all used member variables as static, and end up declaring a whole class as a static.
2) I can use some stand alone function and declare my object as global, but its too bad.
I wonder if there are some ways around so I dont need to make my opengl class static ?? (using c++)
You either have to use a pointer to member, or a free function.
A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)
You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .
What happens if non static members are used in static member function? Explanation: There must be specific memory space allocated for the data members before the static member functions uses them. But the space is not reserved if object is not declared.
You'll need to create a "thunk" or "trampoline": C API function callbacks into C++ member function code
Since the callbacks don't take any parameters, there is not much you can do. One possibility is to create a namespace with functions and a static data member which is your current class. The functions would just forward the function calls to the existing class. This is just basically a slightly cleaner version of your #2.
namespace GLForwader
{
static MyGLClass* = new myGlClass();
void glutDisplayFunc() {myGlClass->glutDisplayFunc();}
void glutMotionFunc() {myGlClass->glutMotionFunc();}
}
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