samer@0: package samer.j3d; samer@0: samer@0: import samer.core.*; samer@0: import java.awt.*; samer@0: import javax.media.j3d.*; samer@0: import javax.vecmath.*; samer@0: import com.sun.j3d.utils.behaviors.keyboard.*; samer@0: import com.sun.j3d.utils.behaviors.mouse.*; samer@0: import com.sun.j3d.utils.geometry.*; samer@0: samer@0: public class Util samer@0: { samer@0: private static Bounds bounds=new BoundingSphere(new Point3d(0,0,0),1000); samer@0: public static void setBounds(Bounds b) { bounds=b; } samer@0: public static Bounds getBounds() { return bounds; } samer@0: samer@0: samer@0: // -------------- TransformGroup behviours ------------------ samer@0: samer@0: public static TransformGroup mouseRotate(TransformGroup tg) samer@0: { samer@0: MouseRotate m = new MouseRotate(readwrite(tg)); samer@0: m.setSchedulingBounds(bounds); samer@0: tg.addChild(m); samer@0: return tg; samer@0: } samer@0: samer@0: public static TransformGroup mouseTranslate(TransformGroup tg) samer@0: { samer@0: MouseTranslate m = new MouseTranslate(readwrite(tg)); samer@0: m.setSchedulingBounds(bounds); samer@0: tg.addChild(m); samer@0: return tg; samer@0: } samer@0: samer@0: public static TransformGroup mouseZoom(TransformGroup tg) samer@0: { samer@0: MouseZoom m = new MouseZoom(readwrite(tg)); samer@0: m.setSchedulingBounds(bounds); samer@0: tg.addChild(m); samer@0: return tg; samer@0: } samer@0: samer@0: public static void addKeyNavigator(Group g, TransformGroup tg) samer@0: { samer@0: // readwrite(tg); samer@0: KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(tg); samer@0: keynav.setSchedulingBounds(bounds); samer@0: g.addChild(keynav); samer@0: } samer@0: samer@0: public static TransformGroup addRotator(int period, TransformGroup tg) samer@0: { samer@0: Alpha alpha = new Alpha(-1, period); samer@0: Behavior rotator = new RotationInterpolator(alpha, writable(tg)); samer@0: rotator.setSchedulingBounds(bounds); samer@0: tg.addChild(rotator); samer@0: return tg; samer@0: } samer@0: samer@0: // -------------- Lighting ------------------ samer@0: samer@0: public static Light color(Light l, Color3f c) { l.setColor(c); return l; } samer@0: public static Light directionalLight(double x, double y, double z) samer@0: { samer@0: DirectionalLight l = new DirectionalLight(); samer@0: l.setDirection(-(float)x,-(float)y,-(float)z); samer@0: l.setInfluencingBounds(bounds); samer@0: return l; samer@0: } samer@0: samer@0: public static Light ambientLight() samer@0: { samer@0: AmbientLight a = new AmbientLight(); samer@0: a.setInfluencingBounds(bounds); samer@0: return a; samer@0: } samer@0: samer@0: public static Light pointLight(double x, double y, double z) samer@0: { samer@0: PointLight l = new PointLight(); samer@0: l.setInfluencingBounds(bounds); samer@0: l.setPosition((float)x,(float)y,(float)z); samer@0: l.setAttenuation(0,0,1); samer@0: return l; samer@0: } samer@0: samer@0: public static Background background(double r, double g, double b) samer@0: { samer@0: Background bg=new Background((float)r,(float)g,(float)b); samer@0: bg.setApplicationBounds(bounds); samer@0: return bg; samer@0: } samer@0: samer@0: samer@0: public static ViewPlatform writable(ViewPlatform vp) { samer@0: vp.setCapability(ViewPlatform.ALLOW_POLICY_WRITE); return vp; samer@0: } samer@0: public static TransformGroup writable(TransformGroup tg) { samer@0: tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); return tg; samer@0: } samer@0: public static TransformGroup readwrite(TransformGroup tg) { samer@0: tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); samer@0: tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); samer@0: return tg; samer@0: } samer@0: samer@0: // ----------------- Appearances ------------------ samer@0: samer@0: // basic appearances samer@0: samer@0: public static Appearance points() samer@0: { samer@0: Appearance a = new Appearance(); samer@0: PolygonAttributes pa = new PolygonAttributes(); samer@0: pa.setPolygonMode(PolygonAttributes.POLYGON_POINT); samer@0: pa.setCullFace(PolygonAttributes.CULL_NONE); samer@0: a.setPolygonAttributes(pa); samer@0: a.setPointAttributes( samer@0: new PointAttributes((float)Shell.getDouble("pointsize",3), false)); samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance wireframe() samer@0: { samer@0: Appearance a = new Appearance(); samer@0: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@0: polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); samer@0: if (!Shell.getBoolean("backfacecull",false)) { samer@0: polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); samer@0: } samer@0: a.setPolygonAttributes(polyAttrib); samer@0: a.setLineAttributes( samer@0: new LineAttributes((float)Shell.getDouble("linewidth",2), samer@0: LineAttributes.PATTERN_SOLID,true)); samer@0: samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance shadedwireframe() samer@0: { samer@0: Appearance a = new Appearance(); samer@0: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@0: polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); samer@0: a.setPolygonAttributes(polyAttrib); samer@0: a.setLineAttributes(new LineAttributes(2f,LineAttributes.PATTERN_SOLID,false)); samer@0: samer@0: Material material = new Material(); samer@0: material.setDiffuseColor(new Color3f(.7f, 0.6f, 0.9f)); samer@0: material.setShininess(128); samer@0: a.setMaterial(material); samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance offsetPolygon() samer@0: { samer@0: Appearance a = new Appearance(); samer@0: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@0: polyAttrib.setPolygonOffset(2f); samer@0: a.setPolygonAttributes(polyAttrib); samer@0: samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance patches() samer@0: { samer@0: Appearance a = new Appearance(); samer@0: PolygonAttributes polyAttrib = new PolygonAttributes(); samer@0: polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); samer@0: a.setPolygonAttributes(polyAttrib); samer@0: samer@0: // I think this is for transparent polygons samer@0: // see depthBufferFreezeTransparent in view samer@0: RenderingAttributes ra=new RenderingAttributes(); samer@0: ra.setDepthBufferEnable(false); samer@0: ra.setIgnoreVertexColors(false); samer@0: a.setRenderingAttributes(ra); samer@0: samer@0: return a; samer@0: } samer@0: samer@0: // modifiers samer@0: samer@0: public static Appearance color(Color3f c, Appearance a) samer@0: { samer@0: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@0: colorAttrib.setColor(c); samer@0: a.setColoringAttributes(colorAttrib); samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance flat(Appearance a) samer@0: { samer@0: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@0: colorAttrib.setShadeModel(ColoringAttributes.SHADE_FLAT); samer@0: a.setColoringAttributes(colorAttrib); samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance flat(Color3f c, Appearance a) samer@0: { samer@0: ColoringAttributes colorAttrib=new ColoringAttributes(); samer@0: colorAttrib.setShadeModel(ColoringAttributes.SHADE_FLAT); samer@0: colorAttrib.setColor(c); samer@0: a.setColoringAttributes(colorAttrib); samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance alpha(double t, Appearance a) samer@0: { samer@0: a.setTransparencyAttributes( samer@0: new TransparencyAttributes(TransparencyAttributes.BLENDED,(float)t)); samer@0: return a; samer@0: } samer@0: samer@0: public static Appearance material(Color3f diff, Color3f spec, int shin, Appearance a) samer@0: { samer@0: Material material = new Material(); samer@0: material.setDiffuseColor(diff); samer@0: material.setSpecularColor(spec); samer@0: material.setShininess(shin); samer@0: a.setMaterial(material); samer@0: return a; samer@0: } samer@0: }