comparison src/samer/j3d/Util.java @ 0:bf79fb79ee13

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