001: /*
002: * $RCSfile: ShapeHolder.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:09 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import java.util.Vector;
048: import javax.vecmath.Vector3f;
049:
050: /**
051: * This class holds all of the vertex/facet/normal/surface-reference
052: * data for a particular object. It has utilities to calculate facet normals,
053: * but this is no longer in use since using the new GeomInfo utilities.
054: */
055:
056: class ShapeHolder extends ParserObject {
057:
058: Vector facetSizesList;
059: Vector facetIndicesList;
060: int facetIndicesArray[];
061: int currentNumIndices = 0;
062: int numSurf;
063: int numVerts;
064: int facetIndices[];
065: int facetSizes[];
066: int normalIndices[];
067: float normalCoords[];
068: float coordsArray[];
069:
070: ShapeHolder() {
071: }
072:
073: ShapeHolder(int debugVals) {
074: super (debugVals);
075: }
076:
077: /**
078: * Print out (to stdout) the geometry data (coords, indices,
079: * and facet sizes). This is a debugging utility.
080: */
081: void printGeometryData(LwoSurface surface) {
082: int i, j;
083: int indicesIndex = 0;
084: System.out.println("\nPolygon Data:");
085: System.out.println(" Surface color = " + surface.color);
086: System.out.println(" Surface diffuse = "
087: + surface.diffuseColor);
088: for (i = 0; i < facetSizes.length; ++i) {
089: int polySize = facetSizes[i];
090: System.out.println("Facet of size " + polySize);
091: for (j = 0; j < polySize; ++j) {
092: int coordIndex = 3 * facetIndices[indicesIndex++];
093: System.out.println("x, y, z = "
094: + coordsArray[coordIndex] + ", "
095: + coordsArray[coordIndex + 1] + ", "
096: + coordsArray[coordIndex + 2]);
097: }
098: }
099: }
100:
101: /**
102: * Constructs geometry arrays given a winding rule (it turns out that
103: * lw3d winding is opposite of j3d winding, so this is always set to
104: * true in J3dLwoParser)
105: */
106: void createArrays(boolean reverseWinding) {
107: debugOutputLn(TRACE, "createArrays()");
108: // debugOutputLn(VALUES, "facetIndices, faceSizesList = " +
109: // facetIndicesList + facetSizesList);
110: //debugOutputLn(VALUES, "ind and sizes size " +
111: // facetIndicesList.size() + ", " +
112: // facetSizesList.size());
113: //facetIndices =
114: // new int[facetIndicesList.size()];
115: facetIndices = new int[currentNumIndices];
116: if (reverseWinding) {
117: int facetBeginIndex = 0;
118: for (int facetIndex = 0; facetIndex < facetSizesList.size(); ++facetIndex) {
119: int currFaceSize = ((Integer) facetSizesList
120: .elementAt(facetIndex)).intValue();
121: int tempFace[] = new int[currFaceSize];
122: for (int j = 0; j < currFaceSize; ++j) {
123: facetIndices[facetBeginIndex + j] = facetIndicesArray[facetBeginIndex
124: + currFaceSize - j - 1];
125: }
126: facetBeginIndex += currFaceSize;
127: }
128:
129: } else {
130: for (int i = 0; i < facetIndices.length; ++i) {
131: facetIndices[i] = facetIndicesArray[i];
132: }
133: }
134:
135: debugOutputLn(LINE_TRACE,
136: "facetIndices.len and coordsArray.len = "
137: + facetIndices.length + ", "
138: + coordsArray.length);
139: if (((Integer) facetSizesList.elementAt(0)).intValue() < 3) {
140: // if we're dealing with point/line primitives, then let's abandon
141: // the indexed route and simply construct a new coordsArray
142: // that holds the direct values we need for a GeometryArray
143: // object
144: debugOutputLn(LINE_TRACE, "Using direct geometry because "
145: + "facetIndices is of size " + facetIndices.length
146: + " and coordsArray is of length "
147: + coordsArray.length);
148: float newCoordsArray[] = new float[facetIndices.length * 3];
149: int newCoordsIndex = 0;
150: for (int i = 0; i < facetIndices.length; ++i) {
151: newCoordsArray[newCoordsIndex++] = coordsArray[facetIndices[i] * 3];
152: newCoordsArray[newCoordsIndex++] = coordsArray[facetIndices[i] * 3 + 1];
153: newCoordsArray[newCoordsIndex++] = coordsArray[facetIndices[i] * 3 + 2];
154: }
155: coordsArray = newCoordsArray;
156: facetIndices = null;
157: }
158:
159: facetSizes = new int[facetSizesList.size()];
160: for (int i = 0; i < facetSizes.length; ++i) {
161: facetSizes[i] = ((Integer) facetSizesList.elementAt(i))
162: .intValue();
163: }
164:
165: facetSizesList = null; // Force garbage collection on Vectors
166: facetIndicesList = null;
167: facetIndicesArray = null;
168: }
169:
170: /**
171: * Force gc on all array objects
172: */
173: void nullify() {
174: facetSizesList = null; // Force garbage collection on everything
175: facetIndicesList = null;
176: facetIndicesArray = null;
177: facetSizes = null;
178: facetIndices = null;
179: normalCoords = null;
180: normalIndices = null;
181: }
182:
183: /**
184: * This method calculates facet normals for the geometry. It is no
185: * longer used, as we're now using the GeometryInfo utility to calculate
186: * smooth normals
187: */
188: void calcNormals() {
189: debugOutputLn(TRACE, "calcNormals()");
190: debugOutputLn(LINE_TRACE, "coordsLength, facetsizes.len = "
191: + coordsArray.length + ", " + facetSizes.length);
192: if (facetSizes[0] > 2) {
193: // points and lines don't need normals, polys do
194: if (facetIndices != null) {
195: normalIndices = new int[facetIndices.length];
196: normalCoords = new float[facetIndices.length * 3];
197: } else {
198: normalCoords = new float[coordsArray.length];
199: }
200: debugOutputLn(LINE_TRACE, "normalCoords, incides len = "
201: + normalCoords.length
202: + ", "
203: + ((facetIndices == null) ? 0
204: : normalIndices.length));
205: int facetIndex = 0;
206: int tempIndex = -1;
207: for (int i = 0; i < facetSizes.length; i += 1) {
208: Vector3f norm;
209: int currFacetSize = facetSizes[i];
210: //debugOutputLn(LINE_TRACE, " i, facetIndex, currSize = " +
211: // i + ", " + facetIndex + ", " + currFacetSize);
212: if (currFacetSize < 3) {
213: // This shouldn't occur
214: norm = new Vector3f(0f, 0f, 1f);
215: } else {
216: Vector3f v1, v2;
217: int index1, index2, index3;
218: if (facetIndices != null) {
219: index1 = facetIndices[facetIndex];
220: index2 = facetIndices[facetIndex + 1];
221: index3 = facetIndices[facetIndex + 2];
222: //debugOutputLn(VALUES, " index123 = " +
223: // index1 + ", " + index2 + ", " + index3);
224: } else {
225: index1 = facetIndex;
226: index2 = facetIndex + 1;
227: index3 = facetIndex + 2;
228: }
229: v1 = new Vector3f(coordsArray[index2 * 3]
230: - coordsArray[index1 * 3],
231: coordsArray[index2 * 3 + 1]
232: - coordsArray[index1 * 3 + 1],
233: coordsArray[index2 * 3 + 2]
234: - coordsArray[index1 * 3 + 2]);
235: v2 = new Vector3f(coordsArray[index3 * 3]
236: - coordsArray[index1 * 3],
237: coordsArray[index3 * 3 + 1]
238: - coordsArray[index1 * 3 + 1],
239: coordsArray[index3 * 3 + 2]
240: - coordsArray[index1 * 3 + 2]);
241: //debugOutputLn(VALUES, "v1, v2 = " + v1 + v2);
242: norm = new Vector3f();
243: norm.cross(v1, v2);
244: norm.normalize(norm);
245: }
246:
247: for (int j = 0; j < currFacetSize; ++j) {
248: int normIndex = facetIndex + j;
249: normalCoords[normIndex * 3] = norm.x;
250: normalCoords[normIndex * 3 + 1] = norm.y;
251: normalCoords[normIndex * 3 + 2] = norm.z;
252: if (facetIndices != null)
253: normalIndices[normIndex] = normIndex;
254: }
255: facetIndex += currFacetSize;
256: }
257: }
258: debugOutputLn(TRACE, "done with calcNormals()");
259: }
260:
261: }
|