StereoGirl.java Source code

Java tutorial

Introduction

Here is the source code for StereoGirl.java

Source

// //Copyright 2001 Resplendent Technology Ltd. greg@resplendent.com

import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.GraphicsConfiguration;
import java.net.URL;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class StereoGirl extends Applet {

    private Canvas3D c1 = new Canvas3D(SimpleUniverse.getPreferredConfiguration());

    private Canvas3D c2 = new Canvas3D(SimpleUniverse.getPreferredConfiguration());

    private static MainFrame mf;

    private SimpleUniverse u = null;

    private BranchGroup scene = null;

    private String URLString = "http://216.250.225.154/renee.obj";

    public void init() {
        setLayout(new FlowLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        String v = getParameter("url");
        if (v != null) {
            URLString = v;
        }
        c1.setSize(140, 140);
        c1.setMonoscopicViewPolicy(View.LEFT_EYE_VIEW);
        add(c1);

        c2.setSize(140, 140);
        c2.setMonoscopicViewPolicy(View.RIGHT_EYE_VIEW);
        add(c2);

        // Create a simple scene and attach it to the virtual universe
        scene = createSceneGraph(0);
        u = new SimpleUniverse(c1);

        View view0 = u.getViewer().getView();
        View view = new View();
        PhysicalBody myBod = view0.getPhysicalBody();
        myBod.setLeftEyePosition(new Point3d(-.006, 0.0, 0.0)); // default
        // is(-0.033,
        // 0.0, 0.0)
        myBod.setRightEyePosition(new Point3d(+.006, 0.0, 0.0));
        view.setPhysicalBody(myBod);
        view.setPhysicalEnvironment(view0.getPhysicalEnvironment());
        view.attachViewPlatform(u.getViewingPlatform().getViewPlatform());
        view.addCanvas3D(c2);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);

    }

    public BranchGroup createSceneGraph(int i) {
        System.out.println("Creating scene for: " + URLString);
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();
        try {

            Transform3D myTransform3D = new Transform3D();
            myTransform3D.setTranslation(new Vector3f(+0.0f, -0.1f, -1.2f));
            TransformGroup objTrans = new TransformGroup(myTransform3D);
            objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            Transform3D t = new Transform3D();
            TransformGroup tg = new TransformGroup(t);
            tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            objTrans.addChild(tg);

            URL url = new URL(URLString);
            ObjectFile f = new ObjectFile();
            f.setFlags(ObjectFile.RESIZE | ObjectFile.TRIANGULATE | ObjectFile.STRIPIFY);
            System.out.println("About to load");
            Scene s = f.load(url);
            tg.addChild(s.getSceneGroup());
            System.out.println("Finished Loading");
            BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
            Color3f light1Color = new Color3f(.9f, 0.8f, 0.8f);
            Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
            DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
            light1.setInfluencingBounds(bounds);
            objTrans.addChild(light1);
            // Set up the ambient light
            Color3f ambientColor = new Color3f(1.0f, .4f, 0.3f);
            AmbientLight ambientLightNode = new AmbientLight(ambientColor);
            ambientLightNode.setInfluencingBounds(bounds);
            objTrans.addChild(ambientLightNode);

            MouseRotate behavior = new MouseRotate();
            behavior.setTransformGroup(tg);
            objTrans.addChild(behavior);
            // Create the translate behavior node
            MouseTranslate behavior3 = new MouseTranslate();
            behavior3.setTransformGroup(tg);
            objTrans.addChild(behavior3);
            behavior3.setSchedulingBounds(bounds);

            KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg);
            keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0));
            objTrans.addChild(keyNavBeh);

            behavior.setSchedulingBounds(bounds);
            objRoot.addChild(objTrans);
        } catch (Throwable t) {
            System.out.println("Error: " + t);
        }
        return objRoot;
    }

    public StereoGirl() {
    }

    public void destroy() {
        u.removeAllLocales();
    }

    public static void main(String[] args) {

        StereoGirl s = new StereoGirl();
        if (args.length > 0) {
            s.URLString = args[0];
        }
        mf = new MainFrame(s, 400, 200);
    }
}