/Users/lyon/j4p/src/j3d/Morph3App.java

1    package j3d; 
2     
3    /* 
4     *      @(#)Morph3App.java 1.0 1.1 00/09/22 14:37 
5     * 
6     * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved. 
7     * 
8     * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 
9     * modify and redistribute this software in source and binary code form, 
10    * provided that i) this copyright notice and license appear on all copies of 
11    * the software; and ii) Licensee does not utilize the software in a manner 
12    * which is disparaging to Sun. 
13    * 
14    * This software is provided "AS IS," without a warranty of any kind. ALL 
15    * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 
16    * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 
17    * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 
18    * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 
19    * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 
20    * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 
21    * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 
22    * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 
23    * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 
24    * POSSIBILITY OF SUCH DAMAGES. 
25    * 
26    * This software is not designed or intended for use in on-line control of 
27    * aircraft, air traffic, aircraft navigation or aircraft communications; or in 
28    * the design, construction, operation or maintenance of any nuclear 
29    * facility. Licensee represents and warrants that it will not use or 
30    * redistribute the Software for such purposes. 
31    */ 
32    
33   import com.sun.j3d.utils.applet.MainFrame; 
34   import com.sun.j3d.utils.universe.SimpleUniverse; 
35    
36   import javax.media.j3d.*; 
37   import javax.vecmath.Vector3f; 
38   import java.applet.Applet; 
39   import java.awt.*; 
40   import java.util.Enumeration; 
41    
42   //   Morph3App renders a single ColorCube 
43   //   that rotates when any key is pressed. 
44    
45   public class Morph3App extends Applet { 
46    
47       public class MorphTangoBehavior extends Behavior { 
48    
49           private Morph targetMorph; 
50           private Alpha alpha; 
51           // the following two members are here for effciency (no memory burn) 
52           private double[] weights = {0, 0, 0, 0}; 
53           private WakeupCondition trigger = new WakeupOnElapsedFrames(0); 
54    
55           // create MorphTangoBehavior 
56           MorphTangoBehavior(Morph targetMorph, Alpha alpha) { 
57               this.targetMorph = targetMorph; 
58               this.alpha = alpha; 
59           } 
60    
61           public void initialize() { 
62               // set initial wakeup condition 
63               this.wakeupOn(trigger); 
64           } 
65    
66           public void processStimulus(Enumeration criteria) { 
67               // don't need to decode event since there is only one trigger 
68    
69               // do what is necessary 
70               weights[0] = 0; 
71               weights[1] = 0; 
72               weights[2] = 0; 
73               weights[3] = 0; 
74               double alphaValue = (double) alpha.value(); 
75               weights[0] = alphaValue; 
76               weights[2] = 1.0 - alphaValue; 
77    
78               targetMorph.setWeights(weights); 
79               // set next wakeup condition 
80               this.wakeupOn(trigger); 
81           } 
82    
83       } // end of class MorphTangoBehavior 
84    
85       public class MorphBrokenBehavior extends Behavior { 
86    
87           private Morph targetMorph; 
88           private Alpha alpha; 
89           // the following two members are here for effciency (no memory burn) 
90           private double[] weights = {0, 0, 0, 0}; 
91           private WakeupCondition trigger = new WakeupOnElapsedFrames(0); 
92    
93           // create MorphBrokenBehavior 
94           MorphBrokenBehavior(Morph targetMorph, Alpha alpha) { 
95               this.targetMorph = targetMorph; 
96               this.alpha = alpha; 
97           } 
98    
99           public void initialize() { 
100              // set initial wakeup condition 
101              this.wakeupOn(trigger); 
102          } 
103   
104          public void processStimulus(Enumeration criteria) { 
105              // don't need to decode event since there is only one trigger 
106   
107              // do what is necessary 
108              weights[0] = 1; 
109              weights[1] = -1; 
110              weights[2] = 1; 
111              weights[3] = -1; 
112              float alphaValue = 4f * alpha.value() - 0.00001f; 
113              int alphaIndex = (int) alphaValue; 
114              weights[alphaIndex] = (double) alphaValue - (double) alphaIndex; 
115              if (alphaIndex < 3) 
116                  weights[alphaIndex + 1] = 1.0 - weights[alphaIndex]; 
117              else 
118                  weights[0] = 1.0 - weights[alphaIndex]; 
119   
120              targetMorph.setWeights(weights); 
121              // set next wakeup condition 
122              this.wakeupOn(trigger); 
123          } 
124   
125      } // end of class MorphBrokenBehavior 
126   
127      public class MorphInPlaceBehavior extends Behavior { 
128   
129          private Morph targetMorph; 
130          private Alpha alpha; 
131          // the following two members are here for effciency (no memory burn) 
132          private double[] weights = {0, 0, 0, 0}; 
133          private WakeupCondition trigger = new WakeupOnElapsedFrames(0); 
134   
135          // create MorphInPlaceBehavior 
136          MorphInPlaceBehavior(Morph targetMorph, Alpha alpha) { 
137              this.targetMorph = targetMorph; 
138              this.alpha = alpha; 
139          } 
140   
141          public void initialize() { 
142              // set initial wakeup condition 
143              this.wakeupOn(trigger); 
144          } 
145   
146          public void processStimulus(Enumeration criteria) { 
147              // don't need to decode event since there is only one trigger 
148   
149              // do what is necessary 
150              weights[0] = 0; 
151              weights[1] = 0; 
152              weights[2] = 0; 
153              weights[3] = 0; 
154              float alphaValue = 4f * alpha.value() - 0.00001f; 
155              int alphaIndex = (int) alphaValue; 
156              weights[alphaIndex] = (double) alphaValue - (double) alphaIndex; 
157              if (alphaIndex < 2) 
158                  weights[alphaIndex + 2] = 1.0 - weights[alphaIndex]; 
159              else 
160                  weights[alphaIndex - 2] = 1.0 - weights[alphaIndex]; 
161   
162              targetMorph.setWeights(weights); 
163              // set next wakeup condition 
164              this.wakeupOn(trigger); 
165          } 
166   
167      } // end of class MorphInPlaceBehavior 
168   
169      public GeometryArray createGeomArray0() { 
170          int[] counts = {7, 5, 2}; 
171          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
172          float[] coordinates = {0.00f, 0.05f, 0f, -0.01f, 0.15f, 0f, 0.05f, 0.25f, 0f, 
173                                 0.00f, 0.60f, 0f, 
174                                 0.01f, 0.30f, 0f, 0.04f, 0.00f, 0f, 0.09f, 0.00f, 0f, 
175                                 0.15f, 0.71f, 0f, -0.01f, 0.69f, 0f, 
176                                 0.06f, 1.10f, 0f, 
177                                 -0.05f, 0.75f, 0f, 0.14f, 0.75f, 0f, 
178                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
179          geom.setCoordinates(0, coordinates); 
180   
181  //         not setting the colors is the same as setting them to black 
182  //         Color3f c = new Color3f(0f, 0f, 0f); 
183  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
184   
185          return geom; 
186      } 
187   
188      public GeometryArray createGeomArray1() { 
189          int[] counts = {7, 5, 2}; 
190          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
191          float[] coordinates = {-0.10f, 0.00f, 0f, -0.15f, 0.08f, 0f, -0.05f, 0.30f, 0f, 
192                                 0.00f, 0.60f, 0f, 
193                                 0.15f, 0.30f, 0f, 0.10f, 0.00f, 0f, 0.15f, 0.05f, 0f, 
194                                 0.05f, 0.70f, 0f, -0.05f, 0.80f, 0f, 
195                                 0.06f, 1.10f, 0f, 
196                                 0.05f, 0.70f, 0f, 0.20f, 0.80f, 0f, 
197                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
198          geom.setCoordinates(0, coordinates); 
199   
200  //         not setting the colors is the same as setting them to black 
201  //         Color3f c = new Color3f(0f, 0f, 0f); 
202  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
203   
204          return geom; 
205      } 
206   
207      public GeometryArray createGeomArray2() { 
208          int[] counts = {7, 5, 2}; 
209          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
210          float[] coordinates = {0.09f, 0.00f, 0f, 0.04f, 0.00f, 0f, 0.01f, 0.30f, 0f, 
211                                 0.00f, 0.60f, 0f, 
212                                 0.05f, 0.25f, 0f, -0.01f, 0.15f, 0f, 0.00f, 0.05f, 0f, 
213                                 0.14f, 0.75f, 0f, -0.05f, 0.75f, 0f, 
214                                 0.06f, 1.10f, 0f, 
215                                 -0.01f, 0.69f, 0f, 0.15f, 0.71f, 0f, 
216                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
217          geom.setCoordinates(0, coordinates); 
218   
219  //         not setting the colors is the same as setting them to black 
220  //         Color3f c = new Color3f(0f, 0f, 0f); 
221  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
222   
223          return geom; 
224      } 
225   
226      public GeometryArray createGeomArray3() { 
227          int[] counts = {7, 5, 2}; 
228          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
229          float[] coordinates = {0.15f, 0.05f, 0f, 0.10f, 0.00f, 0f, 0.15f, 0.30f, 0f, 
230                                 0.00f, 0.60f, 0f, 
231                                 -0.05f, 0.30f, 0f, -0.15f, 0.08f, 0f, -0.10f, 0.00f, 0f, 
232                                 0.20f, 0.80f, 0f, 0.05f, 0.70f, 0f, 
233                                 0.06f, 1.10f, 0f, 
234                                 -0.05f, 0.80f, 0f, 0.05f, 0.70f, 0f, 
235                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
236          geom.setCoordinates(0, coordinates); 
237   
238  //         not setting the colors is the same as setting them to black 
239  //         Color3f c = new Color3f(0f, 0f, 0f); 
240  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
241   
242          return geom; 
243      } 
244   
245      public BranchGroup createSceneGraph() { 
246          // Create the root of the branch graph 
247          BranchGroup objRoot = new BranchGroup(); 
248   
249          Transform3D t3d = new Transform3D(); 
250          t3d.set(new Vector3f(-0.5f, -0.5f, 0f)); 
251          TransformGroup translateT = new TransformGroup(t3d); 
252          t3d.set(new Vector3f(0f, -0.5f, 0f)); 
253          TransformGroup translateIP = new TransformGroup(t3d); 
254          t3d.set(new Vector3f(0.5f, -0.5f, 0f)); 
255          TransformGroup translateB = new TransformGroup(t3d); 
256   
257          // create GeometryArray[] (array of GeometryArray objects) 
258          GeometryArray[] geomArray = new GeometryArray[4]; 
259          geomArray[0] = createGeomArray0(); 
260          geomArray[1] = createGeomArray1(); 
261          geomArray[2] = createGeomArray2(); 
262          geomArray[3] = createGeomArray3(); 
263   
264          // create morph objects 
265          Morph morphTObj = new Morph(geomArray); 
266          morphTObj.setCapability(Morph.ALLOW_WEIGHTS_WRITE); 
267          Morph morphIPObj = new Morph(geomArray); 
268          morphIPObj.setCapability(Morph.ALLOW_WEIGHTS_WRITE); 
269          Morph morphBObj = new Morph(geomArray); 
270          morphBObj.setCapability(Morph.ALLOW_WEIGHTS_WRITE); 
271   
272          // create alpha object 
273          Alpha alpha = new Alpha(-1, 1, 0, 0, 2000, 100, 0, 0, 0, 0); 
274   
275          // create morph driving behavior 
276          MorphTangoBehavior morphTBehav = new MorphTangoBehavior(morphTObj, alpha); 
277          morphTBehav.setSchedulingBounds(new BoundingSphere()); 
278          MorphInPlaceBehavior morphIPBehav = new MorphInPlaceBehavior(morphIPObj, alpha); 
279          morphIPBehav.setSchedulingBounds(new BoundingSphere()); 
280          MorphBrokenBehavior morphBBehav = new MorphBrokenBehavior(morphBObj, alpha); 
281          morphBBehav.setSchedulingBounds(new BoundingSphere()); 
282   
283          //assemble scene graph 
284          objRoot.addChild(translateT); 
285          translateT.addChild(morphTObj); 
286          objRoot.addChild(morphTBehav); 
287          objRoot.addChild(translateIP); 
288          translateIP.addChild(morphIPObj); 
289          objRoot.addChild(morphIPBehav); 
290          objRoot.addChild(translateB); 
291          translateB.addChild(morphBObj); 
292          objRoot.addChild(morphBBehav); 
293   
294          Background background = new Background(); 
295          background.setColor(1f, 1f, 1f); 
296          background.setApplicationBounds(new BoundingSphere()); 
297          objRoot.addChild(background); 
298   
299          // Let Java 3D perform optimizations on this scene graph. 
300          objRoot.compile(); 
301   
302          return objRoot; 
303      } // end of CreateSceneGraph method of Morph3App 
304   
305      // Create a simple scene and attach it to the virtual universe 
306   
307      public Morph3App() { 
308          setLayout(new BorderLayout()); 
309          GraphicsConfiguration config = 
310                  SimpleUniverse.getPreferredConfiguration(); 
311   
312          Canvas3D canvas3D = new Canvas3D(config); 
313          add("Center", canvas3D); 
314   
315          BranchGroup scene = createSceneGraph(); 
316   
317          // SimpleUniverse is a Convenience Utility class 
318          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
319   
320          // This will move the ViewPlatform back a bit so the 
321          // objects in the scene can be viewed. 
322          simpleU.getViewingPlatform().setNominalViewingTransform(); 
323   
324          simpleU.addBranchGraph(scene); 
325      } // end of Morph3App (constructor) 
326      //  The following allows this to be run as an application 
327      //  as well as an applet 
328   
329      public static void main(String[] args) { 
330          System.out.print("Morph3App.java \n- a demonstration of various animations"); 
331          System.out.println("possible using the same key frames and a variety of behaviors.\nNot all are good ;-)\n"); 
332          System.out.println("This is a simple example progam from The Java 3D API Tutorial."); 
333          System.out.println("The Java 3D Tutorial is available on the web at:"); 
334          System.out.println("http://java.sun.com/products/java-media/3D/collateral"); 
335          Frame frame = new MainFrame(new Morph3App(), 256, 256); 
336      } // end of main (method of Morph3App) 
337   
338  } // end of class Morph3App 
339