samer@0
|
1 package samer.j3d;
|
samer@0
|
2
|
samer@0
|
3 import samer.core.*;
|
samer@0
|
4 import samer.maths.*;
|
samer@0
|
5 import samer.tools.*;
|
samer@0
|
6 import java.io.*;
|
samer@0
|
7 import java.awt.event.*;
|
samer@0
|
8 import javax.media.j3d.*;
|
samer@0
|
9 import javax.vecmath.*;
|
samer@0
|
10
|
samer@0
|
11 import java.util.Enumeration;
|
samer@0
|
12
|
samer@0
|
13 public class MorphPoints extends Util implements Agent
|
samer@0
|
14 {
|
samer@0
|
15 Switch switcher;
|
samer@0
|
16 Morph morph;
|
samer@0
|
17 Matrix P1,P2;
|
samer@0
|
18 Appearance app=points();
|
samer@0
|
19 Behavior morpher;
|
samer@0
|
20
|
samer@0
|
21 public MorphPoints(Root root, Matrix P1, Matrix P2, VVector A) throws Exception
|
samer@0
|
22 {
|
samer@0
|
23 Shell.print("creating PointsMatrix");
|
samer@0
|
24
|
samer@0
|
25 this.P1=P1;
|
samer@0
|
26 this.P2=P2;
|
samer@0
|
27
|
samer@0
|
28 GeometryArray geoms[] = {
|
samer@0
|
29 new Points3D(P2,A),
|
samer@0
|
30 new Points3D(P1,A)
|
samer@0
|
31 };
|
samer@0
|
32
|
samer@0
|
33 morph=new Morph(geoms,app);
|
samer@0
|
34 morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
|
samer@0
|
35 morpher=new MorphBehavior();
|
samer@0
|
36 morpher.setSchedulingBounds(getBounds());
|
samer@0
|
37
|
samer@0
|
38 switcher=new Switch();
|
samer@0
|
39 switcher.addChild(new Shape3D(geoms[1], app));
|
samer@0
|
40 switcher.addChild(morph);
|
samer@0
|
41 switcher.setCapability(Switch.ALLOW_SWITCH_WRITE);
|
samer@0
|
42 switcher.setWhichChild(0);
|
samer@0
|
43
|
samer@0
|
44 // root.addChild(rotaterise(morph));
|
samer@0
|
45 root.addChild(morpher);
|
samer@0
|
46 root.addChild(switcher);
|
samer@0
|
47
|
samer@0
|
48 Shell.exposeCommands(this);
|
samer@0
|
49 }
|
samer@0
|
50
|
samer@0
|
51 public void getCommands(Agent.Registry r) { r.add("switch").add("morph"); }
|
samer@0
|
52 public void execute(String cmd, Environment env) throws Exception
|
samer@0
|
53 {
|
samer@0
|
54 if (cmd.equals("switch")) {
|
samer@0
|
55 switcher.setWhichChild(X._int(env.datum(),0));
|
samer@0
|
56 } else if (cmd.equals("morph")) {
|
samer@0
|
57 }
|
samer@0
|
58 }
|
samer@0
|
59
|
samer@0
|
60 public class MorphBehavior extends Behavior
|
samer@0
|
61 {
|
samer@0
|
62 private WakeupCriterion nextframe;
|
samer@0
|
63 private WakeupCriterion trigger;
|
samer@0
|
64 private double t, weights[];
|
samer@0
|
65
|
samer@0
|
66 public MorphBehavior() {
|
samer@0
|
67 trigger = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
|
samer@0
|
68 nextframe = new WakeupOnElapsedFrames(1);
|
samer@0
|
69 weights = new double[2];
|
samer@0
|
70 }
|
samer@0
|
71
|
samer@0
|
72 public void initialize() {
|
samer@0
|
73 Shell.print("behavior init");
|
samer@0
|
74 t=0; this.wakeupOn(trigger);
|
samer@0
|
75 }
|
samer@0
|
76
|
samer@0
|
77 public void processStimulus(Enumeration criteria)
|
samer@0
|
78 {
|
samer@0
|
79 if (criteria.nextElement().equals(trigger)){
|
samer@0
|
80 Shell.print("morphing");
|
samer@0
|
81
|
samer@0
|
82 t=0;
|
samer@0
|
83 weights[0]=1;
|
samer@0
|
84 weights[1]=0;
|
samer@0
|
85 morph.setWeights(weights);
|
samer@0
|
86 wakeupOn(nextframe);
|
samer@0
|
87
|
samer@0
|
88 } else {
|
samer@0
|
89 t += 0.01;
|
samer@0
|
90 if (t < 1){
|
samer@0
|
91
|
samer@0
|
92 weights[0]=1-t;
|
samer@0
|
93 weights[1]=t;
|
samer@0
|
94 morph.setWeights(weights);
|
samer@0
|
95 wakeupOn(nextframe);
|
samer@0
|
96
|
samer@0
|
97 } else {
|
samer@0
|
98 weights[0]=0;
|
samer@0
|
99 weights[1]=1;
|
samer@0
|
100 morph.setWeights(weights);
|
samer@0
|
101
|
samer@0
|
102 Shell.print("finished morphing");
|
samer@0
|
103 wakeupOn(trigger);
|
samer@0
|
104 }
|
samer@0
|
105 }
|
samer@0
|
106 }
|
samer@0
|
107 }
|
samer@0
|
108 }
|