comparison src/samer/j3d/J3DViewerMorph.java.not @ 0:bf79fb79ee13

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