samer@1: import samer.core.*; samer@1: import java.awt.*; samer@1: import javax.media.j3d.*; samer@1: import javax.vecmath.*; samer@1: //import com.sun.j3d.utils.universe.*; samer@1: import com.sun.j3d.utils.behaviors.keyboard.*; samer@1: import com.sun.j3d.utils.behaviors.mouse.*; samer@1: import com.sun.j3d.utils.geometry.*; samer@1: samer@1: public class util samer@1: { samer@1: private static Bounds bounds=new BoundingSphere(new Point3d(0,0,0),1000); samer@1: samer@1: public static void init() { new samer.core.shells.AppShell(); } samer@1: public static void expose(java.awt.Component c,String title) { samer@1: Shell.Window win; samer@1: win=Shell.getWindow(title); samer@1: win.addWindowListener(Shell.exitListener()); samer@1: win.container().setLayout(new BorderLayout()); samer@1: win.container().add(c); samer@1: win.expose(); samer@1: } samer@1: samer@1: public static void setBounds(Bounds b) { bounds=b; } samer@1: samer@1: public static TransformGroup mouseRotate(TransformGroup tg) samer@1: { samer@1: MouseRotate m = new MouseRotate(readwrite(tg)); samer@1: // mr.setTransformGroup(tg); samer@1: m.setSchedulingBounds(bounds); samer@1: tg.addChild(m); samer@1: return tg; samer@1: } samer@1: samer@1: public static TransformGroup mouseTranslate(TransformGroup tg) samer@1: { samer@1: MouseTranslate m = new MouseTranslate(readwrite(tg)); samer@1: // mr.setTransformGroup(tg); samer@1: m.setSchedulingBounds(bounds); samer@1: tg.addChild(m); samer@1: return tg; samer@1: } samer@1: samer@1: public static TransformGroup mouseZoom(TransformGroup tg) samer@1: { samer@1: MouseZoom m = new MouseZoom(readwrite(tg)); samer@1: // mr.setTransformGroup(tg); samer@1: m.setSchedulingBounds(bounds); samer@1: tg.addChild(m); samer@1: return tg; samer@1: } samer@1: samer@1: public static void addKeyNavigator(Group g, TransformGroup tg) samer@1: { samer@1: // readwrite(tg); samer@1: KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(tg); samer@1: keynav.setSchedulingBounds(bounds); samer@1: g.addChild(keynav); samer@1: } samer@1: samer@1: public static void addKeyNavigator(Component c, Group g, TransformGroup tg) samer@1: { samer@1: // readwrite(tg); samer@1: KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(c,tg); samer@1: keynav.setSchedulingBounds(bounds); samer@1: g.addChild(keynav); samer@1: } samer@1: samer@1: public static GeometryInfo yoyoGeometryInfo(int N) samer@1: { samer@1: GeometryInfo gi=new GeometryInfo(GeometryInfo.TRIANGLE_FAN_ARRAY); samer@1: samer@1: int totalN = 4*(N+1); samer@1: Point3f coords[] = new Point3f[totalN]; samer@1: int stripCounts[] = {N+1, N+1, N+1, N+1}; samer@1: float r = 0.5f; samer@1: float w = 0.5f; samer@1: int n; samer@1: double a; samer@1: float x, y; samer@1: samer@1: // set the central points for four triangle fan strips samer@1: coords[0*(N+1)] = new Point3f(0.0f, 0.0f, w); samer@1: coords[1*(N+1)] = new Point3f(0.0f, 0.0f, 0.0f); samer@1: coords[2*(N+1)] = new Point3f(0.0f, 0.0f, 0.0f); samer@1: coords[3*(N+1)] = new Point3f(0.0f, 0.0f, -w); samer@1: samer@1: for(a = 0,n = 0; n < N; a = 2.0*Math.PI/(N-1) * ++n){ samer@1: x = (float) (r * Math.cos(a)); samer@1: y = (float) (r * Math.sin(a)); samer@1: coords[0*(N+1)+n+1] = new Point3f(x, y, w); samer@1: coords[1*(N+1)+N-n] = new Point3f(x, y, w); samer@1: coords[2*(N+1)+n+1] = new Point3f(x, y, -w); samer@1: coords[3*(N+1)+N-n] = new Point3f(x, y, -w); samer@1: } samer@1: samer@1: gi.setCoordinates(coords); samer@1: gi.setStripCounts(stripCounts); samer@1: samer@1: return gi; samer@1: samer@1: } samer@1: samer@1: public static void addRotator(TransformGroup tg) samer@1: { samer@1: Alpha alpha = new Alpha(-1, 24000); samer@1: Behavior rotator = new RotationInterpolator(alpha, writable(tg)); samer@1: rotator.setSchedulingBounds(bounds); samer@1: tg.addChild(rotator); samer@1: } samer@1: samer@1: public static Light color(Light l, Color3f c) { l.setColor(c); return l; } samer@1: public static Light directionalLight(double x, double y, double z) samer@1: { samer@1: DirectionalLight l = new DirectionalLight(); samer@1: l.setDirection(-(float)x,-(float)y,-(float)z); samer@1: l.setInfluencingBounds(bounds); samer@1: return l; samer@1: } samer@1: samer@1: public static Light ambientLight() samer@1: { samer@1: AmbientLight a = new AmbientLight(); samer@1: a.setInfluencingBounds(bounds); samer@1: return a; samer@1: } samer@1: samer@1: public static Light pointLight(double x, double y, double z) samer@1: { samer@1: PointLight l = new PointLight(); samer@1: l.setInfluencingBounds(bounds); samer@1: l.setPosition((float)x,(float)y,(float)z); samer@1: l.setAttenuation(0,0,1); samer@1: return l; samer@1: } samer@1: samer@1: public static void addBackground(Group g, Background bg) samer@1: { // background samer@1: bg.setApplicationBounds(bounds); samer@1: g.addChild(bg); samer@1: } samer@1: samer@1: public static View setViewPolicy(View v) samer@1: { samer@1: v.setWindowEyepointPolicy(View.RELATIVE_TO_WINDOW); samer@1: //v.setWindowMovementPolicy(View.VIRTUAL_WORLD); samer@1: v.setWindowResizePolicy(View.VIRTUAL_WORLD); samer@1: //v.repaint(); samer@1: return v; samer@1: } samer@1: samer@1: public static ViewPlatform writable(ViewPlatform vp) { samer@1: vp.setCapability(ViewPlatform.ALLOW_POLICY_WRITE); return vp; samer@1: } samer@1: public static TransformGroup writable(TransformGroup tg) { samer@1: tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); return tg; samer@1: } samer@1: public static TransformGroup readwrite(TransformGroup tg) { samer@1: tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); samer@1: tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); samer@1: return tg; samer@1: } samer@1: samer@1: public static Group yoyoGroup() { samer@1: return yoyoGroup(yoyoGeometryInfo(17).getGeometryArray()); samer@1: } samer@1: public static Group yoyoGroup(Geometry geom) { samer@1: return yoyoGroup(geom,wireframe2(),translucent()); samer@1: } samer@1: samer@1: public static Group yoyoGroup(Geometry geom, Appearance a1, Appearance a2) samer@1: { samer@1: Shape3D faces=new Shape3D(); samer@1: Shape3D edges=new Shape3D(); samer@1: samer@1: faces.setGeometry(geom); samer@1: faces.setAppearance(a1); samer@1: edges.setGeometry(geom); samer@1: edges.setAppearance(a2); samer@1: samer@1: BranchGroup bg = new BranchGroup(); samer@1: bg.addChild(faces); samer@1: bg.addChild(edges); samer@1: samer@1: return bg; samer@1: } samer@1: samer@1: public static GraphicsConfiguration getGraphicsConfiguration() samer@1: { samer@1: /* samer@1: return GraphicsEnvironment.getLocalGraphicsEnvironment(). samer@1: getDefaultScreenDevice().getDefaultConfiguration(); samer@1: samer@1: */ samer@1: // Canvas default seems to be better than above samer@1: return null; samer@1: samer@1: } samer@1: samer@1: static Appearance material1() samer@1: { samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@1: polyAttrib.setPolygonOffset(2f); samer@1: a.setPolygonAttributes(polyAttrib); samer@1: samer@1: Material material = new Material(); samer@1: material.setDiffuseColor(new Color3f(.6f, 0.5f, 0.7f)); samer@1: material.setSpecularColor(new Color3f(.6f, 0.5f, 0.7f)); samer@1: material.setShininess(48); samer@1: a.setMaterial(material); samer@1: return a; samer@1: } samer@1: samer@1: samer@1: static Appearance shadedwireframe() samer@1: { samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@1: polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); samer@1: a.setPolygonAttributes(polyAttrib); samer@1: a.setLineAttributes(new LineAttributes(2f,LineAttributes.PATTERN_SOLID,false)); samer@1: samer@1: Material material = new Material(); samer@1: material.setDiffuseColor(new Color3f(.7f, 0.6f, 0.9f)); samer@1: material.setShininess(128); samer@1: a.setMaterial(material); samer@1: return a; samer@1: } samer@1: samer@1: samer@1: static Appearance translucent() samer@1: { samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@1: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@1: a.setPolygonAttributes(polyAttrib); samer@1: samer@1: { samer@1: boolean ft=Shell.getBoolean("yoyo.facetransparency",true); samer@1: if (ft) { samer@1: a.setTransparencyAttributes( samer@1: new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f) samer@1: ); samer@1: colorAttrib.setColor(0.45f,0.4f,.5f); samer@1: } else { samer@1: polyAttrib.setPolygonOffset(2f); samer@1: colorAttrib.setColor(0.5f,0.4f,.5f); samer@1: } samer@1: } samer@1: a.setColoringAttributes(colorAttrib); samer@1: return a; samer@1: } samer@1: samer@1: static Appearance wireframe1() samer@1: { samer@1: samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@1: polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); samer@1: if (!Shell.getBoolean("yoyo.bfacecull",false)) { samer@1: polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); samer@1: } samer@1: a.setLineAttributes( samer@1: new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1), samer@1: LineAttributes.PATTERN_SOLID,true)); samer@1: a.setPolygonAttributes(polyAttrib); samer@1: ColoringAttributes redColoring = new ColoringAttributes(); samer@1: redColoring.setColor(.6f, 0.4f, 0.5f); samer@1: a.setColoringAttributes(redColoring); samer@1: a.setTransparencyAttributes( samer@1: new TransparencyAttributes(TransparencyAttributes.BLENDED,0.4f)); samer@1: samer@1: return a; samer@1: } samer@1: samer@1: static Appearance wireframe2() samer@1: { samer@1: Appearance appearance1 = new Appearance(); samer@1: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@1: polyAttrib.setPolygonMode( samer@1: Shell.getBoolean("yoyo.points",true) ? samer@1: PolygonAttributes.POLYGON_POINT : samer@1: PolygonAttributes.POLYGON_LINE samer@1: samer@1: ); samer@1: if (!Shell.getBoolean("yoyo.bfacecull",false)) { samer@1: polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); samer@1: } samer@1: samer@1: appearance1.setPolygonAttributes(polyAttrib); samer@1: appearance1.setPointAttributes( samer@1: new PointAttributes((float)Shell.getDouble("yoyo.pointsize",2), false)); samer@1: appearance1.setLineAttributes( samer@1: new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1), samer@1: LineAttributes.PATTERN_SOLID,true)); samer@1: appearance1.setTransparencyAttributes( samer@1: new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f)); samer@1: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@1: colorAttrib.setColor(0.8f,0.6f,1f); samer@1: appearance1.setColoringAttributes(colorAttrib); samer@1: return appearance1; samer@1: } samer@1: samer@1: static Appearance wireframe3() samer@1: { samer@1: Appearance appearance1 = new Appearance(); samer@1: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@1: polyAttrib.setPolygonMode( samer@1: Shell.getBoolean("yoyo.points",true) ? samer@1: PolygonAttributes.POLYGON_POINT : samer@1: PolygonAttributes.POLYGON_LINE samer@1: samer@1: ); samer@1: if (!Shell.getBoolean("yoyo.bfacecull",false)) { samer@1: polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); samer@1: } samer@1: samer@1: appearance1.setPolygonAttributes(polyAttrib); samer@1: appearance1.setPointAttributes( samer@1: new PointAttributes((float)Shell.getDouble("yoyo.pointsize",2), false)); samer@1: appearance1.setLineAttributes( samer@1: new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1), samer@1: LineAttributes.PATTERN_SOLID,true)); samer@1: appearance1.setTransparencyAttributes( samer@1: new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f)); samer@1: samer@1: Material material = new Material(); samer@1: material.setDiffuseColor(new Color3f(.8f, 0.6f, 1f)); samer@1: appearance1.setMaterial(material); samer@1: return appearance1; samer@1: } samer@1: samer@1: public static Appearance pointy() samer@1: { samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes pa = new PolygonAttributes(); samer@1: pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); samer@1: pa.setCullFace(PolygonAttributes.CULL_NONE); samer@1: a.setPolygonAttributes(pa); samer@1: a.setPointAttributes(new PointAttributes(5,false)); samer@1: a.setTransparencyAttributes( samer@1: new TransparencyAttributes(TransparencyAttributes.BLENDED,0.8f)); samer@1: /* samer@1: new TransparencyAttributes( samer@1: TransparencyAttributes.BLENDED,0.9f, samer@1: TransparencyAttributes.BLEND_SRC_ALPHA, samer@1: TransparencyAttributes.BLEND_ONE)); samer@1: */ samer@1: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@1: colorAttrib.setColor(0.8f,0.6f,1f); samer@1: a.setColoringAttributes(colorAttrib); samer@1: return a; samer@1: } samer@1: samer@1: public static Appearance pointy2() samer@1: { samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes pa = new PolygonAttributes(); samer@1: pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); samer@1: pa.setCullFace(PolygonAttributes.CULL_NONE); samer@1: a.setPolygonAttributes(pa); samer@1: a.setPointAttributes(new PointAttributes(3,false)); samer@1: a.setTransparencyAttributes( samer@1: new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f)); samer@1: samer@1: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@1: colorAttrib.setColor(0.8f,0.6f,1f); samer@1: a.setColoringAttributes(colorAttrib); samer@1: return a; samer@1: } samer@1: samer@1: public static Appearance pointy3() samer@1: { samer@1: Appearance a = new Appearance(); samer@1: PolygonAttributes pa = new PolygonAttributes(); samer@1: pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); samer@1: pa.setCullFace(PolygonAttributes.CULL_NONE); samer@1: a.setPolygonAttributes(pa); samer@1: a.setPointAttributes(new PointAttributes(3,false)); samer@1: samer@1: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@1: colorAttrib.setColor(0.8f,0.6f,1f); samer@1: a.setColoringAttributes(colorAttrib); samer@1: return a; samer@1: } samer@1: }