diff examples/java3d/Viewer.java @ 1:5df24c91468d

Oh my what a mess.
author samer
date Fri, 05 Apr 2019 16:26:00 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/java3d/Viewer.java	Fri Apr 05 16:26:00 2019 +0100
@@ -0,0 +1,97 @@
+/*
+		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(); }
+	}
+}