[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Java3Djp:01599] Re: Z 座標値の取得方法について





こんにちは。

>具体的にどのような処理かと申しますとSimpleUniverse内にある1つの
>物体についてCanvas3Dをマウスでクリックしてその物体のその部分の
>座標値を得たいということなんですが。
>Z座標を返すクラスなどは存在するのでしょうか?
>またそのようなことが可能なんでしょうか?

ちょうど作っていたところだったので、引っこ抜いて
動くようにしてみました。ゴール直前ぐらいまで行っ
ていると思います。

実行し画面内の三角形をクリックすると、次の文字を
標準出力に出力します。

PickResult: sgp:javax.media.j3d.Locale@bdcd2 : javax.media.j3d.Shape3D :
 javax.media.j3d.Shape3Djavax.media.j3d.TriangleArray@4e79f1
LocalToVworld Transform:
1.0, 0.0, 0.0, 0.0
0.0, 1.0, 0.0, 0.0
0.0, 0.0, 1.0, 0.0
0.0, 0.0, 0.0, 1.0

 node:javax.media.j3d.Shape3D@3d928a
PickIntersection:  geomIndex = 0
 dist:2.3669087212332345
 ptVW:(0.005997081766190504, 0.4857636230614245, 
0.09769578083752606)

このうち、ptVWがXYZの座標値にあたると思います。

以下、ソースです。

/*
 * @(#)PickTest.java
 */

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.Applet;
import com.sun.j3d.utils.applet.MainFrame;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;


public class PickTest extends Applet {
    private Canvas3D            canvas;
    private SimpleUniverse      universe;
    private BranchGroup         branchGroup;
    private TransformGroup      transformGroup;

    public PickTest() {
        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        canvas = new Canvas3D(config);
        add("Center", canvas);
        universe       = new SimpleUniverse(canvas);
        branchGroup    = new BranchGroup();
        transformGroup = new TransformGroup();

        BoundingSphere bounds = new BoundingSphere();
        bounds.setRadius(10.0);
        Background backGround = new Background(new Color3f(0.4f, 0.6f, 0.8f));
        backGround.setApplicationBounds(bounds);
        branchGroup.addChild(backGround);
        DirectionalLight light = new DirectionalLight();
        light.setInfluencingBounds(bounds);
        branchGroup.addChild(light);
        transformGroup.addChild(new MyFloor());
        branchGroup.addChild(transformGroup);

        Behavior behavior = new SelectBehavior(canvas, branchGroup);
        branchGroup.addChild(behavior);
        behavior.setSchedulingBounds(bounds);

        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(branchGroup);
    }

    private class MyFloor extends TransformGroup {
        public MyFloor() {
            Point3f[] p = {new Point3f( 0.0f,  0.6f, 0.0f),
                           new Point3f(-0.6f, -0.3f, 0.5f),
                           new Point3f( 0.6f, -0.3f, 1.0f) };
            GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
            gi.setCoordinates(p);
            NormalGenerator ng = new NormalGenerator();
            ng.setCreaseAngle(Math.PI);     // Smooth Shading
            ng.generateNormals(gi);

            Appearance app = new Appearance();
            Color3f  ambient   = new Color3f(0.0F, 0.0F, 0.0F);
            Color3f  emissive  = new Color3f(0.0F, 0.0F, 0.0F);
            Color3f  diffuse   = new Color3f(0.0F, 0.0F, 0.0F);
            Color3f  specular  = new Color3f(1.0F, 1.0F, 1.0F);
            float    shininess = 1.0F;
            Material material  = new Material(ambient, emissive, diffuse, specular, shininess);
            material.setCapability(Material.ALLOW_COMPONENT_WRITE);
            app.setMaterial(material);

            Shape3D s3d = new Shape3D(gi.getGeometryArray(), app);
            s3d.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
            s3d.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
            s3d.setCapability(Shape3D.ENABLE_PICK_REPORTING);
            PickTool.setCapabilities(s3d, PickTool.INTERSECT_FULL);
            addChild(s3d);
        }
    }

    public class SelectBehavior extends PickMouseBehavior {
        public SelectBehavior(Canvas3D canvas, BranchGroup bg) {
            super(canvas, bg, null);
            setMode(PickTool.GEOMETRY);
        }
        public void updateScene(int x, int y) {
            pickCanvas.setShapeLocation(mevent);
            PickResult[] results = pickCanvas.pickAll();
            if (results == null) {
                return;
            }
            System.out.println(results[0].toString());
        }
    }

    public static void main(String[] args) {
        new MainFrame(new PickTest(), 320, 240);
    }
}

--
Tomokazu ISHII <ishii@xxxxxxxxxxxx>
http://www.hogbelly.net/ishii/