Skip to main content

[Java3D] Picking problem (BranchGroup not alive)

3 replies [Last post]
casano
Offline
Joined: 2010-04-23

Hello,

I am developing a JAVA3D application, allowing open CAD 3D files, and select some object faces. The appli must be able too to close the opened file and open another one.
Everything is ok for a first opened 3D file: visualization, selection (PickCanvas used), ...
But, when closing this file, and opening another one, the face mouse selection doesn't work: I have the following error message:
java.lang.IllegalStateException: Picking can only work if BranchGroup is alive

I suppose i am badly closing the BranchGroup associated with the 3D object, or i am badly defining the BranchGroup for the new 3D object, but I don't know how to do...

Following code extracts:

public class Scene3D extends MouseAdapter implements ComponentListener {

private static Scene3D instance = new Scene3D();

private static String filename = "";
private BranchGroup objRoot = null;
private BranchGroup scene = null;
private SimpleUniverse myWorld;
private RefreshCanvas3D canvas;
private PickCanvas pickCanvas = null;
private Locale maLocale;
private Wavefront objFile;

public static Scene3D getInstance() {
return instance;
}

public Scene3D() {
super();
canvas=new RefreshCanvas3D(SimpleUniverse.getPreferredConfiguration());
myWorld=new SimpleUniverse(canvas);
myWorld.getViewingPlatform().setNominalViewingTransform();
objRoot = new BranchGroup();
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
objRoot.compile();
myWorld.addBranchGraph(objRoot);

this.maLocale = myWorld.getLocale();

}

public void chargeObjet(String filename) {
//

scene=createScene(filename);
scene.compile();

objRoot.addChild(scene);

}

public BranchGroup createScene(String filename) {
BranchGroup scene = new BranchGroup();

pickCanvas = new PickCanvas(canvas, scene);
pickCanvas.setMode(PickCanvas.GEOMETRY);
canvas.addMouseListener(new PickMouseListener(this, pickCanvas));

scene.setCapability(BranchGroup.ALLOW_DETACH);
//creation lumiere ambiante de couleur blanche
BoundingSphere bounds = new BoundingSphere(new Point3d(), 1000);
Light ambientLight = new AmbientLight(new Color3f(Color.white));
ambientLight.setInfluencingBounds(bounds);
scene.addChild(ambientLight);

//creation lumiere directionnelle de couleur blanche
Light directionalLight = new DirectionalLight(new Color3f(Color.white), new Vector3f(4, -4, -4));
directionalLight.setInfluencingBounds(bounds);
scene.addChild(directionalLight);

//Creation du groupe de transformation sur lequel vont s'appliquer les comportements
TransformGroup transformMouse = new TransformGroup();

// Le groupe de transformation sera modifie par le comportement de la souris
transformMouse.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transformMouse.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

// Creation comportement rotation a la souris
MouseRotate rotate = new MouseRotate(transformMouse);
rotate.setSchedulingBounds(new BoundingSphere());
scene.addChild(rotate);

// Creation comportement deplacement a la souris
MouseTranslate translate = new MouseTranslate(transformMouse);
translate.setSchedulingBounds(new BoundingSphere());
scene.addChild(translate);

// Creation comportement zoom a la souris
MouseZoom zoom = new MouseZoom(transformMouse);
zoom.setSchedulingBounds(new BoundingSphere());
scene.addChild(zoom);

//System.out.println("date 2 avant chargement wavefront " + new Date().getTime());
objFile = new Wavefront(filename);
float scale = objFile.getScaleFactor();
Transform3D t3d=new Transform3D();
t3d.setScale(scale);

TransformGroup transformScene=new TransformGroup(t3d);

transformScene.addChild(objFile.createObjets3D());
nbSurfaces = objFile.getNbSurfaces();
nbObjets = objFile.getNbObjets();
transformMouse.addChild(transformScene);

scene.addChild(transformMouse);

return scene;
}
}

public class PickMouseListener implements MouseListener {

private PickCanvas pickCanvas;
private Scene3D scene;

public PickMouseListener(Scene3D s, PickCanvas pick) {
super();
pickCanvas = pick;
scene = s;
}

public void mouseClicked(MouseEvent e) {

if (SwingUtilities.isLeftMouseButton(e)) {

pickCanvas.setShapeLocation(e);
PickResult result = pickCanvas.pickClosest();
....
}
}

When creating the application, I do:
JPanel zoneAffichage = new JPanel();
RefreshCanvas3D canvas = Scene3D.getInstance().getCanvas();
zoneAffichage.add(canvas, BorderLayout.CENTER);

When opening a new 3D CAD file:
Scene3D.getInstance().chargeObjet(filename);

When closing a file:
Scene3D.getInstance().getScene().detach();

Is there someone who can help?
Thanks in advance
Regards
Casano

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
casano
Offline
Joined: 2010-04-23

While investigating more, I have noticed this:
I added some code tests like

boolean alive = pickCanvas.getBranchGroup().isLive();
boolean alive2 = scene.getScene().isLive();
boolean name = pickCanvas.getBranchGroup().getName();
boolean name2 = scene.getScene().getName();

in PickMouseListener Class.

and scene.setName("3d scene") int Scene3D class.

The result is:
For the first 3D CAD object opened:
name = name2 = "3d scene"
alive = alive2 = true

After closing this object and opening a second one:
name = name2 = "3d scene"
alive = false
alive2 = true

If the branchgroup of pickCanvas is scene, why don't they have the same alive property in the second case, but the same name?

Thanks

casano
Offline
Joined: 2010-04-23

Hello all,

Just a correction about what I said in my last answer:

I put
scene.setName(filename) int Scene3D class (chargeObjet method).

and the result was:
for the first object opened (filename = objet1)
name = name2 = "objet1"
alive = alive2 = true

After closing this object and opening a second one (filename = objet2):
name = "objet1"
name2 = "objet2"
alive = false
alive2 = true

So, the BranchGroup of pickCanvas is not the new scene created for the second object, but the scene of the first object.
Do I create pickCanvas badly?

Please help me!
Thanks

casano
Offline
Joined: 2010-04-23

By the way, i am using Java3D 1.4, to be compliant with Java JDK 1.4. Maybe this has an impact?...