annotate examples/java3d/util.java @ 8:5e3cbbf173aa tip

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