/*******************************************************************************
 *                      Copyright (c) 1999 Justin Couch
 *                               Java Source
 *
 * Raw J3D Tutorial
 *
 * Version History
 * Date        Version  Programmer
 * ----------  -------  ------------------------------------------
 * 01/08/1998  1.0.0    Justin Couch
 *
 ******************************************************************************/

// no package

// Standard imports
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import javax.vecmath.AxisAngle4f;

// Application specific imports
// none

/**
 * Test class for showing the use of a View and ViewPlatform
 * <P>
 * Basic view consists of the standard placement.
 *
 * @author Justin Couch
 * @version Who Cares!
 */
public class Camera
{
  private static final double BACK_CLIP_DISTANCE = 100.0;
  private static final Color3f White = new Color3f(1, 1, 1);

  private static final BoundingSphere LIGHT_BOUNDS;

  private Group hud_group;
  private TransformGroup root_tx_grp;
  private Transform3D location;
  private ViewPlatform platform;
  private View view;
  private DirectionalLight headlight;

  private PhysicalBody body;
  private PhysicalEnvironment env;

  static
  {
    Point3d origin = new Point3d(0, 0, 0);
    LIGHT_BOUNDS = new BoundingSphere(origin, BACK_CLIP_DISTANCE);
  }

  public Camera()
  {
    hud_group = new Group();
    hud_group.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    platform = new ViewPlatform();
    location = new Transform3D();

    root_tx_grp = new TransformGroup();
    root_tx_grp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    root_tx_grp.setTransform(location);
    root_tx_grp.addChild(platform);
    root_tx_grp.addChild(hud_group);

    // now create the headlight
    headlight = new DirectionalLight();
    headlight.setCapability(Light.ALLOW_STATE_WRITE);
    headlight.setColor(White);
    headlight.setInfluencingBounds(LIGHT_BOUNDS);
    root_tx_grp.addChild(headlight);

    body = new PhysicalBody();
    env = new PhysicalEnvironment();

    view = new View();
    view.setBackClipDistance(BACK_CLIP_DISTANCE);
    view.setPhysicalBody(body);
    view.setPhysicalEnvironment(env);
    view.attachViewPlatform(platform);
  }

  /**
   * Set the canvas that this camera is using
   *
   * @param canvas The canvas that is to be used for this camera
   */
  public void setCanvas(Canvas3D canvas)
  {
    view.addCanvas3D(canvas);
  }

  /**
   * Set the location of the camera. This is the location of the center of
   * the camera relative to whatever is used as its root group node.
   *
   * @param loc The location of the camera
   */
  public void setLocation(Vector3f loc)
  {
    location.setTranslation(loc);
    root_tx_grp.setTransform(location);
  }

  /**
   * Set the orientation of the camera.
   *
   * @param angle The orientation of the camera
   */
  public void setOrientation(AxisAngle4f angle)
  {
    location.setRotation(angle);
    root_tx_grp.setTransform(location);
  }

  /**
   * Add some goemetry to the HUD area. This geometry must come complete with
   * its own parent transform to offset the object by the appropriate amount.
   * The camera does not do any auto-offsets of geometry.
   *
   * @param geom The geometry to add
   */
  public void addHUDObject(Node geom)
  {
    hud_group.addChild(geom);
  }

  /**
   * Enable the headlight that is attached to the camera.
   *
   * @param enable True if the light is to be turned on
   */
  public void setHeadLight(boolean enable)
  {
    headlight.setEnable(enable);
  }

  /**
   * Get the J3D node that is used to represent the camera
   *
   * @return The root TransformGroup of the camera
   */
  Node getNode()
  {
    return root_tx_grp;
  }
}
