|
Try using some part of this code.
public class PickObjectBehavior extends
Behavior {
private int x, y; private Material highlight; private Shape3D selectedShape = null; private WakeupCriterion mouseEvent; private PickCanvas pickCanvas; private PickResult pickResult; private Transform3D startPos = new Transform3D(); // 255 pixel : 1.0 float = x
pixel : y float
private Sphere pointSelected; private Shape3D edgeSelectedShape = null; private LineArray edgeSelected = new LineArray(2,LineArray.COORDINATES); private TransformGroup tgPointSelected = new TransformGroup(); private TransformGroup tgEdgeSelected = new TransformGroup(); public PickObjectBehavior()
{
pickCanvas = new PickCanvas(c, bgData); pickCanvas.setTolerance(3.0f); pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO); // set pointSelected
attributes
pointSelected = new Sphere(0.07f, Sphere.GENERATE_NORMALS, sceneAppearance.getPointsHighlight()); pointSelected.setPickable(false); // set edgeSelected
attributes
edgeSelected.setCapability(LineArray.ALLOW_COORDINATE_WRITE); edgeSelected.setCapability(LineArray.ALLOW_COORDINATE_READ); edgeSelected.setCapability(LineArray.ALLOW_COUNT_READ); Color3f emissiveColor = new Color3f(); sceneAppearance.getHighlightMaterial().getEmissiveColor(emissiveColor); edgeSelectedShape = new Shape3D(edgeSelected); edgeSelectedShape.setAppearance(sceneAppearance.getEdgesHighlight()); edgeSelectedShape.setPickable(false); // pointSelected
TransformGroup
tgPointSelected.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); tgPointSelected.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tgPointSelected.addChild(pointSelected); bgData.addChild(tgPointSelected); // edgeSelected
TransformGroup
tgEdgeSelected.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); tgEdgeSelected.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tgEdgeSelected.addChild(edgeSelectedShape); bgData.addChild(tgEdgeSelected);
resetPositions();
} public void resetPositions()
{
startPos.setTranslation(new Vector3f(0.0f,0.0f,1000.0f)); tgPointSelected.setTransform(startPos); edgeSelected.setCoordinate(0, new Point3d(0,0,1000)); edgeSelected.setCoordinate(1, new Point3d(0,0,1001)); if (selectedShape != null) selectedShape.getAppearance().setMaterial(sceneAppearance.getNormalMaterial()); } public void initialize()
{
mouseEvent = new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED); wakeupOn(mouseEvent); } public void
processStimulus(Enumeration criteria) {
WakeupCriterion wakeup; AWTEvent[] event; int id; while
(criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement(); if (wakeup instanceof WakeupOnAWTEvent) { event = ((WakeupOnAWTEvent)wakeup).getAWTEvent(); for (int i=0; i<event.length; i++) { id = event[i].getID(); if (id == MouseEvent.MOUSE_CLICKED) { x =
((MouseEvent)event[i]).getX();
y = ((MouseEvent)event[i]).getY(); pickCanvas.setShapeLocation(x, y); Point3d
eyePos =
pickCanvas.getStartPosition();
pickResult = pickCanvas.pickClosest(); if
(pickResult != null)
{
// Get node info Node curNode = pickResult.getObject(); Geometry curGeom = ((Shape3D)curNode).getGeometry(); GeometryArray curGeomArray = (GeometryArray)curGeom; Shape3D shape = new Shape3D(); shape = (Shape3D)curNode; // Get closest intersection results PickIntersection pi = pickResult.getClosestIntersection(eyePos);
// Position sphere
at intersection
point
Point3d intPtVW = pi.getPointCoordinatesVW(); Point3d intPt = pi.getPointCoordinates();
// Position
sphere at closest vertex
Point3d closestVertVW = pi.getClosestVertexCoordinatesVW(); Point3d closestVert = pi.getClosestVertexCoordinates();
// Coordinates of selected object
Point3d []ptw = pi.getPrimitiveCoordinatesVW(); Point3d []pt = pi.getPrimitiveCoordinates(); int []coordidx = pi.getPrimitiveCoordinateIndices();
if (shape.getUserData().equals("Surface"))
{
Transform3D movePoint = new Transform3D(); movePoint.setTranslation(new Vector3f(closestVert)); tgPointSelected.setTransform(movePoint); xCoord.setText(truncate(closestVert.x)); yCoord.setText(truncate(closestVert.y)); zCoord.setText(truncate(closestVert.z)); xSlider.setValue((int)Math.round(closestVert.x * xSlider.getMaximum())); ySlider.setValue((int)Math.round(closestVert.y * ySlider.getMaximum())); zSlider.setValue((int)Math.round(closestVert.z * zSlider.getMaximum())); } else if (shape.getUserData().equals("Points")) { Transform3D movePoint = new Transform3D(); movePoint.setTranslation(new Vector3f(closestVert)); tgPointSelected.setTransform(movePoint); xCoord.setText(truncate(closestVert.x)); yCoord.setText(truncate(closestVert.y)); zCoord.setText(truncate(closestVert.z)); xSlider.setValue((int)Math.round(closestVert.x * xSlider.getMaximum())); ySlider.setValue((int)Math.round(closestVert.y * ySlider.getMaximum())); zSlider.setValue((int)Math.round(closestVert.z * zSlider.getMaximum())); } else if (shape.getUserData().equals("Edges")) { // deselect face if (selectedShape != null) { selectedShape.getAppearance().setMaterial(sceneAppearance.getNormalMaterial()); } edgeSelected.setCoordinate(0,pt[0]); edgeSelected.setCoordinate(1,pt[1]); xCoord.setText(truncate(intPt.x)); yCoord.setText(truncate(intPt.y)); zCoord.setText(truncate(intPt.z)); xSlider.setValue((int)Math.round(intPt.x * xSlider.getMaximum())); ySlider.setValue((int)Math.round(intPt.y * ySlider.getMaximum())); zSlider.setValue((int)Math.round(intPt.z * zSlider.getMaximum())); } else if (shape.getUserData().equals("Faces")) { if (selectedShape != null) { selectedShape.getAppearance().setMaterial(shape.getAppearance().getMaterial()); } // deselect other object resetPositions(); shape.getAppearance().setMaterial(sceneAppearance.getHighlightMaterial()); selectedShape = shape; xCoord.setText(truncate(intPt.x)); yCoord.setText(truncate(intPt.y)); zCoord.setText(truncate(intPt.z)); xSlider.setValue((int)Math.round(intPt.x * xSlider.getMaximum())); ySlider.setValue((int)Math.round(intPt.y * ySlider.getMaximum())); zSlider.setValue((int)Math.round(intPt.z * zSlider.getMaximum())); } c.getGraphicsContext3D().flush(true);
} // if (pickResult !=
null)
} // if (id == MouseEvent.MOUSE_CLICKED) } // for (int i=0; i<event.length; i++) } // if (wakeup instanceof WakeupOnAWTEvent) } // while (criteria.hasMoreElements()) wakeupOn(mouseEvent); } // end processStimulus } // end class
You also need to create an istance
like
pickBehavior = new
PickObjectBehavior();
pickBehavior.setSchedulingBounds(bounds); bgData.addChild(pickBehavior); into the Canvas3D in which you need the
behavior.
N.B. bgData is a BranchGroup, tgData is a
TransformGroup and so on.
I hope that this will be useful for
you.
By Mauro.
|
Hello. I�m sorry for the "desperate" subject but i�m loosing my mind with this problem... What i�m trying to do is to find out the (x,y,z) of a point when i click a mouse button Finding out x and y is easy with the MouseEvent class.My problem is knowing the z value! If the mouse is clicked on top of an object that�s has z=10 in that point how can i find out this value? All help is appreciated.... Thank you and keep up the good work Bruno Caiado
|
Hello.
I�m sorry for the "desperate" subject but i�m
loosing my mind with this problem...
What i�m trying to do is to find out the (x,y,z) of
a point when i click a mouse button
Finding out x and y is easy with the MouseEvent
class.My problem is knowing the z value!
If the mouse is clicked on top of an object that�s
has z=10 in that point how can i find out this value?
All help is appreciated....
Thank you and keep up the good work
Bruno Caiado
|
