Now i am writing a MFC program with opengl in it. And when i tried to combine opengl with MFC, i come across some problems:
First, I defined a class named:CSCommTestDlg, and defined a member function with codes as follows:
void CSCommTestDlg::OpglMain(int argc, char* argv[])
{
glutDisplayFunc(displayCude);
}
displayCube is also a member function of class CSCommTestDlg, and glutDisplayFunc is a global funcion(an opengl function), the function declarations of glutDisplayFunc is as follows:
GLUTAPI void APIENTRY glutDisplayFunc(void (GLUTCALLBACK *func)(void));
Yes, just as you can see, the parameter of glutDisplayFunc is a function pointer, and i made it point to a member function displayCube, which seems illegal, and i got an error like that:
error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'
None of the functions with this name in scope match the target type
seems that i can’t point to a member function in a global function, but i need it. How to fix it ?
glutDisplayFunc takes a global function, so, simply put, you can’t make it take a member function pointer.
One solution would be to mark displayCube as static, but that kinda ruins the point of having a class in the first place.
Another option would be to declare a global function as the glut display callback, and then inside that function, call the displayCube member function of your CSCommTestDlg object – if you can get hold of a global pointer to it.