Smoothed models from an STL file

Have jReality programming problems or questions? Post them here.
Post Reply
dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Smoothed models from an STL file

Post by dunmatt » Mon 20. Aug 2012, 06:27

Hello all, I'm using jReality's oh-so-handy STL importer to load some meshes that I exported from Sketchup. They load just fine, but they're very rough. I think the point normals are screwed up or something (I'm not a graphics expert, please forgive me if I'm butchering the lingo), it looks just like rounded surfaces do when you turn off smoothShading in the polygon shader.

Do you happen to know how to fix this? Is there a smoother I can run on the component once it's loaded in from the STL?

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

Re: Smoothed models from an STL file

Post by Andre » Mon 20. Aug 2012, 11:24

can you pls post some of the images you get and if possible even add or e-mail an example STL-File. With this info and data we should be able to fix your problems.

dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Re: Smoothed models from an STL file

Post by dunmatt » Mon 20. Aug 2012, 18:31


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

Re: Smoothed models from an STL file

Post by Andre » Wed 22. Aug 2012, 11:00

I just checked your face.stl

It looks like that some of the normals are screwed up. I'll show you some screenshots:

There is one big normal failure at the upper right side of your face and some smaller at the "grid" directly in the front of the face.

I look if I can find/create some simple .stl files to check our stl-importer
Attachments
face_with_backfaceculling.png
Face with backface culling
face_with_backfaceculling.png (141.11 KiB) Viewed 2198 times
face_without_backfaceculling.png
Face without backface culling
face_without_backfaceculling.png (141.77 KiB) Viewed 2198 times

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

Re: Smoothed models from an STL file

Post by Andre » Wed 22. Aug 2012, 16:17

I've checked the stl-reader-class. It seems to be ok. That means that your data is probably screwed up, when you convert it to stl.

Have you tried to convert it in an other format? obj/vrml etc.

best
andre

dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Re: Smoothed models from an STL file

Post by dunmatt » Wed 22. Aug 2012, 16:36

Huh, I hadn't even noticed that problem, but that's not really the problem I'm trying to solve at the moment. Here are screenshots to match yours, note how much more visible the polygons are for me, that's what I'm trying to solve.

FWIW I found that sketchup has a smooth operation that seems to do what I want (in sketchup), but when I export as STL and reimport it the smoothing is lost. I was going to suggest that perhaps the STL format doesn't support smoothed geometry, but for some reason your screenshots don't have the same problem.

I can try other formats, but it wouldn't surprise me if they're wrong too, I have more faith in the exporters than I do with the source model I got from the manufacturer.
Attachments
faceCulling.jpg
faceCulling.jpg (37.94 KiB) Viewed 2196 times
faceNoCulling.jpg
faceNoCulling.jpg (37.83 KiB) Viewed 2196 times

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

Re: Smoothed models from an STL file

Post by Andre » Wed 22. Aug 2012, 16:48

Have you already tried "flat shading" for the surface?

which jRealityviewer do you use?

In general you can enable flat shading with: "Window" -> "Left Slot" -> left click on "Faces" -> toggle "Flat Shading"

dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Re: Smoothed models from an STL file

Post by dunmatt » Wed 22. Aug 2012, 17:08

No, I haven't tried that, "Window" -> "Left Slot" just shows a ~8px wide gray bar on the side with no controls in it of any sort. That's all I've ever seen it do, I just assumed it was something I could populate later if I saw fit. Is there another way to turn on flat shading?

I use JRViewer.display(rootNode);

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

Re: Smoothed models from an STL file

Post by Andre » Wed 22. Aug 2012, 17:32

Try

Code: Select all

		JRViewer v = new JRViewer();
		v.addBasicUI();
		v.setContent(rootnode);
		v.registerPlugin(new ContentAppearance());
		v.registerPlugin(new ContentLoader());
		v.registerPlugin(new ContentTools());
		v.startup();
Instead of "JRViewer.display(rootNode); "

And than again window->leftslot

dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Re: Smoothed models from an STL file

Post by dunmatt » Wed 22. Aug 2012, 17:55

Alright, the left side panel is now useful, thanks! Unfortunately the flat shading setting there seems to do exactly the same thing as the smooth shading option in the node's Appearance; it changes whether or not the round primitives are smooth, but it does nothing to the STL shapes.

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

Re: Smoothed models from an STL file

Post by gunn » Wed 22. Aug 2012, 19:38

FYI, I also tried loading the face.stl into JRViewer using "File->Load Content..." and got the "smooth-shading" effect shown in Andre's screenshots, including the blemishes. I can toggle to flat shading using the Appearance inspector in the Navigator window. So, it seems like the question to ask is why don't you get the same effect when you load this file.

Usually, not being able to do smooth shading means that the vertex normals are missing. They can be calculated using de.jreality.geometry.IndexedFaceSetUtility.calculateAndSetVertexNormals(IndexedFaceSet ifs), but it would be good to if, and if so, why, they are missing.
jReality core developer

dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Re: Smoothed models from an STL file

Post by dunmatt » Wed 22. Aug 2012, 22:35

Hmmm, I'm not sure how to use that... am I loading the model wrong?

SceneGraphComponent temp = Readers.read(new File(stlFilename));

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

Re: Smoothed models from an STL file

Post by Andre » Thu 23. Aug 2012, 09:13

Hah, now I got it.

You are using software rendering! I switched from jogl to software-rendering and got the same results. Looks like flat-shading for softwarerendering is broken...

Which OS (Linux 32bit/64bit, windows, mac etc. ) do you use?
How do you handle jReality (eclipseproject or jars)

best
andre

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

Re: Smoothed models from an STL file

Post by gunn » Thu 23. Aug 2012, 09:23

The following code is executed in de.jreality.plugin.content.ContentLoader, when you load files from JRViewer menu. You can see that it creates the face and vertex normals if they are not present.

Code: Select all

				SceneGraphComponent read = Readers.read(Input.getInput(file));
				SceneGraphComponent tempRoot = new SceneGraphComponent();
				tempRoot.addChild(read);
				tempRoot.accept(new SceneGraphVisitor() {
					@Override
					public void visit(SceneGraphComponent c) {
						if (removeAppsCheckBox.isSelected() && c.getAppearance() != null) c.setAppearance(null); 
						c.childrenWriteAccept(this, false, false, false, false, true,
								true);
					}
					@Override
					public void visit(IndexedFaceSet i) {
						if (i.getFaceAttributes(Attribute.NORMALS) == null) IndexedFaceSetUtility.calculateAndSetFaceNormals(i);
						if (i.getVertexAttributes(Attribute.NORMALS) == null) IndexedFaceSetUtility.calculateAndSetVertexNormals(i);
						if (smoothNormalsCheckBox.isSelected()) IndexedFaceSetUtility.assignSmoothVertexNormals(i, -1);
					}
				});
Since you are reading the file in directly, but the data file has face normals provided, you'll need to calculate the vertex normals yourself. You can do this with the following code:

Code: Select all

        SceneGraphComponent read = Readers.read(Input.getInput(file));
        de.jreality.geometry.IndexedFaceSetUtility.calculateVertexNormals(read);
after you read the file in.
jReality core developer

dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Re: Smoothed models from an STL file

Post by dunmatt » Sat 25. Aug 2012, 20:06

@Gunn Thanks! That was exactly the issue, your fix worked! And yeah, now that the faces are being smoothed that error on the upper right side is a lot more apparent.

Thanks everyone for your help!

Post Reply