Search code examples
java3djava-3d

Why isn't my object being displayed?


Here is a 3D model I made using Art of Illusion:

enter image description here

I followed this tutorial to make the hourglass for all of those who are interested:

I exported it to a file called hourglass.obj. Now, here is the code I am using to try to display the object:

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);
    }
}

No errors whatsoever when I compile it or run it, I just get a blank universe when it loads. I got this code from here:

Why isn't my object being displayed in the universe?


Solution

  • The object was being displayed, but it couldn't be seen because there was no light source, here is the code I had to add to make the hourglass visible:

    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;
    }