首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java3D编程-无法访问更改的三维对象位置

Java3D编程-无法访问更改的三维对象位置
EN

Stack Overflow用户
提问于 2014-12-29 08:37:00
回答 1查看 336关注 0票数 2

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

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

你能告诉我如何检测改变的角度或位置的三维物体吗?

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-29 15:21:31

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

代码语言:javascript
复制
targetTG.setTransform(vpTrans);

请注意,这会改变应用于组的转换矩阵,它不会改变对象顶点或对象的原点--当呈现时间场景时,转换矩阵将应用于每个向量。

因此,一个简单的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

相关信息:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27685726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档