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

1    package j3d; 
2     
3     
4    /* 
5     *  GeomInfoApp.java demonstrates the use of the GeometryInfo class 
6     *  and related classes. 
7     * 
8     *  This program creates a car shape (not a fancy car, though) using 
9     *  GeometryInfo objects.  One GeometryInfo object specifies the side 
10    *  of the car using a polygon (not a triangle, nor a quad).  The 
11    *  GeometryInfo and other classes convert the polygons into a triangle 
12    *  strips with normals. 
13    * 
14    *  Note that about half of the source code just specifies the 
15    *  input data to the GeometryInfo objects.  The interesting part 
16    *  starts around line 210. 
17    * 
18    *  An alternative data set is provided (in the comments) for further 
19    *  experimentation - see the tutorial text. 
20    */ 
21    
22   import com.sun.j3d.utils.applet.MainFrame; 
23   import com.sun.j3d.utils.geometry.GeometryInfo; 
24   import com.sun.j3d.utils.geometry.NormalGenerator; 
25   import com.sun.j3d.utils.geometry.Stripifier; 
26   import com.sun.j3d.utils.universe.SimpleUniverse; 
27    
28   import javax.media.j3d.*; 
29   import javax.vecmath.Color3f; 
30   import javax.vecmath.Vector3f; 
31   import java.applet.Applet; 
32   import java.awt.*; 
33    
34    
35   public class GeomInfoApp extends Applet { 
36    
37       float[] createCoordinateData() { 
38           float[] data = new float[69 * 3];         // ****** 
39           int i = 0; 
40    
41           data[i++] = -1.3f; 
42           data[i++] = -0.3f; 
43           data[i++] = 0.3f; //0 
44           data[i++] = -0.9f; 
45           data[i++] = -0.3f; 
46           data[i++] = 0.3f; //1 
47           data[i++] = -0.8f; 
48           data[i++] = -0.1f; 
49           data[i++] = 0.3f; //2 
50           data[i++] = -0.6f; 
51           data[i++] = -0.1f; 
52           data[i++] = 0.3f; //3 
53           data[i++] = -0.5f; 
54           data[i++] = -0.3f; 
55           data[i++] = 0.3f; //4 
56           data[i++] = 0.2f; 
57           data[i++] = -0.3f; 
58           data[i++] = 0.3f; //5 
59           data[i++] = 0.3f; 
60           data[i++] = -0.1f; 
61           data[i++] = 0.3f; //6 
62           data[i++] = 0.5f; 
63           data[i++] = -0.1f; 
64           data[i++] = 0.3f; //7 
65           data[i++] = 0.6f; 
66           data[i++] = -0.3f; 
67           data[i++] = 0.3f; //8 
68           data[i++] = 1.3f; 
69           data[i++] = -0.3f; 
70           data[i++] = 0.3f; //9 
71           data[i++] = 1.2f; 
72           data[i++] = -0.1f; 
73           data[i++] = 0.3f; //10 
74           data[i++] = 0.5f; 
75           data[i++] = 0.0f; 
76           data[i++] = 0.3f; //11 
77           data[i++] = 0.1f; 
78           data[i++] = 0.3f; 
79           data[i++] = 0.3f; //12 
80           data[i++] = -0.5f; 
81           data[i++] = 0.3f; 
82           data[i++] = 0.3f; //13 
83           data[i++] = -1.1f; 
84           data[i++] = 0.0f; 
85           data[i++] = 0.3f; //14 
86           data[i++] = -1.3f; 
87           data[i++] = 0.0f; 
88           data[i++] = 0.3f; //15 
89           data[i++] = -1.3f; 
90           data[i++] = -0.3f; 
91           data[i++] = 0.3f; //16 
92           System.out.println("end polygon; total vertex count: " + i / 3); 
93    
94           data[i++] = -1.3f; 
95           data[i++] = -0.3f; 
96           data[i++] = -0.3f; // 0 17 
97           data[i++] = -1.3f; 
98           data[i++] = 0.0f; 
99           data[i++] = -0.3f; // 1 18 
100          data[i++] = -1.1f; 
101          data[i++] = 0.0f; 
102          data[i++] = -0.3f; // 2 19 
103          data[i++] = -0.5f; 
104          data[i++] = 0.3f; 
105          data[i++] = -0.3f; // 3 20 
106          data[i++] = 0.1f; 
107          data[i++] = 0.3f; 
108          data[i++] = -0.3f; // 4 21 
109          data[i++] = 0.5f; 
110          data[i++] = 0.0f; 
111          data[i++] = -0.3f; // 5 22 
112          data[i++] = 1.2f; 
113          data[i++] = -0.1f; 
114          data[i++] = -0.3f; // 6 23 
115          data[i++] = 1.3f; 
116          data[i++] = -0.3f; 
117          data[i++] = -0.3f; // 7 24 
118          data[i++] = 0.6f; 
119          data[i++] = -0.3f; 
120          data[i++] = -0.3f; // 8 25 
121          data[i++] = 0.5f; 
122          data[i++] = -0.1f; 
123          data[i++] = -0.3f; // 9 26 
124          data[i++] = 0.3f; 
125          data[i++] = -0.1f; 
126          data[i++] = -0.3f; //10 27 
127          data[i++] = 0.2f; 
128          data[i++] = -0.3f; 
129          data[i++] = -0.3f; //11 28 
130          data[i++] = -0.5f; 
131          data[i++] = -0.3f; 
132          data[i++] = -0.3f; //12 29 
133          data[i++] = -0.6f; 
134          data[i++] = -0.1f; 
135          data[i++] = -0.3f; //13 30 
136          data[i++] = -0.8f; 
137          data[i++] = -0.1f; 
138          data[i++] = -0.3f; //14 31 
139          data[i++] = -0.9f; 
140          data[i++] = -0.3f; 
141          data[i++] = -0.3f; //15 32 
142          data[i++] = -1.3f; 
143          data[i++] = -0.3f; 
144          data[i++] = -0.3f; //16 33 
145          System.out.println("end polygon; total vertex count: " + i / 3); 
146   
147          data[i++] = 1.3f; 
148          data[i++] = -0.3f; 
149          data[i++] = -0.3f; // 0 34 
150          data[i++] = 1.2f; 
151          data[i++] = -0.1f; 
152          data[i++] = -0.3f; // 1 35 
153          data[i++] = 1.2f; 
154          data[i++] = -0.1f; 
155          data[i++] = 0.3f; // 2 36 
156          data[i++] = 1.3f; 
157          data[i++] = -0.3f; 
158          data[i++] = 0.3f; // 3 37 
159          data[i++] = 1.3f; 
160          data[i++] = -0.3f; 
161          data[i++] = -0.3f; // 4 38 
162          System.out.println("end polygon; total vertex count: " + i / 3); 
163   
164          data[i++] = 1.2f; 
165          data[i++] = -0.1f; 
166          data[i++] = -0.3f; // 0 39 
167          data[i++] = 0.5f; 
168          data[i++] = 0.0f; 
169          data[i++] = -0.3f; // 1 40 
170          data[i++] = 0.5f; 
171          data[i++] = 0.0f; 
172          data[i++] = 0.3f; // 2 41 
173          data[i++] = 1.2f; 
174          data[i++] = -0.1f; 
175          data[i++] = 0.3f; // 3 42 
176          data[i++] = 1.2f; 
177          data[i++] = -0.1f; 
178          data[i++] = -0.3f; // 4 43 
179          System.out.println("end polygon; total vertex count: " + i / 3); 
180   
181          data[i++] = 0.5f; 
182          data[i++] = 0.0f; 
183          data[i++] = -0.3f; // 0 44 
184          data[i++] = 0.1f; 
185          data[i++] = 0.3f; 
186          data[i++] = -0.3f; // 1 45 
187          data[i++] = 0.1f; 
188          data[i++] = 0.3f; 
189          data[i++] = 0.3f; // 2 46 
190          data[i++] = 0.5f; 
191          data[i++] = 0.0f; 
192          data[i++] = 0.3f; // 3 47 
193          data[i++] = 0.5f; 
194          data[i++] = 0.0f; 
195          data[i++] = -0.3f; // 4 48 
196          System.out.println("end polygon; total vertex count: " + i / 3); 
197   
198          data[i++] = 0.1f; 
199          data[i++] = 0.3f; 
200          data[i++] = -0.3f; // 0 49 
201          data[i++] = -0.5f; 
202          data[i++] = 0.3f; 
203          data[i++] = -0.3f; // 1 50 
204          data[i++] = -0.5f; 
205          data[i++] = 0.3f; 
206          data[i++] = 0.3f; // 2 51 
207          data[i++] = 0.1f; 
208          data[i++] = 0.3f; 
209          data[i++] = 0.3f; // 3 52 
210          data[i++] = 0.1f; 
211          data[i++] = 0.3f; 
212          data[i++] = -0.3f; // 4 53 
213          System.out.println("end polygon; total vertex count: " + i / 3); 
214   
215          data[i++] = -0.5f; 
216          data[i++] = 0.3f; 
217          data[i++] = -0.3f; // 0 54 
218          data[i++] = -1.1f; 
219          data[i++] = 0.0f; 
220          data[i++] = -0.3f; // 1 55 
221          data[i++] = -1.1f; 
222          data[i++] = 0.0f; 
223          data[i++] = 0.3f; // 2 56 
224          data[i++] = -0.5f; 
225          data[i++] = 0.3f; 
226          data[i++] = 0.3f; // 3 57 
227          data[i++] = -0.5f; 
228          data[i++] = 0.3f; 
229          data[i++] = -0.3f; // 4 58 
230          System.out.println("end polygon; total vertex count: " + i / 3); 
231   
232          data[i++] = -1.1f; 
233          data[i++] = 0.0f; 
234          data[i++] = -0.3f; // 0 59 
235          data[i++] = -1.3f; 
236          data[i++] = 0.0f; 
237          data[i++] = -0.3f; // 1 60 
238          data[i++] = -1.3f; 
239          data[i++] = 0.0f; 
240          data[i++] = 0.3f; // 2 61 
241          data[i++] = -1.1f; 
242          data[i++] = 0.0f; 
243          data[i++] = 0.3f; // 3 62 
244          data[i++] = -1.1f; 
245          data[i++] = 0.0f; 
246          data[i++] = -0.3f; // 4 63 
247          System.out.println("end polygon; total vertex count: " + i / 3); 
248   
249          data[i++] = -1.3f; 
250          data[i++] = 0.0f; 
251          data[i++] = -0.3f; // 0 64 
252          data[i++] = -1.3f; 
253          data[i++] = -0.3f; 
254          data[i++] = -0.3f; // 1 65 
255          data[i++] = -1.3f; 
256          data[i++] = -0.3f; 
257          data[i++] = 0.3f; // 2 66 
258          data[i++] = -1.3f; 
259          data[i++] = 0.0f; 
260          data[i++] = 0.3f; // 3 67 
261          data[i++] = -1.3f; 
262          data[i++] = 0.0f; 
263          data[i++] = -0.3f; // 4 68 
264          System.out.println("end polygon; total vertex count: " + i / 3); 
265   
266  // ****** This is the data for the hood, roof, trunk, front and rear glass 
267  // ****** remove the comments markers below (slash-star) and (star slash) 
268  // ****** and add the appropriate comment markers above. 
269  // ****** modification of other lines of code is necessary to use this data 
270  // ****** one line above and two lines below 
271  /* 
272          data[i++]=  1.3f; data[i++]= -0.3f; data[i++]=-0.3f; // 0  35 
273          data[i++]=  1.2f; data[i++]= -0.1f; data[i++]=-0.3f; // 1  36 
274          data[i++]=  0.5f; data[i++]=  0.0f; data[i++]=-0.3f; // 2  37 
275          data[i++]=  0.1f; data[i++]=  0.3f; data[i++]=-0.3f; // 3  38 
276          data[i++]= -0.5f; data[i++]=  0.3f; data[i++]=-0.3f; // 4  39 
277          data[i++]= -1.1f; data[i++]=  0.0f; data[i++]=-0.3f; // 5  40 
278          data[i++]= -1.3f; data[i++]=  0.0f; data[i++]=-0.3f; // 6  41 
279          data[i++]= -1.3f; data[i++]= -0.3f; data[i++]=-0.3f; // 7  42 
280          data[i++]= -1.3f; data[i++]= -0.3f; data[i++]= 0.3f; // 8  43 
281          data[i++]= -1.3f; data[i++]=  0.0f; data[i++]= 0.3f; // 9  44 
282          data[i++]= -1.1f; data[i++]=  0.0f; data[i++]= 0.3f; // 10 45 
283          data[i++]= -0.5f; data[i++]=  0.3f; data[i++]= 0.3f; // 11 46 
284          data[i++]=  0.1f; data[i++]=  0.3f; data[i++]= 0.3f; // 12 47 
285          data[i++]=  0.5f; data[i++]=  0.0f; data[i++]= 0.3f; // 13 48 
286          data[i++]=  1.2f; data[i++]= -0.1f; data[i++]= 0.3f; // 14 49 
287          data[i++]=  1.3f; data[i++]= -0.3f; data[i++]= 0.3f; // 15 50 
288          data[i++]=  1.3f; data[i++]= -0.3f; data[i++]=-0.3f; // 16 51 
289          System.out.println("end polygon; total vertex count: "+i/3); 
290  */ 
291  // ****** end of the alternative polygon data 
292          return data; 
293      } 
294   
295   
296      Appearance createMaterialAppearance() { 
297   
298          Appearance materialAppear = new Appearance(); 
299          PolygonAttributes polyAttrib = new PolygonAttributes(); 
300          polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); 
301          materialAppear.setPolygonAttributes(polyAttrib); 
302   
303          Material material = new Material(); 
304          material.setDiffuseColor(new Color3f(1.0f, 0.0f, 0.0f)); 
305          materialAppear.setMaterial(material); 
306   
307          return materialAppear; 
308      } 
309   
310      Appearance createWireFrameAppearance() { 
311   
312          Appearance materialAppear = new Appearance(); 
313          PolygonAttributes polyAttrib = new PolygonAttributes(); 
314          polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); 
315          materialAppear.setPolygonAttributes(polyAttrib); 
316          ColoringAttributes redColoring = new ColoringAttributes(); 
317          redColoring.setColor(1.0f, 0.0f, 0.0f); 
318          materialAppear.setColoringAttributes(redColoring); 
319   
320          return materialAppear; 
321      } 
322   
323      ///////////////////////////////////////////////// 
324      // 
325      // create scene graph branch group 
326      // 
327      public BranchGroup createSceneGraph(boolean wireFrame) { 
328          int total = 0; 
329   
330          System.out.println("\n --- geometry debug information --- \n"); 
331   
332          float[] coordinateData = null; 
333          coordinateData = createCoordinateData(); 
334          int[] stripCount = {17, 17, 5, 5, 5, 5, 5, 5, 5};  // ****** 
335  //        int[] stripCount = {17,17,17};  // ****** 
336   
337          for (int i = 0; i < stripCount.length; i++) { 
338              System.out.println("stripCount[" + i + "] = " + stripCount[i]); 
339              total += stripCount[i]; 
340          } 
341   
342          if (total != coordinateData.length / 3) { 
343              System.out.println("  coordinateData vertex count: " + coordinateData.length / 3); 
344              System.out.println("stripCount total vertex count: " + total); 
345          } 
346   
347          GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); 
348          gi.setCoordinates(coordinateData); 
349          gi.setStripCounts(stripCount); 
350          System.out.println("begin triangulation"); 
351          System.out.println("  END triangulation"); 
352          gi.recomputeIndices(); 
353   
354          NormalGenerator ng = new NormalGenerator(); 
355          ng.generateNormals(gi); 
356          gi.recomputeIndices(); 
357   
358          Stripifier st = new Stripifier(); 
359          st.stripify(gi); 
360          gi.recomputeIndices(); 
361   
362          Shape3D part = new Shape3D(); 
363          if (wireFrame == true) 
364              part.setAppearance(createWireFrameAppearance()); 
365          else 
366              part.setAppearance(createMaterialAppearance()); 
367          part.setGeometry(gi.getGeometryArray()); 
368   
369          ///////////////////////////// 
370   
371          BranchGroup contentRoot = new BranchGroup(); 
372   
373          // Create the transform group node and initialize it to the 
374          // identity. Add it to the root of the subgraph. 
375          TransformGroup objSpin = new TransformGroup(); 
376          objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
377          contentRoot.addChild(objSpin); 
378   
379          objSpin.addChild(part); 
380   
381          //////////////////////// 
382          LineStripArray lineArray = new LineStripArray(69, LineArray.COORDINATES, stripCount); //***** 
383  //        LineStripArray lineArray = new LineStripArray(51, LineArray.COORDINATES, stripCount); //***** 
384          lineArray.setCoordinates(0, coordinateData); 
385          Appearance blueColorAppearance = new Appearance(); 
386          ColoringAttributes blueColoring = new ColoringAttributes(); 
387          blueColoring.setColor(0.0f, 0.0f, 1.0f); 
388          blueColorAppearance.setColoringAttributes(blueColoring); 
389          LineAttributes lineAttrib = new LineAttributes(); 
390          lineAttrib.setLineWidth(2.0f); 
391          blueColorAppearance.setLineAttributes(lineAttrib); 
392          objSpin.addChild(new Shape3D(lineArray, blueColorAppearance)); 
393   
394          Alpha rotationAlpha = new Alpha(-1, 16000); 
395   
396          RotationInterpolator rotator = 
397                  new RotationInterpolator(rotationAlpha, objSpin); 
398   
399          // a bounding sphere specifies a region a behavior is active 
400          // create a sphere centered at the origin with radius of 1 
401          BoundingSphere bounds = new BoundingSphere(); 
402          rotator.setSchedulingBounds(bounds); 
403          objSpin.addChild(rotator); 
404   
405          DirectionalLight lightD = new DirectionalLight(); 
406          lightD.setDirection(new Vector3f(0.0f, -0.7f, -0.7f)); 
407          lightD.setInfluencingBounds(bounds); 
408          contentRoot.addChild(lightD); 
409   
410          AmbientLight lightA = new AmbientLight(); 
411          lightA.setInfluencingBounds(bounds); 
412          contentRoot.addChild(lightA); 
413   
414          Background background = new Background(); 
415          background.setColor(1.0f, 1.0f, 1.0f); 
416          background.setApplicationBounds(bounds); 
417          contentRoot.addChild(background); 
418   
419          // Let Java 3D perform optimizations on this scene graph. 
420          // contentRoot.compile(); 
421   
422          return contentRoot; 
423      } // end of CreateSceneGraph method of MobiusApp 
424   
425      // Create a simple scene and attach it to the virtual universe 
426   
427      public GeomInfoApp(String[] args) { 
428          setLayout(new BorderLayout()); 
429          GraphicsConfiguration config = 
430                  SimpleUniverse.getPreferredConfiguration(); 
431   
432          Canvas3D canvas3D = new Canvas3D(config); 
433          add("Center", canvas3D); 
434   
435          BranchGroup scene = createSceneGraph(args.length > 0); 
436   
437          // SimpleUniverse is a Convenience Utility class 
438          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
439   
440          // This will move the ViewPlatform back a bit so the 
441          // objects in the scene can be viewed. 
442          simpleU.getViewingPlatform().setNominalViewingTransform(); 
443   
444          simpleU.addBranchGraph(scene); 
445      } // end of GeomInfoApp constructor 
446   
447      //  The following allows this to be run as an application 
448      //  as well as an applet 
449   
450      public static void main(String[] args) { 
451          System.out.print("GeomInfoApp - Java 3D API demo program\n"); 
452          System.out.print("A demonstration of using the GeometryInfo class.\n"); 
453          System.out.print("The blue lines show the input geometry - the red "); 
454          System.out.print("geometry was created by the GeometryInfo and other classes.\n"); 
455          System.out.print("Running the program without any command line arguments will show a solid object\n"); 
456          System.out.print("Supplying any command line argument will show the wireframe.\n"); 
457          System.out.print("http://java.sun.com/products/java-media/3D/collateral\n"); 
458          new MainFrame(new GeomInfoApp(args), 256, 256); 
459      } // end of main method of MobiusApp 
460   
461  } // end of class GeomInfoApp 
462