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.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
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;
51 public static final int YC = 1;
52 public static final int ZC = 2;
53 public static final int CC = 3;
54 public static final int OP = 4;
55 public static final int NC = 5;
56 public static final int TC = 6;
57 public static final int AC = 7;
58
59
60
61
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
117
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
212
213
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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