package fullwindowcanvas3d;

import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JWindow;
import javax.media.j3d.*;
import javax.vecmath.Point3d;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Background;
import javax.media.j3d.Material;
import javax.media.j3d.Appearance;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.AmbientLight;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import javax.vecmath.Vector3f;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;

 public class FullWindowCanvas3D extends JWindow
 {
  TransformGroup vpTrans;
  ViewPlatform vp;
  BoundingSphere everywhere;
  View view;
  PhysicalBody body=new PhysicalBody();

     public FullWindowCanvas3D()
     {
       super();
       everywhere = new BoundingSphere(new Point3d(), Double.MAX_VALUE);
       GraphicsConfigTemplate3D g3d = new GraphicsConfigTemplate3D();
       g3d.setStereo(GraphicsConfigTemplate3D.REQUIRED);
       GraphicsConfiguration gc = GraphicsEnvironment.
                                  getLocalGraphicsEnvironment().
                                  getDefaultScreenDevice().
                                  getBestConfiguration(g3d);
//     Canvas3D canvas=new Canvas3D(gc);
       Canvas3D canvas = new  Canvas3D(SimpleUniverse.getPreferredConfiguration());
       PhysicalEnvironment environment = new PhysicalEnvironment();

       canvas.setDoubleBufferEnable(true);
       getContentPane().add(canvas, BorderLayout.CENTER);
       SimpleUniverse universe = new SimpleUniverse(canvas);
       view = new View();
       universe.getViewingPlatform().setNominalViewingTransform();
       BranchGroup lights = new BranchGroup();
       DirectionalLight directionalLight = new DirectionalLight();
       directionalLight.setInfluencingBounds(everywhere);
       lights.addChild(directionalLight);

       AmbientLight ambientLight = new AmbientLight();
       ambientLight.setInfluencingBounds(everywhere);
       lights.addChild(ambientLight);
       universe.getViewingPlatform().getViewPlatformTransform().addChild(lights);

       body.setLeftEyePosition(new Point3d(0.00, -0.0, -0.0));
       body.setRightEyePosition(new Point3d(-0.00                                             , 0.0, 0.0));
       Background bk=new Background();
       bk.setColor(0.6f, 0.6f, 0.6f);
       canvas.setStereoEnable(true);
       view.setPhysicalBody(body);
       view.setPhysicalEnvironment(environment);

       BranchGroup root = new BranchGroup();

       Background background = new Background(0, 0, 0);
       background.setApplicationBounds(everywhere);
       root.addChild(background);

       Material sphereMaterial = new Material();
       sphereMaterial.setDiffuseColor(0, 0, 1);
       Appearance sphereAppearance = new Appearance();
       sphereAppearance.setMaterial(sphereMaterial);

       OrbitBehavior orbitter = new OrbitBehavior(canvas);
       orbitter.setSchedulingBounds(everywhere);
       orbitter.setViewingPlatform(universe.getViewingPlatform());
       root.addChild(orbitter);

       Alpha rotationAlpha = new Alpha(-1, 30000);
       TransformGroup geoTG = new TransformGroup();
       geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       geoTG.addChild(new Sphere(0.5f, Sphere.GENERATE_NORMALS, sphereAppearance));
       RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, geoTG);
       rotator.setSchedulingBounds(everywhere);

       geoTG.addChild(rotator);
       root.addChild(geoTG);
       BranchGroup scene = createViewGraph();

       root.compile();
       universe.addBranchGraph(root);
       universe.addBranchGraph(scene);
     }

 public BranchGroup createViewGraph()
   {
        BranchGroup objRoot = new BranchGroup();
        Transform3D t = new Transform3D();
        t.setTranslation(new Vector3f(0.0f, 0.0f,10.0f));
        vp = new ViewPlatform();
        vpTrans = new TransformGroup();
        vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        vpTrans.setTransform(t);

        DisparityBehavior db = new DisparityBehavior(body);
        NavigationBehavior nav = new NavigationBehavior(vpTrans);
        db.setSchedulingBounds(everywhere);
        nav.setSchedulingBounds(everywhere);

        vpTrans.addChild(nav);
        vpTrans.addChild(vp);
 //      view.attachViewPlatform(vp);
         objRoot.addChild(vpTrans);
         return objRoot;
   }


     public static void main(String[] args)
     {
         FullWindowCanvas3D win = new FullWindowCanvas3D();

         java.awt.Dimension screenSize =
                  java.awt.Toolkit.getDefaultToolkit().getScreenSize();

         win.setSize(screenSize.width/2, screenSize.height);
         win.setVisible(true);
     }
 }