public virtual void encode(Mesh m, CtmOutputStream output) { int vc = m.getVertexCount(); output.writeLittleInt(MeshDecoder.INDX); writeIndicies(m.indices, output); output.writeLittleInt(MeshDecoder.VERT); writeFloatArray(m.vertices, output, vc * 3, 1); // Write normals if (m.normals != null) { output.writeLittleInt(MeshDecoder.NORM); writeFloatArray(m.normals, output, vc, 3); } foreach (AttributeData ad in m.texcoordinates) { output.writeLittleInt(MeshDecoder.TEXC); output.writeString(ad.name); output.writeString(ad.materialName); writeFloatArray(ad.values, output, vc, 2); } foreach (AttributeData ad in m.attributs) { output.writeLittleInt(MeshDecoder.ATTR); output.writeString(ad.name); writeFloatArray(ad.values, output, vc, 4); } }
protected override void writeIndicies(int[] indices, CtmOutputStream output) { int[] tmp = new int[indices.Length]; Array.Copy(indices, tmp, tmp.Length); rearrangeTriangles(tmp); makeIndexDeltas(tmp); output.writePackedInts(tmp, tmp.Length / 3, 3, false); }
public void WriteCompressedData() { byte[] data = new byte[]{1,2,3,4,5,6,7,8,9,10}; //write MemoryStream memory = new MemoryStream(); CtmOutputStream outS = new CtmOutputStream(memory); outS.writeCompressedData(data); //read memory.Seek(0, SeekOrigin.Begin); Stream readMemory = new MemoryStream(memory.ToArray()); CtmInputStream inS = new CtmInputStream(readMemory); Assert.AreEqual(data, inS.readCompressedData(data.Length)); Assert.AreEqual(memory.Length, readMemory.Position); }
protected virtual void writeFloatArray(float[] array, CtmOutputStream output, int count, int size) { output.writeLittleFloatArray(array); }
public override void encode(Mesh m, CtmOutputStream output) { Grid grid = setupGrid (m.vertices); SortableVertex[] sorted = sortVertices (grid, m.vertices); int[] vdeltas = makeVertexDeltas (m.vertices, sorted, grid); int[] gridIndicies = new int[m.getVertexCount ()]; gridIndicies [0] = sorted [0].gridIndex; for (int i = 1; i < m.getVertexCount(); ++i) { gridIndicies [i] = sorted [i].gridIndex - sorted [i - 1].gridIndex; } output.writeLittleInt (MG2Decoder.MG2_HEADER_TAG); output.writeLittleFloat (vertexPrecision); output.writeLittleFloat (normalPrecision); grid.writeToStream (output); output.writeLittleInt (MeshDecoder.VERT); output.writePackedInts (vdeltas, m.getVertexCount (), Mesh.CTM_POSITION_ELEMENT_COUNT, false); output.writeLittleInt (MG2Decoder.GIDX); output.writePackedInts (gridIndicies, m.getVertexCount (), 1, false); output.writeLittleInt (MeshDecoder.INDX); int[] indices = reIndexIndices (sorted, m.indices); rearrangeTriangles (indices); //write indicies { int[] deltas = new int[indices.Length]; Array.Copy(indices, deltas, indices.Length); makeIndexDeltas (deltas); output.writePackedInts (deltas, m.getTriangleCount (), 3, false); } if (m.hasNormals ()) { for (int i = 1; i < m.getVertexCount(); i++) { gridIndicies [i] += gridIndicies [i - 1]; } float[] restoredv = CommonAlgorithm.restoreVertices (vdeltas, gridIndicies, grid, vertexPrecision); output.writeLittleInt (MeshDecoder.NORM); int[] intNormals = makeNormalDeltas (restoredv, m.normals, indices, sorted); output.writePackedInts (intNormals, m.getVertexCount (), Mesh.CTM_NORMAL_ELEMENT_COUNT, false); } foreach (AttributeData ad in m.texcoordinates) { output.writeLittleInt (MeshDecoder.TEXC); output.writeString (ad.name); output.writeString (ad.materialName); output.writeLittleFloat (ad.precision); int[] deltas = makeUVCoordDeltas (ad, sorted); output.writePackedInts (deltas, m.getVertexCount (), Mesh.CTM_UV_ELEMENT_COUNT, true); } foreach (AttributeData ad in m.attributs) { output.writeLittleInt (MeshDecoder.ATTR); output.writeString (ad.name); output.writeLittleFloat (ad.precision); int[] deltas = makeAttribDeltas (ad, sorted); output.writePackedInts (deltas, m.getVertexCount (), Mesh.CTM_ATTR_ELEMENT_COUNT, true); } }
protected override void writeFloatArray(float[] array, CtmOutputStream output, int count, int size) { output.writePackedFloats(array, count, size); }
public CtmFileWriter(Stream o, MeshEncoder e) { output = new CtmOutputStream(o); encoder = e; }
public CtmFileWriter(Stream o, MeshEncoder e, int compressionLevel) { output = new CtmOutputStream(compressionLevel, o); encoder = e; }
protected virtual void writeIndicies(int[] indices, CtmOutputStream output) { output.writeLittleIntArray(indices); }
public void writeToStream(CtmOutputStream output) { output.writeLittleFloatArray(min); output.writeLittleFloatArray(max); output.writeLittleIntArray(division); }
public override void encode(Mesh m, CtmOutputStream output) { Grid grid = setupGrid(m.vertices); SortableVertex[] sorted = sortVertices(grid, m.vertices); int[] vdeltas = makeVertexDeltas(m.vertices, sorted, grid); int[] gridIndicies = new int[m.getVertexCount()]; gridIndicies [0] = sorted [0].gridIndex; for (int i = 1; i < m.getVertexCount(); ++i) { gridIndicies [i] = sorted [i].gridIndex - sorted [i - 1].gridIndex; } output.writeLittleInt(MG2Decoder.MG2_HEADER_TAG); output.writeLittleFloat(vertexPrecision); output.writeLittleFloat(normalPrecision); grid.writeToStream(output); output.writeLittleInt(MeshDecoder.VERT); output.writePackedInts(vdeltas, m.getVertexCount(), Mesh.CTM_POSITION_ELEMENT_COUNT, false); output.writeLittleInt(MG2Decoder.GIDX); output.writePackedInts(gridIndicies, m.getVertexCount(), 1, false); output.writeLittleInt(MeshDecoder.INDX); int[] indices = reIndexIndices(sorted, m.indices); rearrangeTriangles(indices); //write indicies { int[] deltas = new int[indices.Length]; Array.Copy(indices, deltas, indices.Length); makeIndexDeltas(deltas); output.writePackedInts(deltas, m.getTriangleCount(), 3, false); } if (m.hasNormals()) { for (int i = 1; i < m.getVertexCount(); i++) { gridIndicies [i] += gridIndicies [i - 1]; } float[] restoredv = CommonAlgorithm.restoreVertices(vdeltas, gridIndicies, grid, vertexPrecision); output.writeLittleInt(MeshDecoder.NORM); int[] intNormals = makeNormalDeltas(restoredv, m.normals, indices, sorted); output.writePackedInts(intNormals, m.getVertexCount(), Mesh.CTM_NORMAL_ELEMENT_COUNT, false); } foreach (AttributeData ad in m.texcoordinates) { output.writeLittleInt(MeshDecoder.TEXC); output.writeString(ad.name); output.writeString(ad.materialName); output.writeLittleFloat(ad.precision); int[] deltas = makeUVCoordDeltas(ad, sorted); output.writePackedInts(deltas, m.getVertexCount(), Mesh.CTM_UV_ELEMENT_COUNT, true); } foreach (AttributeData ad in m.attributs) { output.writeLittleInt(MeshDecoder.ATTR); output.writeString(ad.name); output.writeLittleFloat(ad.precision); int[] deltas = makeAttribDeltas(ad, sorted); output.writePackedInts(deltas, m.getVertexCount(), Mesh.CTM_ATTR_ELEMENT_COUNT, true); } }