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

1    package j3d; 
2     
3    import com.sun.j3d.utils.applet.MainFrame; 
4    import com.sun.j3d.utils.geometry.ColorCube; 
5    import com.sun.j3d.utils.universe.SimpleUniverse; 
6     
7    import javax.media.j3d.*; 
8    import javax.vecmath.Color3f; 
9    import javax.vecmath.Point3f; 
10   import javax.vecmath.Vector3f; 
11   import java.applet.Applet; 
12   import java.awt.*; 
13    
14    
15   //   InterpolatorApp renders an 
16    
17   public class InterpolatorApp extends Applet { 
18    
19       Shape3D createCar(float xScale, float yScale, boolean createNormals, 
20                         boolean assignColoring) { 
21           Shape3D car = new Shape3D(); 
22           QuadArray carGeom = null; 
23    
24           if (createNormals) 
25               carGeom = new QuadArray(16, GeometryArray.COORDINATES 
26                       | GeometryArray.NORMALS); 
27           else 
28               carGeom = new QuadArray(16, GeometryArray.COORDINATES); 
29    
30           carGeom.setCoordinate(0, new Point3f(xScale * -0.25f, yScale * 0.22f, 0.0f)); 
31           carGeom.setCoordinate(1, new Point3f(xScale * 0.20f, yScale * 0.22f, 0.0f)); 
32           carGeom.setCoordinate(2, new Point3f(xScale * 0.10f, yScale * 0.35f, 0.0f)); 
33           carGeom.setCoordinate(3, new Point3f(xScale * -0.20f, yScale * 0.35f, 0.0f)); 
34           carGeom.setCoordinate(4, new Point3f(xScale * -0.50f, yScale * 0.10f, 0.0f)); 
35           carGeom.setCoordinate(5, new Point3f(xScale * 0.50f, yScale * 0.10f, 0.0f)); 
36           carGeom.setCoordinate(6, new Point3f(xScale * 0.45f, yScale * 0.20f, 0.0f)); 
37           carGeom.setCoordinate(7, new Point3f(xScale * -0.48f, yScale * 0.20f, 0.0f)); 
38           carGeom.setCoordinate(8, new Point3f(xScale * -0.26f, yScale * 0.00f, 0.0f)); 
39           carGeom.setCoordinate(9, new Point3f(xScale * -0.18f, yScale * 0.00f, 0.0f)); 
40           carGeom.setCoordinate(10, new Point3f(xScale * -0.16f, yScale * 0.12f, 0.0f)); 
41           carGeom.setCoordinate(11, new Point3f(xScale * -0.28f, yScale * 0.12f, 0.0f)); 
42           carGeom.setCoordinate(12, new Point3f(xScale * 0.25f, yScale * 0.00f, 0.0f)); 
43           carGeom.setCoordinate(13, new Point3f(xScale * 0.33f, yScale * 0.00f, 0.0f)); 
44           carGeom.setCoordinate(14, new Point3f(xScale * 0.35f, yScale * 0.12f, 0.0f)); 
45           carGeom.setCoordinate(15, new Point3f(xScale * 0.23f, yScale * 0.12f, 0.0f)); 
46    
47           if (createNormals) { 
48               int i; 
49               Vector3f normal = new Vector3f(0.6f, 0.6f, 0.8f); 
50               for (i = 0; i < 8; i++) 
51                   carGeom.setNormal(i, normal); 
52               normal.set(new Vector3f(0.5f, 0.5f, 0.5f)); 
53               for (i = 8; i < 16; i++) 
54                   carGeom.setNormal(i, normal); 
55           } 
56    
57           if (assignColoring) { 
58               ColoringAttributes colorAttrib = 
59                       new ColoringAttributes(0.0f, 0.0f, 1.0f, ColoringAttributes.NICEST); 
60               Appearance carAppear = new Appearance(); 
61               carAppear.setColoringAttributes(colorAttrib); 
62               car.setAppearance(carAppear); 
63           } 
64    
65           car.setGeometry(carGeom); 
66           return car; 
67       } 
68    
69       public BranchGroup createSceneGraph() { 
70           // Create the root of the branch graph 
71           BranchGroup bg = new BranchGroup(); 
72           Transform3D t3d = new Transform3D(); 
73           BoundingSphere bounds = new BoundingSphere(); 
74    
75           // create target TransformGroup with Capabilities 
76           TransformGroup objMove = new TransformGroup(); 
77           objMove.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
78    
79           // create target TransformGroup with Capabilities 
80           TransformGroup objRotate = new TransformGroup(); 
81           objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
82    
83           // create target TransformGroup with Capabilities 
84           TransformGroup objScale = new TransformGroup(); 
85           objScale.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
86    
87           // create target Material with Capabilities 
88           Material objColor = new Material(); 
89           objColor.setCapability(Material.ALLOW_COMPONENT_WRITE); 
90    
91           // create target Transparency with Capabilities 
92           TransparencyAttributes objTransp = new TransparencyAttributes(); 
93           objTransp.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); 
94           objTransp.setTransparencyMode(TransparencyAttributes.BLENDED); 
95    
96           // create target Switch with Capabilities 
97           Switch objSwitch = new Switch(); 
98           objSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); 
99    
100          // create Alpha 
101          Alpha alpha = new Alpha(-1, 
102                  Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE, 
103                  0, 0, 2000, 0, 1000, 2000, 0, 1000); 
104   
105          // create position interpolator 
106          PositionInterpolator posInt 
107                  = createPositionInterpolator(alpha, objMove, bounds); 
108   
109          // create rotation interpolator 
110          RotationInterpolator rotInt 
111                  = createRotationInterpolator(alpha, objRotate, bounds); 
112   
113          // create scale interpolator 
114          ScaleInterpolator scaInt 
115                  = scaleInterpolator(alpha, objScale, bounds); 
116   
117          ColorInterpolator colInt 
118                  = createColorInterpolator(alpha, objColor, bounds); 
119   
120          TransparencyInterpolator traInt 
121                  = createTransparencyInterpolator(alpha, objTransp, bounds); 
122   
123          // create switch value interpolator 
124          SwitchValueInterpolator swiInt = new SwitchValueInterpolator(alpha, objSwitch); 
125          swiInt.setSchedulingBounds(bounds); 
126   
127          addCar1(t3d, bg, objMove, posInt); 
128   
129          addCar2(t3d, bg, objRotate, rotInt); 
130   
131          addCar3(t3d, bg, objScale, scaInt); 
132   
133          t3d.setTranslation(new Vector3f(0.0f, -0.2f, 0.0f)); 
134          TransformGroup objColorPos = new TransformGroup(t3d); 
135          bg.addChild(objColorPos); 
136          Shape3D colorCar = createCar(0.4f, 0.4f, true, false); 
137          Appearance materialAppear = new Appearance(); 
138          materialAppear.setMaterial(objColor); 
139          colorCar.setAppearance(materialAppear); 
140          objColorPos.addChild(colorCar); 
141          bg.addChild(colInt); 
142   
143          t3d.setTranslation(new Vector3f(0.0f, -0.5f, 0.0f)); 
144          TransformGroup objTranspPos = new TransformGroup(t3d); 
145          bg.addChild(objTranspPos); 
146          Shape3D transpCar = createCar(0.4f, 0.4f, false, true); 
147          Appearance transpAppear = transpCar.getAppearance(); 
148          transpAppear.setTransparencyAttributes(objTransp); 
149          objTranspPos.addChild(transpCar); 
150          bg.addChild(traInt); 
151   
152          t3d.setTranslation(new Vector3f(0.0f, -0.8f, 0.0f)); 
153          TransformGroup objSwitchPos = new TransformGroup(t3d); 
154          bg.addChild(objSwitchPos); 
155          objSwitch.addChild(createCar(0.4f, 0.4f, false, true)); 
156          ; 
157          objSwitch.addChild(new ColorCube(0.1f)); 
158          objSwitchPos.addChild(objSwitch); 
159          bg.addChild(swiInt); 
160          swiInt.setLastChildIndex(2);// since switch made after interpolator 
161   
162          DirectionalLight lightD1 = new DirectionalLight(); 
163          // lightD1.setDirection(new Vector3f(-0.7f,-0.7f,0.0f)); 
164          lightD1.setInfluencingBounds(bounds); 
165          bg.addChild(lightD1); 
166   
167          Background background = new Background(); 
168          background.setColor(1.0f, 1.0f, 1.0f); 
169          background.setApplicationBounds(new BoundingSphere()); 
170          bg.addChild(background); 
171   
172          // Let Java 3D perform optimizations on this scene graph. 
173          bg.compile(); 
174   
175          return bg; 
176      } // end of CreateSceneGraph method of InterpolatorApp 
177   
178      private PositionInterpolator createPositionInterpolator(Alpha alpha, TransformGroup objMove, BoundingSphere bounds) { 
179          PositionInterpolator posInt = new PositionInterpolator(alpha, objMove); 
180          posInt.setSchedulingBounds(bounds); 
181          posInt.setStartPosition(-1.0f); 
182          return posInt; 
183      } 
184   
185      private RotationInterpolator createRotationInterpolator(Alpha alpha, TransformGroup objRotate, BoundingSphere bounds) { 
186          RotationInterpolator rotInt = new RotationInterpolator(alpha, objRotate); 
187          rotInt.setSchedulingBounds(bounds); 
188          return rotInt; 
189      } 
190   
191      private ScaleInterpolator scaleInterpolator(Alpha alpha, TransformGroup objScale, BoundingSphere bounds) { 
192          ScaleInterpolator scaInt = new ScaleInterpolator(alpha, objScale); 
193          scaInt.setSchedulingBounds(bounds); 
194          return scaInt; 
195      } 
196   
197      private void addCar3(Transform3D t3d, BranchGroup bg, TransformGroup objScale, ScaleInterpolator scaInt) { 
198          t3d.setTranslation(new Vector3f(0.0f, 0.2f, 0.0f)); 
199          TransformGroup objScalePos = new TransformGroup(t3d); 
200          bg.addChild(objScalePos); 
201          objScalePos.addChild(objScale); 
202          objScale.addChild(createCar(0.4f, 0.4f, false, true)); 
203          bg.addChild(scaInt); 
204      } 
205   
206      private void addCar2(Transform3D t3d, BranchGroup bg, TransformGroup objRotate, RotationInterpolator rotInt) { 
207          t3d.setTranslation(new Vector3f(0.0f, 0.5f, 0.0f)); 
208          TransformGroup objRotPos = new TransformGroup(t3d); 
209          bg.addChild(objRotPos); 
210          objRotPos.addChild(objRotate); 
211          objRotate.addChild(createCar(0.4f, 0.4f, false, true)); 
212          bg.addChild(rotInt); 
213      } 
214   
215      private void addCar1(Transform3D t3d, BranchGroup bg, TransformGroup objMove, PositionInterpolator posInt) { 
216          t3d.setTranslation(new Vector3f(0.0f, 0.8f, 0.0f)); 
217          TransformGroup objMovePos = new TransformGroup(t3d); 
218          bg.addChild(objMovePos); 
219          objMovePos.addChild(objMove); 
220          objMove.addChild(createCar(0.4f, 0.4f, false, true)); 
221          bg.addChild(posInt); 
222      } 
223   
224      private ColorInterpolator createColorInterpolator(Alpha alpha, Material objColor, BoundingSphere bounds) { 
225          ColorInterpolator colInt = new ColorInterpolator(alpha, objColor); 
226          colInt.setStartColor(new Color3f(1.0f, 0.0f, 0.0f)); 
227          colInt.setEndColor(new Color3f(0.0f, 0.0f, 1.0f)); 
228          colInt.setSchedulingBounds(bounds); 
229          return colInt; 
230      } 
231   
232      private TransparencyInterpolator createTransparencyInterpolator(Alpha alpha, TransparencyAttributes objTransp, BoundingSphere bounds) { 
233          TransparencyInterpolator traInt = new TransparencyInterpolator(alpha, objTransp); 
234          traInt.setSchedulingBounds(bounds); 
235          return traInt; 
236      } 
237   
238      // Create a simple scene and attach it to the virtual universe 
239   
240      public InterpolatorApp() { 
241          setLayout(new BorderLayout()); 
242          GraphicsConfiguration gc = 
243                  SimpleUniverse.getPreferredConfiguration(); 
244   
245          Canvas3D canvas3D = new Canvas3D(gc); 
246          add("Center", canvas3D); 
247   
248          BranchGroup scene = createSceneGraph(); 
249   
250          // SimpleUniverse is a Convenience Utility class 
251          SimpleUniverse su = new SimpleUniverse(canvas3D); 
252   
253          // This will move the ViewPlatform back a bit so the 
254          // objects in the scene can be viewed. 
255          su.getViewingPlatform().setNominalViewingTransform(); 
256   
257          su.addBranchGraph(scene); 
258      } // end of InterpolatorApp (constructor) 
259   
260      //  The following allows this to be run as an application 
261      //  as well as an applet 
262   
263      public static void main(String[] args) { 
264          new MainFrame(new InterpolatorApp(), 256, 256); 
265      } // end of main (method of InterpolatorApp) 
266   
267  } // end of class InterpolatorApp 
268