I am migrating my existing code to new Xith Version but picking is giving me hard time ???
This is small test-Case.
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import org.xith3d.geometry.Line;
import org.xith3d.geometry.Sphere;
import org.xith3d.picking.PickResult;
import org.xith3d.render.Canvas3D;
import org.xith3d.render.CanvasPeer;
import org.xith3d.render.Option;
import org.xith3d.render.RenderPeer;
import org.xith3d.render.base.Xith3DEnvironment;
import org.xith3d.render.jsr231.RenderPeerAWTImpl;
import org.xith3d.scenegraph.BranchGroup;
import org.xith3d.scenegraph.Locale;
import org.xith3d.scenegraph.Transform3D;
import org.xith3d.scenegraph.TransformGroup;
import org.xith3d.scenegraph.View;
import org.xith3d.scenegraph.VirtualUniverse;
public class PickingTest {
private TransformGroup objTranslate;
private Transform3D translate;
Canvas3D canvas;
View view;
/**
* Synchronization object to ensure that only one thread has access to pick
* params
*/
private Object pickParamsMutex = new Object();
private boolean picked = false;
private int pickX;
private int pickY;
/**
* Draws a cube.
*/
public PickingTest() {
VirtualUniverse universe = new VirtualUniverse();
view = new View();
view.setProjectionPolicy(View.PARALLEL_PROJECTION);
view.setScreenScale(100);
universe.addView(view);
Locale locale = new Locale();
universe.addLocale(locale);
/*
* Problem : why i have to Instantiate this.
*/
// Xith3DEnvironment env = new Xith3DEnvironment();
locale.addBranchGraph(createScene());
RenderPeer rp = new RenderPeerAWTImpl();
CanvasPeer cp = rp.makeCanvas(null, 640, 480, 32, false);
cp.getRenderOptions().setOption(Option.ENABLE_WIREFRAME_MODE, true);
canvas = new Canvas3D();
canvas.set3DPeer(cp);
view.addCanvas3D(canvas);
view.getTransform().lookAt(new Point3f(0, 0, 3), new Point3f(0, 0, 0),
new Vector3f(0, 1, 0));
Toolkit.getDefaultToolkit().addAWTEventListener(
new EventListener(),
AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK);
// main rendering loop
while (true) {
view.renderOnce();
synchronized (pickParamsMutex) {
if (picked) {
// Pick object at mouse point and within 3 pixels around it
System.out.println("Picking at " + pickX + ", " + pickY);
List<PickResult> results = canvas.pickAll(pickX, pickY, 3,3);
if (results == null) {
System.out.println("Pick results are null");
} else {
System.out.println("Detected " + results.size()
+ " hits");
int i = 0;
for (PickResult pr : results) {
System.out.println(" Hit (" + i+++ "): Shape3D: \""
+ pr.getShape().toString());
}
}
picked = false;
}
}
}
}
/**
* Starts the application.
*
* @param args
* command line parameters
*/
public static void main(String[] args) {
new PickingTest();
}
public void onMousePressed(int x, int y) {
synchronized (pickParamsMutex) {
this.pickX = x;
this.pickY = y;
picked = true;
}
}
/**
* Our own EventListener.
*/
class EventListener implements AWTEventListener {
public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {
MouseEvent e = (MouseEvent) event;
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
onMousePressed(e.getX(), e.getY());
break;
}
}
}
}
private BranchGroup createScene() {
// create a BranchGroup
BranchGroup scene = new BranchGroup();
scene.setPickable(true);
// let objects along this path translate
translate = new Transform3D();
objTranslate = new TransformGroup(translate);
scene.addChild(objTranslate);
Sphere sph = new Sphere(5, 5, 1, 20);
sph.setPickable(true);
objTranslate.addChild(sph);
Line line = new Line(new Point3f(30, 0, 0), new Point3f(100, 0, 0),
new Color3f(1, 1, 1));
line.setPickable(true);
objTranslate.addChild(line);
return scene;
}
}
On mouse click scene disappears but strangely if i just Instantiate Xith3DEnvironment object even though i am not using it
the problem goes away .
Just uncomment the instantiation line in constructor.