GLU tesselation problem

Hello,

I hope someone can help me out. I added GLU tesselation for complex polygons to my renderer,
but it does not work for all cases.
It works for complex non-intersecting polygons,
I tried it with a VMAP0 data set - country boundaries for european states with 100k+ vertices. But once I try to tesselate this simple self-intersecting polygon:

0,0, 100,100, 0,100, 100,0

I get a complaint that the combine callback is not defined. Which is funny because this callback is called right before the error callback and it holds the correct new vertex position.

Also the begin, vertex and end callbacks are apparently not called at all…

I boiled this down into the following code, what am I doing wrong here?

private void drawTesselPoly(GL gl, GLU glu) {
if( this.tesselator == null)
{
tesselator = glu.gluNewTess();
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_VERTEX, this);
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_VERTEX_DATA, this);
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_BEGIN, this);
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_END, this);
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_ERROR, this);
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_COMBINE, this);
glu.gluTessCallback(this.tesselator, GLU.GLU_TESS_COMBINE_DATA, this);
}

        glu.gluTessProperty(this.tesselator, GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_NONZERO);            
                    
        TessPoint3D vertex1 = new TessPoint3D(0.0,0.0,0.0);
        
        glu.gluTessBeginPolygon( tesselator, vertex1);        
        glu.gluTessBeginContour(tesselator);

        glu.gluTessVertex(tesselator, new double[]{0, 0, 0} , new TessPoint3D(0, 0, 0));
        glu.gluTessVertex(tesselator, new double[]{100, 100, 0} , new TessPoint3D(100,100,0));
        glu.gluTessVertex(tesselator, new double[]{0, 100, 0} , new TessPoint3D(0,100,0));
        glu.gluTessVertex(tesselator, new double[]{100, 0, 0} , new TessPoint3D(100,0,0));
                                            
        glu.gluTessEndContour(tesselator);
        glu.gluTessEndPolygon(tesselator);            
  }

I defined the callbacks in my renderer class (which implements the
GLUtesselatorCallback) like this:

  public void begin(int arg0) {      
        gl.glBegin( arg0);      
  }
  
  public void beginData(int arg0, Object arg1) {
        gl.glBegin( arg0);            
  }
  
  public void vertex(Object arg0) {      
        TessPoint3D tessp = (TessPoint3D)arg0;      
        gl.glVertex3d( tessp.x, tessp.y, tessp.z);
  }

  public void vertexData(Object arg0, Object arg1) {            
        TessPoint3D tessp = (TessPoint3D)arg0;
        gl.glVertex3d( tessp.x, tessp.y, tessp.z);      
  }
  
  public void end() {
        gl.glEnd();      
  }
  
  public void endData(Object arg0) {
        gl.glEnd();            
  }
  
  public void combine(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3) {
        gl.glVertex3dv(arg0);                  
  }
  
  public void combineData(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3, Object arg4) {
        gl.glVertex3dv(arg0);
  }
  
  public void error(int arg0) {
        System.out.println("tess error: "+glu.gluErrorString(arg0));
  }
  
  public void errorData(int arg0, Object arg1) {
        System.out.println("tess errorData: "+glu.gluErrorString(arg0));            
  }

TessPoint is a small private class:

  private class TessPoint3D
  {
        public double x;
        public double y;
        public double z;
        
        public TessPoint3D(double p1, double p2, double p3) {
              
              x = p1; y = p2; z = p3;
        }            
  }   

Best regards,

Greg

Hey Greg,
I faced a similar problem and in my case I was getting the same point as an output of tessellation.

Replace your vertex callback function with this

public void vertex(Object vertexData)
{
Point p = new Point();
if (vertexData instanceof double[])
{
double[] myData = (double[]) vertexData;
if (myData.length == 6)
{
double[] d2 = {myData[0], myData[1], myData[2]};
p = new Point(myData[0], myData[1]);
pointList.add§;
gl.glVertex3dv(d2);
}
else
{
double[] d2 = {myData[0], myData[1], myData[2]};
p = new Point(myData[0], myData[1]);
pointList.add§;
gl.glVertex3dv(d2);
}
}

Sorry, i hit post before completing the reply!

Ok, here i continue with the reply.

Disregard the Point data structures. Its just my internal representation class.

public void combine( double[] coords,Object[] data, float[] weight, Object[] outData) {
int i;
double[] vertex = new double[6];
vertex[0] = coords[0];
vertex[1] = coords[1];
vertex[2] = coords[2];

        for (i=3; i < 6; i++)
        {
              vertex[i] = weight[0] * ((double[])data[0])[i] 
                                            + weight[1] * ((double[])data[1])[i]
                                            + weight[2] * ((double[])data[2])[i]
                                            + weight[3] * ((double[])data[3])[i];
        }



  outData[0] = vertex;

}

Also, when you are passing the points to the gluTessVertex(GLUtessellator, data, vertexData) function first fill in a 2-D array of points say P[numPoints][3] and fill it in with the polygon vertices that you need to tessellate. If you want to add additional information about vertex colors then make another array PData[numPoints][6] and fill it in with the first 3 data members as your vertex co-ordinates (x, y, z) and the other 3 as (r, g, b) triples.

now when call gluTessVertex(tesselator, P[i], PData[i]) for every point “i” in your polygon.

This should solve the problem.
It did for me!

Hope that helps!
Poonam

[quote]Hello,
public void combine(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3) {
gl.glVertex3dv(arg0);
}

  public void combineData(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3, Object arg4) {
        gl.glVertex3dv(arg0);
  }

[/quote]
While not an error in the strict sense, the combine callback should not be sending vertices to OpenGL. The intention of this callback is to give you the opportunity to combine your own data structure into a new object. The last Object[] parameter is an output parameter. The idea is that you create the new object and put it in this array. An example of how you could implement it is as follows:

public void combine(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3) {
  arg3[0] = new TessPoint3D(arg0[0], arg0[1], arg0[2]);
}

I don’t remember for sure, but I think the reason why you may be getting an error is that the arg3 parameter contains null if you don’t fill it yourself.
As an aside, if your point TessPoint3D will only contain x,y,z coordinates, it is not needed. You can replace

glu.gluTessVertex(tesselator, new double[]{0, 0, 0} , new TessPoint3D(0, 0, 0));

with

double[] point = new double[] {0,0,0};
glu.gluTessVertex(tesselator, point, point);

and

public void combine(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3) {
  arg3[0] = new TessPoint3D(arg0[0], arg0[1], arg0[2]);
}

with

public void combine(double[] arg0, Object[] arg1, float[] arg2, Object[] arg3) {
  arg3[0] = arg0;
}

The reason why the glu tesselator makes the distinction between the parameters is so that if for instance you have a point object that also contains a color, that you can interpolate the color as well in the combine method.