/Users/lyon/j4p/src/j3d/cr325/killTheStudent/Utils.java

1    /** 
2     * Created by IntelliJ IDEA. 
3     * User: dlyon 
4     * Date: Mar 3, 2004 
5     * Time: 12:37:33 PM 
6     * To change this template use Options | File Templates. 
7     */ 
8    package j3d.cr325.killTheStudent; 
9     
10   import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior; 
11   import com.sun.j3d.utils.geometry.Primitive; 
12   import com.sun.j3d.utils.geometry.Sphere; 
13   import com.sun.j3d.utils.image.TextureLoader; 
14   import com.sun.j3d.utils.universe.SimpleUniverse; 
15   import ip.raul.Fractals; 
16   import j2d.ImageUtils; 
17   import utils.SystemUtils; 
18    
19   import javax.media.j3d.*; 
20   import javax.vecmath.Color3f; 
21   import javax.vecmath.Point3d; 
22   import javax.vecmath.Vector3d; 
23   import javax.vecmath.Vector3f; 
24   import java.awt.*; 
25   import java.io.File; 
26    
27   public class Utils { 
28       private static String userDir = SystemUtils.getUserDir(); 
29       private static final String pathSep = SystemUtils.getDirectorySeparator(); 
30       private static String dataDirectory = 
31               //"C:\\Documents and Settings\\jgervasio\\My Documents\\joeg\\j4p\\data\\"; 
32               userDir + pathSep + "data" + pathSep; 
33    
34       private static String imageDirectory 
35               //="C:\\Documents and Settings\\jgervasio\\My Documents\\joeg\\j4p\\data\\images\\"; 
36               = dataDirectory + "images" + pathSep; 
37    
38       public static void main(String[] args) { 
39           System.out.println("checking images directory:" + imageDirectory); 
40           boolean b = checkImagesDirectory(); 
41           System.out.println("found directory=" + b); 
42       } 
43    
44       public static boolean checkImagesDirectory() { 
45           File f = new File(dataDirectory); 
46           if (f.exists()) return true; 
47           f = futils.Futil.getReadDirFile("please locate:" + dataDirectory); 
48           dataDirectory = f.toString(); 
49           return checkImagesDirectory(); 
50       } 
51    
52       public static Texture getTexture(Component c) { 
53           File f = futils.Futil.getReadFile("select env map"); 
54           // load a texture image using the Java 3D texture loader 
55           Texture tex = new TextureLoader(f.toString(), c).getTexture(); 
56           return tex; 
57       } 
58    
59       public static Appearance getAppearance(Component c) { 
60           Appearance app = new Appearance(); 
61           // apply the texture to the Appearance 
62           app.setTexture(getTexture(c)); 
63           return app; 
64       } 
65    
66       public static Texture getMandleTexture(int w, int h) { 
67           TextureLoader tl = new TextureLoader(ImageUtils.getBufferedImage(Fractals.getMandelbrot(w, h))); 
68           return tl.getTexture(); 
69       } 
70    
71       public static Texture getTexture(Image img) { 
72           return new TextureLoader(ImageUtils.getBufferedImage(img)).getTexture(); 
73       } 
74    
75       public static Background getMandlebrotBackground(int w, int h) { 
76           return new Background(new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, 
77                   ImageUtils.getBufferedImage(Fractals.getMandelbrot(w, h)))); 
78       } 
79    
80    
81       public static void addKeyboardNavigation(TransformGroup tg, BranchGroup bg) { 
82           KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg); 
83           keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 10000.0)); 
84           bg.addChild(keyNavBeh); 
85       } 
86    
87       /** 
88        * SetLayout to border layout and 
89        * 
90        * @param c 
91        * @return a Canvas3D based on the container 
92        */ 
93       public static Canvas3D getCanvas3D(Container c) { 
94           c.setLayout(new BorderLayout()); 
95           GraphicsConfiguration gc = 
96                   SimpleUniverse.getPreferredConfiguration(); 
97           return new Canvas3D(gc); 
98       } 
99    
100      public static void addLights(BranchGroup bg) { 
101          addLights(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0), bg); 
102      } 
103   
104      /** 
105       * This adds some lights to the content branch of the scene graph. 
106       * 
107       * @param bounds1 
108       * @param b       The BranchGroup to add the lights to. 
109       */ 
110      public static void addLights(BoundingSphere bounds1, 
111                                   BranchGroup b) { 
112          Color3f ambLightColour = new Color3f(0.5f, 0.5f, 0.5f); 
113          AmbientLight ambLight = new AmbientLight(ambLightColour); 
114          ambLight.setInfluencingBounds(bounds1); 
115          Color3f dirLightColour = new Color3f(1.0f, 1.0f, 1.0f); 
116          Vector3f dirLightDir = new Vector3f(-1.0f, -1.0f, -1.0f); 
117          DirectionalLight dirLight = new DirectionalLight(dirLightColour, dirLightDir); 
118          dirLight.setInfluencingBounds(bounds1); 
119          b.addChild(ambLight); 
120          b.addChild(dirLight); 
121      } 
122   
123      public static Background getImageBackground(String fileName) { 
124          checkImagesDirectory(); 
125          Image img = ImageUtils.getImage(imageDirectory + fileName); 
126          return new Background(new ImageComponent2D(ImageComponent2D.FORMAT_RGB, 
127                  ImageUtils.getBufferedImage(img))); 
128      } 
129   
130      public static Appearance getImageAppearance(String fileName) { 
131   
132          Appearance app = new Appearance(); 
133          //checkImagesDirectory(); 
134          System.out.println(imageDirectory+fileName); 
135          Image image = ImageUtils.getImage(imageDirectory + fileName); 
136          app.setTexture(getTexture(image)); 
137          Color3f black = new Color3f(0.0f, 0.0f, 0.0f); 
138          Color3f white = new Color3f(1.0f, 1.0f, 1.0f); 
139   
140          app.setMaterial(new Material(white, black, white, 
141                  white, 1.0f)); 
142          return app; 
143      } 
144   
145      public static Appearance getMandelbrotAppearance(int w, int h) { 
146          Appearance app = new Appearance(); 
147          app.setTexture(getMandleTexture(w, h)); 
148          Color3f black = new Color3f(0.0f, 0.0f, 0.0f); 
149          Color3f white = new Color3f(1.0f, 1.0f, 1.0f); 
150   
151          app.setMaterial(new Material(white, black, white, 
152                  white, 1.0f)); 
153          return app; 
154      } 
155   
156      public static BranchGroup getTextureSphere(double scale, 
157                                                 double xpos, double ypos, 
158                                                 String fileName) { 
159          BranchGroup objRoot = new BranchGroup(); 
160          BoundingSphere bounds = 
161                  new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
162          Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f); 
163          Background bg = new Background(bgColor); 
164          bg.setApplicationBounds(bounds); 
165          objRoot.addChild(bg); 
166   
167   
168          Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); 
169   
170          AmbientLight aLgt = new AmbientLight(alColor); 
171          aLgt.setInfluencingBounds(bounds); 
172          getDirectionLight(bounds, objRoot); 
173          objRoot.addChild(aLgt); 
174   
175          Appearance app = //Utils.getMandelbrotAppearance(400, 400); 
176                  getImageAppearance(fileName); 
177          objRoot.addChild(createObject(app, scale, xpos, ypos)); 
178          objRoot.compile(); 
179          return objRoot; 
180      } 
181   
182      public static void getDirectionLight(BoundingSphere bounds, BranchGroup objRoot) { 
183          Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -1.0f); 
184          Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); 
185          DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); 
186          lgt1.setInfluencingBounds(bounds); 
187          objRoot.addChild(lgt1); 
188      } 
189   
190      public static Primitive getSphere() { 
191          return Sphere; 
192      } 
193   
194      private static Primitive Sphere = null; 
195   
196      public static Group createObject(Appearance app, 
197                                       double scale, 
198                                       double xpos, double ypos) { 
199          Transform3D t = new Transform3D(); 
200          t.set(scale, new Vector3d(xpos, ypos, 0.0)); 
201          TransformGroup objTrans = new TransformGroup(t); 
202          TransformGroup spinTg = new TransformGroup(); 
203          spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
204          spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
205          Sphere = new Sphere(1.0f, 
206                  Sphere.GENERATE_NORMALS | 
207                  Sphere.GENERATE_TEXTURE_COORDS, 28, app); 
208          Sphere.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); 
209          //Primitive Sphere = new Box(1.0f, 
210          //        (float) xpos, (float) ypos, 
211          //        Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS, 
212          //        app); 
213   
214          spinTg.addChild(Sphere); 
215          Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 
216                  0, 0, 
217                  10000, 0, 0, 
218                  0, 0, 0); 
219          RotationInterpolator rotator = 
220                  new RotationInterpolator(rotationAlpha, spinTg); 
221          BoundingSphere bounds = 
222                  new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
223          rotator.setSchedulingBounds(bounds); 
224          objTrans.addChild(rotator); 
225          objTrans.addChild(spinTg); 
226          return objTrans; 
227      } 
228   
229      public static void addMotion(Alpha a, 
230                                   TransformGroup objectsXformGroup, 
231                                   BranchGroup theBgToMove) { 
232          Transform3D axis = new Transform3D(); 
233          PositionInterpolator piObj = 
234                  new PositionInterpolator(a, 
235                          objectsXformGroup, 
236                          axis, 
237                          -30.0f, 30.0f); 
238          Bounds bs = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
239          piObj.setSchedulingBounds(bs); 
240          theBgToMove.addChild(piObj); 
241          theBgToMove.addChild(objectsXformGroup); 
242      } 
243   
244      public static BranchGroup getBranchGroup() { 
245          BranchGroup bg = new BranchGroup(); 
246          bg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
247          bg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
248          return bg; 
249      } 
250   
251      public static ViewPlatform getViewPlatform() { 
252          ViewPlatform vp = new ViewPlatform(); 
253          vp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
254          vp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
255          return vp; 
256      } 
257   
258      public static TransformGroup getTransformGroup(Transform3D t3d) { 
259          TransformGroup tg = new TransformGroup(t3d); 
260          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
261          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
262          return tg; 
263      } 
264   
265      public static TransformGroup getTransformGroup() { 
266          TransformGroup tg = new TransformGroup(); 
267          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
268          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
269          tg.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ); 
270          return tg; 
271      } 
272  } 
273