Using OpenGL glutDisplayFunc within class
The problem is that a pointer to an instance bound member function has to include the this
pointer. OpenGL is a C API, and knows nothing about this
pointers. You'll have to use a static member function (which doesn't require an instance, and thus no this
), and set some static data members (to access the instance) in order to use glutDisplayFunc
.
class myPixmap
{
private:
static myPixmap* currentInstance;
static void drawCallback()
{
currentInstance->draw();
}
void setupDrawCallback()
{
currentInstance = this;
::glutDisplayFunc(myPixmap::drawCallback);
}
};
You may also have problems with C linkage vs C++ linkage, in which case you'll have to play around with extern "C"
. If so, you might have to use a global function, rather than a static member function as your callback, and have that call myPixmap::draw
. Something like:
class myPixmap
{
public:
void draw();
private:
void setupDrawCallback();
};
myPixmap* g_CurrentInstance;
extern "C"
void drawCallback()
{
g_CurrentInstance->draw();
}
void myPixmap::setupDrawCallback();
{
::g_CurrentInstance = this;
::glutDisplayFunc(::drawCallback);
}
With all of this, try to make as few changes as possible, since this is really kind of a kludge to deal w/ OpenGL being a C API.
If you want multiple instances (I don't think most people using GLUT make multiple instances, but maybe you are), you'll have to figure out a solution using a std::map to retrieve the instance:
static std::map<int, myPixmap> instanceMap;
Where you'd get the int
to resolve which instance, I am not sure :)
FYI, you should define functions that take no parameters this way:
void some_function() { }
not
void some_function(void) { }
How to pass non static member function into glutDisplayFunc
You'll need to create a "thunk" or "trampoline": C API function callbacks into C++ member function code
how to point to a member function in a global function in C++?
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.
How to create a class to wrap GLUT?
Unfortunately the glutDisplayFunc()
doesn't take a void*
pointer so you could've fake an object context. You will have to make a static function that can call into the correct IO instance using a static variable.
I see some slight trouble with your pattern also, though. As far as I know, glutMainLoop()
never returns until you terminate the GLUT context, therefore you have a constructor that practically never returns, so your program flow is unreasonable. You should move that call into a separate run()
method in your class.
(Personally I would use GLFW, which avoids the entire callback mess with GLUT, although you have to write your mainloop.)
Related Topics
How to Define a Move Constructor
Converting a Vector<Int> to String
How to Use Capturestackbacktrace to Capture the Exception Stack, Not the Calling Stack
How to Use Cmake_Export_Compile_Commands
Is There a Reason Declval Returns Add_Rvalue_Reference Instead of Add_Lvalue_Reference
Noexcept, Stack Unwinding and Performance
Do Stl Maps Initialize Primitive Types on Insert
Qt 4.X: How to Implement Drag-And-Drop Onto the Desktop or into a Folder
Is It Always the Case That Sizeof(T) >= Alignof(T) for All Object Types T
Portable Branch Prediction Hints
Is Std::String Thead-Safe with Gcc 4.3
Is It Valid for a Lambda To, Essentially, Close Over Itself
C++ Performance of Accessing Member Variables Versus Local Variables
What Could Go Wrong If Copy-List-Initialization Allowed Explicit Constructors
Direct Boost Serialization to Char Array
Why Does Visual Studio Not Perform Return Value Optimization (Rvo) in This Case