View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class ImageModeler
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:06 $
11   * 
12   * $Id: ImageModeler.java,v 1.3 2004/03/03 11:53:06 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.modeler.scene;
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.awt.*;
27  import java.beans.*;
28  import java.io.*;
29  import java.lang.reflect.*;
30  import java.net.*;
31  import java.util.*;
32  import javax.vecmath.*;
33  import com.sun.j3d.utils.geometry.*;
34  
35  import net.jbeans.bean.*;
36  import net.jbeans.lang.*;
37  import net.jbeans.j3d.data.*;
38  import net.jbeans.j3d.scenegraph.*;
39  import net.jbeans.j3d.transform.*;
40  import net.jbeans.j3d.util.texture.*;
41  
42  import net.jbeans.util.*;
43  import net.jbeans.util.debug.*;
44  
45  /*====================================================================
46                   Implementation of class ImageModeler                 
47  ====================================================================*/
48  /***
49   * ImageModeler is a class that creates a javax.media.j3d.BranchGroup
50   * object (more specifically net.jbeans.j3d.scenegraph.BranchGroup), which
51   * contains javax.media.j3d.Shape3D objects.
52   * 
53   * @version $Revision: 1.3 $
54   * @author Masahiro Takatsuka (masa@jbeans.net)
55   * @see SceneModeler
56   * @see Serializable, GeometryUpdater
57   */
58  
59  public class ImageModeler extends SceneModeler implements Serializable, javax.media.j3d.GeometryUpdater {
60    	private static final boolean DEBUG = Debug.getDebugFlag(ImageModeler.class);
61  
62  	/* ------------------------ not serialized ------------------------ */
63  	transient private BranchGroup topBranch;
64  	transient private Shape3D shape3D;
65  	transient private TextureLoader textureLoader;
66  	transient private int width;
67  	transient private int height;	
68  	transient private javax.media.j3d.GeometryArray geomArray;
69  	
70  	/* -------------------------- serialized -------------------------- */
71  	private int transparencyMode = TransparencyAttributes.FASTEST;
72  	private float transparencyValue = 0.0f;
73  	private boolean lightingEnable = false;
74  	private String imageFile;
75  	private URL imageURL;
76  	private boolean byReference = true;
77  	private int textureMode = TextureAttributes.REPLACE;
78  	
79  	/***
80  	 * Construct a new DataSetModeler.
81  	 */
82  	public ImageModeler() {
83  		super();
84  		initialize();
85  	}
86  	
87  	private void initialize() {
88  		this.imageFile = null;
89  		this.imageURL = null;
90  		this.width = -1;
91  		this.height = -1;
92  	}
93  
94  	private void reset() {
95  		clearScene();			// remove the topBranch
96  		initialize();
97  	}
98  
99  	public void setByReference(boolean b) {
100 		this.byReference = b;
101 	}
102 
103 	public boolean getByReference() {
104 		return this.byReference;
105 	}
106 
107 	public void setTransparencyMode(int mode) {
108 		this.transparencyMode = mode;
109 		setSceneData();		
110 	}
111 
112 	public int getTransparencyMode() {
113 		return this.transparencyMode;
114 	}
115 
116 	public void setTextureMode(int mode) {
117 		this.textureMode = mode;
118 		setSceneData();
119 	}
120 
121 	public int getTextureMode() {
122 		return this.textureMode;
123 	}
124 	
125 	public void setTransparencyValue(float value) {
126 		if (value < 0.0f) {
127 			value = 0.0f;
128 		}
129 		if (value > 1.0f) {
130 			value = 1.0f;
131 		}
132 		this.transparencyValue = value;
133 		setSceneData();
134 	}
135 
136 	public float getTransparencyValue() {
137 		return this.transparencyValue;
138 	}
139 	
140 	private void setGeometry() {
141 		javax.media.j3d.GeometryArray geom = (javax.media.j3d.GeometryArray) createGeometry();
142 		setCapabilities(geom);
143 		if (this.shape3D == null) {
144 			this.shape3D = new Shape3D();
145 		}
146 		this.shape3D.setGeometry(geom, 0);			
147 	}
148 	
149     private javax.media.j3d.Geometry createGeometry(){
150         javax.media.j3d.QuadArray plane = new javax.media.j3d.QuadArray(4, javax.media.j3d.GeometryArray.COORDINATES | javax.media.j3d.GeometryArray.TEXTURE_COORDINATE_2);
151 		float width = 1.0f * this.width;
152 		float height = 1.0f * this.height;		
153         Point3f p = new Point3f(0.0f,  height,  0.0f);
154         plane.setCoordinate(0, p);
155         p.set(0.0f, 0.0f,  0.0f);
156         plane.setCoordinate(1, p);
157         p.set(width, 0.0f,  0.0f);
158         plane.setCoordinate(2, p);
159         p.set(width,  height,  0.0f);
160         plane.setCoordinate(3, p);
161 		
162         TexCoord2f q = new TexCoord2f( 0.0f,  1.0f);
163         plane.setTextureCoordinate(0, 0, q);
164         q.set(0.0f, 0.0f);
165         plane.setTextureCoordinate(0, 1, q);
166         q.set(1.0f, 0.0f);
167         plane.setTextureCoordinate(0, 2, q);
168         q.set(1.0f, 1.0f);
169         plane.setTextureCoordinate(0, 3, q);
170 		
171         return plane;
172     }
173 
174 	private void setCapabilities(javax.media.j3d.TextureAttributes txattr) {
175 		if (txattr.isLive() || txattr.isCompiled()) {
176 			return;
177 		}
178 		txattr.setCapability(TextureAttributes.ALLOW_BLEND_COLOR_READ);
179 		txattr.setCapability(TextureAttributes.ALLOW_BLEND_COLOR_WRITE);
180 		txattr.setCapability(TextureAttributes.ALLOW_MODE_READ);
181 		txattr.setCapability(TextureAttributes.ALLOW_MODE_WRITE);
182 		txattr.setCapability(TextureAttributes.ALLOW_TRANSFORM_READ);
183 		txattr.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE);
184 	}
185 
186 	private void setCapabilities(javax.media.j3d.TransparencyAttributes tattr) {
187 		if (tattr.isLive() || tattr.isCompiled()) {
188 			return;
189 		}
190 		tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_READ);
191 		tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
192 		tattr.setCapability(TransparencyAttributes.ALLOW_MODE_READ);
193 		tattr.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
194 	}
195 	
196     private javax.media.j3d.Appearance createAppearance() {
197         javax.media.j3d.Appearance appear = this.shape3D.getAppearance();
198 		if (appear == null) {
199 			appear = new javax.media.j3d.Appearance();
200 		}
201 		
202         javax.media.j3d.ImageComponent2D image = this.textureLoader.getImage();
203 
204         if (image == null) {
205 			System.out.println("failed to load a texture.");
206 			return appear;
207         }
208 
209 		if (DEBUG) {
210 			System.out.println("Image width  = " + image.getWidth());
211 			System.out.println("Image height = " + image.getHeight());
212 		}
213         
214         // can't use parameterless constuctor
215         javax.media.j3d.Texture2D texture = new javax.media.j3d.Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight());
216         texture.setImage(0, image);
217         texture.setEnable(true);
218         texture.setMagFilter(Texture.NICEST);
219 		
220         appear.setTexture(texture);
221 		
222 		javax.media.j3d.TextureAttributes txattr = appear.getTextureAttributes();
223 		if (txattr == null) {
224 			txattr = new javax.media.j3d.TextureAttributes();
225 		};
226 		setCapabilities(txattr);
227 		txattr.setTextureMode(this.textureMode);
228 		appear.setTextureAttributes(txattr);
229 
230 		javax.media.j3d.TransparencyAttributes tattr = appear.getTransparencyAttributes();
231 		if (tattr == null) {
232 			tattr = new javax.media.j3d.TransparencyAttributes();
233 		}
234 		setCapabilities(tattr);
235 		tattr.setTransparencyMode(this.transparencyMode);
236 		tattr.setTransparency(this.transparencyValue);
237 		appear.setTransparencyAttributes(tattr);
238 		
239         return appear;
240     }
241 
242 	public void setImageFile(String file) {
243 		this.imageFile = file;
244 		this.imageURL = null;
245 		if (this.byReference) {
246 			this.textureLoader = new TextureLoader(this.imageFile, TextureLoader.BY_REFERENCE);
247 		} else {
248 			this.textureLoader = new TextureLoader(this.imageFile);
249 		}
250 		setSceneData();
251     }    		
252 	
253 	public String getImageFile() {
254 		return this.imageFile;
255 	}
256 	
257 	public void setLightingEnable(boolean b) {
258 		this.lightingEnable = b;
259 		setSceneData();		
260 	}
261 
262 	public boolean getLightingEnable() {
263 		return this.lightingEnable;
264 	}
265 
266 	public void setImage(Image image) {
267 		if (this.byReference) {
268 			this.textureLoader = new TextureLoader(image, TextureLoader.BY_REFERENCE);
269 		} else {
270 			this.textureLoader = new TextureLoader(image);
271 		}
272 		setSceneData();
273 	}
274 
275 	public void setImageURL(URL image) {
276 		this.imageURL = image;
277 		this.imageFile = null;
278 		if (this.byReference) {
279 			this.textureLoader = new TextureLoader(this.imageURL, TextureLoader.BY_REFERENCE);
280 		} else {
281 			this.textureLoader = new TextureLoader(this.imageURL);
282 		}
283 		setSceneData();
284 	}
285 
286 	public URL getImageURL() {
287 		return this.imageURL;
288 	}
289 	
290 	/***
291 	 * Serialization methods
292 	 */
293 	private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
294 		s.defaultReadObject();
295 		initialize();
296 	}
297 	
298 	public void updateData(javax.media.j3d.Geometry geom) {
299 		
300 	}
301 	
302 	/***
303 	* After setting all necessary infomation, call this method to
304 	* construct ImageModel.
305 	*/
306 	public void setSceneData() {
307 		setImage();
308 		setData();
309 	}
310 
311 	private void setCapabilities(javax.media.j3d.ImageComponent ic) {
312 		ic.setCapability(javax.media.j3d.ImageComponent.ALLOW_FORMAT_READ);
313 		ic.setCapability(javax.media.j3d.ImageComponent.ALLOW_IMAGE_READ);
314 		ic.setCapability(javax.media.j3d.ImageComponent.ALLOW_SIZE_READ);
315 	}
316 	
317 	private void setImage() {
318         javax.media.j3d.ImageComponent2D image = this.textureLoader.getImage();
319 		if (!image.isLive() && !image.isCompiled()) {
320 			setCapabilities(image);
321 		}
322 		
323         if (image != null) {
324 			// set geometry
325 			if (this.width != image.getWidth() ||
326 				this.height != image.getHeight()) {
327 				this.width = image.getWidth();
328 				this.height = image.getHeight();
329 				setGeometry();
330 			}
331         }
332 		
333 		this.shape3D.setAppearance(createAppearance());
334 
335 		if (this.topBranch == null) {
336 			this.topBranch = new BranchGroup();
337 			this.topBranch.addChild(this.shape3D);			
338 		}
339 	}
340 	
341 	private void setData() {
342 		boolean needModel = false;
343 		try {
344 			javax.media.j3d.BranchGroup bg = getScene();
345 			if (bg == null || bg.getParent() == null) {
346 				if (DEBUG) {
347 					System.out.println("ImageModeler.bg = " + bg);
348 					if (bg != null) {
349 						System.out.println("ImageModeler.bg.getParent() = " + bg.getParent());
350 					}
351 				}
352 				needModel = true;
353 			}
354 		} catch (javax.media.j3d.RestrictedAccessException e) {
355 			if (DEBUG) {
356 				e.printStackTrace();
357 			}
358 			needModel = false;
359 		}
360 		
361 		if (needModel) { // if entryNum has changed.
362 			if (DEBUG) {
363 				System.out.println("ImageModeler.modeling..");
364 			}
365 			reset();
366 			model();
367 			if (DEBUG) {
368 				System.out.println("ImageModeler.modeling.topBranch = " + getScene());
369 			}
370 		} else {
371 			if (DEBUG) {
372 				System.out.println("ImageModeler.updating..");
373 			}
374 			update();
375 			if (DEBUG) {
376 				System.out.println("ImageModeler.updating.topBranch = " + getScene());
377 			}
378 		}
379 	}
380 
381   	/***
382 	 */
383   	protected void modelScene() {
384 		addScene(this.topBranch);
385 	}
386 	
387   	/***
388   	 *
389   	 */
390 	protected void updateScene() {
391 		// NOP
392 	}
393 
394 	private void setCapabilities(javax.media.j3d.GeometryArray geom) {
395 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_FORMAT_READ);
396 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_COUNT_READ);
397 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_COUNT_WRITE);
398 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_COORDINATE_READ);
399 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_COORDINATE_WRITE);
400 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_NORMAL_READ);
401 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_NORMAL_WRITE);
402 		geom.setCapability(javax.media.j3d.GeometryArray.ALLOW_REF_DATA_WRITE);
403 	}
404 }