Java 3d编程 - 无法访问3d对象的更改位置

时间:2014-12-29 08:37:01

标签: java image-processing java-3d

我们正致力于增强现实项目,其中用户将能够将3d家具物品虚拟地放置在相机上。

我们可以将3d对象(obj文件)放在捕获图像上,java 3d允许我使用鼠标和ViewingPlatform类动态更改或重新定位对象,但我无法检测到对象的新位置。

您能告诉我如何检测3D对象的更改角度或位置吗?

public BranchGroup createSceneGraph(String file) {
    objScale = new TransformGroup();
    objTrans = new TransformGroup();
    trans = new Transform3D();
    BranchGroup objRoot = new BranchGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(scaling);
    trans.setTranslation(new Vector3f(xloc, yloc, 0.0f));
    objScale.setTransform(trans);
    objRoot.addChild(objScale);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
    objTrans.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);
    objTrans.setCapability(Group.ALLOW_BOUNDS_WRITE);
    objTrans.setCapability(Group.ALLOW_BOUNDS_READ);
    objScale.addChild(objTrans);
    int flags = ObjectFile.RESIZE;
    flags |= ObjectFile.TRIANGULATE;
    flags |= ObjectFile.STRIPIFY;
    ObjectFile f = new ObjectFile(flags,
            (float) (creaseAngle * Math.PI / 180.0));
    System.out.println(60 * Math.PI / 180.0);
    try {
        loadscente = f.load(filename);
    } catch (Exception e) {
        System.err.println(e);
    }
    objTrans.addChild(loadscente.getSceneGroup());
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 50.0);
    System.out.println(bounds + "Bounds");
    getBackgroundImage();
    objRoot.addChild(bgNode);
    addLightsToUniverse();
    return objRoot;
}
 public void addCompInPanel() {
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    c = new Canvas3D(config);
    jPanel2.add("Center", c);
    c.addKeyListener(this);
    c.addMouseListener(this);
    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph(filename);
    u = new SimpleUniverse(c);
    // add mouse behaviors to the ViewingPlatform
    ViewingPlatform viewingPlatform = u.getViewingPlatform();
    PlatformGeometry pg = new PlatformGeometry();
    // Set up the ambient light
    Color3f ambientColor = new Color3f(Color.RED);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    pg.addChild(ambientLightNode);
    // Set up the directional lights
    Color3f light1Color = new Color3f(Color.RED);
    Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
    Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight light1 = new DirectionalLight(light1Color,
            light1Direction);
    light1.setInfluencingBounds(bounds);
    pg.addChild(light1);
    DirectionalLight light2 = new DirectionalLight(light2Color,
            light2Direction);
    light2.setInfluencingBounds(bounds);
    pg.addChild(light2);
    viewingPlatform.setPlatformGeometry(pg);

    keyNavBeh = new KeyNavigatorBehavior(objTrans);
    keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(),
            1000.0));
    System.out.println(keyNavBeh.getSchedulingBounds() + " keyNavBeh.getSchedulingBounds();");
    objTrans.addChild(keyNavBeh);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    viewingPlatform.setNominalViewingTransform();
    OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
    orbit.setSchedulingBounds(bounds);
    viewingPlatform.setViewPlatformBehavior(orbit);
    u.addBranchGraph(scene);
    jSlider1StateChanged(null);
}

1 个答案:

答案 0 :(得分:1)

查看KeyNavigator的源代码,方法integrateTransformChanges()中的代码更新了转换组:

targetTG.setTransform(vpTrans);

请注意,这会更改应用于组的变换矩阵,它不会更改对象顶点或对象的原点 - 当渲染while场景时,变换矩阵应用于每个矢量

所以一个简单的objTrans.getTransform()就可以了。

[编辑] 要完成OrbitBehavior的转换,请使用viewingPlatform.getViewPlatformTransform().getTransform()

请参阅https://github.com/hharrison/java3d-utils/blob/1f025a8787edec6e2ae1eab3a3fd0eb59319e3c4/src/classes/share/com/sun/j3d/utils/behaviors/vp/ViewPlatformBehavior.java

相关: