## Forum Stats

• 3,815,622 Users
• 2,259,062 Discussions

Discussions

# Creating polygons from points in Java3D

I am relatively new to Java3D comming from OpenGL/C++ and I was wondering what is the method for creating polygons from vertices? I've checked the tutorials and demos that came with J3D, but they use the prebuilt shapes as their demonstration. Could someone post am explaination or point me to a site with the appropriate information? Thanks.

• I had a dog of a time doing this when I was getting started, but there is a pretty good explanation in the Java3D jump-start book.

Basically you need to get your points, lets say

Point3f A = new Point3f(0.0f, 0.0f, 0.0f);
Point3f B = new Point3f(0.0f, 0.4f, 0.0f);
Point3f C = new Point3f(0.4f, 0.0f, 0.0f);
Point3f D = new Point3f(0.4f, 0.4f, 0.0f);

You dont have to do this, but as you are likely to need to use the same point a whole number of times on one polygon it can work out a whole lot easier.

Next create an array of the points in the order they will appear as each successive face of the shape. To create a face visible from the front, you go through the points clockwise, to create a face visible from the back you go through the points anticlockwise.

Point3f[] pts=new Point3f;
// front face
pts=A;
pts=B;
pts=C;
pts=D;
// back face
pts=D;
pts=C;
pts=B;
pts=A;

Then you need to create a new stripCount array. Each Strip is a single polygon- as far as I can tell this informs the GeometryInfo how many points from the pts array to take for each face. If the sum of these numbers doesn't match the number of elements in the pts array you will have a problem.

stripCounts= new int;
stripCounts=4; // four points for first face.
stripCounts=4; // four points for second face.

Finally the contourCount tells the GeometryInfo how many of the strips in the stripCounts array make part of one face. I will explain this in a moment.

contourCount=new int;
contourCount=1; // 1 stripCount for first face.
contourCount=1; // 1 stripCount for second face.

Then create the GeometryInfo as below.

GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);

gi.setCoordinates(pts);
gi.setStripCounts(stripCounts);
gi.setContourCounts(contourCount);

You can use gi.getGeometryArray() from there to get a usable geometryArray (if you are extending Shape3D you can just use this.setGeometry(geometryArray) and you have a working shape.)

If I wanted to cut a hole in my shape, using four more points,

Point3f Aa = new Point3f(0.1f, 0.1f, 0.0f);
Point3f Bb = new Point3f(0.1f, 0.2f, 0.0f);
Point3f Cc = new Point3f(0.2f, 0.1f, 0.0f);
Point3f Dd = new Point3f(0.2f, 0.2f, 0.0f);

Point3f[] pts=new Point3f;
// front face
pts=A;
pts=B;
pts=C;
pts=D;
// hole in front face
pts=Dd;
pts=Cc;
pts=Bc;
pts=Aa;
// back face
pts=D;
pts=C;
pts=B;
pts=A;

stripCounts= new int;
stripCounts=4; // four points for first face.
stripCounts=4; // four points for hole in front face.
stripCounts=4; // four poinst for the back face.

Now we have to tell it there are two strips to the front contour, so it becomes this:

contourCount=new int;
contourCount=2; // 2 strips for first face.
contourCount=1; // 1 strip for second face.

The hole is only visible from the front, of course.

This example may well not display perfectly- I haven't checked the order of A, B, C and D is correct, but the principles should be clear.

Hope that helps, and if anyone more expert can tell me if there is anything wrong with the way I'm doing this (still quite new here myself) that would be very helpful.
This discussion has been closed.