2011-01-31 10 views
0

私は、画面上の特定のポイントをクリックすると、クリックした3Dシーンのポイントを返す関数呼び出しが必要です。マウスのクリックに基づいて3Dの点の座標を取得するにはどうすればよいですか?

たとえば、画面の左上をクリックすると、x = 0、y = 1、z = 1が返されます。これを行う方法を作成するのを手伝ってください。

編集:

Root = new BranchGroup(); 

setLayout(new BorderLayout()); 
add(canvas3D,BorderLayout.CENTER); 
SimpleUniverse universe = new SimpleUniverse(canvas3D); 

Shape(); 
universe.addBranchGraph(Root); 
ViewingPlatform viewingPlatform = universe.getViewingPlatform(); 
OrbitBehavior behavior = new OrbitBehavior(canvas3D); 
behavior.setSchedulingBounds(bounds); 
viewingPlatform.setViewPlatformBehavior(behavior); 
viewingPlatform.setNominalViewingTransform(); 
} 

私はNetBeansでSimpleUniverseを使用しています。

+2

、あなたは、任意の特定の3Dエンジンを使用していますか? –

+2

いいえNetBeansを使用しています。 –

+3

"私はNetBeansを使用していません。"これはSwingアプリケーションのようですが、正しいのですか?このアプリケーションにMouseListenerを使用していますか?または、GUIの外のユーザーのコンピュータでマウスのクリックを待ち受けようとしていますか?もしそうなら、Javaはこの仕事のための間違ったツールかもしれません。 –

答えて

3

私は答えがまっすぐではないことを恐れています。あなたのシーンにあるものに応じて、画面上をクリックするとマウス座標が変わるはずです。たとえば、フロントオブジェクトがバックオブジェクトを隠すように2つのオブジェクトがある場合は、フロントオブジェクトの座標を取得する必要があります。これはmouse pickingと呼ばれ、いくつかの手法があります。私はいくつかのフォーラムを見つけ、それがどのように行われたかを説明し、コード例を持っています。

基本的には、ユーザーと画面の間にレーザー(または光線を投射するもの)があると想像しています。次に、このオブジェクトは、マウスがスクリーンにクリックされたスクリーン面上の点を通って光線を投影します。レイパスにあるものはすべて選択され、オプションでレイのパス内のオブジェクトのオクルージョン順序が解決され、「スクリーン」に最も近いオブジェクトが得られます。

私はJ3Dに慣れていませんが、いくつかのチュートリアルから次のコードをまとめています。それは少なくともあなたを始めるはずです。あなたが探しているのは、この行の末尾にPoint3D intercept = ...です。

http://www.java3d.org/selection.html

package j3d_picking; 

import java.awt.*; 
import javax.swing.*; 
import javax.media.j3d.*; 
import com.sun.j3d.utils.universe.*; 
import com.sun.j3d.utils.picking.behaviors.*; 
import com.sun.j3d.utils.geometry.*; 
import com.sun.j3d.utils.picking.PickIntersection; 
import com.sun.j3d.utils.picking.PickResult; 
import com.sun.j3d.utils.picking.PickTool; 
import javax.vecmath.Point3d; 

public class HelloJava3D 
     extends JFrame 
{ 

    public HelloJava3D() 
    { 
     GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); 
     Canvas3D canvas3D = new Canvas3D(config); 

     BranchGroup scene = createSceneGraph(); 

     // SimpleUniverse is a Convenience Utility class 
     SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 

     // This moves the ViewPlatform back a bit so the 
     // objects in the scene can be viewed. 
     simpleU.getViewingPlatform().setNominalViewingTransform(); 

     BoundingSphere behaveBounds = new BoundingSphere(); 
     ExamplePickBehavior behavior = new ExamplePickBehavior(canvas3D, scene, behaveBounds); 
     scene.addChild(behavior); 

     scene.compile(); 
     simpleU.addBranchGraph(scene); 

     getContentPane().add(canvas3D, BorderLayout.CENTER); 
    } // end of HelloJava3D (constructor) 

    public BranchGroup createSceneGraph() 
    { 
     // Create the root of the branch graph 
     BranchGroup objRoot = new BranchGroup(); 

     // Create a simple shape leaf node, add it to the scene graph. 
     // ColorCube is a Convenience Utility class 
     ColorCube cube = new ColorCube(0.4); 
     cube.setCapability(Node.ENABLE_PICK_REPORTING); 
     PickTool.setCapabilities(cube, PickTool.INTERSECT_FULL); 
     objRoot.addChild(cube); 

     return objRoot; 
    } // end of createSceneGraph method of HelloJava3D 

    public static void main(String[] args) 
    { 
     JFrame frame = new HelloJava3D(); 
     frame.setTitle("Hello Java3D"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setBounds(0, 0, 400, 300); 
     frame.setVisible(true); 
    } 

    private class ExamplePickBehavior extends PickMouseBehavior 
    { 

     public ExamplePickBehavior(Canvas3D canvas, BranchGroup bg, Bounds bounds) 
     { 
      super(canvas, bg, bounds); 
      setSchedulingBounds(bounds); 

      pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO); 
      // allows PickIntersection objects to be returned 
     } 

     public void updateScene(int xpos, int ypos) 
     { 
      pickCanvas.setShapeLocation(xpos, ypos); 
      // register mouse pointer location on the screen (canvas) 

      Point3d eyePos = pickCanvas.getStartPosition(); 
      // get the viewer's eye location 

      PickResult pickResult = null; 
      pickResult = pickCanvas.pickClosest(); 
      // get the intersected shape closest to the viewer 

      if (pickResult != null) { 
       PickIntersection pi = pickResult.getClosestIntersection(eyePos); 
       // get the closest intersect to the eyePos point 
       Point3d intercept = pi.getPointCoordinatesVW(); 
       System.out.println(intercept); 
       // extract the intersection pt in scene coords space 
       // use the intersection pt in some way... 
      } 
     } // end of updateScene() 
    } // end of ExamplePickBehavior class 
} 
enter code here
+0

あなたは明らかにこの返事に多くの努力を払っています - ありがとう! –

+1

@Hovercraft:ええ、私は自分自身に興味をそそられています - 私が推測する遅れの形です:) –

-1

以下のJavaコード、3Dオブジェクトの(形状)中心が3Dスクリーン座標印刷します。結果は(x = -0.5、yは0.0 =、Z = 0.4)

  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); 

    SimpleUniverse universe = new SimpleUniverse(canvas); 

    BranchGroup group = new BranchGroup(); 


    // create a color cube 


     Transform3D transform= new Transform3D(); 
    Vector3d vector = new Vector3d(-0.5, 0.0, 0.4); 

     Transform3D transform = new Transform3D(); 

     transform.setTranslation(vector); 

     TransformGroup transformGroup = new TransformGroup(transform); 

     ColorCube cube = new ColorCube(0.1f); 

     transformGroup.addChild(cube); 

     group.addChild(transformGroup); 


     universe.getViewingPlatform().setNominalViewingTransform(); 

     universe.addBranchGraph(group); 


     pickCanvas = new PickCanvas(canvas, group); 

     pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO); 

    pencere.add(canvas); 

     canvas.addMouseListener(this); 


} 

public void mouseClicked(MouseEvent e) 

{ 



    pickCanvas.setShapeLocation(e); 

    PickResult result = pickCanvas.pickClosest(); 

    if (result == null) { 


    } else { 

     Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE); 

     Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D); 

     if (p != null) { 

     System.out.println(p.getClass().getName()); 


     } else if (s != null) { 

      System.out.println(s.getClass().getName()); 

      Vector3f position = new Vector3f(); 

     s.getLocalToVworld(transform); 

     transform.get(position); 

     System.out.print(position); 

     // System.out.print(s.getLocalToVworld(transform2); 
     } else{ 

      System.out.println("null"); 

     } 

    } 

} 

}

パブリッククラスtuval1 {

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

新しいsecim2()。あなたのシーンが生成されますどのように

} 

}