How can I use lighting to show that my shape is 3D?

56 Views Asked by At

I am working on my class project in java3D and I have run into an issue. I need to give my shape a material so that the light can show shadows, thus making the shape actually look 3D. Right now I have everything I need to display the shape, but I don't like the solid white color that it has, and I would like to make it so that the light object actually shows reflection on the shape. How can I go about doing this? I will add a picture of the current object at the bottom. Thanks!

public static void main(String[] args) {
    System.setProperty("sun.awt.noerasebackground", "true");
    new MainFrame(new Assignment7(), 640, 480);
}

public void init() {
    // create canvas
    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
    Canvas3D cv = new Canvas3D(gc);
    setLayout(new BorderLayout());
    add(cv, BorderLayout.CENTER);
    BranchGroup bg = createBranchGraph();
    bg.compile();
    SimpleUniverse su = new SimpleUniverse(cv);
    su.getViewingPlatform().setNominalViewingTransform();
    TransformGroup tg = su.getViewingPlatform().getMultiTransformGroup().getTransformGroup(0);
    Transform3D tx = new Transform3D();
    tx.lookAt(new Point3d(0,.5,2), new Point3d(0,0,0), new Vector3d(0,1,0));
    tx.invert();
    tg.setTransform(tx);
    
    View view = su.getViewer().getView();
    //view.setProjectionPolicy(View.PARALLEL_PROJECTION);
    view.setFieldOfView(0.4*Math.PI);
    
    su.addBranchGraph(bg);
}

private BranchGroup createBranchGraph() {
    BranchGroup root = new BranchGroup();
    TransformGroup spin = new TransformGroup();
    spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    root.addChild(spin);
    //object
    Appearance ap = new Appearance();
    PolygonAttributes pa = new PolygonAttributes();
    //pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    Shape3D shape = new Shape3D(new Octahedron(), ap);
    ap.setMaterial(new Material());
    ap.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0));
    Material mat = new Material();
    mat.setLightingEnable(true);
    mat.setShininess(30);
    ap.setMaterial(mat);
    shape.setAppearance(ap);
    Shape3D bracket1 = new Shape3D(new bracket(), ap);
    Shape3D partition2 = new Shape3D(new partition2(), ap);
    Shape3D bracket2 = new Shape3D(new bracket2(), ap);
    Shape3D partition3 = new Shape3D(new partition3(), ap);
    Shape3D head = new Shape3D(new head(), ap);
    Shape3D claw = new Shape3D(new claw1(), ap);
    Shape3D claw2 = new Shape3D(new claw2(), ap);
    
    bracket1.setAppearance(ap);
    partition2.setAppearance(ap);
    bracket2.setAppearance(ap);
    partition3.setAppearance(ap);
    head.setAppearance(ap);
    claw.setAppearance(ap);
    claw2.setAppearance(ap);
    
    //transform
    Transform3D tr = new Transform3D();
    tr.setScale(0.25);
    tr.setTranslation(new Vector3d(0, 0, 0));
    TransformGroup tg = new TransformGroup(tr);
    spin.addChild(tg);
    tg.addChild(shape);
    tg.addChild(bracket1);
    tg.addChild(partition2);
    tg.addChild(bracket2);
    tg.addChild(partition3);
    tg.addChild(head);
    tg.addChild(claw);
    tg.addChild(claw2);

    BoundingSphere bounds = new BoundingSphere();
    
    MouseRotate rt = new MouseRotate(spin);
    rt.setSchedulingBounds(bounds);
    spin.addChild(rt);
    
    Background bg = new Background(0f, .5f, 1.0f);
    bg.setApplicationBounds(bounds);
    root.addChild(bg);
    
    //lighting
    AmbientLight light = new AmbientLight(true, new Color3f(Color.white));
    light.setColor(new Color3f(Color.black));
    light.setInfluencingBounds(bounds);
    root.addChild(light);
    
    PointLight pl = new PointLight(new Color3f(Color.white), new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    pl.setInfluencingBounds(bounds);
    root.addChild(pl);
    
    PointLight pl2 = new PointLight(new Color3f(Color.black), new Point3f(3f,3f,3f), new Point3f(1f,0f,0f));
    pl2.setInfluencingBounds(bounds);
    root.addChild(pl2);
    
    return root;
}

Here is what the current shape looks like (without lighting):

enter image description here

0

There are 0 best solutions below