i have some questions about how to pick up mouse in 3d world.
Now i have several points in 3d world,by using these points i can draw curve
i want to choose a point on this curve using mouse, the viewport of the 3d world can be changed, so i want to know at any viewport when i chick mouse button near the curve, which point is the nearest to the mouse ?or which point is chose b开发者_开发知识库y me?
i do have no idea about how to implement it,help me please if you know.
Thanks. Good luck.
If you are trying to click discrete points (perhaps nodal points on a curve) another approach is to project them back into screen coords and check for a close match to the mouse location - since this is done in integer coordinates and need only be done roughly accurate (assuming your points are more than a few screen pixels apart.
Another trick used to be to redraw the scene in an off-screen buffer with each point (or position on the curve) drawn in a different RGB color - then use the getPixel function to return the color under the mouse point and look up the point id. This doesn't work in complex scenes with fog.
OpenGL has a selection mode, where you can take the position of a mouse click, and create a matrix that restricts drawing to some arbitrary (small) area around the mouse (e.g., a 7x7 pixel block). You can then draw your "stuff" (curve, points, whatever) and it'll create a record of all the objects that fell inside of the block you defined. If more than one item fell inside that block, you get records of whatever did, one record per item, sorted by z-order from front to back.
When you're done, you get a record of the points that were close to the mouse click. Most of the time, you'll just use the closest to the front, but once in a while you'll want to do a bit more work to figure out which of them (if there was more than one, obviously) was really closest to the original mouse click point. gluProject
and (possibly) gluUnProject
can be handy if you decide to do that.
the java code below can make picking primitive shapes ( lines, points etc.)or 3d geometries (cube , sphere etc.)in 3d screen and prints what is selected.
firstly built a main class(i.e.tuval1) and secondly a public class (i.e. secim2).
public class tuval1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new secim2();
}
}
import java.awt.GraphicsConfiguration;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class secim2 extends MouseAdapter{ private PickCanvas pickCanvas;
public secim2(){
JFrame pencere=new JFrame();
pencere.setSize(300, 300);
pencere.setVisible(true);
JFrame frame = new JFrame(" 3D Box Select");
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(config);
canvas.setSize(400, 400);
SimpleUniverse universe = new SimpleUniverse(canvas);
BranchGroup group = new BranchGroup();
// create a color cube
Vector3f vector = new Vector3f(-0.3f, 0.0f, 0.0f);
Transform3D transform = new Transform3D();
transform.setTranslation(vector);
TransformGroup transformGroup = new TransformGroup(transform);
ColorCube cube = new ColorCube(0.2f);
transformGroup.addChild(cube);
group.addChild(transformGroup);
// create sphere
Vector3f vector2=new Vector3f(+0.5f,0.0f,0.0f);
Appearance app=new Appearance();
app.setPolygonAttributes(
new PolygonAttributes(PolygonAttributes.POLYGON_LINE,
PolygonAttributes.CULL_BACK,0.0f));
Transform3D transform2= new Transform3D();
transform2.setTranslation(vector2);
TransformGroup transformGroup2=new TransformGroup(transform2) ;
Sphere sphere= new Sphere(0.2f ,app);
transformGroup2.addChild(sphere );
group.addChild(transformGroup2);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
pickCanvas = new PickCanvas(canvas, group);
pickCanvas.setMode(PickCanvas.BOUNDS);
pencere.add(canvas);
canvas.addMouseListener(this);
}
public void mouseClicked(MouseEvent o)
{
pickCanvas.setShapeLocation(o);
PickResult result = pickCanvas.pickClosest();
if (result == null) {
System.out.println("Nothing picked");
} else {
Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D);
if (p != null) {
// System.out.println("Selected");
System.out.println(p.getClass().getName());
} else if (s != null) {
System.out.println(s.getClass().getName());
} else{
System.out.println("null");
}
}
}
}
精彩评论