001: /*
002: * $RCSfile: TrianglesProcessor.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.1 $
041: * $Date: 2007/08/28 16:44:32 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.loaders.collada.xml_walker;
046:
047: import com.sun.j3d.utils.geometry.GeometryInfo;
048: import com.sun.j3d.utils.geometry.Stripifier;
049: import com.sun.j3d.utils.geometry.StripifierStats;
050: import java.math.BigInteger;
051: import java.util.ArrayList;
052: import java.util.List;
053: import java.util.logging.Logger;
054: import javax.media.j3d.Appearance;
055: import javax.media.j3d.Geometry;
056: import javax.vecmath.Point3f;
057: import javax.vecmath.Vector3f;
058: import org.collada.colladaschema.InputLocalOffset;
059: import org.collada.colladaschema.Polylist;
060: import org.collada.colladaschema.Triangles;
061:
062: /**
063: *
064: * @author paulby
065: */
066: public class TrianglesProcessor extends Processor implements Primitive {
067:
068: private Logger logger = Logger.getLogger("collada.processor");
069:
070: private int[] vertexCount = null;
071: private int[] prim = null;
072:
073: // private ArrayList<Point3f> vertices=null;
074: // private ArrayList<Vector3f> normals=null;
075:
076: private int[] coordinateIndices = null;
077: private float[] coordinates = null;
078: private int[] normalIndices = null;
079: private float[] normals = null;
080:
081: private String materialId = null;
082:
083: /** Creates a new instance of PolylistProcessor */
084: public TrianglesProcessor(Triangles triangles, Processor parent) {
085: super (triangles, parent);
086: logger.info("Triangles " + triangles.getName());
087:
088: List<InputLocalOffset> inputs = triangles.getInputs();
089: List<BigInteger> pList = triangles.getP();
090: int triangleCount = triangles.getCount().intValue();
091:
092: if (pList != null) {
093: prim = new int[pList.size()];
094: int i = 0;
095: for (BigInteger p : pList)
096: prim[i++] = p.intValue();
097: }
098:
099: int maxOffset = 0;
100: for (InputLocalOffset in : inputs) {
101: maxOffset = Math.max(in.getOffset().intValue(), maxOffset);
102: }
103:
104: for (InputLocalOffset in : inputs) {
105: logger.info("Inputs " + in.getSemantic() + " source "
106: + in.getSource() + " offset " + in.getOffset());
107:
108: if (in.getSemantic().equalsIgnoreCase("VERTEX")) {
109: processVertices(in, maxOffset);
110: } else if (in.getSemantic().equalsIgnoreCase("NORMAL")) {
111: processNormals(in, maxOffset);
112: } else if (in.getSemantic().equalsIgnoreCase("TEXCOORD")) {
113: logger
114: .warning("TexCoords not handled in TrianglesProcessor");
115: } else {
116: logger.warning("UNIMPLEMENTED SEMANTIC "
117: + in.getSemantic());
118: }
119:
120: }
121:
122: materialId = triangles.getMaterial();
123:
124: }
125:
126: private void processVertices(InputLocalOffset in, int maxOffset) {
127: VerticesProcessor source = ElementCache.cache().getVertices(
128: in.getSource());
129: int offset = in.getOffset().intValue();
130:
131: if (source == null) {
132: logger.warning("Failed to get Vertices " + in.getSource());
133: } else {
134: coordinates = source.getFloatArray();
135:
136: ArrayList<Integer> coordInd = new ArrayList();
137: for (int i = offset; i < prim.length; i += maxOffset + 1) {
138: coordInd.add(prim[i]);
139: }
140:
141: coordinateIndices = new int[coordInd.size()];
142: int i = 0;
143: for (int index : coordInd)
144: coordinateIndices[i++] = index;
145: }
146: }
147:
148: private void processNormals(InputLocalOffset in, int maxOffset) {
149: SourceProcessor source = ElementCache.cache().getSource(
150: in.getSource());
151:
152: int offset = in.getOffset().intValue();
153:
154: if (source == null) {
155: logger.warning("Failed to get Normals " + in.getSource());
156: } else {
157: normals = source.getFloatArray();
158: ArrayList<Integer> normInd = new ArrayList();
159: for (int i = offset; i < prim.length; i += maxOffset + 1) {
160: normInd.add(prim[i]);
161: }
162:
163: normalIndices = new int[normInd.size()];
164: int i = 0;
165: for (int index : normInd)
166: normalIndices[i++] = index;
167: }
168: }
169:
170: public Appearance getAppearance() {
171: MaterialProcessor matProc = (MaterialProcessor) ElementCache
172: .cache().get(materialId);
173:
174: return null;
175: }
176:
177: public Geometry getGeometry() {
178: // TODO check if the parent is convex
179:
180: GeometryInfo geomInfo = new GeometryInfo(
181: GeometryInfo.TRIANGLE_ARRAY);
182: geomInfo.setCoordinates(coordinates);
183: geomInfo.setCoordinateIndices(coordinateIndices);
184:
185: if (normals != null) {
186: geomInfo.setNormals(normals);
187: geomInfo.setNormalIndices(normalIndices);
188: }
189:
190: Stripifier strip = new Stripifier(Stripifier.COLLECT_STATS);
191: strip.stripify(geomInfo);
192: StripifierStats stats = strip.getStripifierStats();
193: System.out.println(stats);
194:
195: return geomInfo.getGeometryArray();
196: }
197:
198: }
|