001: /*
002: * $RCSfile: GeometryInfoGenerator.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:19 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.geometry;
046:
047: import javax.media.j3d.GeometryArray;
048: import javax.media.j3d.GeometryStripArray;
049: import javax.media.j3d.TriangleFanArray;
050: import javax.media.j3d.TriangleStripArray;
051: import javax.media.j3d.TriangleArray;
052: import javax.media.j3d.QuadArray;
053: import javax.media.j3d.IndexedGeometryArray;
054: import javax.media.j3d.IndexedGeometryStripArray;
055: import javax.media.j3d.IndexedQuadArray;
056: import javax.media.j3d.IndexedTriangleArray;
057: import javax.media.j3d.IndexedTriangleFanArray;
058: import javax.media.j3d.IndexedTriangleStripArray;
059: import javax.vecmath.*;
060: import com.sun.j3d.utils.geometry.GeometryInfo;
061: import com.sun.j3d.internal.J3dUtilsI18N;
062: import com.sun.j3d.internal.BufferWrapper;
063: import com.sun.j3d.internal.ByteBufferWrapper;
064: import com.sun.j3d.internal.FloatBufferWrapper;
065: import com.sun.j3d.internal.DoubleBufferWrapper;
066: import javax.media.j3d.J3DBuffer;
067:
068: /**
069: * Populate a GeometryInfo object from the Geometry provided. Used
070: * by GeometryInfo.
071: */
072: class GeometryInfoGenerator extends Object {
073:
074: public static void create(GeometryInfo geomInfo,
075: GeometryArray geomArray) {
076: if (geomArray instanceof GeometryStripArray)
077: create(geomInfo, (GeometryStripArray) geomArray);
078: else if (geomArray instanceof TriangleArray) {
079: geomInfo.reset(GeometryInfo.TRIANGLE_ARRAY);
080: processGeometryArray(geomInfo, geomArray);
081: } else if (geomArray instanceof QuadArray) {
082: geomInfo.reset(GeometryInfo.QUAD_ARRAY);
083: processGeometryArray(geomInfo, geomArray);
084: } else if (geomArray instanceof IndexedGeometryArray)
085: create(geomInfo, (IndexedGeometryArray) geomArray);
086: else
087: throw new IllegalArgumentException(J3dUtilsI18N
088: .getString("GeometryInfoGenerator0"));
089: } // End of create(GeometryInfo, GeometryArray)
090:
091: private static void create(GeometryInfo geomInfo,
092: GeometryStripArray geomArray) {
093: if (geomArray instanceof TriangleFanArray) {
094: geomInfo.reset(GeometryInfo.TRIANGLE_FAN_ARRAY);
095: } else if (geomArray instanceof TriangleStripArray) {
096: geomInfo.reset(GeometryInfo.TRIANGLE_STRIP_ARRAY);
097: } else
098: throw new IllegalArgumentException(J3dUtilsI18N
099: .getString("GeometryInfoGenerator0"));
100:
101: processGeometryArray(geomInfo, geomArray);
102: processStripArray(geomInfo, geomArray);
103: } // End of create(GeometryInfo, GeometryStripArray)
104:
105: private static void create(GeometryInfo geomInfo,
106: IndexedGeometryArray geomArray) {
107: if (geomArray instanceof IndexedQuadArray) {
108: geomInfo.reset(GeometryInfo.QUAD_ARRAY);
109: } else if (geomArray instanceof IndexedTriangleArray) {
110: geomInfo.reset(GeometryInfo.TRIANGLE_ARRAY);
111: } else if (geomArray instanceof IndexedTriangleFanArray) {
112: geomInfo.reset(GeometryInfo.TRIANGLE_FAN_ARRAY);
113: processIndexStripArray(geomInfo,
114: (IndexedGeometryStripArray) geomArray);
115: } else if (geomArray instanceof IndexedTriangleStripArray) {
116: geomInfo.reset(GeometryInfo.TRIANGLE_STRIP_ARRAY);
117: processIndexStripArray(geomInfo,
118: (IndexedGeometryStripArray) geomArray);
119: }
120:
121: processGeometryArray(geomInfo, geomArray);
122: processIndexedArray(geomInfo, geomArray);
123: } // End of create(GeometryInfo, IndexedGeometryArray)
124:
125: private static void processGeometryArray(GeometryInfo geomInfo,
126: GeometryArray geomArray) {
127: int i, j;
128: int vertexFormat = geomArray.getVertexFormat();
129: int texSets = geomArray.getTexCoordSetCount();
130: int valid;
131:
132: // Calculate validVertexCount
133: if (geomArray instanceof GeometryStripArray) {
134: // Does not include IndexedGeometryStripArray
135: GeometryStripArray gsa = (GeometryStripArray) geomArray;
136: int[] strips = new int[gsa.getNumStrips()];
137: gsa.getStripVertexCounts(strips);
138: valid = 0;
139: for (i = 0; i < strips.length; i++) {
140: valid += strips[i];
141: }
142: } else if (geomArray instanceof IndexedGeometryArray) {
143: valid = geomArray.getVertexCount();
144: } else
145: valid = geomArray.getValidVertexCount();
146:
147: if ((vertexFormat & GeometryArray.INTERLEAVED) != 0) {
148:
149: // Calculate words_per_vertex (wpv)
150: int wpv = 3; // Always have coordinate data
151: if ((vertexFormat & GeometryArray.NORMALS) != 0)
152: wpv += 3;
153: if ((vertexFormat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4)
154: wpv += 4;
155: else if ((vertexFormat & GeometryArray.COLOR_3) != 0)
156: wpv += 3;
157: if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0)
158: wpv += 2 * texSets;
159: else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_3) != 0)
160: wpv += 3 * texSets;
161: else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_4) != 0)
162: wpv += 4 * texSets;
163:
164: int initial;
165: if (!(geomArray instanceof IndexedGeometryArray)) {
166: initial = geomArray.getInitialVertexIndex();
167: } else
168: initial = 0;
169:
170: float[] d;
171: if ((vertexFormat & GeometryArray.USE_NIO_BUFFER) != 0) {
172: J3DBuffer b = geomArray.getInterleavedVertexBuffer();
173: FloatBufferWrapper w = new FloatBufferWrapper(b);
174: d = new float[w.limit()];
175: w.position(0);
176: w.get(d);
177: } else
178: d = geomArray.getInterleavedVertices();
179:
180: int offset = 0;
181: if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0) {
182: geomInfo.setTextureCoordinateParams(texSets, 2);
183: int[] map = new int[geomArray.getTexCoordSetMapLength()];
184: geomArray.getTexCoordSetMap(map);
185: geomInfo.setTexCoordSetMap(map);
186: for (i = 0; i < texSets; i++) {
187: TexCoord2f[] tex = new TexCoord2f[valid];
188: for (j = 0; j < valid; j++) {
189: tex[j] = new TexCoord2f(d[wpv * (j + initial)
190: + offset], d[wpv * (j + initial)
191: + offset + 1]);
192: }
193: geomInfo.setTextureCoordinates(i, tex);
194: offset += 2;
195: }
196: } else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_3) != 0) {
197: geomInfo.setTextureCoordinateParams(texSets, 3);
198: int[] map = new int[geomArray.getTexCoordSetMapLength()];
199: geomArray.getTexCoordSetMap(map);
200: geomInfo.setTexCoordSetMap(map);
201: for (i = 0; i < texSets; i++) {
202: TexCoord3f[] tex = new TexCoord3f[valid];
203: for (j = 0; j < valid; j++) {
204: tex[j] = new TexCoord3f(d[wpv * (j + initial)
205: + offset], d[wpv * (j + initial)
206: + offset + 1], d[wpv * (j + initial)
207: + offset + 2]);
208: }
209: geomInfo.setTextureCoordinates(i, tex);
210: offset += 3;
211: }
212: } else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_4) != 0) {
213: geomInfo.setTextureCoordinateParams(texSets, 4);
214: int[] map = new int[geomArray.getTexCoordSetMapLength()];
215: geomArray.getTexCoordSetMap(map);
216: geomInfo.setTexCoordSetMap(map);
217: for (i = 0; i < texSets; i++) {
218: TexCoord4f[] tex = new TexCoord4f[valid];
219: for (j = 0; j < valid; j++) {
220: tex[j] = new TexCoord4f(d[wpv * (j + initial)
221: + offset], d[wpv * (j + initial)
222: + offset + 1], d[wpv * (j + initial)
223: + offset + 2], d[wpv * (j + initial)
224: + offset + 3]);
225: }
226: geomInfo.setTextureCoordinates(i, tex);
227: offset += 4;
228: }
229: }
230:
231: if ((vertexFormat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) {
232: Color4f[] color = new Color4f[valid];
233: for (i = 0; i < valid; i++) {
234: color[i] = new Color4f(d[wpv * (i + initial)
235: + offset], d[wpv * (i + initial) + offset
236: + 1], d[wpv * (i + initial) + offset + 2],
237: d[wpv * (i + initial) + offset + 3]);
238: }
239: geomInfo.setColors(color);
240: offset += 4;
241: } else if ((vertexFormat & GeometryArray.COLOR_3) != 0) {
242: Color3f[] color = new Color3f[valid];
243: for (i = 0; i < valid; i++) {
244: color[i] = new Color3f(d[wpv * (i + initial)
245: + offset], d[wpv * (i + initial) + offset
246: + 1], d[wpv * (i + initial) + offset + 2]);
247: }
248: geomInfo.setColors(color);
249: offset += 3;
250: }
251:
252: if ((vertexFormat & GeometryArray.NORMALS) != 0) {
253: Vector3f[] normals = new Vector3f[valid];
254: for (i = 0; i < valid; i++) {
255: normals[i] = new Vector3f(d[wpv * (i + initial)
256: + offset], d[wpv * (i + initial) + offset
257: + 1], d[wpv * (i + initial) + offset + 2]);
258: }
259: geomInfo.setNormals(normals);
260: offset += 3;
261: }
262:
263: Point3f[] coords = new Point3f[valid];
264: for (i = 0; i < valid; i++) {
265: coords[i] = new Point3f(
266: d[wpv * (i + initial) + offset], d[wpv
267: * (i + initial) + offset + 1], d[wpv
268: * (i + initial) + offset + 2]);
269: }
270: geomInfo.setCoordinates(coords);
271: } else {
272: // Data is not INTERLEAVED
273: boolean byRef = ((vertexFormat & GeometryArray.BY_REFERENCE) != 0);
274: boolean nio = ((vertexFormat & GeometryArray.USE_NIO_BUFFER) != 0);
275:
276: Point3f[] coords = null;
277: if (byRef) {
278:
279: int initial;
280: if (!(geomArray instanceof IndexedGeometryArray)) {
281: initial = geomArray.getInitialCoordIndex();
282: } else
283: initial = 0;
284:
285: if (nio) {
286: J3DBuffer buf = geomArray.getCoordRefBuffer();
287:
288: switch (BufferWrapper.getBufferType(buf)) {
289:
290: case BufferWrapper.TYPE_FLOAT: {
291: FloatBufferWrapper bb = new FloatBufferWrapper(
292: buf);
293: float[] c = new float[valid * 3];
294: bb.position(initial * 3);
295: bb.get(c, 0, valid * 3);
296: coords = new Point3f[valid];
297: for (i = 0; i < valid; i++) {
298: coords[i] = new Point3f(c[i * 3 + 0],
299: c[i * 3 + 1], c[i * 3 + 2]);
300: }
301: }
302: break;
303:
304: case BufferWrapper.TYPE_DOUBLE: {
305: DoubleBufferWrapper bb = new DoubleBufferWrapper(
306: buf);
307: double[] c = new double[valid * 3];
308: bb.position(initial * 3);
309: bb.get(c, 0, valid * 3);
310: coords = new Point3f[valid];
311: for (i = 0; i < valid; i++) {
312: coords[i] = new Point3f(
313: (float) (c[i * 3 + 0]),
314: (float) (c[i * 3 + 1]),
315: (float) (c[i * 3 + 2]));
316: }
317: }
318: break;
319: }
320: } else if (geomArray.getCoordRef3f() != null) {
321: if (initial != 0) {
322: Point3f[] c = geomArray.getCoordRef3f();
323: coords = new Point3f[valid];
324: for (i = 0; i < valid; i++) {
325: coords[i] = new Point3f(c[i + initial]);
326: }
327: } else
328: coords = geomArray.getCoordRef3f();
329: } else if (geomArray.getCoordRef3d() != null) {
330: Point3d[] c = geomArray.getCoordRef3d();
331: coords = new Point3f[valid];
332: for (i = 0; i < valid; i++) {
333: coords[i] = new Point3f(c[i + initial]);
334: }
335: } else if (geomArray.getCoordRefFloat() != null) {
336: float[] c = geomArray.getCoordRefFloat();
337: coords = new Point3f[valid];
338: for (i = 0; i < valid; i++) {
339: coords[i] = new Point3f(c[(i + initial) * 3],
340: c[(i + initial) * 3 + 1],
341: c[(i + initial) * 3 + 2]);
342: }
343: } else if (geomArray.getCoordRefDouble() != null) {
344: double[] c = geomArray.getCoordRefDouble();
345: coords = new Point3f[valid];
346: for (i = 0; i < valid; i++) {
347: coords[i] = new Point3f(
348: (float) (c[(i + initial) * 3]),
349: (float) (c[(i + initial) * 3 + 1]),
350: (float) (c[(i + initial) * 3 + 2]));
351: }
352: }
353: // No coordinate data - let GeometryInfo handle this.
354: } else {
355: // Not BY_REFERENCE
356: int initial;
357: if (!(geomArray instanceof IndexedGeometryArray)) {
358: initial = geomArray.getInitialVertexIndex();
359: } else
360: initial = 0;
361: coords = new Point3f[valid];
362: for (i = 0; i < valid; i++)
363: coords[i] = new Point3f();
364: geomArray.getCoordinates(initial, coords);
365: }
366: geomInfo.setCoordinates(coords);
367:
368: if ((vertexFormat & GeometryArray.NORMALS) != 0) {
369: Vector3f[] normals = null;
370: if (byRef) {
371:
372: int initial;
373: if (!(geomArray instanceof IndexedGeometryArray)) {
374: initial = geomArray.getInitialNormalIndex();
375: } else
376: initial = 0;
377:
378: if (nio) {
379: J3DBuffer buf = geomArray.getNormalRefBuffer();
380:
381: if (BufferWrapper.getBufferType(buf) == BufferWrapper.TYPE_FLOAT) {
382: FloatBufferWrapper bb = new FloatBufferWrapper(
383: buf);
384: float[] c = new float[valid * 3];
385: bb.position(initial * 3);
386: bb.get(c, 0, valid * 3);
387: normals = new Vector3f[valid];
388: for (i = 0; i < valid; i++) {
389: normals[i] = new Vector3f(c[i * 3 + 0],
390: c[i * 3 + 1], c[i * 3 + 2]);
391: }
392: }
393: // Normals were set in vertexFormat but none were set - OK
394: } else if (geomArray.getNormalRef3f() != null) {
395: if (initial != 0) {
396: Vector3f[] n = geomArray.getNormalRef3f();
397: normals = new Vector3f[valid];
398: for (i = 0; i < valid; i++) {
399: normals[i] = new Vector3f(
400: n[i + initial]);
401: }
402: } else
403: normals = geomArray.getNormalRef3f();
404: } else if (geomArray.getNormalRefFloat() != null) {
405: float[] n = geomArray.getNormalRefFloat();
406: normals = new Vector3f[valid];
407: for (i = 0; i < valid; i++) {
408: normals[i] = new Vector3f(
409: n[(i + initial) * 3],
410: n[(i + initial) * 3 + 1],
411: n[(i + initial) * 3 + 2]);
412: }
413: }
414: // Normals were set in vertexFormat but none were set - OK
415: } else {
416: // Not BY_REFERENCE
417: int initial;
418: if (!(geomArray instanceof IndexedGeometryArray)) {
419: initial = geomArray.getInitialVertexIndex();
420: } else
421: initial = 0;
422: normals = new Vector3f[valid];
423: for (i = 0; i < valid; i++)
424: normals[i] = new Vector3f();
425: geomArray.getNormals(initial, normals);
426: }
427: geomInfo.setNormals(normals);
428: }
429:
430: if ((vertexFormat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) {
431: Color4f[] colors = null;
432: if (byRef) {
433:
434: int initial;
435: if (!(geomArray instanceof IndexedGeometryArray)) {
436: initial = geomArray.getInitialColorIndex();
437: } else
438: initial = 0;
439:
440: if (nio) {
441: J3DBuffer buf = geomArray.getColorRefBuffer();
442:
443: switch (BufferWrapper.getBufferType(buf)) {
444:
445: case BufferWrapper.TYPE_FLOAT: {
446: FloatBufferWrapper bb = new FloatBufferWrapper(
447: buf);
448: float[] c = new float[valid * 4];
449: bb.position(initial * 4);
450: bb.get(c, 0, valid * 4);
451: colors = new Color4f[valid];
452: for (i = 0; i < valid; i++) {
453: colors[i] = new Color4f(c[i * 4 + 0],
454: c[i * 4 + 1], c[i * 4 + 2],
455: c[i * 4 + 3]);
456: }
457: }
458: break;
459:
460: case BufferWrapper.TYPE_BYTE: {
461: ByteBufferWrapper bb = new ByteBufferWrapper(
462: buf);
463: byte[] c = new byte[valid * 4];
464: bb.position(initial * 4);
465: bb.get(c, 0, valid * 4);
466: colors = new Color4f[valid];
467: for (i = 0; i < valid; i++) {
468: colors[i] = new Color4f(
469: (float) (c[i * 4 + 0] & 0xff) / 255.0f,
470: (float) (c[i * 4 + 1] & 0xff) / 255.0f,
471: (float) (c[i * 4 + 2] & 0xff) / 255.0f,
472: (float) (c[i * 4 + 3] & 0xff) / 255.0f);
473: }
474: }
475: break;
476: }
477: } else if (geomArray.getColorRef4f() != null) {
478: if (initial != 0) {
479: Color4f[] c = geomArray.getColorRef4f();
480: colors = new Color4f[valid];
481: for (i = 0; i < valid; i++) {
482: colors[i] = new Color4f(c[i + initial]);
483: }
484: } else
485: colors = geomArray.getColorRef4f();
486: } else if (geomArray.getColorRefFloat() != null) {
487: float[] c = geomArray.getColorRefFloat();
488: colors = new Color4f[valid];
489: for (i = 0; i < valid; i++) {
490: colors[i] = new Color4f(
491: c[(i + initial) * 4 + 0],
492: c[(i + initial) * 4 + 1],
493: c[(i + initial) * 4 + 2],
494: c[(i + initial) * 4 + 3]);
495: }
496: } else if (geomArray.getColorRefByte() != null) {
497: byte[] c = geomArray.getColorRefByte();
498: colors = new Color4f[valid];
499: for (i = 0; i < valid; i++) {
500: colors[i] = new Color4f(
501: (float) (c[(i + initial) * 4 + 0] & 0xff) / 255.0f,
502: (float) (c[(i + initial) * 4 + 1] & 0xff) / 255.0f,
503: (float) (c[(i + initial) * 4 + 2] & 0xff) / 255.0f,
504: (float) (c[(i + initial) * 4 + 3] & 0xff) / 255.0f);
505: }
506: } else if (geomArray.getColorRef4b() != null) {
507: Color4b[] c = geomArray.getColorRef4b();
508: colors = new Color4f[valid];
509: for (i = 0; i < valid; i++) {
510: colors[i] = new Color4f(
511: (float) (c[i + initial].x & 0xff) / 255.0f,
512: (float) (c[i + initial].y & 0xff) / 255.0f,
513: (float) (c[i + initial].z & 0xff) / 255.0f,
514: (float) (c[i + initial].w & 0xff) / 255.0f);
515: }
516: }
517: // Colors4 were set in vertexFormat but none were set - OK
518: } else {
519: // Not BY_REFERENCE
520: int initial;
521: if (!(geomArray instanceof IndexedGeometryArray)) {
522: initial = geomArray.getInitialVertexIndex();
523: } else
524: initial = 0;
525: colors = new Color4f[valid];
526: for (i = 0; i < valid; i++)
527: colors[i] = new Color4f();
528: geomArray.getColors(initial, colors);
529: }
530: geomInfo.setColors(colors);
531: } else if ((vertexFormat & GeometryArray.COLOR_3) != 0) {
532: Color3f[] colors = null;
533: if (byRef) {
534:
535: int initial;
536: if (!(geomArray instanceof IndexedGeometryArray)) {
537: initial = geomArray.getInitialColorIndex();
538: } else
539: initial = 0;
540:
541: if (nio) {
542: J3DBuffer buf = geomArray.getColorRefBuffer();
543:
544: switch (BufferWrapper.getBufferType(buf)) {
545:
546: case BufferWrapper.TYPE_FLOAT: {
547: FloatBufferWrapper bb = new FloatBufferWrapper(
548: buf);
549: float[] c = new float[valid * 3];
550: bb.position(initial * 3);
551: bb.get(c, 0, valid * 3);
552: colors = new Color3f[valid];
553: for (i = 0; i < valid; i++) {
554: colors[i] = new Color3f(c[i * 3 + 0],
555: c[i * 3 + 1], c[i * 3 + 2]);
556: }
557: }
558: break;
559:
560: case BufferWrapper.TYPE_BYTE: {
561: ByteBufferWrapper bb = new ByteBufferWrapper(
562: buf);
563: byte[] c = new byte[valid * 3];
564: bb.position(initial * 3);
565: bb.get(c, 0, valid * 3);
566: colors = new Color3f[valid];
567: for (i = 0; i < valid; i++) {
568: colors[i] = new Color3f(
569: (float) (c[i * 3 + 0] & 0xff) / 255.0f,
570: (float) (c[i * 3 + 1] & 0xff) / 255.0f,
571: (float) (c[i * 3 + 2] & 0xff) / 255.0f);
572: }
573: }
574: break;
575: }
576: } else if (geomArray.getColorRef3f() != null) {
577: if (initial != 0) {
578: Color3f[] c = geomArray.getColorRef3f();
579: colors = new Color3f[valid];
580: for (i = 0; i < valid; i++) {
581: colors[i] = new Color3f(c[i + initial]);
582: }
583: } else
584: colors = geomArray.getColorRef3f();
585: } else if (geomArray.getColorRefFloat() != null) {
586: float[] c = geomArray.getColorRefFloat();
587: colors = new Color3f[valid];
588: for (i = 0; i < valid; i++) {
589: colors[i] = new Color3f(
590: c[(i + initial) * 3 + 0],
591: c[(i + initial) * 3 + 1],
592: c[(i + initial) * 3 + 2]);
593: }
594: } else if (geomArray.getColorRefByte() != null) {
595: byte[] c = geomArray.getColorRefByte();
596: colors = new Color3f[valid];
597: for (i = 0; i < valid; i++) {
598: colors[i] = new Color3f(
599: (float) (c[(i + initial) * 3 + 0] & 0xff) / 255.0f,
600: (float) (c[(i + initial) * 3 + 1] & 0xff) / 255.0f,
601: (float) (c[(i + initial) * 3 + 2] & 0xff) / 255.0f);
602: }
603: } else if (geomArray.getColorRef3b() != null) {
604: Color3b[] c = geomArray.getColorRef3b();
605: colors = new Color3f[valid];
606: for (i = 0; i < valid; i++) {
607: colors[i] = new Color3f(
608: (float) (c[i + initial].x & 0xff) / 255.0f,
609: (float) (c[i + initial].y & 0xff) / 255.0f,
610: (float) (c[i + initial].z & 0xff) / 255.0f);
611: }
612: }
613: // Colors3 were set in vertexFormat but none were set - OK
614: } else {
615: // Not BY_REFERENCE
616: int initial;
617: if (!(geomArray instanceof IndexedGeometryArray)) {
618: initial = geomArray.getInitialVertexIndex();
619: } else
620: initial = 0;
621: colors = new Color3f[valid];
622: for (i = 0; i < valid; i++)
623: colors[i] = new Color3f();
624: geomArray.getColors(initial, colors);
625: }
626: geomInfo.setColors(colors);
627: }
628:
629: if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_4) != 0) {
630: geomInfo.setTextureCoordinateParams(texSets, 4);
631: for (i = 0; i < texSets; i++) {
632: TexCoord4f[] tex = null;
633: if (byRef) {
634:
635: int initial;
636: if (!(geomArray instanceof IndexedGeometryArray)) {
637: initial = geomArray
638: .getInitialTexCoordIndex(i);
639: } else
640: initial = 0;
641:
642: if (nio) {
643: J3DBuffer buf = geomArray
644: .getTexCoordRefBuffer(i);
645:
646: if (BufferWrapper.getBufferType(buf) == BufferWrapper.TYPE_FLOAT) {
647: FloatBufferWrapper bb = new FloatBufferWrapper(
648: buf);
649: float[] c = new float[valid * 4];
650: bb.position(initial * 4);
651: bb.get(c, 0, valid * 4);
652: tex = new TexCoord4f[valid];
653: for (j = 0; j < valid; j++) {
654: tex[j] = new TexCoord4f(
655: c[j * 4 + 0], c[j * 4 + 1],
656: c[j * 4 + 2], c[j * 4 + 3]);
657: }
658: }
659: // TexCoords4 were set in vertexFormat but none were set - OK
660: } else {
661: // There if no TexCoordRef4f, so we know it's float
662: float[] t = geomArray
663: .getTexCoordRefFloat(i);
664: tex = new TexCoord4f[valid];
665: for (j = 0; j < valid; j++) {
666: tex[j] = new TexCoord4f(
667: t[(j + initial) * 4],
668: t[(j + initial) * 4 + 1],
669: t[(j + initial) * 4 + 2],
670: t[(j + initial) * 4 + 3]);
671: }
672: }
673: } else {
674: // Not BY_REFERENCE
675: int initial;
676: if (!(geomArray instanceof IndexedGeometryArray)) {
677: initial = geomArray.getInitialVertexIndex();
678: } else
679: initial = 0;
680: tex = new TexCoord4f[valid];
681: for (j = 0; j < valid; j++)
682: tex[j] = new TexCoord4f();
683: geomArray
684: .getTextureCoordinates(i, initial, tex);
685: }
686: geomInfo.setTextureCoordinates(i, tex);
687: }
688: int[] map = new int[geomArray.getTexCoordSetMapLength()];
689: geomArray.getTexCoordSetMap(map);
690: geomInfo.setTexCoordSetMap(map);
691: } else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_3) != 0) {
692: geomInfo.setTextureCoordinateParams(texSets, 3);
693: for (i = 0; i < texSets; i++) {
694: TexCoord3f[] tex = null;
695: if (byRef) {
696:
697: int initial;
698: if (!(geomArray instanceof IndexedGeometryArray)) {
699: initial = geomArray
700: .getInitialTexCoordIndex(i);
701: } else
702: initial = 0;
703:
704: if (nio) {
705: J3DBuffer buf = geomArray
706: .getTexCoordRefBuffer(i);
707:
708: if (BufferWrapper.getBufferType(buf) == BufferWrapper.TYPE_FLOAT) {
709: FloatBufferWrapper bb = new FloatBufferWrapper(
710: buf);
711: float[] c = new float[valid * 3];
712: bb.position(initial * 3);
713: bb.get(c, 0, valid * 3);
714: tex = new TexCoord3f[valid];
715: for (j = 0; j < valid; j++) {
716: tex[j] = new TexCoord3f(
717: c[j * 3 + 0], c[j * 3 + 1],
718: c[j * 3 + 2]);
719: }
720: }
721: // TexCoords3 were set in vertexFormat but none were set - OK
722: } else if (geomArray.getTexCoordRef3f(i) != null) {
723: if (initial != 0) {
724: TexCoord3f[] t = geomArray
725: .getTexCoordRef3f(i);
726: tex = new TexCoord3f[valid];
727: for (j = 0; j < valid; j++) {
728: tex[j] = new TexCoord3f(t[j
729: + initial]);
730: }
731: } else
732: tex = geomArray.getTexCoordRef3f(i);
733: } else if (geomArray.getTexCoordRefFloat(i) != null) {
734: float[] t = geomArray
735: .getTexCoordRefFloat(i);
736: tex = new TexCoord3f[valid];
737: for (j = 0; j < valid; j++) {
738: tex[j] = new TexCoord3f(
739: t[(j + initial) * 3],
740: t[(j + initial) * 3 + 1],
741: t[(j + initial) * 3 + 2]);
742: }
743: }
744: // TexCoords3 were set in vertexFormat but none were set - OK
745: } else {
746: // Not BY_REFERENCE
747: int initial;
748: if (!(geomArray instanceof IndexedGeometryArray)) {
749: initial = geomArray.getInitialVertexIndex();
750: } else
751: initial = 0;
752: tex = new TexCoord3f[valid];
753: for (j = 0; j < valid; j++)
754: tex[j] = new TexCoord3f();
755: geomArray
756: .getTextureCoordinates(i, initial, tex);
757: }
758: geomInfo.setTextureCoordinates(i, tex);
759: }
760: int[] map = new int[geomArray.getTexCoordSetMapLength()];
761: geomArray.getTexCoordSetMap(map);
762: geomInfo.setTexCoordSetMap(map);
763: } else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0) {
764: geomInfo.setTextureCoordinateParams(texSets, 2);
765: for (i = 0; i < texSets; i++) {
766: TexCoord2f[] tex = null;
767: if (byRef) {
768:
769: int initial;
770: if (!(geomArray instanceof IndexedGeometryArray)) {
771: initial = geomArray
772: .getInitialTexCoordIndex(i);
773: } else
774: initial = 0;
775:
776: if (nio) {
777: J3DBuffer buf = geomArray
778: .getTexCoordRefBuffer(i);
779:
780: if (BufferWrapper.getBufferType(buf) == BufferWrapper.TYPE_FLOAT) {
781: FloatBufferWrapper bb = new FloatBufferWrapper(
782: buf);
783: float[] c = new float[valid * 2];
784: bb.position(initial * 2);
785: bb.get(c, 0, valid * 2);
786: tex = new TexCoord2f[valid];
787: for (j = 0; j < valid; j++) {
788: tex[j] = new TexCoord2f(
789: c[j * 2 + 0], c[j * 2 + 1]);
790: }
791: }
792: // TexCoords2 were set in vertexFormat but none were set - OK
793: } else if (geomArray.getTexCoordRefFloat(i) != null) {
794: float[] t = geomArray
795: .getTexCoordRefFloat(i);
796: tex = new TexCoord2f[valid];
797: for (j = 0; j < valid; j++) {
798: tex[j] = new TexCoord2f(
799: t[(j + initial) * 2 + 0],
800: t[(j + initial) * 2 + 1]);
801: }
802: } else if (geomArray.getTexCoordRef2f(i) != null) {
803: if (initial != 0) {
804: TexCoord2f[] t = geomArray
805: .getTexCoordRef2f(i);
806: tex = new TexCoord2f[valid];
807: for (j = 0; j < valid; j++) {
808: tex[j] = new TexCoord2f(t[j
809: + initial]);
810: }
811: } else
812: tex = geomArray.getTexCoordRef2f(i);
813: }
814: // TexCoords2 were set in vertexFormat but none were set - OK
815: } else {
816: // Not BY_REFERENCE
817: int initial;
818: if (!(geomArray instanceof IndexedGeometryArray)) {
819: initial = geomArray.getInitialVertexIndex();
820: } else
821: initial = 0;
822: tex = new TexCoord2f[valid];
823: for (j = 0; j < valid; j++)
824: tex[j] = new TexCoord2f();
825: geomArray
826: .getTextureCoordinates(i, initial, tex);
827: }
828: geomInfo.setTextureCoordinates(i, tex);
829: }
830: int[] map = new int[geomArray.getTexCoordSetMapLength()];
831: geomArray.getTexCoordSetMap(map);
832: geomInfo.setTexCoordSetMap(map);
833: }
834: }
835: } // End of processGeometryArray
836:
837: private static void processIndexedArray(GeometryInfo geomInfo,
838: IndexedGeometryArray geomArray) {
839: int initial = geomArray.getInitialIndexIndex();
840: int vertexFormat = geomArray.getVertexFormat();
841: int texSets = geomArray.getTexCoordSetCount();
842:
843: int valid;
844: if (geomArray instanceof IndexedGeometryStripArray) {
845: IndexedGeometryStripArray igsa = (IndexedGeometryStripArray) geomArray;
846: int[] strips = new int[igsa.getNumStrips()];
847: igsa.getStripIndexCounts(strips);
848: valid = 0;
849: for (int i = 0; i < strips.length; i++) {
850: valid += strips[i];
851: }
852: } else {
853: valid = geomArray.getValidIndexCount();
854: }
855:
856: int[] coordI = new int[valid];
857: geomArray.getCoordinateIndices(initial, coordI);
858: geomInfo.setCoordinateIndices(coordI);
859:
860: if ((vertexFormat & GeometryArray.USE_COORD_INDEX_ONLY) != 0) {
861: if ((vertexFormat & GeometryArray.NORMALS) != 0)
862: geomInfo.setNormalIndices(coordI);
863: if (((vertexFormat & GeometryArray.COLOR_3) != 0)
864: || ((vertexFormat & GeometryArray.COLOR_4) != 0))
865: geomInfo.setColorIndices(coordI);
866: if (((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0)
867: || ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_3) != 0)
868: || ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_4) != 0)) {
869: for (int i = 0; i < texSets; i++) {
870: geomInfo.setTextureCoordinateIndices(i, coordI);
871: }
872: }
873: } else {
874: if ((vertexFormat & GeometryArray.NORMALS) != 0) {
875: int[] normalI = new int[valid];
876: geomArray.getNormalIndices(initial, normalI);
877: geomInfo.setNormalIndices(normalI);
878: }
879:
880: if (((vertexFormat & GeometryArray.COLOR_3) != 0)
881: || ((vertexFormat & GeometryArray.COLOR_4) != 0)) {
882: int[] colorI = new int[valid];
883: geomArray.getColorIndices(initial, colorI);
884: geomInfo.setColorIndices(colorI);
885: }
886:
887: if (((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0)
888: || ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_3) != 0)
889: || ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_4) != 0)) {
890: for (int i = 0; i < texSets; i++) {
891: int[] texI = new int[valid];
892: geomArray.getTextureCoordinateIndices(i, initial,
893: texI);
894: geomInfo.setTextureCoordinateIndices(i, texI);
895: }
896: }
897: }
898: } // End of processIndexedArray
899:
900: private static void processStripArray(GeometryInfo geomInfo,
901: GeometryStripArray geomArray) {
902: int[] strips = new int[geomArray.getNumStrips()];
903: geomArray.getStripVertexCounts(strips);
904: geomInfo.setStripCounts(strips);
905: } // End of processStripArray
906:
907: private static void processIndexStripArray(GeometryInfo geomInfo,
908: IndexedGeometryStripArray geomArray) {
909: int[] strips = new int[geomArray.getNumStrips()];
910: geomArray.getStripIndexCounts(strips);
911: geomInfo.setStripCounts(strips);
912: } // End of processIndexStripArray
913:
914: } // End of class GeometryInfoGenerator
|