1 package com.jme3.scene.mesh; 2 3 import com.jme3.scene.Mesh; 4 import com.jme3.scene.Mesh.Mode; 5 import com.jme3.scene.VertexBuffer.Type; 6 import java.nio.Buffer; 7 import java.nio.IntBuffer; 8 import java.nio.ShortBuffer; 9 10 /** 11 * <code>WrappedIndexBuffer</code> converts vertex indices from a non list based 12 * mesh mode such as {@link Mode#TriangleStrip} or {@link Mode#LineLoop} 13 * into a list based mode such as {@link Mode#Triangles} or {@link Mode#Lines}. 14 * As it is often more convenient to read vertex data in list format 15 * than in a non-list format, using this class is recommended to avoid 16 * convoluting classes used to process mesh data from an external source. 17 * 18 * @author Kirill Vainer 19 */ 20 public class WrappedIndexBuffer extends VirtualIndexBuffer { 21 22 private final IndexBuffer ib; 23 WrappedIndexBuffer(Mesh mesh)24 public WrappedIndexBuffer(Mesh mesh){ 25 super(mesh.getVertexCount(), mesh.getMode()); 26 this.ib = mesh.getIndexBuffer(); 27 switch (meshMode){ 28 case Points: 29 numIndices = mesh.getTriangleCount(); 30 break; 31 case Lines: 32 case LineLoop: 33 case LineStrip: 34 numIndices = mesh.getTriangleCount() * 2; 35 break; 36 case Triangles: 37 case TriangleStrip: 38 case TriangleFan: 39 numIndices = mesh.getTriangleCount() * 3; 40 break; 41 default: 42 throw new UnsupportedOperationException(); 43 } 44 } 45 46 @Override get(int i)47 public int get(int i) { 48 int superIdx = super.get(i); 49 return ib.get(superIdx); 50 } 51 52 @Override getBuffer()53 public Buffer getBuffer() { 54 return ib.getBuffer(); 55 } 56 convertToList(Mesh mesh)57 public static void convertToList(Mesh mesh){ 58 IndexBuffer inBuf = mesh.getIndicesAsList(); 59 IndexBuffer outBuf = IndexBuffer.createIndexBuffer(mesh.getVertexCount(), 60 inBuf.size()); 61 62 for (int i = 0; i < inBuf.size(); i++){ 63 outBuf.put(i, inBuf.get(i)); 64 } 65 66 mesh.clearBuffer(Type.Index); 67 switch (mesh.getMode()){ 68 case LineLoop: 69 case LineStrip: 70 mesh.setMode(Mode.Lines); 71 break; 72 case TriangleStrip: 73 case TriangleFan: 74 mesh.setMode(Mode.Triangles); 75 break; 76 default: 77 break; 78 } 79 if (outBuf instanceof IndexIntBuffer){ 80 mesh.setBuffer(Type.Index, 3, (IntBuffer)outBuf.getBuffer()); 81 }else{ 82 mesh.setBuffer(Type.Index, 3, (ShortBuffer)outBuf.getBuffer()); 83 } 84 } 85 86 } 87