Polygons

Have jReality programming problems or questions? Post them here.
Post Reply
Ronin
Posts: 50
Joined: Wed 9. Nov 2011, 17:22

Polygons

Post by Ronin » Wed 8. Feb 2012, 16:49

I'm working with polygons to reflect some data in my tool right now, using regularPolygonFactory. The problem I have is that the polygon is only rendered on one side, so the back/underside is all black and unclickable.
Is there an easy way to make a polygon with a surface on both sides?

Also, I would need the polygon to support billboarding. I'm trying to use a rotation listener so when I rotate the scene, it will use the calculateBillboardMatrix() instead of the transform set by rotation, but it doesn't seem to work too well.

Andre
Posts: 226
Joined: Fri 18. Sep 2009, 11:30

Re: Polygons

Post by Andre » Wed 8. Feb 2012, 17:09

Can you pls show an example.png about your results and past the code you are using for. Then I'll have a look into it.

Ronin
Posts: 50
Joined: Wed 9. Nov 2011, 17:22

Re: Polygons

Post by Ronin » Wed 8. Feb 2012, 17:29

Here's the polygons in top and bottom view:

Top view
Image

Bottom view
Image

As you can see, the top is colored with a face, but the bottom is just black. Top is also clickable, bottom isn't. It's as if there's no face on the bottom part of the polygon.

I had to remove the rotationListener code and such for a build earlier, but here's how I implement the polygons at least:

Code: Select all

private void setCoordinates(GeoPoint2D wellPickCoordinates) {
		double[] vertices = new double[3];
		
		vertices[0] = wellPickCoordinates.getEasting().meters();
		vertices[1] = wellPickCoordinates.getNorthing().meters();
		vertices[2] = -wellPick.getTrueVerticalDepthSubSurface().meters();
		
		IndexedFaceSetFactory factory = Primitives.regularPolygonFactory(4, 0.0);
		MatrixBuilder.euclidean().translate(vertices).scale(200, 200, 200).assignTo(this);
		
                // Some remnants from the billboarding attempt
//		SceneGraphPath cameraPath = new SceneGraphPath(getCamera(), this);
//		double[] camToWellpick = cameraPath.getMatrix(null);
//		getTransformation().setMatrix(LabelUtility.calculateBillboardMatrix(null, 1.0, 1.0, new double[]{0,0,0}, SwingConstants.CENTER, camToWellpick, vertices, Pn.EUCLIDEAN));
		
		setGeometry(factory.getGeometry());
	}

Andre
Posts: 226
Joined: Fri 18. Sep 2009, 11:30

Re: Polygons

Post by Andre » Thu 9. Feb 2012, 23:40

Sorry, but I cannot reproduce your problem. When I'm building a polygon with the PolygonFactory and displaying the geometry in a viewer I get normal results and both sides of the polygon are displayed normal, see the attached example.png (left frontside, right rearside).

Code: Select all

public class PolygonFactoryExample {
  
  public static void main(String[] args) {
    JRViewer.display(Primitives.regularPolygonFactory(4, 0.0).getGeometry());
 }
}
Attachments
polygonexample.png
polygonexample.png (43.43 KiB) Viewed 1407 times

Ronin
Posts: 50
Joined: Wed 9. Nov 2011, 17:22

Re: Polygons

Post by Ronin » Fri 10. Feb 2012, 12:46

Yeah I do find it a bit weird... Anyways, I convinced the project owner that using cubes works better, so no more billboarding or dualface problems :)

Thanks for your help anyway Andre :)

Arve
Posts: 6
Joined: Thu 29. Dec 2011, 17:33

Re: Polygons

Post by Arve » Fri 2. Mar 2012, 11:09

Hi!

I'm experiencing the same problem as Ronin. I am importing a triangulated surface and trying to display it in our standalone viewer (pretty much followed the guide in http://www3.math.tu-berlin.de/jreality/ ... one_viewer ). I then built the surface with the indexed face set factory.

Every other triangle faces the wrong way - probably caused by them being specified in clockwise and counter-clockwise orders (haven't checked).

Is there a way to explicitly enable / disable back-face culling? My initial guess is that this is related to that - it would also explain Ronin's problems.

Code: Select all

public class SurfaceVisualizable extends SceneGraphComponent {
	public SurfaceVisualizable(Surface tsurf) {
		super("Surface");
		IndexedFaceSetFactory factory = new IndexedFaceSetFactory();
		
		factory.setGenerateFaceNormals(true);
		factory.setGenerateEdgesFromFaces(true);
		factory.setGenerateVertexNormals(true);
		
		factory.setVertexCount(tsurf.vertices.size());
		factory.setVertexCoordinates(tsurf.getVertexCoordinates());

		factory.setFaceCount(tsurf.triangles.length);
		factory.setFaceIndices(tsurf.triangles);
		
		factory.setVertexColors(tsurf.getVertexColors()); // This gets a color for each point, which should give a smoothly gradiented surface.
		
		factory.update();
		setGeometry(factory.getGeometry());
	}
}
Attachments
screenshot culling.png
The problem
screenshot culling.png (62.43 KiB) Viewed 1388 times

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Re: Polygons

Post by gunn » Sat 3. Mar 2012, 18:13

If SceneGraphComponent sgc lies above your geometry in the scene graph, use the following code to enable back-face culling:

Code: Select all

Appearance ap = sgc.getAppearance();
ap.setAttribute(CommonAttributes.BACK_FACE_CULLING_ENABLED, true);
You might also try this static method from IndexedFaceSetUtility to get the normals to be oriented in the same direction:

Code: Select all

makeConsistentOrientation(IndexedFaceSet ifs);
jReality core developer

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

P.S.: Re: Polygons

Post by gunn » Sat 3. Mar 2012, 18:18

P. S. If you do use the method makeConsistentOrientation(), don't forget to also recalculate the normals using, for example, the static method (also from IndexedFaceSetUtility):

Code: Select all

calculateAndSetFaceNormals(IndexedFaceSet ifs)
jReality core developer

Arve
Posts: 6
Joined: Thu 29. Dec 2011, 17:33

Re: Polygons

Post by Arve » Mon 5. Mar 2012, 12:23

Thanks for a quick reply, Gunn. The methods did not help me, as the _makeConsistentOrientation did not change anything (probably returned false, but did not check that). Maybe because of the regularity in the inconsistency (50% of the triangles faced the wrong direction).

I changed the triangulation code instead, so that all polygons now face the same way. Looks beautiful now :-)

Off-topic: Have you considered publishing jReality using maven? That would really make it easier to set up jReality in a new project, including showing sources and javadoc in e.g. Eclipse.

Post Reply