View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class QuadModeler
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:05 $
11   * 
12   * $Id: QuadModeler.java,v 1.3 2004/03/03 11:53:05 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.modeler.geometry;
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.awt.event.*;
28  import java.io.*;
29  import java.lang.reflect.*;
30  import java.util.*;
31  import javax.media.j3d.*;
32  import javax.vecmath.*;
33  import com.sun.j3d.utils.geometry.*;
34  
35  import net.jbeans.util.debug.*;
36  
37  /*====================================================================
38                   Implementation of class QuadModeler                  
39  ====================================================================*/
40  /***
41   * QuadModeler generates a suface geometry from the specified data.
42   * 
43   * @version $Revision: 1.3 $
44   * @author Masahiro Takatsuka (masa@jbeans.net)
45   * @see SurfaceModeler
46   */
47  
48  public class QuadModeler extends SurfaceModeler {
49  	private static final boolean DEBUG = Debug.getDebugFlag(QuadModeler.class);
50  	public static final int XC = 0; // X coordinates
51  	public static final int YC = 1; // Y coordinates
52  	public static final int ZC = 2; // Z coordinates
53  	public static final int CC = 3; // color
54  	public static final int OP = 4; // alpha
55  	public static final int NC = 5; // normals
56  	public static final int TC = 6; // texture coordinates
57  	public static final int AC = 7; // Attributes such as scalar, tensor, vector
58  
59  	/* ------------------------ not serialized ------------------------ */
60  
61  	/* -------------------------- serialized -------------------------- */
62  	private boolean generateTextureCoords = true;
63  	
64  	public QuadModeler() {
65  		super();
66  	}
67  
68  	protected void initialize() {
69  		super.initialize();
70  		setPrimitive(GeometryInfo.QUAD_ARRAY);
71  	}
72  	
73  	protected void initGeometry() {
74  		this.totalIndex = 8;
75  		this.essentials = new int[] {XC, YC, ZC};
76  		this.entryArray = new Object[this.totalIndex];
77  	}
78  
79  	public void setGenerateTextureCoords(boolean b) {
80  		this.generateTextureCoords = b;
81  	}
82  	
83  	public boolean getGenerateTextureCoords() {
84  		return this.generateTextureCoords;
85  	}
86  	
87  	public void setXCoordinates(double[] x) {
88  		setValueAt(XC, x);
89  	}
90  
91  	public void setYCoordinates(double[] y) {
92  		setValueAt(YC, y);
93  	}
94  
95  	public void setZCoordinates(double[] z) {
96  		setValueAt(ZC, z);
97  	}
98  
99  	public void setColors(double[] color) {
100 		setValueAt(CC, color);
101 	}
102 	
103 	public void setTransparency(double[] alpha) {
104 		setValueAt(OP, alpha);
105 	}
106 	
107 	public void setNormals(double[][] normal) {
108 		setValueAt(NC, normal);
109 	}
110 
111 	public void setTextureCoords(double[][] tCoords) {
112 		setValueAt(TC, tCoords);
113 	}
114 
115 	/*
116 	  public void setAttributtes(Object[] attri) {
117 	  setValueAt(AC, attri);
118 	  }
119 	*/
120 	
121 	protected boolean checkSize() {
122 		double[] x = (double[]) this.entryArray[XC];
123 		double[] y = (double[]) this.entryArray[YC];
124 		double[] z = (double[]) this.entryArray[ZC];
125 		double[] color = (double[]) this.entryArray[CC];
126 		double[] alpha = (double[]) this.entryArray[OP];		
127 		double[][] normal = (double[][]) this.entryArray[NC];
128 		double[][] tCoord = (double[][]) this.entryArray[TC];
129 
130 		int size = -1;
131 
132 		for (int i = 0; i < this.entryArray.length; i++) {
133 			if (this.entryArray[i] != null) {
134 				size = Array.getLength(this.entryArray[i]);
135 				break;
136 			}
137 		}
138 		if (DEBUG) {
139 			System.out.println("size = " + size);
140 		}
141 
142 		if ((x == null || x.length != size) ||
143 			(y == null || y.length != size) ||
144 			(z == null || z.length != size) ||
145 			(color != null && color.length != size) ||
146 			(alpha != null && alpha.length != size) ||			
147 			(normal != null && normal.length != size) ||
148 			(tCoord != null && tCoord.length != size)
149 			) {
150 			if (DEBUG) {
151 				System.out.println("size false");
152 			}
153 			return false;
154 		}
155 		return true;
156 	}
157 
158 	protected boolean needsModel() {
159 		double[] x = (double[]) this.entryArray[XC];
160 		double[] color = (double[]) this.entryArray[CC];
161 		double[] alpha = (double[]) this.entryArray[OP];		
162 		double[] normal = (double[]) this.entryArray[NC];
163 		double[] tcoord = (double[]) this.entryArray[TC];				
164 
165 		if (DEBUG) {
166 			System.out.println("x = " + x);
167 			System.out.println("x = " + this.x);
168 		}
169 		if ((this.x == null) || (x.length != this.x.length) ||
170 			(this.color == null && color != null) ||
171 			(this.alpha == null && alpha != null) ||			
172 			(this.normal == null && normal != null) ||
173 			(this.tCoords == null && tcoord != null)) {
174 			return true;
175 		}
176 		
177 		return false;
178 	}
179 
180 	protected synchronized void setData() {
181 		double[] x = (double[]) this.entryArray[XC];
182 		double[] y = (double[]) this.entryArray[YC];
183 		double[] z = (double[]) this.entryArray[ZC];
184 		double[] color = (double[]) this.entryArray[CC];
185 		double[] alpha = (double[]) this.entryArray[OP];		
186 		double[][] normal = (double[][]) this.entryArray[NC];
187 		double[][] tCoord = (double[][]) this.entryArray[TC];				
188 
189 		super.setXCoordinates(x);
190 		super.setYCoordinates(y);
191 		super.setZCoordinates(z);
192 		
193 		if (color != null) {
194 			super.setColors(color);
195 		}
196 
197 		if (alpha != null) {
198 			super.setTransparency(alpha);
199 		}
200 		
201 		if (normal != null) {
202 			super.setNormals(normal);
203 		}
204 		
205 		if (tCoord != null) {
206 			super.setTextureCoords(tCoord);
207 		}
208 	}
209 
210 	/*
211 	* uncomment the calculateNormals and modelGeometry method in order to
212 	* create geometry without the help of GeometryInfo.
213 	* Masahiro Takatsuka/2002-Feb-06
214 	*/
215     private void calculateNormals() {
216         int i = this.coords3f.length;
217         int j = i / 4;
218 
219 		if (this.normals3f == null || this.normals3f.length != i) {
220             this.normals3f = new Vector3f[i];
221             for (int k = 0; k < j; k++) {
222                 Vector3f vector3f = new Vector3f(SurfaceModeler.geomCalcTriNormals(this.coords3f[k * 4], this.coords3f[k * 4 + 1], this.coords3f[k * 4 + 2]));
223                 this.normals3f[k * 4] = vector3f;
224                 this.normals3f[k * 4 + 1] = vector3f;
225                 this.normals3f[k * 4 + 2] = vector3f;
226                 this.normals3f[k * 4 + 3] = vector3f;
227 				if (DEBUG) {
228 					System.out.println("calcnormal(" + k + ")");
229 					System.out.println("normal = " + vector3f);
230 				}
231             }
232 
233         } else {
234             for (int l = 0; l < j; l++) {
235                 Vector3f vector3f1 = SurfaceModeler.geomCalcTriNormals(this.coords3f[l * 4], this.coords3f[l * 4 + 1], this.coords3f[l * 4 + 2]);
236                 this.normals3f[l * 4].set(vector3f1);
237                 this.normals3f[l * 4 + 1].set(vector3f1);
238                 this.normals3f[l * 4 + 2].set(vector3f1);
239                 this.normals3f[l * 4 + 3].set(vector3f1);
240             }
241         }
242     }
243 
244 	/***
245 	 * Construct a javax.media.j3d.GeometryArray object from quad data.
246 	 */
247 // 	protected void modelGeometry() {
248 // 		int size = this.x.length;
249 // 		this.coords3f = new Point3f[size];
250 // 		int format = GeometryArray.BY_REFERENCE | GeometryArray.COORDINATES | GeometryArray.NORMALS;
251 // 		if (this.color != null) {
252 // 			this.hasColors = true;
253 // 			if (this.alpha != null) {
254 // 				format |= GeometryArray.COLOR_4;
255 // 				this.colors4f = new Color4f[size];
256 // 				this.colors3f = null;
257 // 			} else {
258 // 				format |= GeometryArray.COLOR_3;
259 // 				this.colors4f = null;
260 // 				this.colors3f = new Color3f[size];
261 // 			}
262 // 		}
263 // 		if (this.tCoords != null) {
264 // 			this.hasTCoords = true;
265 // 			if (this.tCoords[0].length == 2) {
266 // 				format |= GeometryArray.TEXTURE_COORDINATE_2;
267 // 				this.texCoords2f = new TexCoord2f[size];
268 // 				this.texCoords3f = null;
269 // 			} else {
270 // 				format |= GeometryArray.TEXTURE_COORDINATE_3;
271 // 				this.texCoords2f = null;
272 // 				this.texCoords3f = new TexCoord3f[size];
273 // 			}
274 // 		}
275 
276 // 		QuadArray geom = new QuadArray(this.x.length, format);
277 // 		for (int i = 0; i < size; i++) {
278 // 			this.coords3f[i] = new Point3f((float) this.x[i], (float) this.y[i], (float) this.z[i]);
279 // 			if (this.hasColors) {
280 // 				double[] tmp = this.lookupTable.mapValue(this.color[i]);
281 // 				if (this.colors3f != null) {
282 // 					this.colors3f[i] = new Color3f((float) tmp[0], (float) tmp[1], (float) tmp[2]);
283 // 				} else {
284 // 					this.colors4f[i] = new Color4f((float) tmp[0], (float) tmp[1], (float) tmp[2], (float) this.alpha[i]);
285 // 				}
286 // 			}
287 // 			if (this.hasTCoords) {
288 // 				if (this.texCoords2f != null) {
289 // 					this.texCoords2f[i] = new TexCoord2f((float) this.tCoords[i][0], (float) this.tCoords[i][1]);
290 // 				} else {
291 // 					this.texCoords3f[i] = new TexCoord3f((float) this.tCoords[i][0], (float) this.tCoords[i][1], (float) this.tCoords[i][2]);
292 // 				}
293 // 			}
294 // 		}
295 // 		calculateNormals();
296 
297 // 		geom.setCoordRef3f(this.coords3f);
298 // 		geom.setNormalRef3f(this.normals3f);
299 // 		if (this.hasColors) {
300 // 			if (this.colors3f != null) {
301 // 				geom.setColorRef3f(this.colors3f);
302 // 			} else {
303 // 				geom.setColorRef4f(this.colors4f);
304 // 			}
305 // 		}
306 // 		if (this.hasTCoords) {
307 // 			if (this.texCoords2f != null) {
308 // 				geom.setTexCoordRef2f(0, this.texCoords2f);
309 // 			} else {
310 // 				geom.setTexCoordRef3f(0, this.texCoords3f);
311 // 			}
312 // 		}
313 
314 // 		// register this new geometry.
315 // 		setGeometry(geom);
316 // 	}
317 
318 	protected void modelGeometry() {
319 		setPrimitive(GeometryInfo.POLYGON_ARRAY);		
320 		setupStripCounts();
321 		super.modelGeometry();
322 	}
323 
324 	protected void setupStripCounts() {
325 		int quadNum = this.x.length / 4;
326 		int[] stripcounts = new int[quadNum];
327 		for (int i = 0; i < quadNum; i++) {
328 			stripcounts[i] = 4;
329 		}
330 		setStripCounts(stripcounts);
331 	}
332 
333 	public void test() {
334 		double[] x = {0.0, 3.0, 2.0, 0.0, 1.0, 2.0, 2.0, 4.0, 7.0, 7.0, 4.0, 3.0, 5.0, 6.0, 6.0, 5.0};
335 		double[] y = {0.0, 0.0, 3.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0, 5.0, 5.0, 4.0, 2.0, 3.0, 4.0, 4.0};
336 		double[] z = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
337 		double[] color = {0.0, 0.2, 0.4, 0.6, 0.8, 0.8, 0.8, 0.0, 0.2, 0.4, 0.6, 0.8, 0.8, 0.8, 1.0, 1.0};
338 
339 		setXCoordinates(x);
340 		setYCoordinates(y);
341 		setZCoordinates(z);
342 		setColors(color);
343 	}
344 }
345