Search code examples
c++glutmanpage

What exactly is the data parameter of the gluTessVertex()?


Reading the manual page of the gluTessVertex(), the definition goes like this:

void gluTessVertex( GLUtesselator* tess, GLdouble * location, void * data);

I would imagine that the location is the vertex and for data, at least to me, it sounds like that would be a pointer to my C++ object so I can handle the callback properly.

data
    Specifies an opaque pointer passed back to the program with the vertex callback (as specified by gluTessCallback).

However, I see that all the examples I can find use their vertex pointer for both parameters and even in my implementation, it works only if I do that:

gluTessVertex(tobj, polygon->at(p).f_coordinates, polygon->at(p).f_coordinates);

If I don't do that, the coordinates in my output are quite random. Yet, that's hardly an opaque pointer if the GLUT interprets the data pointer as a vertex of positions...

So my question is: Am I misunderstanding the documentation or is the documentation incorrect?


List of callbacks as defined in the gluTessCallback are:

void beginData( GLenum type, void *polygon_data );
void edgeFlagData( GLboolean flag, void *polygon_data );
void vertexData( void *vertex_data, void *polygon_data );
void endData( void *polygon_data );
void combineData( GLdouble coords[3], void *vertex_data[4], 
                  GLfloat weight[4], void **outData, 
                  void *polygon_data );
void errorData( GLenum errno, void *polygon_data );

My functions are defined as such:

static void tess_callback_edge(GLboolean edge, font_impl * impl);
static void tess_callback_begin(GLenum type, font_impl * impl);
static void tess_callback_vertex(GLdouble const * vertex, font_impl * impl);
static void tess_callback_combine(
                                  GLdouble coords[3]
                                , GLdouble * vertex_data[4]
                                , GLfloat weight[4]
                                , GLdouble ** out_data
                                , font_impl * impl);
static void tess_callback_end(font_impl * impl);
static void tess_callback_error(GLenum errCode, font_impl * impl);

Solution

  • Its a bit confusing, but if you follow through the Mesa source code for example

    https://gitlab.freedesktop.org/mesa/glu/-/blob/master/src/libtess/tess.c and render.c

    The two parameters for vertexCallback are coming from different gluTess functions. By naming the parameters accordingly maybe this makes it clearer:

    static void vertexCallback(GLvoid* vertexData, GLvoid* polygonData);
    
    gluTessBeginPolygon(t,&polygonData);
    
    gluTessVertex(t,coord,&vertexData);
    

    This is why it's common to just pass the pointer to the vertex but you could also just supply anything and use it accordingly in the vertexCallback.