annotate src/samer/j3d/Util.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 package samer.j3d;
samer@0 2
samer@0 3 import samer.core.*;
samer@0 4 import java.awt.*;
samer@0 5 import javax.media.j3d.*;
samer@0 6 import javax.vecmath.*;
samer@0 7 import com.sun.j3d.utils.behaviors.keyboard.*;
samer@0 8 import com.sun.j3d.utils.behaviors.mouse.*;
samer@0 9 import com.sun.j3d.utils.geometry.*;
samer@0 10
samer@0 11 public class Util
samer@0 12 {
samer@0 13 private static Bounds bounds=new BoundingSphere(new Point3d(0,0,0),1000);
samer@0 14 public static void setBounds(Bounds b) { bounds=b; }
samer@0 15 public static Bounds getBounds() { return bounds; }
samer@0 16
samer@0 17
samer@0 18 // -------------- TransformGroup behviours ------------------
samer@0 19
samer@0 20 public static TransformGroup mouseRotate(TransformGroup tg)
samer@0 21 {
samer@0 22 MouseRotate m = new MouseRotate(readwrite(tg));
samer@0 23 m.setSchedulingBounds(bounds);
samer@0 24 tg.addChild(m);
samer@0 25 return tg;
samer@0 26 }
samer@0 27
samer@0 28 public static TransformGroup mouseTranslate(TransformGroup tg)
samer@0 29 {
samer@0 30 MouseTranslate m = new MouseTranslate(readwrite(tg));
samer@0 31 m.setSchedulingBounds(bounds);
samer@0 32 tg.addChild(m);
samer@0 33 return tg;
samer@0 34 }
samer@0 35
samer@0 36 public static TransformGroup mouseZoom(TransformGroup tg)
samer@0 37 {
samer@0 38 MouseZoom m = new MouseZoom(readwrite(tg));
samer@0 39 m.setSchedulingBounds(bounds);
samer@0 40 tg.addChild(m);
samer@0 41 return tg;
samer@0 42 }
samer@0 43
samer@0 44 public static void addKeyNavigator(Group g, TransformGroup tg)
samer@0 45 {
samer@0 46 // readwrite(tg);
samer@0 47 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(tg);
samer@0 48 keynav.setSchedulingBounds(bounds);
samer@0 49 g.addChild(keynav);
samer@0 50 }
samer@0 51
samer@0 52 public static TransformGroup addRotator(int period, TransformGroup tg)
samer@0 53 {
samer@0 54 Alpha alpha = new Alpha(-1, period);
samer@0 55 Behavior rotator = new RotationInterpolator(alpha, writable(tg));
samer@0 56 rotator.setSchedulingBounds(bounds);
samer@0 57 tg.addChild(rotator);
samer@0 58 return tg;
samer@0 59 }
samer@0 60
samer@0 61 // -------------- Lighting ------------------
samer@0 62
samer@0 63 public static Light color(Light l, Color3f c) { l.setColor(c); return l; }
samer@0 64 public static Light directionalLight(double x, double y, double z)
samer@0 65 {
samer@0 66 DirectionalLight l = new DirectionalLight();
samer@0 67 l.setDirection(-(float)x,-(float)y,-(float)z);
samer@0 68 l.setInfluencingBounds(bounds);
samer@0 69 return l;
samer@0 70 }
samer@0 71
samer@0 72 public static Light ambientLight()
samer@0 73 {
samer@0 74 AmbientLight a = new AmbientLight();
samer@0 75 a.setInfluencingBounds(bounds);
samer@0 76 return a;
samer@0 77 }
samer@0 78
samer@0 79 public static Light pointLight(double x, double y, double z)
samer@0 80 {
samer@0 81 PointLight l = new PointLight();
samer@0 82 l.setInfluencingBounds(bounds);
samer@0 83 l.setPosition((float)x,(float)y,(float)z);
samer@0 84 l.setAttenuation(0,0,1);
samer@0 85 return l;
samer@0 86 }
samer@0 87
samer@0 88 public static Background background(double r, double g, double b)
samer@0 89 {
samer@0 90 Background bg=new Background((float)r,(float)g,(float)b);
samer@0 91 bg.setApplicationBounds(bounds);
samer@0 92 return bg;
samer@0 93 }
samer@0 94
samer@0 95
samer@0 96 public static ViewPlatform writable(ViewPlatform vp) {
samer@0 97 vp.setCapability(ViewPlatform.ALLOW_POLICY_WRITE); return vp;
samer@0 98 }
samer@0 99 public static TransformGroup writable(TransformGroup tg) {
samer@0 100 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); return tg;
samer@0 101 }
samer@0 102 public static TransformGroup readwrite(TransformGroup tg) {
samer@0 103 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
samer@0 104 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
samer@0 105 return tg;
samer@0 106 }
samer@0 107
samer@0 108 // ----------------- Appearances ------------------
samer@0 109
samer@0 110 // basic appearances
samer@0 111
samer@0 112 public static Appearance points()
samer@0 113 {
samer@0 114 Appearance a = new Appearance();
samer@0 115 PolygonAttributes pa = new PolygonAttributes();
samer@0 116 pa.setPolygonMode(PolygonAttributes.POLYGON_POINT);
samer@0 117 pa.setCullFace(PolygonAttributes.CULL_NONE);
samer@0 118 a.setPolygonAttributes(pa);
samer@0 119 a.setPointAttributes(
samer@0 120 new PointAttributes((float)Shell.getDouble("pointsize",3), false));
samer@0 121 return a;
samer@0 122 }
samer@0 123
samer@0 124 public static Appearance wireframe()
samer@0 125 {
samer@0 126 Appearance a = new Appearance();
samer@0 127 PolygonAttributes polyAttrib = new PolygonAttributes();
samer@0 128 polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
samer@0 129 if (!Shell.getBoolean("backfacecull",false)) {
samer@0 130 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
samer@0 131 }
samer@0 132 a.setPolygonAttributes(polyAttrib);
samer@0 133 a.setLineAttributes(
samer@0 134 new LineAttributes((float)Shell.getDouble("linewidth",2),
samer@0 135 LineAttributes.PATTERN_SOLID,true));
samer@0 136
samer@0 137 return a;
samer@0 138 }
samer@0 139
samer@0 140 public static Appearance shadedwireframe()
samer@0 141 {
samer@0 142 Appearance a = new Appearance();
samer@0 143 PolygonAttributes polyAttrib = new PolygonAttributes();
samer@0 144 polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
samer@0 145 a.setPolygonAttributes(polyAttrib);
samer@0 146 a.setLineAttributes(new LineAttributes(2f,LineAttributes.PATTERN_SOLID,false));
samer@0 147
samer@0 148 Material material = new Material();
samer@0 149 material.setDiffuseColor(new Color3f(.7f, 0.6f, 0.9f));
samer@0 150 material.setShininess(128);
samer@0 151 a.setMaterial(material);
samer@0 152 return a;
samer@0 153 }
samer@0 154
samer@0 155 public static Appearance offsetPolygon()
samer@0 156 {
samer@0 157 Appearance a = new Appearance();
samer@0 158 PolygonAttributes polyAttrib = new PolygonAttributes();
samer@0 159 polyAttrib.setPolygonOffset(2f);
samer@0 160 a.setPolygonAttributes(polyAttrib);
samer@0 161
samer@0 162 return a;
samer@0 163 }
samer@0 164
samer@0 165 public static Appearance patches()
samer@0 166 {
samer@0 167 Appearance a = new Appearance();
samer@0 168 PolygonAttributes polyAttrib = new PolygonAttributes();
samer@0 169 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
samer@0 170 a.setPolygonAttributes(polyAttrib);
samer@0 171
samer@0 172 // I think this is for transparent polygons
samer@0 173 // see depthBufferFreezeTransparent in view
samer@0 174 RenderingAttributes ra=new RenderingAttributes();
samer@0 175 ra.setDepthBufferEnable(false);
samer@0 176 ra.setIgnoreVertexColors(false);
samer@0 177 a.setRenderingAttributes(ra);
samer@0 178
samer@0 179 return a;
samer@0 180 }
samer@0 181
samer@0 182 // modifiers
samer@0 183
samer@0 184 public static Appearance color(Color3f c, Appearance a)
samer@0 185 {
samer@0 186 ColoringAttributes colorAttrib=new ColoringAttributes();
samer@0 187 colorAttrib.setColor(c);
samer@0 188 a.setColoringAttributes(colorAttrib);
samer@0 189 return a;
samer@0 190 }
samer@0 191
samer@0 192 public static Appearance flat(Appearance a)
samer@0 193 {
samer@0 194 ColoringAttributes colorAttrib=new ColoringAttributes();
samer@0 195 colorAttrib.setShadeModel(ColoringAttributes.SHADE_FLAT);
samer@0 196 a.setColoringAttributes(colorAttrib);
samer@0 197 return a;
samer@0 198 }
samer@0 199
samer@0 200 public static Appearance flat(Color3f c, Appearance a)
samer@0 201 {
samer@0 202 ColoringAttributes colorAttrib=new ColoringAttributes();
samer@0 203 colorAttrib.setShadeModel(ColoringAttributes.SHADE_FLAT);
samer@0 204 colorAttrib.setColor(c);
samer@0 205 a.setColoringAttributes(colorAttrib);
samer@0 206 return a;
samer@0 207 }
samer@0 208
samer@0 209 public static Appearance alpha(double t, Appearance a)
samer@0 210 {
samer@0 211 a.setTransparencyAttributes(
samer@0 212 new TransparencyAttributes(TransparencyAttributes.BLENDED,(float)t));
samer@0 213 return a;
samer@0 214 }
samer@0 215
samer@0 216 public static Appearance material(Color3f diff, Color3f spec, int shin, Appearance a)
samer@0 217 {
samer@0 218 Material material = new Material();
samer@0 219 material.setDiffuseColor(diff);
samer@0 220 material.setSpecularColor(spec);
samer@0 221 material.setShininess(shin);
samer@0 222 a.setMaterial(material);
samer@0 223 return a;
samer@0 224 }
samer@0 225 }