GLU call backs.

ok, I’m trying to do some tesselation work with the GLU.

I’ve have the data entered into the GLU using…

glu.gluBeginPolygon(tess);
    for (double[][] ring:rings) {
        for (double[] coord:ring) {
            double[] coord2 = new double[3];
            coord2[0] = coord[0];
            coord2[1] = coord[1];
            coord2[2] = 0;
            glu.gluTessVertex(tess,coord2,null);
        }
        glu.gluNextContour(tess,GLU.GLU_UNKNOWN);
    }
    glu.gluEndPolygon(tess);

and that seems to work ok, however… the call backs are handing back begins, ends and vertexs

the begins are cool, they are telling me if its meant to be a fan or strip etc… we are getting back the right number of vertexs, but vertexs are all nulls.

apart from this I seem to be doing well, but I don’t get how to get the data back.

— Blair

sorry, full code given here…

public static void tesselate(Vector<double[][]> rings, GLU glu) {
    // this turns rings into gl primitives.
    System.out.println("new Tess!");
    GLUtesselator tess = glu.gluNewTess();
    glu.gluTessProperty(tess,GLU.GLU_TESS_BOUNDARY_ONLY,GLU.GLU_FALSE);
    glu.gluTessProperty(tess,GLU.GLU_TESS_WINDING_RULE,GLU.GLU_TESS_WINDING_ODD);
    
    //set up the call back.
    TessCallback callBack = new TessCallback(glu);
    glu.gluTessCallback(tess,GLU.GLU_TESS_BEGIN,callBack);
    glu.gluTessCallback(tess,GLU.GLU_TESS_VERTEX,callBack);
    glu.gluTessCallback(tess,GLU.GLU_TESS_END,callBack);
    glu.gluTessCallback(tess,GLU.GLU_TESS_ERROR,callBack);
    
    // load the data into the glu.
    glu.gluBeginPolygon(tess);
    for (double[][] ring:rings) {
        for (double[] coord:ring) {
            double[] coord2 = new double[3];
            coord2[0] = coord[0];
            coord2[1] = coord[1];
            coord2[2] = 0;
            glu.gluTessVertex(tess,coord2,null);
        }
        glu.gluNextContour(tess,GLU.GLU_UNKNOWN);
    }
    glu.gluEndPolygon(tess);
    
    //The callback should have been called a whole heap, we are done.
}

public static class TessCallback extends GLUtesselatorCallbackAdapter {
    
    GLU glu;
    
    public TessCallback(GLU glu) {
        this.glu = glu;
    }
    
    public void vertex(Object obj) {
        double[] coord = (double[]) obj;
        if (coord!=null) {
            System.out.println("Tess - vertex (" + coord[0] + "," + coord[1] + ")");
        } else {
            System.out.println("Tess - null vertex");
        }
    }
    
    public void error(int error) {
        System.out.println("Tess - GLU TESS ERROR!!! - " + glu.gluErrorString(error));
    }
    public void end() {
        System.out.println("Tess - end");
    }
    
    public void begin(int param) {
        System.out.print("Tess - begin");
        switch (param) {
            case (GL.GL_TRIANGLE_FAN):
                System.out.print(" FAN");
                break;
            case (GL.GL_TRIANGLE_STRIP):
                System.out.print(" STRIP");
                break;
            case (GL.GL_TRIANGLES):
                System.out.print(" TRIANGLES");
                break;
            default :
                System.out.print(" ERROR -tess being not reconised");
        }
        System.out.println();
    }
}

the output looks like…

new Tess!
Tess - begin FAN
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - end
Tess - begin STRIP
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - end
Tess - begin STRIP
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - end
Tess - begin TRIANGLES
Tess - null vertex
Tess - null vertex
Tess - null vertex
Tess - end

which is cool except for the vertexs being null.

Does anyone have any ideas whats I’m doing wrong?

— Blair (thanks for any help!)

The problem is this call:

glu.gluTessVertex(tess,coord2,null);

You should replace this with

glu.gluTessVertex(tess,coord2,coord2);

The tesselator uses the third Object parameter as the value to pass back to the vertex callback. The rationale behind this is that GLU does not limit you to one specific vertex representation. In your application you might wish to hold some extra data besides x,y,z coordinates (e.g a color for the vertex). Say for instance vertices are represented using the following class in your app

class Vertex {
  int x, y, z;
  Color color;
}

If you pass Vertex objects as the data parameter, these instances will also be passed to the vertex callback. What’s more interesting though is that these objects are also passed to the combine callback. This allows you to for instance make a linear interpolation of all the colors that are being combined.
Because you can use any arbitrary class to represent a vertex the tesselator has no way to figure out the coordinates of the vertex from the data parameter alone. That’s why you have to specifiy the coordinates of the vertex explicitly as the second (location) parameter to gluTessVertex.

cheers!

Its now working a treat. Thanks for the help :slight_smile:

— Blair (happy Blair!)