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 MorphPoints extends Util implements Agent samer@0: { samer@0: Switch switcher; samer@0: Morph morph; samer@0: Matrix P1,P2; samer@0: Appearance app=points(); samer@0: Behavior morpher; samer@0: samer@0: public MorphPoints(Root root, Matrix P1, Matrix P2, VVector A) throws Exception samer@0: { samer@0: Shell.print("creating PointsMatrix"); samer@0: samer@0: this.P1=P1; samer@0: this.P2=P2; samer@0: samer@0: GeometryArray geoms[] = { samer@0: new Points3D(P2,A), samer@0: new Points3D(P1,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(switcher); samer@0: samer@0: Shell.exposeCommands(this); 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 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: }