[SOLVED] GLU_TESS_VERTEX looping problem


I’m having some trouble with tesselating polygons with GLU_TESS. I want to send in x-number of vertices and tesselate. I am using this code:

 double coord[3];
vector<double>::iterator PolyIter = polygon.begin();
double temp = *PolyIter;
//vector<double[3]>::iterator PolyIter = Polygon.begin();

while (PolyIter != polygon.end())
	temp = *PolyIter;
	coord[0] = temp;

	temp = *PolyIter;
	coord[1] = temp;
	temp = *PolyIter;
	coord[2] = temp;
gluTessVertex(tess,coord, coord);

The problem here is that only the last vertex coordinate is stored, the rest are overwritten. I am aware that this is a common problem, as it is stated in the documentation;

It is a common error to use a local variable for location or
data and store values into it as part of a loop. For
example: for (i = 0; i < NVERTICES; ++i) {
GLdouble data[3];
data[0] = vertex[i][0];
data[1] = vertex[i][1];
data[2] = vertex[i][2];
gluTessVertex(tobj, data, data);

This doesn't work.  Because the pointers specified by
location and data might not be dereferenced until
gluTessEndPolygon is executed, all the vertex	coordinates
but the very last set	could be overwritten before
tessellation begins.
Two common symptoms of this problem are consists of a	single
point	(when a	local variable is used for data) and a
GLU_TESS_NEED_COMBINE_CALLBACK error (when a local variable
is used for location).

Is there any way to solve this problem, as I can’t predefine how many vertices are going to be tesselated each time.

Thanks in advance

Yes your code is wrong - you cannot pass the same memory location to every vertex as the third parameter in gluTessVertex and expect it to work.

It is not that hard to fix - but I would need to know how you are handling the other callbacks before I can suggest what you should change it to. Post all your code to do with the tessellation to be safe.

I got it working now, I created a struct that had a double[3] array, and created a new variable for each point, so that the memory location didn’t stay the same each time.

struct coord
	double table[3];


vector<coord>::iterator PolyIter = polygon.begin();

while (PolyIter != polygon.end())
  gluTessVertex(tess,PolyIter->table, PolyIter->table);

Thanks for the answer though =)!