Yes, I've done that. I'm including a sample program that is as simple as
humanly possible. The two files can be compiled and run.
My problems seem to be twofold:
#1. Java is not picking the point, or the Shape3D with the point in it.
Does something additional need to be defined here, like a bounding area or
something?
#2. If I replace the Shape3D (with the point assigned to it's geometry)
with something like a Sphere, it will pick the sphere. However, I then
cannot change the appearance of the sphere becuase I get a capabilitynotset
exception (or something along those lines) even though I have tried to
address the issue by using setCapability(Shape3D.ALLOW_APPEARANCE_WRITE).
Shouldn't I then be able to change the appearance attribute with
setAppearanace(Appearance) when the scenegraph is live? Does this have to
do with my casting of the SceneGraphPath.getObject() as a (Shape3D)??
I am really stumped here.
Thanks again,
Todd
/*
* Main.java
*
* Created on July 10, 2005, 12:38 PM
*/
package picktest;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.AWTEvent;
import java.util.Enumeration;
// HelloJava3Da renders a single, rotating cube.
public class pickTest extends Applet {
public pickTest() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
Highlight mousemoveBehavior = new Highlight(canvas3D, scene);
mousemoveBehavior.setSchedulingBounds(new BoundingSphere());
scene.addChild(mousemoveBehavior);
scene.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup group = new BranchGroup();
//create a sphere
Point3d p = new Point3d(0.0,0.0,0.0);
PointArray pa = new PointArray(1, PointArray.COORDINATES);
pa.setCoordinate(0, p);
PointAttributes patt = new PointAttributes(5, false);
Appearance app = new Appearance();
app.setPointAttributes(patt);
Shape3D s = new Shape3D();
// set the geometry and standard appearance
s.setGeometry(pa);
s.setAppearance(app);
s.setPickable(true);
s.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
group.addChild(s);
return group;
}
// The following allows this to be run as an application
// as well as an applet
public static void main(String[] args) {
Frame frame = new MainFrame(new pickTest(), 256, 256);
}
}
/*
* Highlight.java
*
* Created on July 10, 2005, 12:40 PM
*/
package picktest;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.AWTEvent;
import java.util.Enumeration;
/**
*
* @author Todd L. Peters
*/
public class Highlight extends Behavior {
private Canvas3D mCanvas;
private BranchGroup mPickGroup;
private ColoringAttributes mColoringAttributes;
private Appearance mHighlightAppearance;
private int mX,mY;
/** Creates a new instance of Highlight */
public Highlight(Canvas3D cv3d, BranchGroup bg) {
mCanvas = cv3d;
mPickGroup = bg;
mColoringAttributes = new ColoringAttributes();
mColoringAttributes.setColor(1.0f, 0.0f, 0.0f);
mHighlightAppearance = new Appearance();
mHighlightAppearance.setColoringAttributes(mColoringAttributes);
}
/**
* initialize the Behavior
* set initial wakeup condition
* called when behavior becomes live
*/
public void initialize(){
// set initial wakeup condition
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED));
return;
}
protected void processAWTEvent(AWTEvent e) {
MouseEvent evt = (MouseEvent)e;
mX = evt.getX();
mY = evt.getY();
return;
}
// called by Java 3D when appropriate stimulus occurs
public void processStimulus(Enumeration criteria){
WakeupCondition cond;
AWTEvent event;
cond = (WakeupCondition)criteria.nextElement();
event = ((WakeupOnAWTEvent)cond).getAWTEvent()[0];
processAWTEvent(event);
PickRay ray = createPickRay(mCanvas, mX, mY);
SceneGraphPath[] path = null;
path = mPickGroup.pickAll(ray);
if (path != null) {
Shape3D s = (Shape3D)path[0].getObject();
if (s != null) {
s.setAppearance(mHighlightAppearance);
}
}
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED));
return;
}
private PickRay createPickRay(Canvas3D canvas, int x, int y) {
Point3d eye_pos = new Point3d();
Point3d mouse_pos = new Point3d();
canvas.getCenterEyeInImagePlate(eye_pos);
canvas.getPixelLocationInImagePlate(x, y, mouse_pos);
Transform3D motion = new Transform3D();
canvas.getImagePlateToVworld(motion);
motion.transform(eye_pos);
motion.transform(mouse_pos);
Vector3d direction = new Vector3d(mouse_pos);
direction.sub(eye_pos);
return new PickRay(eye_pos, direction);
}
}
-----Original Message-----
From: Discussion list for Java 3D API
[mailto:[EMAIL PROTECTED] Behalf Of John Wright
Sent: Sunday, July 10, 2005 10:34 AM
To: [email protected]
Subject: Re: [JAVA3D] Picking Behaviors in Java3D
Todd,
You have to set the objects you want to pick as "pickable". If they
aren't set as pickable they won't trigger a pick.
- John Wright
Starfire Research
Todd L. Peters wrote:
> John-
>
> I still have the problem where it cannot find anything.
>
> SceneGraphPath[] path = null;
> path = mPickGroup.pickAll(ray);
>
> path.length is equal to zero.
>
> Do I have to set any attributes on the group? On the derived shape3d
> objects? Can Java3D identify an object derived from Shape3D as described
> below for picking? Does it matter what group I insert the Highlight
> Behavior into?
>
> Thanks,
> Todd
>
>
> -----Original Message-----
> From: Discussion list for Java 3D API
> [mailto:[EMAIL PROTECTED] Behalf Of John Wright
> Sent: Saturday, July 09, 2005 7:17 PM
> To: [email protected]
> Subject: Re: [JAVA3D] Picking Behaviors in Java3D
>
>
> Todd,
>
> I've found that picking was not reliable unless you do a "pickAll" and
> then loop through the scene graph path array returned (to find your
> closest hit). Clumsy but it works.
>
> - John Wright
> Starfire Research
>
> Todd L. Peters wrote:
>
>>I am new to Java 3D and am having trouble with picking behaviors. The
>>tutorial is not really clear to me and the examples on the web seem to use
>>deprecated methods. I think I have the basic concept correct, but things
>>do not seem to be working out.
>>
>>I have a branchgroup (not the top level group) that has a number of
>>children. These children are all of one class that is derived from
>>Shape3D. The geometry of the object is set to a PointArray with one
>>point. In short, the branchgroup contains a number of points.
>>
>>I pass the branchgroup and the canvas3D object to the following behavior:
>>
>>public class Highlight extends Behavior {
>>
>> private Canvas3D mCanvas;
>> private BranchGroup mPickGroup;
>> private static ColoringAttributes mColoringAttributes;
>> private static Appearance mHighlightAppearance;
>> private int mX,mY;
>>
>> /** Creates a new instance of Highlight */
>> public Highlight(Canvas3D cv3d, BranchGroup bg) {
>>
>> mCanvas = cv3d;
>> mPickGroup = bg;
>> mColoringAttributes = new ColoringAttributes();
>> mColoringAttributes.setColor(1.0f, 0.0f, 0.0f);
>> mHighlightAppearance = new Appearance();
>> mHighlightAppearance.setColoringAttributes(mColoringAttributes);
>>
>> }
>>
>> /**
>> * initialize the Behavior
>> * set initial wakeup condition
>> * called when behavior becomes live
>> */
>> public void initialize(){
>> // set initial wakeup condition
>> this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED));
>> return;
>> }
>>
>>
>> protected void processAWTEvent(AWTEvent e) {
>> MouseEvent evt = (MouseEvent)e;
>> mX = evt.getX();
>> mY = evt.getY();
>> return;
>> }
>>
>> // called by Java 3D when appropriate stimulus occurs
>> public void processStimulus(Enumeration criteria){
>>
>> WakeupCondition cond;
>> AWTEvent event;
>>
>> cond = (WakeupCondition)criteria.nextElement();
>> event = ((WakeupOnAWTEvent)cond).getAWTEvent()[0];
>> processAWTEvent(event);
>>
>> PickRay ray = createPickRay(mCanvas, mX, mY);
>> SceneGraphPath path = null;
>> path = mPickGroup.pickClosest(ray);
>> if (path != null) {
>> Shape3D s = (Shape3D)path.getObject();
>> if (s != null) {
>> s.setAppearance(mHighlightAppearance);
>> }
>> }
>> this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED));
>> return;
>>
>>
>> }
>>
>> private PickRay createPickRay(Canvas3D canvas, int x, int y) {
>> Point3d eye_pos = new Point3d();
>> Point3d mouse_pos = new Point3d();
>>
>> canvas.getCenterEyeInImagePlate(eye_pos);
>> canvas.getPixelLocationInImagePlate(x, y, mouse_pos);
>>
>> Transform3D motion = new Transform3D();
>> canvas.getImagePlateToVworld(motion);
>> motion.transform(eye_pos);
>> motion.transform(mouse_pos);
>>
>> Vector3d direction = new Vector3d(mouse_pos);
>> direction.sub(eye_pos);
>>
>> return new PickRay(eye_pos, direction);
>> }
>>
>>}
>>
>>It is initialized as such:
>>
>> Highlight mousemoveBehavior = new Highlight(cv, bgNodes);
>> mousemoveBehavior.setSchedulingBounds(new BoundingSphere());
>> bg.addChild(mousemoveBehavior);
>>
>>Where bgNodes is the aforementioned branchgroup with all the points and bg
>>is the top-level branchgroup.
>>
>>However, the Highlight behavior does not seem to detect any geometry. If
>>I assign the behavior to the top-level branchgroup, it appears to detect
>>other geometry in the scene (although I have trouble following the
>>debugger output).
>>
>>Can anyone help clarify how I should be doing this?
>>
>>Regards,
>>Todd
>>
>>
>
>
===========================================================================
>
>>To unsubscribe, send email to [EMAIL PROTECTED] and include in the
>
> body
>
>>of the message "signoff JAVA3D-INTEREST". For general help, send email to
>>[EMAIL PROTECTED] and include in the body of the message "help".
>>
>
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff JAVA3D-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff JAVA3D-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".