001: /*
002: * $RCSfile: SourceProcessor.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 java.util.List;
048: import java.util.logging.Logger;
049: import javax.vecmath.Tuple3f;
050: import javax.vecmath.Vector3f;
051: import org.collada.colladaschema.Accessor;
052: import org.collada.colladaschema.BoolArray;
053: import org.collada.colladaschema.FloatArray;
054: import org.collada.colladaschema.IDREFArray;
055: import org.collada.colladaschema.IntArray;
056: import org.collada.colladaschema.Mesh;
057: import org.collada.colladaschema.NameArray;
058: import org.collada.colladaschema.Source;
059: import org.collada.colladaschema.Source.TechniqueCommon;
060:
061: /**
062: *
063: * @author paulby
064: */
065: public class SourceProcessor extends Processor {
066:
067: private Logger logger = Logger.getLogger("collada.processor");
068:
069: private Source source = null;
070:
071: private enum SourceType {
072: BOOL_ARRAY, FLOAT_ARRAY, INT_ARRAY, NAME_ARRAY, ID_REF_ARRAY
073: };
074:
075: private SourceType sourceType = null;
076:
077: private float[] processedFloatArray = null;
078:
079: /** Creates a new instance of MeshProcessor */
080: public SourceProcessor(Mesh mesh, Processor parent) {
081: super (mesh, parent);
082: logger.info("Processing Mesh ");
083: List<Source> sources = mesh.getSources();
084: for (Source s : sources) {
085: ProcessorFactory.createProcessor(s, this );
086: }
087:
088: ProcessorFactory.createProcessor(mesh.getVertices(), this );
089: }
090:
091: public SourceProcessor(Source source, Processor parent) {
092: super (source, parent);
093: logger.info("Processing Source id=" + source.getId() + " name="
094: + source.getName());
095:
096: this .source = source;
097: ElementCache.cache().putSource(source.getId(), this );
098:
099: BoolArray boolArray = source.getBoolArray();
100: FloatArray floatArray = source.getFloatArray();
101: IntArray intArray = source.getIntArray();
102: NameArray nameArray = source.getNameArray();
103: IDREFArray idRefArray = source.getIDREFArray();
104:
105: if (boolArray != null)
106: logger.info("BoolArray ");
107: else if (floatArray != null) {
108: List<Double> data = floatArray.getValues();
109: logger.info("FloatArray " + data.size());
110: float[] floatData = new float[data.size()];
111: int i = 0;
112: for (Double d : data)
113: floatData[i++] = d.floatValue();
114: ElementCache.cache().putFloatArray(floatArray.getId(),
115: floatData);
116: sourceType = SourceType.FLOAT_ARRAY;
117: } else if (intArray != null)
118: logger.info("IntArray ");
119: else if (nameArray != null)
120: logger.info("NameArray ");
121: else if (idRefArray != null)
122: logger.info("IdRefArray ");
123:
124: TechniqueCommon techCommon = source.getTechniqueCommon();
125: Accessor accessor = techCommon.getAccessor();
126:
127: switch (sourceType) {
128: case FLOAT_ARRAY:
129: // TODO Check ordering of x,y,z in accessor
130: break;
131: default:
132: logger.warning("Unimplemented SourceType " + sourceType);
133: }
134: }
135:
136: // private void processFloatArray(Accessor accessor) {
137: // int size = accessor.getCount().intValue();
138: // int stride = accessor.getStride().intValue();
139: // float[] data = ElementCache.cache().getFloatArray(accessor.getSource());
140: // }
141:
142: Tuple3f getTuple3f(Tuple3f result, int index) {
143: TechniqueCommon techCommon = source.getTechniqueCommon();
144: Accessor accessor = techCommon.getAccessor();
145: int size = accessor.getCount().intValue();
146: int stride = accessor.getStride().intValue();
147: int offset = accessor.getOffset().intValue();
148: float[] data = ElementCache.cache().getFloatArray(
149: accessor.getSource());
150:
151: index += offset;
152: result.x = data[index];
153: result.y = data[index + 1];
154: result.z = data[index + 2];
155:
156: return result;
157: }
158:
159: float[] getFloatArray() {
160: TechniqueCommon techCommon = source.getTechniqueCommon();
161: Accessor accessor = techCommon.getAccessor();
162:
163: // TODO, does offset apply here ?
164:
165: return ElementCache.cache().getFloatArray(accessor.getSource());
166: }
167: }
|