import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.behaviors.picking.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.picking.PickTool;


public class IntersectTest extends Applet {

  BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);

  public BranchGroup createSceneGraph () {

    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);

    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
    Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);

    DirectionalLight light1
      = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    DirectionalLight light2
      = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

    Transform3D t3 = new Transform3D ();

    // Shapes
          t3.setTranslation (new Vector3d(-4+4.0, -4+0.0, -20-4.0));
          TransformGroup objTrans = new TransformGroup(t3);
          objRoot.addChild(objTrans);
          BranchGroup geomNode = new BranchGroup();
    Box box = new Box();
    box.setCapability(Box.ALLOW_CHILDREN_READ);
    box.setCapability(Box.ALLOW_CHILDREN_WRITE);
    PickTool.setCapabilities(box.getShape(Box.BACK), PickTool.INTERSECT_FULL);
    PickTool.setCapabilities(box.getShape(Box.BOTTOM), PickTool.INTERSECT_FULL);
    PickTool.setCapabilities(box.getShape(Box.FRONT), PickTool.INTERSECT_FULL);
    PickTool.setCapabilities(box.getShape(Box.LEFT), PickTool.INTERSECT_FULL);
    PickTool.setCapabilities(box.getShape(Box.RIGHT), PickTool.INTERSECT_FULL);
    PickTool.setCapabilities(box.getShape(Box.TOP), PickTool.INTERSECT_FULL);
    geomNode.addChild( (Primitive) box);
          objTrans.addChild(geomNode);

          t3.setTranslation (new Vector3d(-4+4.0, -4+5.0, -20-4.0));
          objTrans = new TransformGroup(t3);
          objRoot.addChild(objTrans);
          geomNode = new BranchGroup();
    Cone cone = new Cone();
    cone.setCapability(Cone.ALLOW_CHILDREN_READ);
    PickTool.setCapabilities(cone.getShape(Cone.BODY), PickTool.INTERSECT_FULL);
    PickTool.setCapabilities(cone.getShape(Cone.CAP), PickTool.INTERSECT_FULL);
          geomNode.addChild( (Primitive) cone);
          objTrans.addChild(geomNode);
    
    
    t3.setTranslation (new Vector3d(-4+4.0, -4+10.0, -20-4.0));
    objTrans = new TransformGroup(t3);
    objRoot.addChild(objTrans);
    geomNode = new BranchGroup();
    Sphere sphere = new Sphere();
    PickTool.setCapabilities(sphere.getShape(Sphere.BODY), PickTool.INTERSECT_FULL);
    geomNode.addChild( (Primitive) sphere);
    objTrans.addChild(geomNode);


    return objRoot;
  }

  public IntersectTest () {
    setLayout(new BorderLayout());

    GraphicsConfiguration config =
      SimpleUniverse.getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
        PickObject po = new PickObject(c, scene);
    SimpleUniverse u = new SimpleUniverse(c);

    // Add picking behavior
    IntersectInfoBehavior behavior = new IntersectInfoBehavior (c, scene,0.05f);
    behavior.setSchedulingBounds (bounds);
    scene.addChild (behavior);

    TransformGroup vpTrans = u.getViewingPlatform().getViewPlatformTransform();

    KeyNavigatorBehavior keybehavior = new KeyNavigatorBehavior (vpTrans);
    keybehavior.setSchedulingBounds (bounds);
    scene.addChild (keybehavior);
    scene.setCapability (Group.ALLOW_CHILDREN_EXTEND);
    scene.compile();
    u.addBranchGraph(scene);

    View view = u.getViewer().getView();
    view.setBackClipDistance (100000);

  }

  //
  // The following allows IntersectTest to be run as an application
  // as well as an applet
  //
  public static void main(String[] args) {
    new MainFrame(new IntersectTest(), 640, 640);
  }
}
