/*
 * RotatingSphere.java
 *
 * Created on 20 jully 2001, 9:25
 */


/**
 *
 * @author  Frank Bellegarde
 * @version 
 */

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.applet.MainFrame;
//import hplb.java3d.*;
//import chat.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
import java.util.Properties;
import java.net.URL;
import javax.vecmath.*;
import javax.swing.*;
import javax.media.j3d.*;
import java.applet.Applet;


public class RotatingSphere extends Applet{
    public SimpleUniverse univers;
    public Canvas3D canvas;
    public PointLight lgt1;
    public BranchGroup objets;
    public TransformGroup objetsTG;
    public Sphere sphere;
    
    public RotatingSphere(){
    
    // initialization of the universe
    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
    canvas = new Canvas3D(gc);
    canvas.setSize(300, 200);
    add(canvas);
    univers = new SimpleUniverse(canvas);
    ViewingPlatform vp = univers.getViewingPlatform();
    TransformGroup viewTransformGroup = vp.getViewPlatformTransform();

  // light settings
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 10000.0);
    lgt1 = new PointLight(new Color3f(0.8f,0.8f,0.8f), new Point3f(0f,0.0f,20.0f) , new Point3f(1.0f,0.0f,0.0f));
    lgt1.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    lgt1.setInfluencingBounds(bounds);
    BranchGroup bg1 = new BranchGroup();
    bg1.addChild(lgt1);
    viewTransformGroup.addChild(bg1);
    
     // repositioning the viewing platform
        Transform3D bouge = new Transform3D();
        bouge.set(new Vector3f(0.0f,0.0f,30.0f));
        viewTransformGroup.setTransform(bouge);
     
     // setting up groups
        objets = new BranchGroup();
        objets.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
        objets.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        
        objetsTG = new TransformGroup();
        objetsTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objetsTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

      // Interpolator  
        Alpha alpha = new Alpha (-1, 60000);
        RotationInterpolator rotInt = new RotationInterpolator (alpha, objetsTG);
        rotInt.setSchedulingBounds(new BoundingSphere());
      
      // Appearance
        Appearance appearance = new Appearance();
        Material mat = new Material(new Color3f (0.2f,0.4f,0.5f), new Color3f (0.05f,0.05f,0.05f), new Color3f (0.6f,0.8f,0.9f), new Color3f (1.0f,0.9f,1.0f), 80.0f);
        mat.setLightingEnable(true);
        appearance.setMaterial(mat);
        appearance.setTransparencyAttributes(new TransparencyAttributes());
       
      // setting up sphere
        sphere= new Sphere(5.0f);
        TransformGroup sectionTG = new TransformGroup();
        sectionTG.addChild(sphere);
        
      // proceding with transforms
        objets.addChild(objetsTG);        
        objetsTG.addChild(sectionTG);
        objets.addChild(rotInt);
        
      
     // setting up an ugly background
        Color3f bgColor   = new Color3f(0f, 0f, 1.0f);    
        Background bg = new Background(bgColor);
        bg.setApplicationBounds(bounds);
        objets.addChild(bg);
        objets.compile();
        
     // adding branchGraph
        univers.addBranchGraph(objets);
        //repaint();
 }
   
 
 public static void main(String[] args) {
        System.out.print("RotatingSphere.java - Auteur : Frank Bellegarde \n");
        Frame frame = new MainFrame(new RotatingSphere(), 300, 200);
    } 
}
