Forum Stats

  • 3,827,902 Users
  • 2,260,838 Discussions
  • 7,897,403 Comments

Discussions

Creating polygons from points in Java3D

843799
843799 Member Posts: 49,999
edited Jul 30, 2002 6:32AM in Java 3D
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.

Comments

  • 843799
    843799 Member Posts: 49,999
    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[8];
    // front face
    pts[0]=A;
    pts[1]=B;
    pts[2]=C;
    pts[3]=D;
    // back face
    pts[4]=D;
    pts[5]=C;
    pts[6]=B;
    pts[7]=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[2];
    stripCounts[0]=4; // four points for first face.
    stripCounts[1]=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[2];
    contourCount[0]=1; // 1 stripCount for first face.
    contourCount[1]=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[8];
    // front face
    pts[0]=A;
    pts[1]=B;
    pts[2]=C;
    pts[3]=D;
    // hole in front face
    pts[0]=Dd;
    pts[1]=Cc;
    pts[2]=Bc;
    pts[3]=Aa;
    // back face
    pts[8]=D;
    pts[9]=C;
    pts[10]=B;
    pts[11]=A;

    stripCounts= new int[2];
    stripCounts[0]=4; // four points for first face.
    stripCounts[1]=4; // four points for hole in front face.
    stripCounts[2]=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[2];
    contourCount[0]=2; // 2 strips for first face.
    contourCount[1]=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.