What is wrong with my code that it crashes when gluTessEndPolygon is reached? Note I’m using C++/CLI wrapper wround the glu library to get it working and called from C#.
public ref class GluTessellator
{
// TODO: Add your methods for this class here.
private:
static GLUtesselator *tess;
static TessellatedSurface^ tessSurf;
public: GluTessellator()
{
tess = gluNewTess();
tessSurf = gcnew TessellatedSurface();
if (tess)
{
gluTessCallback(tess, GLU_TESS_BEGIN, (void (__stdcall *)())begin);
gluTessCallback(tess, GLU_TESS_END, (void (__stdcall *)())end);
gluTessCallback(tess, GLU_TESS_VERTEX, (void (__stdcall *)())vertex);
gluTessCallback(tess, GLU_TESS_COMBINE, (void (__stdcall *)())combine);
gluTessCallback(tess, GLU_TESS_ERROR, (void (__stdcall *)())error);
}
}
public: ~GluTessellator()
{
gluDeleteTess(tess);
tessSurf = nullptr;
}
public: TessellatedSurface^ TessellateSurface(DrawingSurface^ drawingSurf)
{
gluTessBeginPolygon(tess, NULL);
gluTessBeginContour(tess);
double *vdata;
int i = 0;
for each(Point3D^ p in drawingSurf->outline->poly->Points)
{
vdata = new double[5];
vdata[0] = p->X;
vdata[1] = p->Y;
vdata[2] = p->Z;
vdata[3] = 0;// drawingSurf.outline.texCoords[i].s;
vdata[4] = 0;// drawingSurf.outline.texCoords[i].t;
gluTessVertex(tess, vdata, vdata);
i++;
}
gluTessEndContour(tess);
for each(DrawingPolygon^ hole in drawingSurf->holes)
{
gluTessBeginContour(tess);
i = 0;
for each(Point3D^ p in hole->poly->Points)
{
vdata = new double[5];
vdata[0] = p->X;
vdata[1] = p->Y;
vdata[2] = p->Z;
vdata[3] = 0;// hole.texCoords[i].s;
vdata[4] = 0;// hole.texCoords[i].t;
gluTessVertex(tess, vdata, vdata);
i++;
}
gluTessEndContour(tess);
}
gluTessEndPolygon(tess);
return tessSurf;
}
static void __stdcall begin(GLenum type)
{
DrawingPrimitive^ prim = gcnew DrawingPrimitive();
prim->primType = type;
tessSurf->primitves->Add( prim );
}
static void __stdcall end()
{
}
static void __stdcall vertex(GLvoid *vertexData)
{
DrawingPrimitive^ prim = tessSurf->primitves[tessSurf->primitves->Count - 1];
prim->vertices->Add( gcnew VertexCoord( ((double *) vertexData)[0], ((double *) vertexData)[1], ((double *) vertexData)[2] ) );
prim->texcoords->Add( gcnew TextureCoord( ((double *) vertexData)[3], ((double *) vertexData)[4] ) );
}
static void __stdcall combine(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **outData)
{
double *v = (double *)malloc(5 * sizeof(double));
v[0] = coords[0];
v[1] = coords[1];
v[2] = coords[2];
for( int i = 3; i < 5; i++ )
{
v[i] = 0;
for( int j = 0; j < 4; j++ )
if( vertex_data[j] != 0 )
v[i] += weight[j] * vertex_data[j][i];
}
*outData = v;
}
static void __stdcall error(GLenum errCode)
{
const GLubyte *errStr;
errStr = gluErrorString(errCode);
}
};