polygon tessellator

Hi

I’m trying to render arbitrary polygons using the method below.

Is it required to use callbacks and set the tesselator proerties, as I thought I could get away with the default setup.

Thanks

Graham

public void toOpenGL(final javax.media.opengl.GLAutoDrawable gLDrawable, final hedgehog.jogl.JOGLObject joglObject)
{
javax.media.opengl.GL gl = gLDrawable.getGL();
javax.media.opengl.glu.GLU glu = new javax.media.opengl.glu.GLU();
javax.media.opengl.glu.GLUtessellator tess = glu.gluNewTess(); // create a pointer to the tesselator object

    glu.gluTessBeginPolygon(tess,null);
    glu.gluTessBeginContour(tess);
    
    for (int i=0; i<mVertices.size(); i++)
    {
        double[] coords = mVertices.get(i).toArray();
        glu.gluTessVertex(tess,coords,0,coords);
    }
    
    glu.gluTessEndContour(tess);
    glu.gluTessEndPolygon(tess);
    
    glu.gluDeleteTess(tess);    // free up memory allocated to the tesselator object
}

Hi

Got it working after a bit of Googling.

The modified polygon method is:

public void toOpenGL(final javax.media.opengl.GLAutoDrawable gLDrawable, final hedgehog.jogl.JOGLObject joglObject)
{
    javax.media.opengl.GL                   gl   = gLDrawable.getGL();
    javax.media.opengl.glu.GLU              glu  = new javax.media.opengl.glu.GLU();
    javax.media.opengl.glu.GLUtessellator   tess = glu.gluNewTess();                    // create a pointer to the tesselator object
    
    // tessellator callback
    hedgehog.jogl.TessellatorCallBack tessCallback = new hedgehog.jogl.TessellatorCallBack(gl,glu);

    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_VERTEX,   tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_BEGIN,    tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_END,      tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_ERROR,    tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_COMBINE,  tessCallback);
            
    glu.gluTessBeginPolygon(tess,null);
    glu.gluTessBeginContour(tess);
    
    for (int i=0; i<mVertices.size(); i++)
    {
        double[] coords = mVertices.get(i).toArray();
        glu.gluTessVertex(tess,coords,0,coords);
    }
    
    glu.gluTessEndContour(tess);
    glu.gluTessEndPolygon(tess);
    
    glu.gluDeleteTess(tess);                                                            // free up memory allocated to the tesselator object
}

which adds the lines:

   // tessellator callback
    hedgehog.jogl.TessellatorCallBack tessCallback = new hedgehog.jogl.TessellatorCallBack(gl,glu);

    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_VERTEX,   tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_BEGIN,    tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_END,      tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_ERROR,    tessCallback);
    glu.gluTessCallback(tess, javax.media.opengl.glu.GLU.GLU_TESS_COMBINE,  tessCallback);

The tessellator callback class is given by:

public class TessellatorCallBack extends javax.media.opengl.glu.GLUtessellatorCallbackAdapter
{

////////////
// fields //
////////////

/** The GL. */
private javax.media.opengl.GL       gl;
/** The GL utility. */
private javax.media.opengl.glu.GLU  glu;

//////////////////
// constructors //
//////////////////

/**
 * Constructor.
 * @param gl The GL.
 * @param glu The GL utility.
 */
public TessellatorCallBack(javax.media.opengl.GL gl, javax.media.opengl.glu.GLU glu)
{
    super();
    this.gl  = gl;
    this.glu = glu;
}

////////////////////////
// overridden methods //
////////////////////////

/**
 * Invoked like glBegin to indicate the start of a (triangle) primitive.
 * @param type Specifics the type of begin/end pair being defined. The following values are valid: GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP, GL_TRIANGLES or GL_LINE_LOOP.
 */
@Override
public void begin(int type)
{
    gl.glBegin(type);
}

/**
 * The end callback serves the same purpose as glEnd.
 */
@Override
public void end()
{
    gl.glEnd();
}

/**
 * Is invoked between the begin and end callback methods.
 * @param vertexData Vertex data.
 */
@Override
public void vertex(Object vertexData)
{
    gl.glVertex3dv((double[])vertexData,0);
}

/**
 * Called to create a new vertex when the tessellation detects an intersection, or wishes to merge features.
 * Coordinate location is trivial to calculate, but weight[4] may be used to average color, normal, or texture coordinate data.
 * @param coords Specifics the location of the new vertex.
 * @param data Specifics the vertices used to create the new vertex.
 * @param weight Specifics the weights used to create the new vertex.
 * @param outData Reference user the put the coodinates of the new vertex.
 */
@Override
public void combine(double[] coords, Object[] data, float[] weight, Object[] outData)
{
    double[] vertex = new double[3];

    vertex[0]  = coords[0];
    vertex[1]  = coords[1];
    vertex[2]  = coords[2];
    outData[0] = vertex;
}

/**
 * The error callback method is called when an error is encountered.
 * @param errnum Error number.
 */
@Override
public void error(int errnum)
{
    System.err.println("Tessellation Error: " + glu.gluErrorString(errnum));
    System.exit(0);
}

} // class TessellatorCallBack