Additional Callback Function Options

For each of the callback function types we have seen to this point, there is an alternative "DATA" version. The expected function prototype for these "DATA" versions has one additional void* parameter:

"callbackType"expected "cbFcn" prototype
GLU_TESS_BEGIN_DATAvoid beginCB(GLenum mode, void* data);
GLU_TESS_VERTEX_DATAvoid vertexCB(void* pvas, void* data);
GLU_TESS_END_DATAvoid endCB(void* data);
GLU_TESS_ERROR_DATAvoid errorCB(GLenum errorCode, void* data);

If you register one of these versions of a callback function, you should not also register its non-DATA version.

So what is this "void* data" parameter? Recall that there is a second parameter to gluTessBeginPolygon. In examples to this point, we have seen nullptr passed for that. The DATA versions of these callbacks allow you to have that pointer provided to the callback. One good use of this facility is to pass a pointer to your ModelView subclass as the second parameter to gluTessBeginPolygon, then you can use instance variables and methods during the processing of any of the callbacks. For example:

void MyMVSubclass::someMethodCalledAtConstructorTime(…)
{
	gluTessBeginPolygon(tObj, this);
	…
}

and then, for example:

void MyMVSubclass::beginCB(GLenum mode, void* data)
{
	MyMVSubclass* obj = reinterpret_cast<MyMVSubclass*>(data);
	obj->beginCBInstanceHandlerMethod(mode);
}