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

1    package j3d; 
2     
3    /* 
4     *      @(#)DistanceLODApp.java 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.geometry.Sphere; 
35   import com.sun.j3d.utils.universe.SimpleUniverse; 
36    
37   import javax.media.j3d.*; 
38   import javax.vecmath.AxisAngle4f; 
39   import javax.vecmath.Point3f; 
40   import javax.vecmath.Vector3f; 
41   import java.applet.Applet; 
42   import java.awt.*; 
43    
44   //   DistanceLODApp renders a simple landscape 
45    
46   public class DistanceLODApp extends Applet { 
47    
48    
49       public BranchGroup createSceneGraph() { 
50           BranchGroup objRoot = new BranchGroup(); 
51           BoundingSphere bounds = new BoundingSphere(); 
52    
53           // create target TransformGroup with Capabilities 
54           TransformGroup objMove = new TransformGroup(); 
55           objMove.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
56    
57           // create Alpha 
58           Alpha alpha = new Alpha(-1, 
59                   Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE, 
60                   0, 0, 5000, 1000, 1000, 5000, 1000, 1000); 
61    
62           // specify the axis of translation 
63           AxisAngle4f axisOfTra = new AxisAngle4f(0.0f, 1.0f, 0.0f, (float) Math.PI / -2.0f); 
64           Transform3D axisT3D = new Transform3D(); 
65           axisT3D.set(axisOfTra); 
66    
67           // create position interpolator 
68           PositionInterpolator posInt 
69                   = new PositionInterpolator(alpha, objMove, axisT3D, 0.0f, -35.0f); 
70           posInt.setSchedulingBounds(bounds); 
71    
72           // create DistanceLOD target object 
73           Switch targetSwitch = new Switch(); 
74           targetSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); 
75    
76           // add visual objects of various levels of detail to the target switch 
77           Appearance sphereAppearA = new Appearance(); 
78           ColoringAttributes sphereCAa = new ColoringAttributes(); 
79           sphereCAa.setColor(0.1f, 0.8f, 0.1f); 
80           sphereAppearA.setColoringAttributes(sphereCAa); 
81    
82           Appearance sphereAppearB = new Appearance(); 
83           ColoringAttributes sphereCAb = new ColoringAttributes(); 
84           sphereCAb.setColor(0.8f, 0.1f, 0.1f); 
85           sphereAppearB.setColoringAttributes(sphereCAb); 
86    
87           targetSwitch.addChild(new Sphere(.40f, 0, 25, sphereAppearA)); 
88           targetSwitch.addChild(new Sphere(.40f, 0, 15, sphereAppearB)); 
89           targetSwitch.addChild(new Sphere(.40f, 0, 10, sphereAppearA)); 
90           targetSwitch.addChild(new Sphere(.40f, 0, 4, sphereAppearB)); 
91    
92           // create DistanceLOD object 
93           float[] distances = {5.0f, 10.0f, 20.0f}; 
94           DistanceLOD dLOD = new DistanceLOD(distances, new Point3f()); 
95           dLOD.addSwitch(targetSwitch); 
96           dLOD.setSchedulingBounds(bounds); 
97    
98           if ((targetSwitch.numChildren() - 1) != dLOD.numDistances()) { 
99               System.out.println("DistanceLOD not initialized properly"); 
100              System.out.println(targetSwitch.numChildren()); 
101              System.out.println(dLOD.numDistances()); 
102          } 
103   
104          // assemble scene graph 
105   
106          objRoot.addChild(objMove);     // target TG of position interp to move vo 
107          objRoot.addChild(posInt);      // add position interpolator 
108          objMove.addChild(dLOD);        // make the bounds move with visual object 
109          objMove.addChild(targetSwitch);// must add target switch to scene graph too 
110   
111   
112          // show a level 3 object up close for comparison 
113          Transform3D t3d = new Transform3D(); 
114          t3d.set(new Vector3f(0.6f, 0.0f, 0.0f)); 
115          TransformGroup tga = new TransformGroup(t3d); 
116          objRoot.addChild(tga); 
117          tga.addChild(new Sphere(.40f, 0, 4, sphereAppearB)); 
118   
119          // show a level 0 object at a distance for comparison 
120          t3d.set(new Vector3f(-5.0f, 0.0f, -35.0f)); 
121          TransformGroup tgb = new TransformGroup(t3d); 
122          objRoot.addChild(tgb); 
123          tgb.addChild(new Sphere(.40f, 0, 25, sphereAppearA)); 
124   
125          // a white background is better for printing images in tutorial 
126          Background background = new Background(); 
127          background.setColor(1.0f, 1.0f, 1.0f); 
128          background.setApplicationBounds(new BoundingSphere()); 
129          objRoot.addChild(background); 
130   
131          // Let Java 3D perform optimizations on this scene graph. 
132          objRoot.compile(); 
133   
134          return objRoot; 
135      } // end of CreateSceneGraph method of DistanceLODApp 
136   
137      public DistanceLODApp() { 
138          setLayout(new BorderLayout()); 
139          GraphicsConfiguration config = 
140                  SimpleUniverse.getPreferredConfiguration(); 
141   
142          Canvas3D canvas3D = new Canvas3D(config); 
143          add("Center", canvas3D); 
144   
145          BranchGroup scene = createSceneGraph(); 
146   
147          // SimpleUniverse is a Convenience Utility class 
148          SimpleUniverse simpleU = new SimpleUniverse(canvas3D); 
149   
150          // This will move the ViewPlatform back a bit so the 
151          // objects in the scene can be viewed. 
152          simpleU.getViewingPlatform().setNominalViewingTransform(); 
153   
154          simpleU.addBranchGraph(scene); 
155      } // end of DistanceLODApp (constructor) 
156   
157   
158      //  The following allows this to be run as an application 
159      //  as well as an applet 
160   
161      public static void main(String[] args) { 
162          System.out.print("DistanceLODApp.java\n- a demonstration of using the DistanceLOD "); 
163          System.out.println("class to provide variable \n  level of detail (LOD) for a visual object in a Java 3D scene."); 
164          System.out.println("The sphere is represented by one of four different spheres \ndepending on the viewing distance."); 
165          System.out.println("In this example the different spheres are colored differently to \nillustrate swithcing."); 
166          System.out.println("In practice the spheres would all be colored alike."); 
167          System.out.println("The large static sphere is a duplicate of the sphere with the least detail."); 
168          System.out.println("The small static sphere is a duplicate of the sphere with the most detail.\n"); 
169          System.out.println("This is an example progam from The Java 3D API Tutorial."); 
170          System.out.println("The Java 3D Tutorial is available on the web at:"); 
171          System.out.println("http://java.sun.com/products/java-media/3D/collateral"); 
172          Frame frame = new MainFrame(new DistanceLODApp(), 256, 256); 
173      } // end of main (method of DistanceLODApp) 
174   
175  } // end of class DistanceLODApp 
176