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