/*
 *      Based on:
 *
 *      @(#)MouseBehaviorApp.java 1.0 99/03/25 11:42:40
 *
 * Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
 *
 * John Dickinson
 *
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame; 
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import java.awt.event.*;
import java.util.Enumeration;

// geometry stuff
import javax.media.j3d.TriangleArray;

//   TestApp renders a single, interactively rotatable,
//   traslatable, and zoomable ASCII STL object.

public class TestApp extends Applet {

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

                // need lights
                // Create a bounds for the background and lights
                BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);

                // Set up the global lights
                Color3f lColor1 = new Color3f(0.9f, 0.9f, 0.9f);
                Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
                Color3f alColor = new Color3f(0.4f, 0.4f, 0.4f);

                AmbientLight aLgt = new AmbientLight(alColor);
                aLgt.setInfluencingBounds(bounds);
                DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
                lgt1.setInfluencingBounds(bounds);
                objRoot.addChild(aLgt);
                objRoot.addChild(lgt1);


                TransformGroup objTransform = new TransformGroup();
           objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
           objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	
           objRoot.addChild(objTransform);

                TriangleArray TriA = new TriangleArray(12, TriangleArray.COORDINATES);
                Point3d vertarr[] = new Point3d[12];
// adds a 4 facets with three vertices each making a tetrahedra
                vertarr[0] = new Point3d(0.0,0.0,0.0);
                vertarr[1] = new Point3d(1.0,0.0,0.0);
                vertarr[2] = new Point3d(0.0,0.0,1.0);

                vertarr[3] = new Point3d(0.0,0.0,0.0);
                vertarr[4] = new Point3d(0.0,0.0,1.0);
                vertarr[5] = new Point3d(0.0,1.0,0.0);

                vertarr[6] = new Point3d(0.0,0.0,0.0);
                vertarr[7] = new Point3d(0.0,1.0,0.0);
                vertarr[8] = new Point3d(1.0,0.0,0.0);

                vertarr[9] = new Point3d(1.0,0.0,0.0);
                vertarr[10] = new Point3d(0.0,1.0,0.0);
                vertarr[11] = new Point3d(0.0,0.0,1.0);

                TriA.setCoordinates(0,vertarr);
                Shape3D STLShape = new Shape3D(TriA);
           objTransform.addChild(STLShape);
// adds colorcube, soon to be eliminated.       	
//              objTransform.addChild(new ColorCube(0.4));
                Appearance TriApp = new Appearance();

//              ColoringAttributes TriCol= new ColoringAttributes(0.8f, 0.0f, 0.3f, ColoringAttributes.NICEST);
//              TriApp.setColoringAttributes(TriCol);
                Color3f TriCol = new Color3f(0.9f, 0.4f, 0.4f);

// Globally used colors
                Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
                Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

                TriApp.setMaterial(new Material(TriCol, black, TriCol, white, 80.0f));
                STLShape.setAppearance(TriApp);
	
           MouseRotate myMouseRotate = new MouseRotate();
           myMouseRotate.setTransformGroup(objTransform);
           myMouseRotate.setSchedulingBounds(new BoundingSphere());
           objRoot.addChild(myMouseRotate);

           MouseTranslate myMouseTranslate = new MouseTranslate();
           myMouseTranslate.setTransformGroup(objTransform);
        myMouseTranslate.setSchedulingBounds(new BoundingSphere());
           objRoot.addChild(myMouseTranslate);

           MouseZoom myMouseZoom = new MouseZoom();
           myMouseZoom.setTransformGroup(objTransform);
           myMouseZoom.setSchedulingBounds(new BoundingSphere());
           objRoot.addChild(myMouseZoom);

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

                return objRoot;
        } // end of CreateSceneGraph method of TestApp

   // Create a simple scene and attach it to the virtual universe

   public TestApp() {
           setLayout(new BorderLayout());
        Canvas3D canvas3D = new Canvas3D(null);
           add("Center", canvas3D);

           BranchGroup scene = createSceneGraph();

           // SimpleUniverse is a Convenience Utility class
        SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

                // This will move the ViewPlatform back a bit so the
                // objects in the scene can be viewed.
           simpleU.getViewingPlatform().setNominalViewingTransform();

           simpleU.addBranchGraph(scene);
   } // end of TestApp (constructor)

   //  The following allows this to be run as an application
   //  as well as an applet
	
   public static void main(String[] argv) {
           System.out.print("TestApp.java \n");
           System.out.println("Hold the mouse button while moving the mouse to make the cube move.");
           System.out.println("     left mouse button      - rotate cube");
           System.out.println("     right mouse button     - translate cube");
           System.out.println("     Alt+left mouse button  - zoom cube");
                if (argv.length != 0) {
                        System.out.println("TestApp expects no command line arguments.\n");
                } else {
                   Frame frame = new MainFrame(new TestApp(), 512, 512);
                }
//                      for (int i=0; i< argv.length;i++) System.out.print(argv[i] + " ");
   } // end of main (method of TestApp)

} // end of class TestApp


