I'm writing an application that uses Java3D to do rendering of snapshot stills of its state, in other words doesn't have a live a object graph that renders on the screen.
For every snapshot requested the application builds up a new Canvas, new SimpleUniverse and creates a new object graph and triggers a render. So far, so good. But randomly some or all objects do not render in what appears to be some sort of race condition. A scene that renders perfectly once may be completely empty when re-rendered later, although the scene contents and input is exactly the same.
The background, which is a solid color, always appears to render, though. The problem seems to occur more often when there is a larger number of objects in the graph.
Any advice or tips or theories on how to deal with this would be greatly appreciated.
This is the implementation of my OffScreenCanvas:
public class OffScreenCanvas3D extends Canvas3D { public OffScreenCanvas3D(GraphicsConfiguration config) { super(config, true); } BufferedImage doRender(int width, int height) { BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); ImageComponent2D buffer = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bImage); setOffScreenBuffer(buffer); Screen3D virtualScreen = getScreen3D(); virtualScreen.setSize(new Dimension(width, height)); virtualScreen.setPhysicalScreenWidth(0.0254/90.0 * width); virtualScreen.setPhysicalScreenHeight(0.0254/90.0 * height); renderOffScreenBuffer(); waitForOffScreenRendering(); return getOffScreenBuffer().getImage(); } }Basically, the object graph is built and then the doRender method is called to return a BufferedImage. |
Administrator
|
Hi
Does this full example work? http://www.java2s.com/Code/Java/3D/PrintCanvas3D.htm
Julien Gouesse | Personal blog | Website
|
Administrator
|
In reply to this post by tinker
This probably solves your problem:
http://codepimpsdotorg.blogspot.fr/2010/03/java3d-offscreen-rendering-and-changing.html I've found it by using a decent search engine (i.e not Google) ;)
Julien Gouesse | Personal blog | Website
|
tinker,
If Julien's ideas don't help I've onyl got a few thoughts that might get you started. Firstly I'd check very carefully to make sure that you are access your doRender method in a synchronous manner primarily by putting the keyword synchronized on it, the two calls it makes to renderOffScreenBuffer and wait ForOffScreenBuffer are async calls so you definitely don't want multiple threads calling them at the same time. I would then put a big fat Thread.sleep(5000) at the start of the method to see if the race condition can't be untangled by giving each pass some time. Though if you are single threaded this shouldn't really be required. Then if the issue is still there I would get the java3d-core code cloned from here https://github.com/hharrison/java3d-core.git Into your IDE then in the JoglPipeline.java file change private static final boolean VERBOSE = false; to private static final boolean VERBOSE = true; And compare a working render to a non-working render, perhaps pasting the output here for comment. |
I've looked at the examples Julien posted before, and they seem to work as they are. Did not have the patience to rework them into my own code base, but I've tinkered on...
So, I did solve the issue in the end, quite unexpectedly, by simply moving the code that sets the dimensions of the Screen3D to before I create my object graph and add them to the universe. I can't really say that I understand why that would make a difference, but it did. I have since not experienced any problems with the rendering or disappearing objects. This might make sense if one is familiar with the j3d internals, but I'm not. :) I did try to add Thread.sleep's into the doRender method before, but experienced no difference. The rendering process is triggered through a single-threaded ExecutorService, so there should have been no concurrency issues. My only guess is that there is some kind of race condition triggered inside j3d when fiddling with the screen dimensions. Thanks for your input, guys! |
That's great news, I'm glad it was a fairly simple fix.
That information might help someone in the future, so it's good to have it up on the forum, I certainly didn't know about that oddity. |
Free forum by Nabble | Edit this page |