samer@0: /* samer@0: hello6.java samer@0: samer@0: Construct universe without using SimpleUniverse samer@0: utility class - this puts together the view samer@0: group explicitly. samer@0: samer@0: Now has dynamically creatable views. samer@0: Have to construct views on BranchGroups so they samer@0: can be added while universe is live? samer@0: */ samer@0: package samer.j3d; samer@0: samer@0: import samer.core.*; samer@0: import samer.maths.*; samer@0: import samer.tools.*; samer@0: import java.io.*; samer@0: import java.awt.event.*; samer@0: import javax.media.j3d.*; samer@0: import javax.vecmath.*; samer@0: samer@0: import java.util.Enumeration; samer@0: samer@0: public class J3DViewerMorph extends Util implements Agent samer@0: { samer@0: Root root=new Root(); samer@0: Switch switcher; samer@0: Morph morph; samer@0: Viewer V; samer@0: Matrix P,P2; samer@0: Appearance app=points(new Appearance()); samer@0: Behavior morpher; samer@0: samer@0: public J3DViewerMorph(VVector A) throws Exception samer@0: { samer@0: Shell.print("creating PointsMatrix"); samer@0: samer@0: P=new Matrix("PointsMatrix",A.size(),3); samer@0: new MatrixAgent(P).execute("load",Shell.env()); samer@0: P2=new Matrix("PointsMatrix2",A.size(),3); samer@0: new MatrixAgent(P2).execute("load",Shell.env()); samer@0: samer@0: // create scene samer@0: addBackground(root,new Background(new Color3f(0.26f,0.23f,0.35f))); samer@0: samer@0: GeometryArray geoms[] = { samer@0: new MatrixPointArray(P2,A), samer@0: new MatrixPointArray(P,A) samer@0: }; samer@0: samer@0: morph=new Morph(geoms,app); samer@0: morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE); samer@0: morpher=new MorphBehavior(); samer@0: morpher.setSchedulingBounds(getBounds()); samer@0: samer@0: switcher=new Switch(); samer@0: switcher.addChild(new Shape3D(geoms[1], app)); samer@0: switcher.addChild(morph); samer@0: switcher.setCapability(Switch.ALLOW_SWITCH_WRITE); samer@0: switcher.setWhichChild(0); samer@0: samer@0: // root.addChild(rotaterise(morph)); samer@0: root.addChild(morpher); samer@0: root.addChild(rotaterise(switcher)); samer@0: samer@0: V=new Viewer("particles"); samer@0: samer@0: root.addChild(V); samer@0: root.addChild(new FPS(200)); samer@0: root.compile(); samer@0: root.golive(); samer@0: samer@0: Shell.registerAgent(this); samer@0: Shell.exposeCommands(this); samer@0: samer@0: V.V.startView(); samer@0: } samer@0: samer@0: public void getCommands(Agent.Registry r) { r.add("switch").add("morph"); } samer@0: public void execute(String cmd, Environment env) throws Exception samer@0: { samer@0: if (cmd.equals("switch")) { samer@0: switcher.setWhichChild(X._int(env.datum(),0)); samer@0: } else if (cmd.equals("morph")) { samer@0: } samer@0: } samer@0: samer@0: public Task getTask() { return V.V; } samer@0: samer@0: private static Group rotaterise(javax.media.j3d.Node n) samer@0: { samer@0: TransformGroup tg = new TransformGroup(); samer@0: // TransformGroup tg2=new TransformGroup(); samer@0: samer@0: mouseRotate(tg); samer@0: mouseTranslate(tg); samer@0: mouseZoom(tg); samer@0: // addRotator(tg,Shell.getInt("rotation.period",10000)); samer@0: tg.addChild(n); samer@0: // tg.addChild(tg2); samer@0: return tg; samer@0: } samer@0: samer@0: public class MorphBehavior extends Behavior samer@0: { samer@0: private WakeupCriterion nextframe; samer@0: private WakeupCriterion trigger; samer@0: private double t, weights[]; samer@0: samer@0: public MorphBehavior() { samer@0: trigger = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); samer@0: nextframe = new WakeupOnElapsedFrames(1); samer@0: weights = new double[2]; samer@0: } samer@0: samer@0: public void initialize() { samer@0: Shell.print("behavior init"); samer@0: t=0; this.wakeupOn(trigger); samer@0: } samer@0: samer@0: public void processStimulus(Enumeration criteria) samer@0: { samer@0: if (criteria.nextElement().equals(trigger)){ samer@0: Shell.print("morphing"); samer@0: samer@0: t=0; samer@0: weights[0]=1; samer@0: weights[1]=0; samer@0: morph.setWeights(weights); samer@0: wakeupOn(nextframe); samer@0: samer@0: } else { samer@0: t += 0.01; samer@0: if (t < 1){ samer@0: samer@0: weights[0]=1-t; samer@0: weights[1]=t; samer@0: morph.setWeights(weights); samer@0: wakeupOn(nextframe); samer@0: samer@0: } else { samer@0: weights[0]=0; samer@0: weights[1]=1; samer@0: morph.setWeights(weights); samer@0: samer@0: Shell.print("finished morphing"); samer@0: wakeupOn(trigger); samer@0: } samer@0: } samer@0: } samer@0: } samer@0: }