How to draw solid polygon (newbie)

Please Help,

I am very new at Java3D and trying to draw a solid polygon based on a set of Points (X, Y, and Z Coordinates). I was using JOGL but like the purely Object-oriented paradigm provided by Java3D. Anyway, I have a series of Points stored in a Vector and need to draw them as a Polygon that is solid. In JOGL I used the following code:


Vector pts = getPoints();
gl.glBegin(GL.GL_POLYGON);
for (int i = 0; i < pts.size(); i++)  {
    PointData p = (PointData)pts.get(i);
    gl.glVertex3f(p.x, p.y, p.z);
}
gl.glEnd();

The problem is that when I have tried it with Java3D using the GeometryInfo Class I sometimes get strange triangulation renderings. Can anyone out there help a newbie out?

Thanks in advance.

You need to use a QuadArray.

So something like this


Point3f point0 = new Point3f(0,0,0);
Point3f point1 = new Point3f(1,0,0);
Point3f point2 = new Point3f(1,1,0);
Point3f point3 = new Point3f(0,1,0);

QuadArray quadArray = new QuadArray(4,QuadArray.COORDINATES | QuadArray.NORMALS);
quadArray.setCoordinates(0,point0);
quadArray.setCoordinates(1,point1);
quadArray.setCoordinates(2,point2);
quadArray.setCoordinates(3,point3);

Vector3f norm = new Vector3f(0f,0f,1f);
quadArray.setNormal(0,norm);
quadArray.setNormal(1,norm);
quadArray.setNormal(2,norm);
quadArray.setNormal(3,norm);

Now just add the QuadArray to the scene graph and your all set.

Thank you very much for the reply Conzar.

However, I probably should clarify the issue a little more. The Model I am rendering is made up of a series of potentially complex (contain “holes”) polygons that may or may not fall on a total vertex number that is a factor of 4. Each polygon being rendered could have any number of vertices. When this is the situation what is the best idea to do with Java3D ?

Once again, thank you for the response.

All right,

I think I am begining to understand enough about Java3D to properly ask the question that I have regarding rendering Polygons. I have a set of Polygons that I would like to render together to form a single Object. I believe the problem is occurring when I try to append multiple Geometries, the following snippet of code shows what I am trying to do (this Class extends the Shape3D Class):


public void setGeometry() {
        // The outers Vector contains a Collection of Polygon Objects (each of which is
       // composed of a series of Point3d Objects)  ...
        //
        for (int i = 0; i < this.outers.size(); i++) {
            this.gInf = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
            Polygon poly = (Polygon)this.outers.get(i);
            Point3d[] outerPts = poly.outerToArray();
            int[] stripCounts = {outerPts.length};
            int[] contourCount = {1};

            this.gInf.setCoordinates(outerPts);
            this.gInf.setStripCounts(stripCounts);
            Triangulator tr = new Triangulator();
            tr.triangulate(this.gInf);
            this.gInf.recomputeIndices();

            NormalGenerator ng = new NormalGenerator();
            ng.generateNormals(this.gInf);
            this.gInf.recomputeIndices();

            Stripifier st = new Stripifier();
            st.stripify(this.gInf);
            this.gInf.recomputeIndices();

           //this.setGeometry(this.gInf.getGeometryArray());
           this.addGeometry(this.gInf.getGeometryArray());
        }// End of FOR-LOOP
}// End of setGeometry()

When I uncomment the setGeometry(this.gInf.getGeometryArray()) and comment-out the addGeometry(this.gInf.getGeometryArray()) methods I get a correct Polygon rendering (although only the LAST polygon in the series represented by the “outers” Vector is rendered. I am hoping that this is a SIMPLE fix. Can anyone out there PLEASE help a newbie out?

Thanks in advance.

Fixed the problem!! ;D

It had nothing to do with the GeometryInfo Class. I had forgot to check the direction of the Point3d Objects to be rendered, thus the points sometimes yielded malformed triangle divisions.

Thanks everybody for all the help/hints. It really helps a newbie like myself.

Glad you got it working man!