parent
9759e24961
commit
8b300f5109
@ -1,183 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model; |
||||
|
||||
import com.jme3.scene.*; |
||||
import com.jme3.scene.Mesh.Mode; |
||||
import com.jme3.scene.VertexBuffer.Format; |
||||
import com.jme3.scene.VertexBuffer.Type; |
||||
import com.jme3.scene.mesh.IndexBuffer; |
||||
import com.jme3.util.IntMap; |
||||
import com.jme3.util.IntMap.Entry; |
||||
import java.nio.Buffer; |
||||
import java.util.Arrays; |
||||
import java.util.Comparator; |
||||
import jme3tools.converters.model.strip.PrimitiveGroup; |
||||
import jme3tools.converters.model.strip.TriStrip; |
||||
|
||||
public class ModelConverter { |
||||
|
||||
private static final class PrimComparator |
||||
implements Comparator<PrimitiveGroup> { |
||||
|
||||
public int compare(PrimitiveGroup g1, PrimitiveGroup g2) { |
||||
if (g1.type < g2.type) |
||||
return -1; |
||||
else if (g1.type > g2.type) |
||||
return 1; |
||||
else |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
private static final PrimComparator primComp = new PrimComparator(); |
||||
|
||||
public static void generateStrips(Mesh mesh, boolean stitch, boolean listOnly, int cacheSize, int minStripSize){ |
||||
TriStrip ts = new TriStrip(); |
||||
ts.setStitchStrips(stitch); |
||||
ts.setCacheSize(cacheSize); |
||||
ts.setListsOnly(listOnly); |
||||
ts.setMinStripSize(minStripSize); |
||||
|
||||
IndexBuffer ib = mesh.getIndicesAsList(); |
||||
int[] indices = new int[ib.size()]; |
||||
for (int i = 0; i < indices.length; i++) |
||||
indices[i] = ib.get(i); |
||||
|
||||
PrimitiveGroup[] groups = ts.generateStrips(indices); |
||||
Arrays.sort(groups, primComp); |
||||
|
||||
int numElements = 0; |
||||
for (PrimitiveGroup group : groups) |
||||
numElements += group.numIndices; |
||||
|
||||
VertexBuffer original = mesh.getBuffer(Type.Index); |
||||
Buffer buf = VertexBuffer.createBuffer(original.getFormat(), |
||||
original.getNumComponents(), |
||||
numElements); |
||||
original.updateData(buf); |
||||
ib = mesh.getIndexBuffer(); |
||||
|
||||
int curIndex = 0; |
||||
int[] modeStart = new int[]{ -1, -1, -1 }; |
||||
int[] elementLengths = new int[groups.length]; |
||||
for (int i = 0; i < groups.length; i++){ |
||||
PrimitiveGroup group = groups[i]; |
||||
elementLengths[i] = group.numIndices; |
||||
|
||||
if (modeStart[group.type] == -1){ |
||||
modeStart[group.type] = i; |
||||
} |
||||
|
||||
int[] trimmedIndices = group.getTrimmedIndices(); |
||||
for (int j = 0; j < trimmedIndices.length; j++){ |
||||
ib.put(curIndex + j, trimmedIndices[j]); |
||||
} |
||||
|
||||
curIndex += group.numIndices; |
||||
} |
||||
|
||||
if (modeStart[0] == -1 && modeStart[1] == 0 && modeStart[2] == -1 && |
||||
elementLengths.length == 1){ |
||||
original.compact(elementLengths[0]); |
||||
mesh.setMode(Mode.TriangleStrip); |
||||
}else{ |
||||
mesh.setElementLengths(elementLengths); |
||||
mesh.setModeStart(modeStart); |
||||
mesh.setMode(Mode.Hybrid); |
||||
} |
||||
|
||||
mesh.updateCounts(); |
||||
} |
||||
|
||||
public static void optimize(Mesh mesh, boolean toFixed){ |
||||
// update any data that need updating
|
||||
mesh.updateBound(); |
||||
mesh.updateCounts(); |
||||
|
||||
// set all buffers into STATIC_DRAW mode
|
||||
mesh.setStatic(); |
||||
|
||||
if (mesh.getBuffer(Type.Index) != null){ |
||||
// compress index buffer from UShort to UByte (if possible)
|
||||
FloatToFixed.compressIndexBuffer(mesh); |
||||
|
||||
// generate triangle strips stitched with degenerate tris
|
||||
generateStrips(mesh, false, false, 16, 0); |
||||
} |
||||
|
||||
IntMap<VertexBuffer> bufs = mesh.getBuffers(); |
||||
for (Entry<VertexBuffer> entry : bufs){ |
||||
VertexBuffer vb = entry.getValue(); |
||||
if (vb == null || vb.getBufferType() == Type.Index) |
||||
continue; |
||||
|
||||
if (vb.getFormat() == Format.Float){ |
||||
if (vb.getBufferType() == Type.Color){ |
||||
// convert the color buffer to UByte
|
||||
vb = FloatToFixed.convertToUByte(vb); |
||||
vb.setNormalized(true); |
||||
}else if (toFixed){ |
||||
// convert normals, positions, and texcoords
|
||||
// to fixed-point (16.16)
|
||||
vb = FloatToFixed.convertToFixed(vb); |
||||
// vb = FloatToFixed.convertToFloat(vb);
|
||||
} |
||||
mesh.clearBuffer(vb.getBufferType()); |
||||
mesh.setBuffer(vb); |
||||
} |
||||
} |
||||
//mesh.setInterleaved();
|
||||
} |
||||
|
||||
private static void optimizeScene(Spatial source, boolean toFixed){ |
||||
if (source instanceof Geometry){ |
||||
Geometry geom = (Geometry) source; |
||||
Mesh mesh = geom.getMesh(); |
||||
optimize(mesh, toFixed); |
||||
}else if (source instanceof Node){ |
||||
Node node = (Node) source; |
||||
for (int i = node.getQuantity() - 1; i >= 0; i--){ |
||||
Spatial child = node.getChild(i); |
||||
optimizeScene(child, toFixed); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static void optimize(Spatial source, boolean toFixed){ |
||||
optimizeScene(source, toFixed); |
||||
source.updateLogicalState(0); |
||||
source.updateGeometricState(); |
||||
} |
||||
|
||||
} |
@ -1,53 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
class EdgeInfo { |
||||
|
||||
FaceInfo m_face0, m_face1; |
||||
int m_v0, m_v1; |
||||
EdgeInfo m_nextV0, m_nextV1; |
||||
|
||||
public EdgeInfo(int v0, int v1) { |
||||
m_v0 = v0; |
||||
m_v1 = v1; |
||||
m_face0 = null; |
||||
m_face1 = null; |
||||
m_nextV0 = null; |
||||
m_nextV1 = null; |
||||
|
||||
} |
||||
} |
@ -1,50 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
class EdgeInfoVec extends ArrayList<EdgeInfo> { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public EdgeInfoVec() { |
||||
super(); |
||||
} |
||||
|
||||
public EdgeInfo at(int index) { |
||||
return get(index); |
||||
} |
||||
|
||||
|
||||
} |
@ -1,59 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
|
||||
class FaceInfo { |
||||
|
||||
int m_v0, m_v1, m_v2; |
||||
int m_stripId; // real strip Id
|
||||
int m_testStripId; // strip Id in an experiment
|
||||
int m_experimentId; // in what experiment was it given an experiment Id?
|
||||
|
||||
public FaceInfo(int v0, int v1, int v2){ |
||||
m_v0 = v0; m_v1 = v1; m_v2 = v2; |
||||
m_stripId = -1; |
||||
m_testStripId = -1; |
||||
m_experimentId = -1; |
||||
} |
||||
|
||||
public void set(FaceInfo o) { |
||||
m_v0 = o.m_v0; |
||||
m_v1 = o.m_v1; |
||||
m_v2 = o.m_v2; |
||||
|
||||
m_stripId = o.m_stripId; |
||||
m_testStripId = o.m_testStripId; |
||||
m_experimentId = o.m_experimentId; |
||||
} |
||||
} |
@ -1,54 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
class FaceInfoVec extends ArrayList<FaceInfo> { |
||||
|
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public FaceInfoVec() { |
||||
super(); |
||||
} |
||||
|
||||
public FaceInfo at(int index) { |
||||
return get(index); |
||||
} |
||||
|
||||
public void reserve(int i) { |
||||
super.ensureCapacity(i); |
||||
} |
||||
|
||||
} |
@ -1,71 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
|
||||
|
||||
public class IntVec { |
||||
|
||||
private int[] data; |
||||
private int count = 0; |
||||
|
||||
public IntVec() { |
||||
data = new int[16]; |
||||
} |
||||
|
||||
public IntVec(int startSize) { |
||||
data = new int[startSize]; |
||||
} |
||||
|
||||
public int size() { |
||||
return count; |
||||
} |
||||
|
||||
public int get(int i) { |
||||
return data[i]; |
||||
} |
||||
|
||||
public void add(int val) { |
||||
if ( count == data.length ) { |
||||
int[] ndata = new int[count*2]; |
||||
System.arraycopy(data,0,ndata,0,count); |
||||
data = ndata; |
||||
} |
||||
data[count] = val; |
||||
count++; |
||||
} |
||||
|
||||
public void clear() { |
||||
count = 0; |
||||
} |
||||
} |
@ -1,107 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
public class PrimitiveGroup { |
||||
|
||||
public static final int PT_LIST = 0; |
||||
public static final int PT_STRIP = 1; |
||||
public static final int PT_FAN = 2; |
||||
|
||||
public int type; |
||||
public int[] indices; |
||||
public int numIndices; |
||||
|
||||
public PrimitiveGroup() { |
||||
type = PT_STRIP; |
||||
} |
||||
|
||||
public String getTypeString() { |
||||
switch(type) { |
||||
case PT_LIST : return "list"; |
||||
case PT_STRIP: return "strip"; |
||||
case PT_FAN: return "fan"; |
||||
default: return "????"; |
||||
} |
||||
} |
||||
|
||||
public String toString() { |
||||
return getTypeString() + " : " + numIndices; |
||||
} |
||||
|
||||
public String getFullInfo() { |
||||
if ( type != PT_STRIP ) |
||||
return toString(); |
||||
|
||||
int[] stripLengths = new int[numIndices]; |
||||
|
||||
int prev = -1; |
||||
int length = -1; |
||||
for ( int i =0; i < numIndices; i++) { |
||||
if (indices[i] == prev) { |
||||
stripLengths[length]++; |
||||
length = -1; |
||||
prev = -1; |
||||
} else { |
||||
prev = indices[i]; |
||||
length++; |
||||
} |
||||
} |
||||
stripLengths[length]++; |
||||
|
||||
StringBuffer sb = new StringBuffer(); |
||||
sb.append("Strip:").append(numIndices).append("\n"); |
||||
for ( int i =0; i < stripLengths.length; i++) { |
||||
if ( stripLengths[i] > 0) { |
||||
sb.append(i).append("->").append(stripLengths[i]).append("\n"); |
||||
} |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
/** |
||||
* @return |
||||
*/ |
||||
public int[] getTrimmedIndices() { |
||||
if ( indices.length == numIndices ) |
||||
return indices; |
||||
int[] nind = new int[numIndices]; |
||||
System.arraycopy(indices,0,nind,0,numIndices); |
||||
return nind; |
||||
} |
||||
|
||||
} |
||||
|
@ -1,355 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
class StripInfo { |
||||
|
||||
StripStartInfo m_startInfo; |
||||
FaceInfoVec m_faces = new FaceInfoVec(); |
||||
int m_stripId; |
||||
int m_experimentId; |
||||
|
||||
boolean visited; |
||||
|
||||
int m_numDegenerates; |
||||
|
||||
|
||||
public StripInfo(StripStartInfo startInfo,int stripId, int experimentId) { |
||||
|
||||
m_startInfo = startInfo; |
||||
m_stripId = stripId; |
||||
m_experimentId = experimentId; |
||||
visited = false; |
||||
m_numDegenerates = 0; |
||||
} |
||||
|
||||
boolean isExperiment() { |
||||
return m_experimentId >= 0; |
||||
} |
||||
|
||||
boolean isInStrip(FaceInfo faceInfo) { |
||||
if(faceInfo == null) |
||||
return false; |
||||
|
||||
return (m_experimentId >= 0 ? faceInfo.m_testStripId == m_stripId : faceInfo.m_stripId == m_stripId); |
||||
} |
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IsMarked()
|
||||
//
|
||||
// If either the faceInfo has a real strip index because it is
|
||||
// already assign to a committed strip OR it is assigned in an
|
||||
// experiment and the experiment index is the one we are building
|
||||
// for, then it is marked and unavailable
|
||||
boolean isMarked(FaceInfo faceInfo){ |
||||
return (faceInfo.m_stripId >= 0) || (isExperiment() && faceInfo.m_experimentId == m_experimentId); |
||||
} |
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// MarkTriangle()
|
||||
//
|
||||
// Marks the face with the current strip ID
|
||||
//
|
||||
void markTriangle(FaceInfo faceInfo){ |
||||
if (isExperiment()){ |
||||
faceInfo.m_experimentId = m_experimentId; |
||||
faceInfo.m_testStripId = m_stripId; |
||||
} |
||||
else{ |
||||
faceInfo.m_experimentId = -1; |
||||
faceInfo.m_stripId = m_stripId; |
||||
} |
||||
} |
||||
|
||||
|
||||
boolean unique(FaceInfoVec faceVec, FaceInfo face) |
||||
{ |
||||
boolean bv0, bv1, bv2; //bools to indicate whether a vertex is in the faceVec or not
|
||||
bv0 = bv1 = bv2 = false; |
||||
|
||||
for(int i = 0; i < faceVec.size(); i++) |
||||
{ |
||||
if(!bv0) |
||||
{ |
||||
if( (faceVec.at(i).m_v0 == face.m_v0) || |
||||
(faceVec.at(i).m_v1 == face.m_v0) || |
||||
(faceVec.at(i).m_v2 == face.m_v0) ) |
||||
bv0 = true; |
||||
} |
||||
|
||||
if(!bv1) |
||||
{ |
||||
if( (faceVec.at(i).m_v0 == face.m_v1) || |
||||
(faceVec.at(i).m_v1 == face.m_v1) || |
||||
(faceVec.at(i).m_v2 == face.m_v1) ) |
||||
bv1 = true; |
||||
} |
||||
|
||||
if(!bv2) |
||||
{ |
||||
if( (faceVec.at(i).m_v0 == face.m_v2) || |
||||
(faceVec.at(i).m_v1 == face.m_v2) || |
||||
(faceVec.at(i).m_v2 == face.m_v2) ) |
||||
bv2 = true; |
||||
} |
||||
|
||||
//the face is not unique, all it's vertices exist in the face vector
|
||||
if(bv0 && bv1 && bv2) |
||||
return false; |
||||
} |
||||
|
||||
//if we get out here, it's unique
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Build()
|
||||
//
|
||||
// Builds a strip forward as far as we can go, then builds backwards, and joins the two lists
|
||||
//
|
||||
void build(EdgeInfoVec edgeInfos, FaceInfoVec faceInfos) |
||||
{ |
||||
// used in building the strips forward and backward
|
||||
IntVec scratchIndices = new IntVec(); |
||||
|
||||
// build forward... start with the initial face
|
||||
FaceInfoVec forwardFaces = new FaceInfoVec(); |
||||
FaceInfoVec backwardFaces = new FaceInfoVec(); |
||||
forwardFaces.add(m_startInfo.m_startFace); |
||||
|
||||
markTriangle(m_startInfo.m_startFace); |
||||
|
||||
int v0 = (m_startInfo.m_toV1 ? m_startInfo.m_startEdge.m_v0 : m_startInfo.m_startEdge.m_v1); |
||||
int v1 = (m_startInfo.m_toV1 ? m_startInfo.m_startEdge.m_v1 : m_startInfo.m_startEdge.m_v0); |
||||
|
||||
// easiest way to get v2 is to use this function which requires the
|
||||
// other indices to already be in the list.
|
||||
scratchIndices.add(v0); |
||||
scratchIndices.add(v1); |
||||
int v2 = Stripifier.getNextIndex(scratchIndices, m_startInfo.m_startFace); |
||||
scratchIndices.add(v2); |
||||
|
||||
//
|
||||
// build the forward list
|
||||
//
|
||||
int nv0 = v1; |
||||
int nv1 = v2; |
||||
|
||||
FaceInfo nextFace = Stripifier.findOtherFace(edgeInfos, nv0, nv1, m_startInfo.m_startFace); |
||||
while (nextFace != null && !isMarked(nextFace)) |
||||
{ |
||||
//check to see if this next face is going to cause us to die soon
|
||||
int testnv0 = nv1; |
||||
int testnv1 = Stripifier.getNextIndex(scratchIndices, nextFace); |
||||
|
||||
FaceInfo nextNextFace = Stripifier.findOtherFace(edgeInfos, testnv0, testnv1, nextFace); |
||||
|
||||
if( (nextNextFace == null) || (isMarked(nextNextFace)) ) |
||||
{ |
||||
//uh, oh, we're following a dead end, try swapping
|
||||
FaceInfo testNextFace = Stripifier.findOtherFace(edgeInfos, nv0, testnv1, nextFace); |
||||
|
||||
if( ((testNextFace != null) && !isMarked(testNextFace)) ) |
||||
{ |
||||
//we only swap if it buys us something
|
||||
|
||||
//add a "fake" degenerate face
|
||||
FaceInfo tempFace = new FaceInfo(nv0, nv1, nv0); |
||||
|
||||
forwardFaces.add(tempFace); |
||||
markTriangle(tempFace); |
||||
|
||||
scratchIndices.add(nv0); |
||||
testnv0 = nv0; |
||||
|
||||
++m_numDegenerates; |
||||
} |
||||
|
||||
} |
||||
|
||||
// add this to the strip
|
||||
forwardFaces.add(nextFace); |
||||
|
||||
markTriangle(nextFace); |
||||
|
||||
// add the index
|
||||
//nv0 = nv1;
|
||||
//nv1 = NvStripifier::GetNextIndex(scratchIndices, nextFace);
|
||||
scratchIndices.add(testnv1); |
||||
|
||||
// and get the next face
|
||||
nv0 = testnv0; |
||||
nv1 = testnv1; |
||||
|
||||
nextFace = Stripifier.findOtherFace(edgeInfos, nv0, nv1, nextFace); |
||||
|
||||
} |
||||
|
||||
// tempAllFaces is going to be forwardFaces + backwardFaces
|
||||
// it's used for Unique()
|
||||
FaceInfoVec tempAllFaces = new FaceInfoVec(); |
||||
for(int i = 0; i < forwardFaces.size(); i++) |
||||
tempAllFaces.add(forwardFaces.at(i)); |
||||
|
||||
//
|
||||
// reset the indices for building the strip backwards and do so
|
||||
//
|
||||
scratchIndices.clear(); |
||||
scratchIndices.add(v2); |
||||
scratchIndices.add(v1); |
||||
scratchIndices.add(v0); |
||||
nv0 = v1; |
||||
nv1 = v0; |
||||
nextFace = Stripifier.findOtherFace(edgeInfos, nv0, nv1, m_startInfo.m_startFace); |
||||
while (nextFace != null && !isMarked(nextFace)) |
||||
{ |
||||
//this tests to see if a face is "unique", meaning that its vertices aren't already in the list
|
||||
// so, strips which "wrap-around" are not allowed
|
||||
if(!unique(tempAllFaces, nextFace)) |
||||
break; |
||||
|
||||
//check to see if this next face is going to cause us to die soon
|
||||
int testnv0 = nv1; |
||||
int testnv1 = Stripifier.getNextIndex(scratchIndices, nextFace); |
||||
|
||||
FaceInfo nextNextFace = Stripifier.findOtherFace(edgeInfos, testnv0, testnv1, nextFace); |
||||
|
||||
if( (nextNextFace == null) || (isMarked(nextNextFace)) ) |
||||
{ |
||||
//uh, oh, we're following a dead end, try swapping
|
||||
FaceInfo testNextFace = Stripifier.findOtherFace(edgeInfos, nv0, testnv1, nextFace); |
||||
if( ((testNextFace != null) && !isMarked(testNextFace)) ) |
||||
{ |
||||
//we only swap if it buys us something
|
||||
|
||||
//add a "fake" degenerate face
|
||||
FaceInfo tempFace = new FaceInfo(nv0, nv1, nv0); |
||||
|
||||
backwardFaces.add(tempFace); |
||||
markTriangle(tempFace); |
||||
scratchIndices.add(nv0); |
||||
testnv0 = nv0; |
||||
|
||||
++m_numDegenerates; |
||||
} |
||||
|
||||
} |
||||
|
||||
// add this to the strip
|
||||
backwardFaces.add(nextFace); |
||||
|
||||
//this is just so Unique() will work
|
||||
tempAllFaces.add(nextFace); |
||||
|
||||
markTriangle(nextFace); |
||||
|
||||
// add the index
|
||||
//nv0 = nv1;
|
||||
//nv1 = NvStripifier::GetNextIndex(scratchIndices, nextFace);
|
||||
scratchIndices.add(testnv1); |
||||
|
||||
// and get the next face
|
||||
nv0 = testnv0; |
||||
nv1 = testnv1; |
||||
nextFace = Stripifier.findOtherFace(edgeInfos, nv0, nv1, nextFace); |
||||
} |
||||
|
||||
// Combine the forward and backwards stripification lists and put into our own face vector
|
||||
combine(forwardFaces, backwardFaces); |
||||
} |
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Combine()
|
||||
//
|
||||
// Combines the two input face vectors and puts the result into m_faces
|
||||
//
|
||||
void combine(FaceInfoVec forward, FaceInfoVec backward){ |
||||
|
||||
// add backward faces
|
||||
int numFaces = backward.size(); |
||||
for (int i = numFaces - 1; i >= 0; i--) |
||||
m_faces.add(backward.at(i)); |
||||
|
||||
// add forward faces
|
||||
numFaces = forward.size(); |
||||
for (int i = 0; i < numFaces; i++) |
||||
m_faces.add(forward.at(i)); |
||||
} |
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// SharesEdge()
|
||||
//
|
||||
// Returns true if the input face and the current strip share an edge
|
||||
//
|
||||
boolean sharesEdge(FaceInfo faceInfo, EdgeInfoVec edgeInfos) |
||||
{ |
||||
//check v0.v1 edge
|
||||
EdgeInfo currEdge = Stripifier.findEdgeInfo(edgeInfos, faceInfo.m_v0, faceInfo.m_v1); |
||||
|
||||
if(isInStrip(currEdge.m_face0) || isInStrip(currEdge.m_face1)) |
||||
return true; |
||||
|
||||
//check v1.v2 edge
|
||||
currEdge = Stripifier.findEdgeInfo(edgeInfos, faceInfo.m_v1, faceInfo.m_v2); |
||||
|
||||
if(isInStrip(currEdge.m_face0) || isInStrip(currEdge.m_face1)) |
||||
return true; |
||||
|
||||
//check v2.v0 edge
|
||||
currEdge = Stripifier.findEdgeInfo(edgeInfos, faceInfo.m_v2, faceInfo.m_v0); |
||||
|
||||
if(isInStrip(currEdge.m_face0) || isInStrip(currEdge.m_face1)) |
||||
return true; |
||||
|
||||
return false; |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} |
@ -1,51 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
|
||||
class StripInfoVec extends ArrayList<StripInfo> { |
||||
|
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public StripInfoVec() { |
||||
super(); |
||||
} |
||||
|
||||
public StripInfo at(int index) { |
||||
return get(index); |
||||
} |
||||
|
||||
} |
@ -1,49 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
class StripStartInfo { |
||||
|
||||
|
||||
FaceInfo m_startFace; |
||||
EdgeInfo m_startEdge; |
||||
boolean m_toV1; |
||||
|
||||
|
||||
public StripStartInfo(FaceInfo startFace, EdgeInfo startEdge, boolean toV1){ |
||||
m_startFace = startFace; |
||||
m_startEdge = startEdge; |
||||
m_toV1 = toV1; |
||||
} |
||||
|
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,311 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* To use, call generateStrips method, passing your triangle index list and |
||||
* then construct geometry/render resulting PrimitiveGroup objects. |
||||
* Features: |
||||
* <ul> |
||||
* <li>generates strips from arbitrary geometry. |
||||
* <li>flexibly optimizes for post TnL vertex caches (16 on GeForce1/2, 24 on GeForce3). |
||||
* <li>can stitch together strips using degenerate triangles, or not. |
||||
* <li>can output lists instead of strips. |
||||
* <li>can optionally throw excessively small strips into a list instead. |
||||
* <li>can remap indices to improve spatial locality in your vertex buffers. |
||||
* </ul> |
||||
* On cache sizes: Note that it's better to UNDERESTIMATE the cache size |
||||
* instead of OVERESTIMATING. So, if you're targetting GeForce1, 2, and 3, be |
||||
* conservative and use the GeForce1_2 cache size, NOT the GeForce3 cache size. |
||||
* This will make sure you don't "blow" the cache of the GeForce1 and 2. Also |
||||
* note that the cache size you specify is the "actual" cache size, not the |
||||
* "effective" cache size you may have heard about. This is 16 for GeForce1 and 2, |
||||
* and 24 for GeForce3. |
||||
* |
||||
* Credit goes to Curtis Beeson and Joe Demers for the basis for this |
||||
* stripifier and to Jason Regier and Jon Stone at Blizzard for providing a |
||||
* much cleaner version of CreateStrips(). |
||||
* |
||||
* Ported to java by Artur Biesiadowski <abies@pg.gda.pl> |
||||
*/ |
||||
public class TriStrip { |
||||
|
||||
public static final int CACHESIZE_GEFORCE1_2 = 16; |
||||
public static final int CACHESIZE_GEFORCE3 = 24; |
||||
|
||||
int cacheSize = CACHESIZE_GEFORCE1_2; |
||||
boolean bStitchStrips = true; |
||||
int minStripSize = 0; |
||||
boolean bListsOnly = false; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
public TriStrip() { |
||||
super(); |
||||
} |
||||
|
||||
/** |
||||
* If set to true, will return an optimized list, with no strips at all. |
||||
* Default value: false |
||||
*/ |
||||
public void setListsOnly(boolean _bListsOnly) { |
||||
bListsOnly = _bListsOnly; |
||||
} |
||||
|
||||
/** |
||||
* Sets the cache size which the stripfier uses to optimize the data. |
||||
* Controls the length of the generated individual strips. This is the |
||||
* "actual" cache size, so 24 for GeForce3 and 16 for GeForce1/2 You may |
||||
* want to play around with this number to tweak performance. Default |
||||
* value: 16 |
||||
*/ |
||||
public void setCacheSize(int _cacheSize) { |
||||
cacheSize = _cacheSize; |
||||
} |
||||
|
||||
/** |
||||
* bool to indicate whether to stitch together strips into one huge strip |
||||
* or not. If set to true, you'll get back one huge strip stitched together |
||||
* using degenerate triangles. If set to false, you'll get back a large |
||||
* number of separate strips. Default value: true |
||||
*/ |
||||
public void setStitchStrips(boolean _bStitchStrips) { |
||||
bStitchStrips = _bStitchStrips; |
||||
} |
||||
|
||||
/** |
||||
* Sets the minimum acceptable size for a strip, in triangles. All strips |
||||
* generated which are shorter than this will be thrown into one big, |
||||
* separate list. Default value: 0 |
||||
*/ |
||||
public void setMinStripSize(int _minStripSize) { |
||||
minStripSize = _minStripSize; |
||||
} |
||||
|
||||
/** |
||||
* @param in_indices |
||||
* input index list, the indices you would use to render |
||||
* @return array of optimized/stripified PrimitiveGroups |
||||
*/ |
||||
public PrimitiveGroup[] generateStrips(int[] in_indices) { |
||||
int numGroups = 0; |
||||
PrimitiveGroup[] primGroups; |
||||
//put data in format that the stripifier likes
|
||||
IntVec tempIndices = new IntVec(); |
||||
int maxIndex = 0; |
||||
|
||||
for (int i = 0; i < in_indices.length; i++) { |
||||
tempIndices.add(in_indices[i]); |
||||
if (in_indices[i] > maxIndex) |
||||
maxIndex = in_indices[i]; |
||||
} |
||||
|
||||
StripInfoVec tempStrips = new StripInfoVec(); |
||||
FaceInfoVec tempFaces = new FaceInfoVec(); |
||||
|
||||
Stripifier stripifier = new Stripifier(); |
||||
|
||||
//do actual stripification
|
||||
stripifier.stripify(tempIndices, cacheSize, minStripSize, maxIndex, tempStrips, tempFaces); |
||||
|
||||
//stitch strips together
|
||||
IntVec stripIndices = new IntVec(); |
||||
int numSeparateStrips = 0; |
||||
|
||||
if (bListsOnly) { |
||||
//if we're outputting only lists, we're done
|
||||
numGroups = 1; |
||||
primGroups = new PrimitiveGroup[numGroups]; |
||||
primGroups[0] = new PrimitiveGroup(); |
||||
PrimitiveGroup[] primGroupArray = primGroups; |
||||
|
||||
//count the total number of indices
|
||||
int numIndices = 0; |
||||
for (int i = 0; i < tempStrips.size(); i++) { |
||||
numIndices += tempStrips.at(i).m_faces.size() * 3; |
||||
} |
||||
|
||||
//add in the list
|
||||
numIndices += tempFaces.size() * 3; |
||||
|
||||
primGroupArray[0].type = PrimitiveGroup.PT_LIST; |
||||
primGroupArray[0].indices = new int[numIndices]; |
||||
primGroupArray[0].numIndices = numIndices; |
||||
|
||||
//do strips
|
||||
int indexCtr = 0; |
||||
for (int i = 0; i < tempStrips.size(); i++) { |
||||
for (int j = 0; j < tempStrips.at(i).m_faces.size(); j++) { |
||||
//degenerates are of no use with lists
|
||||
if (!Stripifier.isDegenerate(tempStrips.at(i).m_faces.at(j))) { |
||||
primGroupArray[0].indices[indexCtr++] = tempStrips.at(i).m_faces.at(j).m_v0; |
||||
primGroupArray[0].indices[indexCtr++] = tempStrips.at(i).m_faces.at(j).m_v1; |
||||
primGroupArray[0].indices[indexCtr++] = tempStrips.at(i).m_faces.at(j).m_v2; |
||||
} else { |
||||
//we've removed a tri, reduce the number of indices
|
||||
primGroupArray[0].numIndices -= 3; |
||||
} |
||||
} |
||||
} |
||||
|
||||
//do lists
|
||||
for (int i = 0; i < tempFaces.size(); i++) { |
||||
primGroupArray[0].indices[indexCtr++] = tempFaces.at(i).m_v0; |
||||
primGroupArray[0].indices[indexCtr++] = tempFaces.at(i).m_v1; |
||||
primGroupArray[0].indices[indexCtr++] = tempFaces.at(i).m_v2; |
||||
} |
||||
} else { |
||||
numSeparateStrips = stripifier.createStrips(tempStrips, stripIndices, bStitchStrips); |
||||
|
||||
//if we're stitching strips together, we better get back only one
|
||||
// strip from CreateStrips()
|
||||
|
||||
//convert to output format
|
||||
numGroups = numSeparateStrips; //for the strips
|
||||
if (tempFaces.size() != 0) |
||||
numGroups++; //we've got a list as well, increment
|
||||
primGroups = new PrimitiveGroup[numGroups]; |
||||
for (int i = 0; i < primGroups.length; i++) { |
||||
primGroups[i] = new PrimitiveGroup(); |
||||
} |
||||
|
||||
PrimitiveGroup[] primGroupArray = primGroups; |
||||
|
||||
//first, the strips
|
||||
int startingLoc = 0; |
||||
for (int stripCtr = 0; stripCtr < numSeparateStrips; stripCtr++) { |
||||
int stripLength = 0; |
||||
|
||||
if (!bStitchStrips) { |
||||
int i; |
||||
//if we've got multiple strips, we need to figure out the
|
||||
// correct length
|
||||
for (i = startingLoc; i < stripIndices.size(); i++) { |
||||
if (stripIndices.get(i) == -1) |
||||
break; |
||||
} |
||||
|
||||
stripLength = i - startingLoc; |
||||
} else |
||||
stripLength = stripIndices.size(); |
||||
|
||||
primGroupArray[stripCtr].type = PrimitiveGroup.PT_STRIP; |
||||
primGroupArray[stripCtr].indices = new int[stripLength]; |
||||
primGroupArray[stripCtr].numIndices = stripLength; |
||||
|
||||
int indexCtr = 0; |
||||
for (int i = startingLoc; i < stripLength + startingLoc; i++) |
||||
primGroupArray[stripCtr].indices[indexCtr++] = stripIndices.get(i); |
||||
|
||||
//we add 1 to account for the -1 separating strips
|
||||
//this doesn't break the stitched case since we'll exit the
|
||||
// loop
|
||||
startingLoc += stripLength + 1; |
||||
} |
||||
|
||||
//next, the list
|
||||
if (tempFaces.size() != 0) { |
||||
int faceGroupLoc = numGroups - 1; //the face group is the last
|
||||
// one
|
||||
primGroupArray[faceGroupLoc].type = PrimitiveGroup.PT_LIST; |
||||
primGroupArray[faceGroupLoc].indices = new int[tempFaces.size() * 3]; |
||||
primGroupArray[faceGroupLoc].numIndices = tempFaces.size() * 3; |
||||
int indexCtr = 0; |
||||
for (int i = 0; i < tempFaces.size(); i++) { |
||||
primGroupArray[faceGroupLoc].indices[indexCtr++] = tempFaces.at(i).m_v0; |
||||
primGroupArray[faceGroupLoc].indices[indexCtr++] = tempFaces.at(i).m_v1; |
||||
primGroupArray[faceGroupLoc].indices[indexCtr++] = tempFaces.at(i).m_v2; |
||||
} |
||||
} |
||||
} |
||||
return primGroups; |
||||
} |
||||
|
||||
/** |
||||
* Function to remap your indices to improve spatial locality in your |
||||
* vertex buffer. |
||||
* |
||||
* in_primGroups: array of PrimitiveGroups you want remapped numGroups: |
||||
* number of entries in in_primGroups numVerts: number of vertices in your |
||||
* vertex buffer, also can be thought of as the range of acceptable values |
||||
* for indices in your primitive groups. remappedGroups: array of remapped |
||||
* PrimitiveGroups |
||||
* |
||||
* Note that, according to the remapping handed back to you, you must |
||||
* reorder your vertex buffer. |
||||
* |
||||
*/ |
||||
|
||||
public static int[] remapIndices(int[] indices, int numVerts) { |
||||
int[] indexCache = new int[numVerts]; |
||||
Arrays.fill(indexCache, -1); |
||||
|
||||
int numIndices = indices.length; |
||||
int[] remappedIndices = new int[numIndices]; |
||||
int indexCtr = 0; |
||||
for (int j = 0; j < numIndices; j++) { |
||||
int cachedIndex = indexCache[indices[j]]; |
||||
if (cachedIndex == -1) //we haven't seen this index before
|
||||
{ |
||||
//point to "last" vertex in VB
|
||||
remappedIndices[j] = indexCtr; |
||||
|
||||
//add to index cache, increment
|
||||
indexCache[indices[j]] = indexCtr++; |
||||
} else { |
||||
//we've seen this index before
|
||||
remappedIndices[j] = cachedIndex; |
||||
} |
||||
} |
||||
|
||||
return remappedIndices; |
||||
} |
||||
|
||||
public static void remapArrays(float[] vertexBuffer, int vertexSize, int[] indices) { |
||||
int[] remapped = remapIndices(indices, vertexBuffer.length / vertexSize); |
||||
float[] bufferCopy = vertexBuffer.clone(); |
||||
for (int i = 0; i < remapped.length; i++) { |
||||
int from = indices[i] * vertexSize; |
||||
int to = remapped[i] * vertexSize; |
||||
for (int j = 0; j < vertexSize; j++) { |
||||
vertexBuffer[to + j] = bufferCopy[from + j]; |
||||
} |
||||
} |
||||
|
||||
System.arraycopy(remapped, 0, indices, 0, indices.length); |
||||
} |
||||
|
||||
} |
@ -1,100 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2009-2012 jMonkeyEngine |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package jme3tools.converters.model.strip; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
|
||||
class VertexCache { |
||||
|
||||
int[] entries; |
||||
int numEntries; |
||||
|
||||
public VertexCache() { |
||||
this(16); |
||||
} |
||||
|
||||
public VertexCache(int size) { |
||||
numEntries = size; |
||||
entries = new int[numEntries]; |
||||
clear(); |
||||
} |
||||
|
||||
public boolean inCache(int entry) { |
||||
for(int i = 0; i < numEntries; i++) |
||||
{ |
||||
if(entries[i] == entry) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public int addEntry(int entry) { |
||||
int removed; |
||||
|
||||
removed = entries[numEntries - 1]; |
||||
|
||||
//push everything right one
|
||||
for(int i = numEntries - 2; i >= 0; i--) |
||||
{ |
||||
entries[i + 1] = entries[i]; |
||||
} |
||||
|
||||
entries[0] = entry; |
||||
|
||||
return removed; |
||||
} |
||||
|
||||
public void clear() { |
||||
Arrays.fill(entries,-1); |
||||
} |
||||
|
||||
public int at(int index) { |
||||
return entries[index]; |
||||
} |
||||
|
||||
public void set(int index, int value) { |
||||
entries[index] = value; |
||||
} |
||||
|
||||
public void copy(VertexCache inVcache) |
||||
{ |
||||
for(int i = 0; i < numEntries; i++) |
||||
{ |
||||
inVcache.set(i, entries[i]); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue