正确使用TransformGroup

时间:2013-03-12 21:46:53

标签: java graphics 3d java-3d

我刚开始进行图形分配,这是我第一次使用Java3D。我开始试图建造一把椅子(不要嘲笑以下内容:P)。

我只是想知道是否有人可以告诉我正确的使用模式,因为我很确定我有点偏离。

这是我画的:

chair attempt number one

正如你所看到的那样,它是一个用于座位的盒子和用于腿部的4个圆柱体。

以下是我用来绘制它的代码:

public class Chair {

    public Chair() {

        SimpleUniverse universe = new SimpleUniverse();

        BranchGroup group = new BranchGroup();

        // Main seat component

        Box box = new Box(.39f,.03f,.37f, new Appearance());

        TransformGroup seatGroup = new TransformGroup();

        Transform3D transform = new Transform3D();

        Vector3f vector = new Vector3f(.0f, .0f, .0f);
        transform.setTranslation(vector);
        seatGroup.setTransform(transform);
        seatGroup.addChild(box);

        // Legs of chair
        Cylinder leg1 = new Cylinder(.07f, .45f);
        Cylinder leg2 = new Cylinder(.07f, .45f);
        Cylinder leg3 = new Cylinder(.07f, .45f);
        Cylinder leg4 = new Cylinder(.07f, .45f);

            //Create new TransformGroup, vector position for each leg
        TransformGroup leg1Group = new TransformGroup();
        Transform3D legTransform = new Transform3D();
        Vector3f leg1Position = new Vector3f(-.37f, -0.225f, -.35f);
        legTransform.setTranslation(leg1Position);
        leg1Group.setTransform(legTransform);
        leg1Group.addChild(leg1);

        seatGroup.addChild(leg1Group);

        TransformGroup leg2Group = new TransformGroup();
        Transform3D leg2Transform = new Transform3D();
        Vector3f leg2Position = new Vector3f(.37f, -0.225f, .35f);
        leg2Transform.setTranslation(leg2Position);
        leg2Group.setTransform(leg2Transform);
        leg2Group.addChild(leg2);

        seatGroup.addChild(leg2Group);

        TransformGroup leg3Group = new TransformGroup();
        Transform3D leg3Transform = new Transform3D();
        Vector3f leg3Position = new Vector3f(.37f, -0.225f, -.35f);
        leg3Transform.setTranslation(leg3Position);
        leg3Group.setTransform(leg3Transform);
        leg3Group.addChild(leg3);

        seatGroup.addChild(leg3Group);

        TransformGroup leg4Group = new TransformGroup();
        Transform3D leg4Transform = new Transform3D();
        Vector3f leg4Position = new Vector3f(-.37f, -0.225f, .35f);
        leg4Transform.setTranslation(leg4Position);
        leg4Group.setTransform(leg4Transform);
        leg4Group.addChild(leg4);

        seatGroup.addChild(leg4Group);

            //Add seat group to main object group
        group.addChild(seatGroup);


            //Stuff to see the universe//

        Color3f light1Color = new Color3f(.1f, 1.4f, .1f); // green light

        BoundingSphere bounds =

        new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);

        DirectionalLight light1

        = new DirectionalLight(light1Color, light1Direction);

        light1.setInfluencingBounds(bounds);

        group.addChild(light1);

        universe.getViewingPlatform().setNominalViewingTransform();

        // add the group of objects to the Universe

        universe.addBranchGraph(group);

        Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        ViewingPlatform viewPlatform = universe.getViewingPlatform();
        BoundingSphere boundingSphere = new BoundingSphere(new Point3d(0f, 0f, 0f), 100f);
        // Add a behaviour to viewPlatform, allowing the user to rotate, zoom, and straff the scene
        OrbitBehavior orbitBehaviour = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
        orbitBehaviour.setSchedulingBounds(boundingSphere);
        viewPlatform.setViewPlatformBehavior(orbitBehaviour);

    }

    public static void main(String[] args) {

        new Chair();

    }

}

我只是想知道这是否正确使用了群组,因为似乎有很多重复...如果有人可以对此有所了解,我会非常感激!!

1 个答案:

答案 0 :(得分:1)

这是学习java3d的一个很好的一步。采用自制或甚至每个脚本,并能够阅读,理解和记忆代码,这样你就可以成为一个更快,更好的程序员。但要更好地定义就是把经验带到你身上。学习很好,但为了真正掌握这一点,你需要遵循自己的方法。因为每个人在某种程度上都是不同的。按照你的脚步进行,但不要直接进入缩进。