为什么我的对象不显示?

时间:2012-07-12 00:24:14

标签: java 3d java-3d

这是我使用幻觉艺术制作的3D模型:

enter image description here

我按照本教程为所有感兴趣的人制作沙漏:

我将它导出到名为hourglass.obj的文件中。现在,这是我用来尝试显示对象的代码:

public class LoadAnObject extends Applet
{
    public LoadAnObject()
    {
        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(config);
        add("Center", canvas);

        BranchGroup content = getScene();
        content.compile();

        SimpleUniverse universe = new SimpleUniverse(canvas);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(content);
    }

    public BranchGroup getScene()
    {
        BranchGroup group = new BranchGroup();

        ObjectFile object = new ObjectFile();
        Scene scene = null;

        try
        {
            scene = object.load("/Users/John/ArtOfIllusion/Hourglass.obj");
        }catch(Exception e){e.printStackTrace();}

        group.addChild(scene.getSceneGroup());
        return group;
    }

    public static void main(String args[])
    {
        Frame frame = new MainFrame(new LoadAnObject(), 256, 256);
    }
}

当我编译或运行它时没有任何错误,我只是在加载时得到一个空白的宇宙。我从这里得到了这段代码:

为什么我的对象不在宇宙中显示?

1 个答案:

答案 0 :(得分:0)

正在显示该对象,但由于没有光源而无法看到,这里是为了使沙漏可见而必须添加的代码:

public BranchGroup getScene()
{
    BranchGroup group = new BranchGroup();

    ObjectFile object = new ObjectFile();
    Scene scene = null;

    try
    {
        scene = object.load("/Users/John/ArtOfIllusion/Hourglass.obj");
    }catch(Exception e){e.printStackTrace();}

    group.addChild(scene.getSceneGroup());

    Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
    Vector3f light1Direction = new Vector3f(.3f, 0, 0);
    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    group.addChild(light1);

    return group;
}