package alive;

/**
 * <ALIVE Project>
 * LightWave Object File Actor
 * Default Actor for simple experiments
 * attributes can be mapped into size, color and
 * transparency
 * LSI - POLI - USP
 */

import com.sun.j3d.loaders.lw3d.Lw3dLoader;
import com.sun.j3d.loaders.Loader;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.vecmath.Matrix4d;

public class Actor_LwsFile extends Actor {
  String file = null;
  public java.net.URL filename = null;
  boolean noTriangulate = false;
  boolean noStripify = false;
  private double creaseAngle = 60.0;
  Transform3D t = new Transform3D();
  TransformGroup sceneTransform = null;

  public Actor_LwsFile(String fn) {
    file = fn;
    model = "LWO:" + file;
    build();
  }

  public void createGeometry() {
    if (filename == null) {
        try { filename = new java.net.URL(file);
        } catch (java.net.MalformedURLException ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace();
            System.exit(1);
        }
    }

    Loader lw3dLoader = new Lw3dLoader(Loader.LOAD_ALL);
    Scene loaderScene = null;
    try { loaderScene = lw3dLoader.load(filename);
    } catch (Exception e) { e.printStackTrace(); System.exit(1); }

    if (loaderScene.getSceneGroup() != null) {
        // Instead of using the default view location (which may be
        // completely bogus for the particular file you're loading),
        // let's use the initial view from the file.  We can get
        // this by getting the  view groups from the scene (there's
        // only one for Lightwave 3D), then using the inverse of the
        // transform on that view as the transform for the entire scene.

        // First, get the view groups (shouldn't be null unless there
        // was something wrong in the load
        TransformGroup viewGroups[] = loaderScene.getViewGroups();

        // Get the Transform3D from the view and invert it

        Transform3D t = new Transform3D();
        t.setScale(.01);
        //viewGroups[0].getTransform(t);
        //Matrix4d m = new Matrix4d();
        //t.get(m);
        //m.invert();
        //t.set(m);

        // Now we've got the transform we want.  Create an
        // appropriate TransformGroup and parent the scene to it.
        // Then insert the new group into the main BranchGroup.
        sceneTransform = new TransformGroup(t);
        sceneTransform.addChild(loaderScene.getSceneGroup());
        tg.addChild(sceneTransform);
    }
  }
  public void rotX(double val) {
    t.rotX(val);
    t.setScale(.01);
    sceneTransform.setTransform(t);
  }
  public void rotY(double val) {
    t.rotY(val);
    t.setScale(.01);
    sceneTransform.setTransform(t);
  }
  public void rotZ(double val) {
    t.rotZ(val);
    t.setScale(.01);
    sceneTransform.setTransform(t);
  }

}
