Mercurial > hg > jslab
comparison examples/java3d/util.java @ 1:5df24c91468d
Oh my what a mess.
author | samer |
---|---|
date | Fri, 05 Apr 2019 16:26:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:bf79fb79ee13 | 1:5df24c91468d |
---|---|
1 import samer.core.*; | |
2 import java.awt.*; | |
3 import javax.media.j3d.*; | |
4 import javax.vecmath.*; | |
5 //import com.sun.j3d.utils.universe.*; | |
6 import com.sun.j3d.utils.behaviors.keyboard.*; | |
7 import com.sun.j3d.utils.behaviors.mouse.*; | |
8 import com.sun.j3d.utils.geometry.*; | |
9 | |
10 public class util | |
11 { | |
12 private static Bounds bounds=new BoundingSphere(new Point3d(0,0,0),1000); | |
13 | |
14 public static void init() { new samer.core.shells.AppShell(); } | |
15 public static void expose(java.awt.Component c,String title) { | |
16 Shell.Window win; | |
17 win=Shell.getWindow(title); | |
18 win.addWindowListener(Shell.exitListener()); | |
19 win.container().setLayout(new BorderLayout()); | |
20 win.container().add(c); | |
21 win.expose(); | |
22 } | |
23 | |
24 public static void setBounds(Bounds b) { bounds=b; } | |
25 | |
26 public static TransformGroup mouseRotate(TransformGroup tg) | |
27 { | |
28 MouseRotate m = new MouseRotate(readwrite(tg)); | |
29 // mr.setTransformGroup(tg); | |
30 m.setSchedulingBounds(bounds); | |
31 tg.addChild(m); | |
32 return tg; | |
33 } | |
34 | |
35 public static TransformGroup mouseTranslate(TransformGroup tg) | |
36 { | |
37 MouseTranslate m = new MouseTranslate(readwrite(tg)); | |
38 // mr.setTransformGroup(tg); | |
39 m.setSchedulingBounds(bounds); | |
40 tg.addChild(m); | |
41 return tg; | |
42 } | |
43 | |
44 public static TransformGroup mouseZoom(TransformGroup tg) | |
45 { | |
46 MouseZoom m = new MouseZoom(readwrite(tg)); | |
47 // mr.setTransformGroup(tg); | |
48 m.setSchedulingBounds(bounds); | |
49 tg.addChild(m); | |
50 return tg; | |
51 } | |
52 | |
53 public static void addKeyNavigator(Group g, TransformGroup tg) | |
54 { | |
55 // readwrite(tg); | |
56 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(tg); | |
57 keynav.setSchedulingBounds(bounds); | |
58 g.addChild(keynav); | |
59 } | |
60 | |
61 public static void addKeyNavigator(Component c, Group g, TransformGroup tg) | |
62 { | |
63 // readwrite(tg); | |
64 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(c,tg); | |
65 keynav.setSchedulingBounds(bounds); | |
66 g.addChild(keynav); | |
67 } | |
68 | |
69 public static GeometryInfo yoyoGeometryInfo(int N) | |
70 { | |
71 GeometryInfo gi=new GeometryInfo(GeometryInfo.TRIANGLE_FAN_ARRAY); | |
72 | |
73 int totalN = 4*(N+1); | |
74 Point3f coords[] = new Point3f[totalN]; | |
75 int stripCounts[] = {N+1, N+1, N+1, N+1}; | |
76 float r = 0.5f; | |
77 float w = 0.5f; | |
78 int n; | |
79 double a; | |
80 float x, y; | |
81 | |
82 // set the central points for four triangle fan strips | |
83 coords[0*(N+1)] = new Point3f(0.0f, 0.0f, w); | |
84 coords[1*(N+1)] = new Point3f(0.0f, 0.0f, 0.0f); | |
85 coords[2*(N+1)] = new Point3f(0.0f, 0.0f, 0.0f); | |
86 coords[3*(N+1)] = new Point3f(0.0f, 0.0f, -w); | |
87 | |
88 for(a = 0,n = 0; n < N; a = 2.0*Math.PI/(N-1) * ++n){ | |
89 x = (float) (r * Math.cos(a)); | |
90 y = (float) (r * Math.sin(a)); | |
91 coords[0*(N+1)+n+1] = new Point3f(x, y, w); | |
92 coords[1*(N+1)+N-n] = new Point3f(x, y, w); | |
93 coords[2*(N+1)+n+1] = new Point3f(x, y, -w); | |
94 coords[3*(N+1)+N-n] = new Point3f(x, y, -w); | |
95 } | |
96 | |
97 gi.setCoordinates(coords); | |
98 gi.setStripCounts(stripCounts); | |
99 | |
100 return gi; | |
101 | |
102 } | |
103 | |
104 public static void addRotator(TransformGroup tg) | |
105 { | |
106 Alpha alpha = new Alpha(-1, 24000); | |
107 Behavior rotator = new RotationInterpolator(alpha, writable(tg)); | |
108 rotator.setSchedulingBounds(bounds); | |
109 tg.addChild(rotator); | |
110 } | |
111 | |
112 public static Light color(Light l, Color3f c) { l.setColor(c); return l; } | |
113 public static Light directionalLight(double x, double y, double z) | |
114 { | |
115 DirectionalLight l = new DirectionalLight(); | |
116 l.setDirection(-(float)x,-(float)y,-(float)z); | |
117 l.setInfluencingBounds(bounds); | |
118 return l; | |
119 } | |
120 | |
121 public static Light ambientLight() | |
122 { | |
123 AmbientLight a = new AmbientLight(); | |
124 a.setInfluencingBounds(bounds); | |
125 return a; | |
126 } | |
127 | |
128 public static Light pointLight(double x, double y, double z) | |
129 { | |
130 PointLight l = new PointLight(); | |
131 l.setInfluencingBounds(bounds); | |
132 l.setPosition((float)x,(float)y,(float)z); | |
133 l.setAttenuation(0,0,1); | |
134 return l; | |
135 } | |
136 | |
137 public static void addBackground(Group g, Background bg) | |
138 { // background | |
139 bg.setApplicationBounds(bounds); | |
140 g.addChild(bg); | |
141 } | |
142 | |
143 public static View setViewPolicy(View v) | |
144 { | |
145 v.setWindowEyepointPolicy(View.RELATIVE_TO_WINDOW); | |
146 //v.setWindowMovementPolicy(View.VIRTUAL_WORLD); | |
147 v.setWindowResizePolicy(View.VIRTUAL_WORLD); | |
148 //v.repaint(); | |
149 return v; | |
150 } | |
151 | |
152 public static ViewPlatform writable(ViewPlatform vp) { | |
153 vp.setCapability(ViewPlatform.ALLOW_POLICY_WRITE); return vp; | |
154 } | |
155 public static TransformGroup writable(TransformGroup tg) { | |
156 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); return tg; | |
157 } | |
158 public static TransformGroup readwrite(TransformGroup tg) { | |
159 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
160 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); | |
161 return tg; | |
162 } | |
163 | |
164 public static Group yoyoGroup() { | |
165 return yoyoGroup(yoyoGeometryInfo(17).getGeometryArray()); | |
166 } | |
167 public static Group yoyoGroup(Geometry geom) { | |
168 return yoyoGroup(geom,wireframe2(),translucent()); | |
169 } | |
170 | |
171 public static Group yoyoGroup(Geometry geom, Appearance a1, Appearance a2) | |
172 { | |
173 Shape3D faces=new Shape3D(); | |
174 Shape3D edges=new Shape3D(); | |
175 | |
176 faces.setGeometry(geom); | |
177 faces.setAppearance(a1); | |
178 edges.setGeometry(geom); | |
179 edges.setAppearance(a2); | |
180 | |
181 BranchGroup bg = new BranchGroup(); | |
182 bg.addChild(faces); | |
183 bg.addChild(edges); | |
184 | |
185 return bg; | |
186 } | |
187 | |
188 public static GraphicsConfiguration getGraphicsConfiguration() | |
189 { | |
190 /* | |
191 return GraphicsEnvironment.getLocalGraphicsEnvironment(). | |
192 getDefaultScreenDevice().getDefaultConfiguration(); | |
193 | |
194 */ | |
195 // Canvas default seems to be better than above | |
196 return null; | |
197 | |
198 } | |
199 | |
200 static Appearance material1() | |
201 { | |
202 Appearance a = new Appearance(); | |
203 PolygonAttributes polyAttrib = new PolygonAttributes(); | |
204 polyAttrib.setPolygonOffset(2f); | |
205 a.setPolygonAttributes(polyAttrib); | |
206 | |
207 Material material = new Material(); | |
208 material.setDiffuseColor(new Color3f(.6f, 0.5f, 0.7f)); | |
209 material.setSpecularColor(new Color3f(.6f, 0.5f, 0.7f)); | |
210 material.setShininess(48); | |
211 a.setMaterial(material); | |
212 return a; | |
213 } | |
214 | |
215 | |
216 static Appearance shadedwireframe() | |
217 { | |
218 Appearance a = new Appearance(); | |
219 PolygonAttributes polyAttrib = new PolygonAttributes(); | |
220 polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); | |
221 a.setPolygonAttributes(polyAttrib); | |
222 a.setLineAttributes(new LineAttributes(2f,LineAttributes.PATTERN_SOLID,false)); | |
223 | |
224 Material material = new Material(); | |
225 material.setDiffuseColor(new Color3f(.7f, 0.6f, 0.9f)); | |
226 material.setShininess(128); | |
227 a.setMaterial(material); | |
228 return a; | |
229 } | |
230 | |
231 | |
232 static Appearance translucent() | |
233 { | |
234 Appearance a = new Appearance(); | |
235 PolygonAttributes polyAttrib = new PolygonAttributes(); | |
236 ColoringAttributes colorAttrib=new ColoringAttributes(); | |
237 a.setPolygonAttributes(polyAttrib); | |
238 | |
239 { | |
240 boolean ft=Shell.getBoolean("yoyo.facetransparency",true); | |
241 if (ft) { | |
242 a.setTransparencyAttributes( | |
243 new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f) | |
244 ); | |
245 colorAttrib.setColor(0.45f,0.4f,.5f); | |
246 } else { | |
247 polyAttrib.setPolygonOffset(2f); | |
248 colorAttrib.setColor(0.5f,0.4f,.5f); | |
249 } | |
250 } | |
251 a.setColoringAttributes(colorAttrib); | |
252 return a; | |
253 } | |
254 | |
255 static Appearance wireframe1() | |
256 { | |
257 | |
258 Appearance a = new Appearance(); | |
259 PolygonAttributes polyAttrib = new PolygonAttributes(); | |
260 polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); | |
261 if (!Shell.getBoolean("yoyo.bfacecull",false)) { | |
262 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); | |
263 } | |
264 a.setLineAttributes( | |
265 new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1), | |
266 LineAttributes.PATTERN_SOLID,true)); | |
267 a.setPolygonAttributes(polyAttrib); | |
268 ColoringAttributes redColoring = new ColoringAttributes(); | |
269 redColoring.setColor(.6f, 0.4f, 0.5f); | |
270 a.setColoringAttributes(redColoring); | |
271 a.setTransparencyAttributes( | |
272 new TransparencyAttributes(TransparencyAttributes.BLENDED,0.4f)); | |
273 | |
274 return a; | |
275 } | |
276 | |
277 static Appearance wireframe2() | |
278 { | |
279 Appearance appearance1 = new Appearance(); | |
280 PolygonAttributes polyAttrib = new PolygonAttributes(); | |
281 polyAttrib.setPolygonMode( | |
282 Shell.getBoolean("yoyo.points",true) ? | |
283 PolygonAttributes.POLYGON_POINT : | |
284 PolygonAttributes.POLYGON_LINE | |
285 | |
286 ); | |
287 if (!Shell.getBoolean("yoyo.bfacecull",false)) { | |
288 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); | |
289 } | |
290 | |
291 appearance1.setPolygonAttributes(polyAttrib); | |
292 appearance1.setPointAttributes( | |
293 new PointAttributes((float)Shell.getDouble("yoyo.pointsize",2), false)); | |
294 appearance1.setLineAttributes( | |
295 new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1), | |
296 LineAttributes.PATTERN_SOLID,true)); | |
297 appearance1.setTransparencyAttributes( | |
298 new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f)); | |
299 ColoringAttributes colorAttrib=new ColoringAttributes(); | |
300 colorAttrib.setColor(0.8f,0.6f,1f); | |
301 appearance1.setColoringAttributes(colorAttrib); | |
302 return appearance1; | |
303 } | |
304 | |
305 static Appearance wireframe3() | |
306 { | |
307 Appearance appearance1 = new Appearance(); | |
308 PolygonAttributes polyAttrib = new PolygonAttributes(); | |
309 polyAttrib.setPolygonMode( | |
310 Shell.getBoolean("yoyo.points",true) ? | |
311 PolygonAttributes.POLYGON_POINT : | |
312 PolygonAttributes.POLYGON_LINE | |
313 | |
314 ); | |
315 if (!Shell.getBoolean("yoyo.bfacecull",false)) { | |
316 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); | |
317 } | |
318 | |
319 appearance1.setPolygonAttributes(polyAttrib); | |
320 appearance1.setPointAttributes( | |
321 new PointAttributes((float)Shell.getDouble("yoyo.pointsize",2), false)); | |
322 appearance1.setLineAttributes( | |
323 new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1), | |
324 LineAttributes.PATTERN_SOLID,true)); | |
325 appearance1.setTransparencyAttributes( | |
326 new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f)); | |
327 | |
328 Material material = new Material(); | |
329 material.setDiffuseColor(new Color3f(.8f, 0.6f, 1f)); | |
330 appearance1.setMaterial(material); | |
331 return appearance1; | |
332 } | |
333 | |
334 public static Appearance pointy() | |
335 { | |
336 Appearance a = new Appearance(); | |
337 PolygonAttributes pa = new PolygonAttributes(); | |
338 pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); | |
339 pa.setCullFace(PolygonAttributes.CULL_NONE); | |
340 a.setPolygonAttributes(pa); | |
341 a.setPointAttributes(new PointAttributes(5,false)); | |
342 a.setTransparencyAttributes( | |
343 new TransparencyAttributes(TransparencyAttributes.BLENDED,0.8f)); | |
344 /* | |
345 new TransparencyAttributes( | |
346 TransparencyAttributes.BLENDED,0.9f, | |
347 TransparencyAttributes.BLEND_SRC_ALPHA, | |
348 TransparencyAttributes.BLEND_ONE)); | |
349 */ | |
350 ColoringAttributes colorAttrib=new ColoringAttributes(); | |
351 colorAttrib.setColor(0.8f,0.6f,1f); | |
352 a.setColoringAttributes(colorAttrib); | |
353 return a; | |
354 } | |
355 | |
356 public static Appearance pointy2() | |
357 { | |
358 Appearance a = new Appearance(); | |
359 PolygonAttributes pa = new PolygonAttributes(); | |
360 pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); | |
361 pa.setCullFace(PolygonAttributes.CULL_NONE); | |
362 a.setPolygonAttributes(pa); | |
363 a.setPointAttributes(new PointAttributes(3,false)); | |
364 a.setTransparencyAttributes( | |
365 new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f)); | |
366 | |
367 ColoringAttributes colorAttrib=new ColoringAttributes(); | |
368 colorAttrib.setColor(0.8f,0.6f,1f); | |
369 a.setColoringAttributes(colorAttrib); | |
370 return a; | |
371 } | |
372 | |
373 public static Appearance pointy3() | |
374 { | |
375 Appearance a = new Appearance(); | |
376 PolygonAttributes pa = new PolygonAttributes(); | |
377 pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); | |
378 pa.setCullFace(PolygonAttributes.CULL_NONE); | |
379 a.setPolygonAttributes(pa); | |
380 a.setPointAttributes(new PointAttributes(3,false)); | |
381 | |
382 ColoringAttributes colorAttrib=new ColoringAttributes(); | |
383 colorAttrib.setColor(0.8f,0.6f,1f); | |
384 a.setColoringAttributes(colorAttrib); | |
385 return a; | |
386 } | |
387 } |