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

1    package j3d; 
2     
3    /* 
4     *      @(#)MorphApp.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   //   MorphApp renders a single ColorCube 
43   //   that rotates when any key is pressed. 
44    
45   public class MorphApp extends Applet { 
46    
47       public class MorphBehavior 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 MorphBehavior 
56           MorphBehavior(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               float alphaValue = 4f * alpha.value() - 0.00001f; 
75               int alphaIndex = (int) alphaValue; 
76               weights[alphaIndex] = (double) alphaValue - (double) alphaIndex; 
77               if (alphaIndex < 3) 
78                   weights[alphaIndex + 1] = 1.0 - weights[alphaIndex]; 
79               else 
80                   weights[0] = 1.0 - weights[alphaIndex]; 
81    
82               targetMorph.setWeights(weights); 
83               // set next wakeup condition 
84               this.wakeupOn(trigger); 
85           } 
86    
87       } // end of class MorphBehavior 
88    
89       public GeometryArray createGeomArray0() { 
90           int[] counts = {7, 5, 2}; 
91           LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
92           float[] coordinates = {0.00f, 0.05f, 0f, -0.01f, 0.15f, 0f, 0.05f, 0.25f, 0f, 
93                                  0.00f, 0.60f, 0f, 
94                                  0.01f, 0.30f, 0f, 0.04f, 0.00f, 0f, 0.09f, 0.00f, 0f, 
95                                  0.15f, 0.71f, 0f, -0.01f, 0.69f, 0f, 
96                                  0.06f, 1.10f, 0f, 
97                                  -0.05f, 0.75f, 0f, 0.14f, 0.75f, 0f, 
98                                  0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
99           geom.setCoordinates(0, coordinates); 
100   
101  //         not setting the colors is the same as setting them to black 
102  //         Color3f c = new Color3f(0f, 0f, 0f); 
103  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
104   
105          return geom; 
106      } 
107   
108      public GeometryArray createGeomArray1() { 
109          int[] counts = {7, 5, 2}; 
110          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
111          float[] coordinates = {-0.10f, 0.00f, 0f, -0.15f, 0.08f, 0f, -0.05f, 0.30f, 0f, 
112                                 0.00f, 0.60f, 0f, 
113                                 0.15f, 0.30f, 0f, 0.10f, 0.00f, 0f, 0.15f, 0.05f, 0f, 
114                                 0.05f, 0.70f, 0f, -0.05f, 0.80f, 0f, 
115                                 0.06f, 1.10f, 0f, 
116                                 0.05f, 0.70f, 0f, 0.20f, 0.80f, 0f, 
117                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
118          geom.setCoordinates(0, coordinates); 
119   
120  //         not setting the colors is the same as setting them to black 
121  //         Color3f c = new Color3f(0f, 0f, 0f); 
122  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
123   
124          return geom; 
125      } 
126   
127      public GeometryArray createGeomArray2() { 
128          int[] counts = {7, 5, 2}; 
129          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
130          float[] coordinates = {0.09f, 0.00f, 0f, 0.04f, 0.00f, 0f, 0.01f, 0.30f, 0f, 
131                                 0.00f, 0.60f, 0f, 
132                                 0.05f, 0.25f, 0f, -0.01f, 0.15f, 0f, 0.00f, 0.05f, 0f, 
133                                 0.14f, 0.75f, 0f, -0.05f, 0.75f, 0f, 
134                                 0.06f, 1.10f, 0f, 
135                                 -0.01f, 0.69f, 0f, 0.15f, 0.71f, 0f, 
136                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
137          geom.setCoordinates(0, coordinates); 
138   
139  //         not setting the colors is the same as setting them to black 
140  //         Color3f c = new Color3f(0f, 0f, 0f); 
141  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
142   
143          return geom; 
144      } 
145   
146      public GeometryArray createGeomArray3() { 
147          int[] counts = {7, 5, 2}; 
148          LineStripArray geom = new LineStripArray(14, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts); 
149          float[] coordinates = {0.15f, 0.05f, 0f, 0.10f, 0.00f, 0f, 0.15f, 0.30f, 0f, 
150                                 0.00f, 0.60f, 0f, 
151                                 -0.05f, 0.30f, 0f, -0.15f, 0.08f, 0f, -0.10f, 0.00f, 0f, 
152                                 0.20f, 0.80f, 0f, 0.05f, 0.70f, 0f, 
153                                 0.06f, 1.10f, 0f, 
154                                 -0.05f, 0.80f, 0f, 0.05f, 0.70f, 0f, 
155                                 0.06f, 1.10f, 0f, 0.00f, 0.60f, 0f}; 
156          geom.setCoordinates(0, coordinates); 
157   
158  //         not setting the colors is the same as setting them to black 
159  //         Color3f c = new Color3f(0f, 0f, 0f); 
160  //         for (int i = 0; i < 14; i++) geom.setColor(i, c); 
161   
162          return geom; 
163      } 
164   
165      public BranchGroup createSceneGraph() { 
166          // Create the root of the branch graph 
167          BranchGroup objRoot = new BranchGroup(); 
168   
169          Transform3D t3d = new Transform3D(); 
170          t3d.set(new Vector3f(0f, -0.5f, 0f)); 
171          TransformGroup translate = new TransformGroup(t3d); 
172   
173          // create GeometryArray[] (array of GeometryArray objects) 
174          GeometryArray[] geomArray = new GeometryArray[4]; 
175          geomArray[0] = createGeomArray0(); 
176          geomArray[1] = createGeomArray1(); 
177          geomArray[2] = createGeomArray2(); 
178          geomArray[3] = createGeomArray3(); 
179   
180          // create morph object 
181          Morph morphObj = new Morph(geomArray); 
182          morphObj.setCapability(Morph.ALLOW_WEIGHTS_WRITE); 
183   
184          // create alpha object 
185          Alpha alpha = new Alpha(-1, 1, 0, 0, 2000, 100, 0, 0, 0, 0); 
186   
187          // create morph driving behavior 
188          MorphBehavior morphBehav = new MorphBehavior(morphObj, alpha); 
189          morphBehav.setSchedulingBounds(new BoundingSphere()); 
190   
191          //assemble scene graph 
192          objRoot.addChild(translate); 
193          translate.addChild(morphObj); 
194          objRoot.addChild(morphBehav); 
195   
196          Background background = new Background(); 
197          background.setColor(1f, 1f, 1f); 
198          background.setApplicationBounds(new BoundingSphere()); 
199          objRoot.addChild(background); 
200   
201          // Let Java 3D perform optimizations on this scene graph. 
202          objRoot.compile(); 
203   
204          return objRoot; 
205      } // end of CreateSceneGraph method of MorphApp 
206   
207      // Create a simple scene and attach it to the virtual universe 
208   
209      public MorphApp() { 
210          setLayout(new BorderLayout()); 
211          GraphicsConfiguration config = 
212                  SimpleUniverse.getPreferredConfiguration(); 
213   
214          Canvas3D canvas3D = new Canvas3D(config); 
215          add("Center", canvas3D); 
216   
217          BranchGroup scene = createSceneGraph(); 
218   
219          // SimpleUniverse is a Convenience Utility class 
220          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
221   
222          // This will move the ViewPlatform back a bit so the 
223          // objects in the scene can be viewed. 
224          simpleU.getViewingPlatform().setNominalViewingTransform(); 
225   
226          simpleU.addBranchGraph(scene); 
227      } // end of MorphApp (constructor) 
228      //  The following allows this to be run as an application 
229      //  as well as an applet 
230   
231      public static void main(String[] args) { 
232          System.out.print("MorphApp.java \n- a demonstration of animation"); 
233          System.out.println("using the Morph class in a Java 3D scene."); 
234          System.out.println("This is a simple example progam from The Java 3D API Tutorial."); 
235          System.out.println("The Java 3D Tutorial is available on the web at:"); 
236          System.out.println("http://java.sun.com/products/java-media/3D/collateral"); 
237          Frame frame = new MainFrame(new MorphApp(), 256, 256); 
238      } // end of main (method of MorphApp) 
239   
240  } // end of class MorphApp 
241