Tesselation and Texture

Hello,

I am currently experiencing a problem with the GLUtesselator. The goal is to use tesselator to render concave polygons with textures on them.

I tried the following

gluTessCallback(tess, GLU_TESS_BEGIN, (void (__stdcall*) ()) &glBegin);
gluTessCallback(tess, GLU_TESS_END, (void (__stdcall*)(void)) &glEnd);
gluTessCallback(tess, GLU_TESS_VERTEX, (void (__stdcall*)(void)) &glVertex3dv);

glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D, planetexture[i]);

gluTessBeginPolygon(tess, 0);
gluTessBeginContour(tess);

for (int j = 0; j < corners.size(); j++) {
GLdouble* d = new GLdouble[3];
d[0] = cornersj;
d[1] = cornersj;
d[2] = cornersj;
glTexCoord3dv(d); gluTessVertex(tess,d,d);
}

gluTessEndContour(tess);
gluTessEndPolygon(tess);

The result is a dark grey polygon, but no texture. Any idea why this is happening?

I also tried to change gluTessCallback(tess, GLU_TESS_VERTEX, (void (__stdcall*)(void)) &glVertex3dv); to my own callback function gluTessCallback(tess, GLU_TESS_VERTEX, (void (__stdcall*)(void)) &vertexCB); and put the glTexCoord and glVertex call in there, but I always get type conversion errors. This is the definition I used for the callback, which is in class Viewer: void CALLBACK tessVertexCB(const GLvoid *data); What conversion do I have to use in the gluTessCallback?

Any suggestions would be welcome.
Thank you very much!

You need to supply texture coordinates. Set your own GLU_TESS_VERTEX callback. Move the call of glTexCoord3dv from your for() loop to the vertex callback. The declaration of your tessVertexCB function is correct. Remove the & sign before the function name when calling gluTessCallback

Please use [ code]/[ /code] tags around source snippets.

he result is a dark grey polygon, but no texture. Any idea why this is happening?

you are answering this for yourself in the next paragraph: your vertex callback does not generate texture coordinates.

This is the definition I used for the callback, which is in class Viewer: void CALLBACK tessVertexCB(const GLvoid *data); What conversion do I have to use in the gluTessCallback?

The simplified answer is: GLU is a C interface, you can not pass in C++ member functions as callbacks.
Class member functions have an additional implicit parameter (accessible as “this” inside the function body), you can think of them as actually having a signature like: void Viewer::tessVertexCB(Viewer this, const GLvoid data) which clearly is not compatible with the signature expected by gluTessCallback.
Use a free function or a static member function as callback instead. If you need access to an instance of the Viewer class inside the callback pass it as through the third argument of gluTessVertex and cast from GLvoid
to Viewer
inside the callback.

Also you program is leaking memory:


for (int j = 0; j < corners.size(); j++) {
    GLdouble* d = new GLdouble[3];
    // ...
}

there is no corresponding delete d; call.

Thank you very much!

Class member functions have an additional implicit parameter (accessible as “this” inside the function body), you can think of them as actually having a signature like: void Viewer::tessVertexCB(Viewer *this, const GLvoid *data) which clearly is not compatible with the signature expected by gluTessCallback.

This was the solution to the problem I was having.

About the leak: I am unable to call delete straight away, as then the plane disappears. I assume that happens, because the tesselator still needs the information from memory. To avoid the leak, I load the data into a 2D array before the tesselator and remove them once the tesselator has done its work.

Thanks again!