view src/samer/j3d/J3DViewerMorph.java.not @ 8:5e3cbbf173aa tip

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

		Construct universe without using SimpleUniverse 
		utility class - this puts together the view
		group explicitly.

		Now has dynamically creatable views.
		Have to construct views on BranchGroups so they
		can be added while universe is live?	
 */
package samer.j3d;

import samer.core.*;
import samer.maths.*;
import samer.tools.*;
import java.io.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import java.util.Enumeration;

public class J3DViewerMorph extends Util implements Agent
{
	Root			root=new Root();
	Switch		switcher;
	Morph			morph;
	Viewer		V;
	Matrix		P,P2;
	Appearance	app=points(new Appearance());
	Behavior		morpher;
					
	public J3DViewerMorph(VVector A) throws Exception
	{		
		Shell.print("creating PointsMatrix");

		P=new Matrix("PointsMatrix",A.size(),3);
		new MatrixAgent(P).execute("load",Shell.env());
		P2=new Matrix("PointsMatrix2",A.size(),3);
		new MatrixAgent(P2).execute("load",Shell.env());

		// create scene
		addBackground(root,new Background(new Color3f(0.26f,0.23f,0.35f)));

		GeometryArray geoms[] = {
			new MatrixPointArray(P2,A),
			new MatrixPointArray(P,A)
		};

		morph=new Morph(geoms,app);
		morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
		morpher=new MorphBehavior();
		morpher.setSchedulingBounds(getBounds());

		switcher=new Switch();
		switcher.addChild(new Shape3D(geoms[1], app));
		switcher.addChild(morph);
		switcher.setCapability(Switch.ALLOW_SWITCH_WRITE);
		switcher.setWhichChild(0);

		// root.addChild(rotaterise(morph));
		root.addChild(morpher);
		root.addChild(rotaterise(switcher));

		V=new Viewer("particles");
	
		root.addChild(V);
		root.addChild(new FPS(200));
		root.compile();
		root.golive();

		Shell.registerAgent(this);
		Shell.exposeCommands(this);

		V.V.startView();
	}

	public void getCommands(Agent.Registry r) { r.add("switch").add("morph"); }
	public void execute(String cmd, Environment env) throws Exception
	{
		if (cmd.equals("switch")) { 
			switcher.setWhichChild(X._int(env.datum(),0));
		} else if (cmd.equals("morph")) {
		}
	}
	
	public Task getTask() { return V.V; }
	
	private static Group rotaterise(javax.media.j3d.Node n)
	{
		TransformGroup	tg = new TransformGroup();
//		TransformGroup tg2=new TransformGroup();

		mouseRotate(tg);
		mouseTranslate(tg);
		mouseZoom(tg);
//		addRotator(tg,Shell.getInt("rotation.period",10000));
		tg.addChild(n);
		// tg.addChild(tg2);
		return tg;
	}

	public class MorphBehavior extends Behavior 
	{
		private WakeupCriterion nextframe;
		private WakeupCriterion trigger;
		private	double t, weights[];

		public MorphBehavior() {
			trigger = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
			nextframe = new WakeupOnElapsedFrames(1);
			weights = new double[2];
		}

		public void initialize() { 
			Shell.print("behavior init");
			t=0; this.wakeupOn(trigger); 
		}

		public void processStimulus(Enumeration criteria)
		{
			if (criteria.nextElement().equals(trigger)){
				Shell.print("morphing");

				t=0;
				weights[0]=1;
				weights[1]=0;
				morph.setWeights(weights);
				wakeupOn(nextframe);

			} else { 
				t += 0.01; 
				if (t < 1){

					weights[0]=1-t;
					weights[1]=t;
					morph.setWeights(weights);
					wakeupOn(nextframe);

				} else { 
					weights[0]=0;
					weights[1]=1;
					morph.setWeights(weights);

					Shell.print("finished morphing");
					wakeupOn(trigger);
				}
			}
		}
	}
}