package j3deditor.bin.editor;

import j3deditor.bin.hierarchy.J3DeScene;

import j3deditor.bin.util.Utilities;

import com.sun.j3d.utils.universe.SimpleUniverse;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.View;


/**
 * Visualizes the scene.
 *
 * @author Risto Seene
 */
public class Scene3D extends Canvas3D{


	/** the scene this <code>Canvas3D</code> renders */
	private J3DeScene scene;


	/** virtual universe for the scene */
	private SimpleUniverse universe;


	/**
	 * Creates an instance of <code>Scene3D</code>
	 * whidh visualizes the given scene.
	 *
	 * @param scene
	 *	the scene this <code>Canvas3D</code> renders
	 */
	public Scene3D(J3DeScene scene){
		super(SimpleUniverse.getPreferredConfiguration());

		this.scene = scene;

		universe = new SimpleUniverse(this);
		initView();
		updateScene();

		scene.getCamera().setCamera(universe.getViewingPlatform());
	}


	/**
	 * Initializes the <code>View</code> this <code>Canvas3D</code>
	 * references to
	 */
	private void initView(){
		try{
			Utilities.userdata.setData("tex3Dsupport", this.queryProperties().get("texture3DAvailable"));

			View view = getView();
			Utilities.userdata.setData("antialiasing", view.getSceneAntialiasingEnable());

			//view.setFieldOfView(1.1);
			//view.setProjectionPolicy(View.PARALLEL_PROJECTION);
		} catch(Throwable t){
		}
	}


	/**
	 * Reattaches the <code>BranchGroup</code> of the scene to the
	 * <code>SimpleUniverse</code> object this <code>Scene3D</code> contains.
	 */
	public void updateScene(){
		BranchGroup bg = scene.getBranchGroup();
		try{
			universe.getLocale().addBranchGraph(bg);
		} catch(Throwable t){
		}
	}

	/**
	 * Frees up all  resources.
	 */
	public void cleanUp(){
		this.universe.cleanup();
		this.scene = null;
		this.universe = null;
	}

}
