Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187.1K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 443 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
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.
Comments
-
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.