view examples/java3d/Viewer.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents 5df24c91468d
children
line wrap: on
line source
/*
		Viewer.java

		A View branch group containing a ViewPlatform
		and associated View, Canvas etc. Creates a
		window for itself. Can be added dynamically
		to a live universe.
 */

import samer.core.*;
import java.awt.event.*;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.keyboard.*;

import samer.core.Node;

public class Viewer extends BranchGroup implements Agent
{
	Node			node;
	Canvas3D		canvas;
	TransformGroup	VT=new TransformGroup();
	View			V=new View();
	Shell.Window	win;

	public void lookFrom(Point3d eye, Vector3d up)
	{
		// view platform placement			
		Transform3D		t=new Transform3D();
		t.lookAt(eye, new Point3d(0,0,0), up);
		t.invert();
		VT.setTransform(t);
	}

	public Viewer(String name)
	{
		node=new Node(name);
		Shell.push(node);

		// create view bits
		canvas=new Canvas3D(util.getGraphicsConfiguration());

		// view platform placement			
		ViewPlatform	VP=new ViewPlatform();

		lookFrom(new Point3d(0,1,6), new Vector3d(0,1,0));

		if (Shell.getBoolean("keynav",false)) {
			// !!! all key navigators seem to be slaved!			
			util.addKeyNavigator(VT,util.readwrite(VT));
		}

		VT.addChild(VP);
		addChild(VT);

		// view itself
		V.addCanvas3D(canvas);
		V.setPhysicalBody(new PhysicalBody());
		V.setPhysicalEnvironment(new PhysicalEnvironment());
		V.attachViewPlatform(VP);
		V.setWindowEyepointPolicy(View.RELATIVE_TO_WINDOW);
		V.setWindowResizePolicy(View.VIRTUAL_WORLD);
		//V.setLocalEyeLightingEnable(true);
		//v.setWindowMovementPolicy(View.VIRTUAL_WORLD);

		setCapability(ALLOW_DETACH);
		setCapability(ALLOW_CHILDREN_WRITE);
		compile();
		Shell.pop();

		win=Shell.getWindow(name);
		win.container().setLayout(new BorderLayout());
		win.container().add(canvas);
		win.expose();
		win.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) { dispose(); }	
		});

		Shell.registerAgent(this);
	}

	public void dispose() 
	{
		Shell.deregisterAgent(this);
		win.dispose(); 
		detach();
	}						

	protected void finalize() { Shell.trace("finalizing view "+this); }

	public void getCommands(Agent.Registry r) { r.add("start").add("stop"); }
	public void execute(String cmd, Environment env) {
		if (cmd.equals("start")) { V.startView(); } // canvas.startRenderer(); }
		else if (cmd.equals("stop")) { V.stopView(); } // canvas.stopRenderer(); }
	}
}