package alive;

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

import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import java.io.*;

public class Actor_ObjFile extends Actor {
  String file = null;
  java.net.URL  filename = null;
  boolean noTriangulate = false;
  boolean noStripify = false;
  private double creaseAngle = 60.0;
  public TransformGroup t0 = null;
  public Transform3D t = null;

  public Actor_ObjFile(String fn) {
    file = fn;
    model = "OBJ:" + 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);
        }
    }

    int flags = ObjectFile.RESIZE;
    if (!noTriangulate) flags |= ObjectFile.TRIANGULATE;
    if (!noStripify) flags |= ObjectFile.STRIPIFY;
    ObjectFile f = new ObjectFile(flags,
      (float)(creaseAngle * Math.PI / 180.0));
    Scene s = null;
    try {
      s = f.load(filename);
    }
    catch (FileNotFoundException e) {
      System.err.println(e);
      System.exit(1);
    }
    catch (ParsingErrorException e) {
      System.err.println(e);
      System.exit(1);
    }
    catch (IncorrectFormatException e) {
      System.err.println(e);
      System.exit(1);
    }
    t = new Transform3D();
    t0 = new TransformGroup(t);
    t0.addChild(s.getSceneGroup());
    tg.addChild(t0);
  }

  public void rotX(double val) {
    t.rotX(val);
    t0.setTransform(t);
  }
  public void rotY(double val) {
    t.rotY(val);
    t0.setTransform(t);
  }
  public void rotZ(double val) {
    t.rotZ(val);
    t0.setTransform(t);
  }
}
