1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.jbeans.j3d.scenegraph;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26 import java.io.*;
27 import java.lang.reflect.*;
28 import java.util.*;
29 import javax.swing.event.*;
30 import javax.media.j3d.Geometry;
31 import javax.media.j3d.Node;
32
33 import net.jbeans.lang.*;
34 import net.jbeans.j3d.event.*;
35 import net.jbeans.util.debug.*;
36
37
38
39
40 /***
41 * generally describe Shape3D in here
42 *
43 * @version $Revision: 1.3 $
44 * @author Masahiro Takatsuka (masa@jbeans.net)
45 * @see javax.media.j3d.Shape3D
46 * @see Serializable
47 */
48
49 public class Shape3D extends javax.media.j3d.Shape3D implements Serializable {
50 private static final boolean DEBUG = Debug.getDebugFlag(Shape3D.class);
51
52
53
54 transient private EventListenerList sceneGraphObjectListeners;
55 transient private EventObject myEvent;
56
57 public Shape3D() {
58 super();
59 initialize();
60 }
61
62 public Shape3D(Geometry geometry) {
63 super(geometry);
64 initialize();
65 }
66
67 public Shape3D(Geometry geometry, Appearance appearance) {
68 super(geometry, appearance);
69 initialize();
70 }
71
72 protected void initialize() {
73 this.sceneGraphObjectListeners = new EventListenerList();
74 this.myEvent = new EventObject(this);
75
76 setCapability(Node.ALLOW_BOUNDS_READ);
77 setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
78 setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
79 setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_WRITE);
80 setCapability(Node.ALLOW_BOUNDS_READ);
81 setCapability(Node.ALLOW_BOUNDS_WRITE);
82 setCapability(Node.ALLOW_COLLIDABLE_READ);
83 setCapability(Node.ALLOW_COLLIDABLE_WRITE);
84 setCapability(Node.ALLOW_PICKABLE_READ);
85 setCapability(Node.ALLOW_PICKABLE_WRITE);
86 setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_READ);
87 setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_WRITE);
88 setCapability(Shape3D.ALLOW_APPEARANCE_READ);
89 setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
90 setCapability(Shape3D.ALLOW_COLLISION_BOUNDS_READ);
91 setCapability(Shape3D.ALLOW_COLLISION_BOUNDS_WRITE);
92 setCapability(Shape3D.ALLOW_GEOMETRY_READ);
93 setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
94
95 setBoundsAutoCompute(true);
96
97
98 setAppearance(createMaterialAppearance());
99 }
100
101 private boolean contains(Geometry geometry) {
102 int cnt = numGeometries();
103 for (int i = 0; i < cnt; i++) {
104 if (geometry == getGeometry(i)) {
105 if (DEBUG) {
106 System.out.println("duplicate geometry");
107 }
108 return true;
109 }
110 }
111 return false;
112 }
113
114 /***
115 *
116 */
117 public void addGeometry(Geometry geometry) {
118 if (contains(geometry)) {
119 return;
120 }
121 if (DEBUG) {
122 System.out.println("Shape3D : adding " + geometry);
123 }
124 super.addGeometry(geometry);
125 fireSceneGraphObjectChanged();
126 }
127
128 /***
129 *
130 */
131 public void insertGeometry(Geometry geometry, int i) {
132 if (contains(geometry)) {
133 return;
134 }
135 super.insertGeometry(geometry, i);
136 fireSceneGraphObjectChanged();
137 }
138
139 /***
140 *
141 */
142 public void setGeometry(Geometry geometry) {
143 super.setGeometry(geometry);
144 fireSceneGraphObjectChanged();
145 }
146
147 /***
148 *
149 */
150 public void setGeometry(Geometry geometry, int i) {
151 super.setGeometry(geometry, i);
152 fireSceneGraphObjectChanged();
153 }
154
155 /***
156 *
157 */
158 public void removeGeometry(Geometry geom) {
159
160
161
162
163 int allCnt = numGeometries();
164 int i = -1;
165 for (i = 0; i < allCnt; i++) {
166 if (geom.equals(getGeometry(i))) {
167 removeGeometry(i);
168 return;
169 }
170 }
171 }
172
173 /***
174 *
175 */
176 public void removeAllGeometry() {
177 int allCnt = numGeometries();
178 for (int i = 0; i < allCnt; i++) {
179 removeGeometry(i);
180 }
181 }
182
183 /***
184 *
185 */
186 public void setGeometries(Geometry[] geometries) {
187 for (int i = 0; i < geometries.length; i++) {
188 super.setGeometry(geometries[i], i);
189 }
190 fireSceneGraphObjectChanged();
191 }
192
193 /***
194 *
195 */
196 public void addGeometries(Geometry[] geometries) {
197 for (int i = 0; i < geometries.length; i++) {
198 super.addGeometry(geometries[i]);
199 }
200 fireSceneGraphObjectChanged();
201 }
202
203 /***
204 * adds an SceneGraphObjectListener to the shape
205 */
206 public void addSceneGraphObjectListener(SceneGraphObjectListener l) {
207 this.sceneGraphObjectListeners.add(SceneGraphObjectListener.class, l);
208 }
209
210 /***
211 * removes an GeometryListener from the shape.
212 */
213 public void removeSceneGraphObjectListener(SceneGraphObjectListener l) {
214 this.sceneGraphObjectListeners.remove(SceneGraphObjectListener.class, l);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 }
239
240 /***
241 * Notify all listeners that have registered interest for
242 * notification on this event type.
243 */
244 public void fireSceneGraphObjectChanged() {
245 Object[] listeners = this.sceneGraphObjectListeners.getListenerList();
246
247
248 for (int i = listeners.length - 2; i >= 0; i -= 2) {
249 if (listeners[i] == SceneGraphObjectListener.class) {
250
251 ((SceneGraphObjectListener)listeners[i+1]).objectChanged(this.myEvent);
252 }
253 }
254 }
255
256 /***
257 * Serialization methods
258 */
259 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
260 initialize();
261 }
262
263 /***
264 * Serialization methods
265 */
266 private void writeObject(ObjectOutputStream s) throws IOException {
267
268 }
269
270
271 private static Appearance createMaterialAppearance(){
272
273 Appearance materialAppear = new Appearance();
274 setCapabilities(materialAppear);
275 PolygonAttributes polyAttrib = new PolygonAttributes();
276 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
277 materialAppear.setPolygonAttributes(polyAttrib);
278
279 Material material = new Material();
280 material.setLightingEnable(true);
281 materialAppear.setMaterial(material);
282
283 return materialAppear;
284 }
285
286 private static void setCapabilities(Appearance appear) {
287 appear.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
288 appear.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
289 appear.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ);
290 appear.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);
291 appear.setCapability(Appearance.ALLOW_MATERIAL_READ);
292 appear.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
293 appear.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_READ);
294 appear.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
295 appear.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
296 appear.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
297 appear.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_READ);
298 appear.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_WRITE);
299 appear.setCapability(Appearance.ALLOW_TEXGEN_READ);
300 appear.setCapability(Appearance.ALLOW_TEXGEN_WRITE);
301 appear.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_READ);
302 appear.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);
303 appear.setCapability(Appearance.ALLOW_TEXTURE_READ);
304 appear.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
305 appear.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_READ);
306 appear.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
307 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
308 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
309 }
310 }