package twoeyeimages;

import java.util.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import javax.swing.JWindow;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.Locale;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.image.TextureLoader;

public class RenderPictures extends JWindow implements Runnable
{
   boolean polarity;
   BranchGroup root=new BranchGroup();
   TransformGroup vpTrans;
   Locale locale;
   Background backgd1=new Background();
   Background backgd2=new Background();
   PhysicalBody body;
   View view;
   Bounds bounds;
    private Canvas3D canvas;
    float imageSize;
    private GraphicsContext3D gc = null;
    String leftEyeAddress, rightEyeAddress;
    private Shape3D shape1, shape2;
    private SimpleUniverse u = null;
    private VirtualUniverse vuniverse;
    static Dimension screenSize =java.awt.
                                 Toolkit.
                                 getDefaultToolkit().
                                 getScreenSize();
    int LHeight, LWidth, RHeight, RWidth;

/*    public void setAddresses(String leftAddress,
                             int LHeightIn, int LWidthIn,
                             String rightAddress,
                             int RHeightIn, int RWidthIn)
    {
      leftEyeAddress=leftAddress;
      rightEyeAddress=rightAddress;
      LHeight=LHeightIn;
      LWidth=LWidthIn;
      RHeight=RHeightIn;
      RWidth=RWidthIn;
    }
    public void setImageSize(float sizeIn)
    {
      imageSize=sizeIn;
    }
    public void setInitPolarity(boolean polart)
    {
      polarity=polart;
    }

    public void setPolarity()
    {
      polarity=!polarity;
    }

*/
    public void renderTwo()
    {
        if (gc == null)
        {
         gc = canvas.getGraphicsContext3D();
         gc.setBufferOverride(true);
        TextureLoader loader = new TextureLoader("c:/texture/earth.jpg", null);
        ImageComponent2D image = loader.getImage();
        backgd1.setImage(image);
        backgd1.setCapability(Background.ALLOW_APPLICATION_BOUNDS_READ);
        backgd1.setCapability(Background.ALLOW_APPLICATION_BOUNDS_WRITE);
        backgd1.setCapability(backgd1.SCALE_FIT_MIN);
        backgd1.setApplicationBoundingLeaf(new BoundingLeaf(bounds));
        loader=new TextureLoader("c:/texture/dragon.jpg", null);
        image=loader.getImage();
        backgd2.setImage(image);
        backgd2.setBoundsAutoCompute(true);
        backgd2.setApplicationBounds(bounds);
        }
        gc.setStereoMode(GraphicsContext3D.STEREO_LEFT);
        gc.clear();

        if (polarity==false)
          {
            gc.setBackground(backgd1);
          }
        else
          {
            gc.setBackground(backgd2);
          }

        gc.setStereoMode(GraphicsContext3D.STEREO_RIGHT);
        gc.clear();
        if (polarity==false)
           {
            gc.setBackground(backgd2);
            }
        else
            {
            gc.setBackground(backgd1);
            }
        canvas.swap();
    }

    public void run()
    {
     while (true)
      {
       renderTwo();  ////Problem happened here Why?
      }
    }

   public BranchGroup createMouse()
   {
      BranchGroup objRoot = new BranchGroup();
      TransformGroup geoTG = new TransformGroup();
      geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
      BoundingSphere mousebounds =
                    new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
      MouseRotate mouseBeh = new MouseRotate(geoTG);
      geoTG.addChild(mouseBeh);
      Alpha rotationAlpha = new Alpha(-1, 10000);
      if (false)
      {
       RotationInterpolator rotator =
             new RotationInterpolator(rotationAlpha, geoTG);
       rotator.setSchedulingBounds(mousebounds);
       rotator.getTransformAxis().rotX(Math.PI/2);
       rotator.getTransformAxis().rotY(Math.PI/2);
       geoTG.addChild(rotator);
       }
      objRoot.addChild(geoTG);
      mouseBeh.setSchedulingBounds(mousebounds);
      return objRoot;
    }
   public BranchGroup createViewGraph()
   {
        BranchGroup objRoot = new BranchGroup();
        Transform3D t = new Transform3D();
        t.setTranslation(new Vector3f(0.0f, 0.0f,2.0f));
        ViewPlatform 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(bounds);
        nav.setSchedulingBounds(bounds);

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

    public RenderPictures()
    {
     super();
        GraphicsConfigTemplate3D g3d = new GraphicsConfigTemplate3D();
        g3d.setStereo(GraphicsConfigTemplate3D.REQUIRED);
        GraphicsConfiguration config = GraphicsEnvironment.
                                   getLocalGraphicsEnvironment().
                                   getDefaultScreenDevice().
                                   getBestConfiguration(g3d);
        canvas = new Canvas3D(config);
        canvas.setStereoEnable(true);
      if (canvas.getStereoAvailable()) {
            System.out.println("stereo support  ");
      }

        canvas.stopRenderer();
        canvas.setDoubleBufferEnable(true);
        getContentPane().add(canvas, BorderLayout.CENTER);
        vuniverse=new VirtualUniverse();
        locale=new Locale(vuniverse);
        body = new PhysicalBody();

        PhysicalEnvironment environment = new PhysicalEnvironment();
        body.setLeftEyePosition(new Point3d(-0.0005, -0.0, -0.0));
        body.setRightEyePosition(new Point3d(0.0005, 0.0, 0.0));
        view = new View();
        view.addCanvas3D(canvas);
        view.setPhysicalBody(body);
        view.setPhysicalEnvironment(environment);
        bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

        BranchGroup vgraph = createViewGraph();
        BranchGroup mouseGo = createMouse();
        locale.addBranchGraph(mouseGo);
        locale.addBranchGraph(vgraph);

        root.addChild(shape1);
        root.addChild(shape2);
        (new Thread(this)).start();
    }
}

