samer@0
|
1 /*
|
samer@0
|
2 hello6.java
|
samer@0
|
3
|
samer@0
|
4 Construct universe without using SimpleUniverse
|
samer@0
|
5 utility class - this puts together the view
|
samer@0
|
6 group explicitly.
|
samer@0
|
7
|
samer@0
|
8 Now has dynamically creatable views.
|
samer@0
|
9 Have to construct views on BranchGroups so they
|
samer@0
|
10 can be added while universe is live?
|
samer@0
|
11 */
|
samer@0
|
12 package samer.j3d;
|
samer@0
|
13
|
samer@0
|
14 import samer.core.*;
|
samer@0
|
15 import samer.maths.*;
|
samer@0
|
16 import samer.tools.*;
|
samer@0
|
17 import java.io.*;
|
samer@0
|
18 import java.awt.event.*;
|
samer@0
|
19 import javax.media.j3d.*;
|
samer@0
|
20 import javax.vecmath.*;
|
samer@0
|
21
|
samer@0
|
22 import java.util.Enumeration;
|
samer@0
|
23
|
samer@0
|
24 public class J3DViewerMorph extends Util implements Agent
|
samer@0
|
25 {
|
samer@0
|
26 Root root=new Root();
|
samer@0
|
27 Switch switcher;
|
samer@0
|
28 Morph morph;
|
samer@0
|
29 Viewer V;
|
samer@0
|
30 Matrix P,P2;
|
samer@0
|
31 Appearance app=points(new Appearance());
|
samer@0
|
32 Behavior morpher;
|
samer@0
|
33
|
samer@0
|
34 public J3DViewerMorph(VVector A) throws Exception
|
samer@0
|
35 {
|
samer@0
|
36 Shell.print("creating PointsMatrix");
|
samer@0
|
37
|
samer@0
|
38 P=new Matrix("PointsMatrix",A.size(),3);
|
samer@0
|
39 new MatrixAgent(P).execute("load",Shell.env());
|
samer@0
|
40 P2=new Matrix("PointsMatrix2",A.size(),3);
|
samer@0
|
41 new MatrixAgent(P2).execute("load",Shell.env());
|
samer@0
|
42
|
samer@0
|
43 // create scene
|
samer@0
|
44 addBackground(root,new Background(new Color3f(0.26f,0.23f,0.35f)));
|
samer@0
|
45
|
samer@0
|
46 GeometryArray geoms[] = {
|
samer@0
|
47 new MatrixPointArray(P2,A),
|
samer@0
|
48 new MatrixPointArray(P,A)
|
samer@0
|
49 };
|
samer@0
|
50
|
samer@0
|
51 morph=new Morph(geoms,app);
|
samer@0
|
52 morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
|
samer@0
|
53 morpher=new MorphBehavior();
|
samer@0
|
54 morpher.setSchedulingBounds(getBounds());
|
samer@0
|
55
|
samer@0
|
56 switcher=new Switch();
|
samer@0
|
57 switcher.addChild(new Shape3D(geoms[1], app));
|
samer@0
|
58 switcher.addChild(morph);
|
samer@0
|
59 switcher.setCapability(Switch.ALLOW_SWITCH_WRITE);
|
samer@0
|
60 switcher.setWhichChild(0);
|
samer@0
|
61
|
samer@0
|
62 // root.addChild(rotaterise(morph));
|
samer@0
|
63 root.addChild(morpher);
|
samer@0
|
64 root.addChild(rotaterise(switcher));
|
samer@0
|
65
|
samer@0
|
66 V=new Viewer("particles");
|
samer@0
|
67
|
samer@0
|
68 root.addChild(V);
|
samer@0
|
69 root.addChild(new FPS(200));
|
samer@0
|
70 root.compile();
|
samer@0
|
71 root.golive();
|
samer@0
|
72
|
samer@0
|
73 Shell.registerAgent(this);
|
samer@0
|
74 Shell.exposeCommands(this);
|
samer@0
|
75
|
samer@0
|
76 V.V.startView();
|
samer@0
|
77 }
|
samer@0
|
78
|
samer@0
|
79 public void getCommands(Agent.Registry r) { r.add("switch").add("morph"); }
|
samer@0
|
80 public void execute(String cmd, Environment env) throws Exception
|
samer@0
|
81 {
|
samer@0
|
82 if (cmd.equals("switch")) {
|
samer@0
|
83 switcher.setWhichChild(X._int(env.datum(),0));
|
samer@0
|
84 } else if (cmd.equals("morph")) {
|
samer@0
|
85 }
|
samer@0
|
86 }
|
samer@0
|
87
|
samer@0
|
88 public Task getTask() { return V.V; }
|
samer@0
|
89
|
samer@0
|
90 private static Group rotaterise(javax.media.j3d.Node n)
|
samer@0
|
91 {
|
samer@0
|
92 TransformGroup tg = new TransformGroup();
|
samer@0
|
93 // TransformGroup tg2=new TransformGroup();
|
samer@0
|
94
|
samer@0
|
95 mouseRotate(tg);
|
samer@0
|
96 mouseTranslate(tg);
|
samer@0
|
97 mouseZoom(tg);
|
samer@0
|
98 // addRotator(tg,Shell.getInt("rotation.period",10000));
|
samer@0
|
99 tg.addChild(n);
|
samer@0
|
100 // tg.addChild(tg2);
|
samer@0
|
101 return tg;
|
samer@0
|
102 }
|
samer@0
|
103
|
samer@0
|
104 public class MorphBehavior extends Behavior
|
samer@0
|
105 {
|
samer@0
|
106 private WakeupCriterion nextframe;
|
samer@0
|
107 private WakeupCriterion trigger;
|
samer@0
|
108 private double t, weights[];
|
samer@0
|
109
|
samer@0
|
110 public MorphBehavior() {
|
samer@0
|
111 trigger = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
|
samer@0
|
112 nextframe = new WakeupOnElapsedFrames(1);
|
samer@0
|
113 weights = new double[2];
|
samer@0
|
114 }
|
samer@0
|
115
|
samer@0
|
116 public void initialize() {
|
samer@0
|
117 Shell.print("behavior init");
|
samer@0
|
118 t=0; this.wakeupOn(trigger);
|
samer@0
|
119 }
|
samer@0
|
120
|
samer@0
|
121 public void processStimulus(Enumeration criteria)
|
samer@0
|
122 {
|
samer@0
|
123 if (criteria.nextElement().equals(trigger)){
|
samer@0
|
124 Shell.print("morphing");
|
samer@0
|
125
|
samer@0
|
126 t=0;
|
samer@0
|
127 weights[0]=1;
|
samer@0
|
128 weights[1]=0;
|
samer@0
|
129 morph.setWeights(weights);
|
samer@0
|
130 wakeupOn(nextframe);
|
samer@0
|
131
|
samer@0
|
132 } else {
|
samer@0
|
133 t += 0.01;
|
samer@0
|
134 if (t < 1){
|
samer@0
|
135
|
samer@0
|
136 weights[0]=1-t;
|
samer@0
|
137 weights[1]=t;
|
samer@0
|
138 morph.setWeights(weights);
|
samer@0
|
139 wakeupOn(nextframe);
|
samer@0
|
140
|
samer@0
|
141 } else {
|
samer@0
|
142 weights[0]=0;
|
samer@0
|
143 weights[1]=1;
|
samer@0
|
144 morph.setWeights(weights);
|
samer@0
|
145
|
samer@0
|
146 Shell.print("finished morphing");
|
samer@0
|
147 wakeupOn(trigger);
|
samer@0
|
148 }
|
samer@0
|
149 }
|
samer@0
|
150 }
|
samer@0
|
151 }
|
samer@0
|
152 } |