/*
 *	@(#)Billboard3d.java 1.00 00/04/00 13:13:46
 *
 *
 *    "Reality Check"  For 3DWEB Project for Anthony Engineering
 *
 */

import java.util.* ;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
//
import java.io.* ;
import TexturedPlane ;

public class Billboard3d extends Applet {

  public BranchGroup createSceneGraph(SimpleUniverse su) {

    // Create the root of the branch graph
    BranchGroup rootBg = new BranchGroup();
    BoundingSphere bs = new BoundingSphere(new Point3d(),1e3);
    rootBg.setBounds(bs);
    
    // Add background to the root
    Background rootBackground = new Background( 0f, 0f, 0f) ;
    rootBackground.setApplicationBounds(bs) ;
    rootBg.addChild(rootBackground) ;
    
    // Add ambient light to the rootBranchGroup
    AmbientLight etherLight = new AmbientLight();
    etherLight.setInfluencingBounds(bs) ;
    rootBg.addChild(etherLight) ;
    
    // Add mainLigth to rootBg
    DirectionalLight mainLight = new DirectionalLight( new Color3f(1.0f,1.0f,1.0f),  new Vector3f( -1f, -1f, -1f)) ;
    mainLight.setInfluencingBounds (bs) ;
    rootBg.addChild(mainLight) ;
    // Add fillLight to rootBg
    DirectionalLight fillLight = new DirectionalLight( new Color3f(1.0f,1.0f,1.0f),  new Vector3f( 0.5f, 0.5f, 0.5f)) ;
    fillLight.setInfluencingBounds (bs) ;
    rootBg.addChild(fillLight) ;

    // Origin Tg
    TransformGroup originTg = new TransformGroup();
    originTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    originTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    rootBg.addChild(originTg);
    // Attach MouseBehavior to originTg.
    MouseRotate behavior = new MouseRotate(originTg);
    originTg.addChild(behavior);
    behavior.setSchedulingBounds(bs);
    // Create the zoom behavior node
    MouseZoom behavior2 = new MouseZoom(originTg);
    originTg.addChild(behavior2);
    behavior2.setSchedulingBounds(bs);
    // Create the translate behavior node
    MouseTranslate behavior3 = new MouseTranslate(originTg);
    originTg.addChild(behavior3);
    behavior3.setSchedulingBounds(bs);

   //  Add KeyBehavior to ViewingPlatform.
    TransformGroup suVpTg = null;
    Vector3f translate = new Vector3f();
    Transform3D T3D = new Transform3D();
    TransformGroup TG = null;
    //
    suVpTg =  su.getViewingPlatform().getViewPlatformTransform();
    su.getViewingPlatform().getViewPlatform().setActivationRadius(1e3f);
    translate.set( 0.0f, 0.0f, 10.0f);
    T3D.setTranslation(translate);
    suVpTg.setTransform(T3D);
    KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(suVpTg);
    keyNavBeh.setSchedulingBounds(bs);
    rootBg.addChild(keyNavBeh);
    //  Attach a Spotlight to the ViewPlatform.
    PlatformGeometry lightsPg = new PlatformGeometry() ;
    BranchGroup lightsBg = new BranchGroup() ;
    lightsPg.addChild(lightsBg) ;
    TransformGroup lightsTg = new TransformGroup() ;
    lightsBg.addChild(lightsTg) ;
    // Set up ViewSpotLight
    SpotLight viewSpotlight = new SpotLight( new Color3f(1f,0f,0f), new Point3f (0f,0f,0f),
                new Point3f(1f,0f,0f), new Vector3f (0f,0f,-1f), 0.1f,  10f) ;
    viewSpotlight.setInfluencingBounds(bs);
    lightsTg.addChild(viewSpotlight);
    su.getViewingPlatform().setPlatformGeometry(lightsPg) ;
    
    // Add a Tg for a Textured Plane TexturedPlane
    TransformGroup earthTexPlaneTg = new TransformGroup();
    earthTexPlaneTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE) ;
    earthTexPlaneTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ) ;
     // Make the Geometry
    TexturedPlane earthTexPlane = new TexturedPlane("earth.jpg") ;
    earthTexPlane.setAlignmentMode(OrientedShape3D.ROTATE_ABOUT_POINT);
    // Assemble Scene Graph
    originTg.addChild(earthTexPlaneTg) ;
    earthTexPlaneTg.addChild(earthTexPlane) ;

    
/*
    // Create first Globe1 TransformGroup 'globe1Tg'
    TransformGroup globe1Tg = new TransformGroup();
    globe1Tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    globe1Tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Modify the T3d of 'globe1Tg'
    Transform3D globe1T3d = new Transform3D();
    globe1T3d.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
    // Update the T3d of globe1Tg
    globe1Tg.setTransform(globe1T3d) ;
    // add it to the scene graph.
    originTg.addChild(globe1Tg);

    //  add Rotation
    Alpha globe1RotAlpha = new Alpha(-1, 3000);
    RotationInterpolator globe1RotInt = new RotationInterpolator(globe1RotAlpha, globe1Tg) ;
    globe1RotInt.setSchedulingBounds(new BoundingSphere()) ;
    globe1Tg.addChild(globe1RotInt) ;

    //Create a Sphere
    Sphere globe1Obj = new Sphere(1.0f, Sphere.GENERATE_NORMALS |
				  Sphere.GENERATE_TEXTURE_COORDS, 90);


    TextureLoader texL = new TextureLoader(new String("earth.jpg"), new String("RGB"), this);
    if (texL  != null){
      Appearance ap = globe1Obj.getAppearance();
      TextureAttributes texA = new TextureAttributes() ;
      texA.setTextureBlendColor( 1f, 1f, 1f ,1f) ;
      texA.setTextureMode (TextureAttributes.MODULATE) ;
      ap.setTextureAttributes(texA) ;
      ap.setTexture(texL.getTexture());
    }
    // Add 'globe1Obj' to 'globe1Tg'
    globe1Tg.addChild(globe1Obj) ;
*/


    // Let Java 3D perform optimizations on this scene graph.
    rootBg.compile();
    return rootBg;
  }

  public Billboard3d (String argv[]){
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D c = new Canvas3D(config);
    add("Center", c);
    NewTextureLoader.setImageObserver(c);
    SimpleUniverse u = new SimpleUniverse(c);
    
    View uView = u.getViewer().getView() ;
    uView.setBackClipDistance(1e2) ;
    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();


    BranchGroup scene = createSceneGraph(u);

    u.addBranchGraph(scene);
  }


  public static void main(String argv[])
  {
    BranchGroup group;

    File cwd = new File(".") ;
    System.out.println("Billboard3d Version 01") ;
    System.out.println("CWD:  " + cwd.getAbsolutePath()) ;

    new MainFrame(new Billboard3d(argv), 750, 750);
    System.out.println("Exiting normally.") ;

  }
}

