package game.util3d;

import java.awt.GraphicsEnvironment;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;

//To get this out of the com.sun.j3d.utils.universe package, have to get
//rid of all references to ViewingPlatform and Viewer

public class MyUniverse extends VirtualUniverse
{
    private TransformGroup vpTrans;

    private Locale locale;
    private Viewer viewer;

    private ViewingPlatform viewingPlatform;

    public MyUniverse(Canvas3D canvas) {
            locale = new Locale(this);
        // Create the ViewingPlatform and Viewer objects, passing
        // down the appropriate parameters.
        viewingPlatform = new ViewingPlatform(1);
        viewer = new Viewer(canvas /*, userConfig */);
        viewer.getView().setBackClipDistance(1000);

        vpTrans = viewingPlatform.getViewPlatformTransform();
        vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    }
    public void live()
    {
        viewer.setViewingPlatform(viewingPlatform);

        locale.addBranchGraph(viewingPlatform);
    }

    public TransformGroup getVPTransformGroup()
    {return vpTrans;}

    public Locale getLocale() {
        return locale;
    }

    public Viewer getViewer() {
        return viewer;
    }

    public ViewingPlatform getViewingPlatform() {
        return viewingPlatform;
    }

    public Canvas3D getCanvas() {
        return getCanvas(0);
    }

    public Canvas3D getCanvas(int canvasNum) {
        return viewer.getCanvases();
    }

    public void addBranchGraph(BranchGroup bg) {
        locale.addBranchGraph(bg);
    }

    public static GraphicsConfiguration getPreferredConfiguration() {
        GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
        String stereo;

        // Check if the user has set the Java 3D stereo option.
        // Getting the system properties causes appletviewer to fail with a
        //  security exception without a try/catch.
        stereo = (String) java.security.AccessController.doPrivileged(
           new java.security.PrivilegedAction() {
           public Object run() {
               return System.getProperties().getProperty("j3d.stereo");
           }
        });

        // update template based on properties.
        if (stereo != null) {
            if (stereo.equals("REQUIRED"))
                template.setStereo(template.REQUIRED);
            else if (stereo.equals("PREFERRED"))
                template.setStereo(template.PREFERRED);
        }

        // Return the GraphicsConfiguration that best fits our needs.
        return GraphicsEnvironment.getLocalGraphicsEnvironment().
                getDefaultScreenDevice().getBestConfiguration(template);
    }

}
