commit bc2ab8c50244e13796dbb7de3334030f9a2f7ffb
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Jun 13 15:52:10 2020 +0100

    Reading a mesh from the .dmesh file might work now. Checked on two small meshes.

diff --git a/src/main/java/org/distorted/library/mesh/MeshBase.java b/src/main/java/org/distorted/library/mesh/MeshBase.java
index f8e63cb..d7a596d 100644
--- a/src/main/java/org/distorted/library/mesh/MeshBase.java
+++ b/src/main/java/org/distorted/library/mesh/MeshBase.java
@@ -30,11 +30,14 @@ import org.distorted.library.main.InternalBuffer;
 import org.distorted.library.program.DistortedProgram;
 import org.distorted.library.type.Static4D;
 
+import java.io.DataOutputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.DataInputStream;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
-import java.nio.channels.FileChannel;
 import java.util.ArrayList;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -716,6 +719,19 @@ public abstract class MeshBase
      if( mJobNode[0]!=null )
        {
        mJobNode[0].execute();  // this will set itself to null
+/*
+       try
+         {
+         String name = "/sdcard/"+mNumVertices+".dmesh";
+         DataOutputStream dos = new DataOutputStream(new FileOutputStream(name));
+         write(dos);
+         android.util.Log.e("mesh", "file wrritten: "+name);
+         }
+       catch(FileNotFoundException ex)
+         {
+         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
+         }
+ */
        }
 
      int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
@@ -770,51 +786,78 @@ public abstract class MeshBase
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-   void read(FileChannel file) throws IOException
+   void read(DataInputStream stream)
      {
-     ByteBuffer buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*4);
-     int version, numVertices=0, numEff=0, numTex=0;
+     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
+     int version, numEff, numTex;
 
      try
        {
-       file.read(buffer);
+       stream.read(buffer);
+       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
 
-       version = buffer.getInt(0);
+       version = byteBuf.getInt(0);
 
        if( version==1 )
          {
-         numVertices = buffer.getInt(4);
-         numTex      = buffer.getInt(8);
-         numEff      = buffer.getInt(12);
+         mNumVertices= byteBuf.getInt(4);
+         numTex      = byteBuf.getInt(8);
+         numEff      = byteBuf.getInt(12);
+
+    //     android.util.Log.e("mesh", "version: "+version+" vert: "+mNumVertices+" numTex: "+numTex+" numEff: "+numEff);
          }
        else
          {
-         android.util.Log.e("mesh", "Error: unknown mesh file version "+version);
-         file.close();
+         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
          return;
          }
        }
      catch(IOException e)
        {
-       file.close();
-       throw e;
+       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
+       return;
        }
 
-     if( numVertices>0 && numEff>0 && numTex>0 )
+     if( mNumVertices>0 && numEff>0 && numTex>0 )
        {
        int size = numEff+TEX_COMP_SIZE*numTex;
        float[] tmp = new float[size];
-       mVertAttribs1 = new float[VERT1_SIZE*mNumVertices];
-       mVertAttribs2 = new float[VERT2_SIZE*mNumVertices];
+       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
+       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
 
-       buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*size + mNumVertices*(VERT1_SIZE+VERT2_SIZE));
+       buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(VERT1_ATTRIBS+VERT2_ATTRIBS))];
 
-       file.read(buffer);
+       try
+         {
+         stream.read(buffer);
+         }
+       catch(IOException e)
+         {
+         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
+         return;
+         }
+
+
+       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
+       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
 
-       buffer.asFloatBuffer().get(tmp,0,size);
-       buffer.asFloatBuffer().get(mVertAttribs1, 0, VERT1_SIZE*mNumVertices);
-       buffer.asFloatBuffer().get(mVertAttribs2, 0, VERT2_SIZE*mNumVertices);
+       floatBuf.get(tmp,0,size);
 
+/*
+for(int f=0; f<size; f++)
+  {
+  android.util.Log.e("mesh", "tmp "+f+": "+tmp[f]);
+  }
+*/
+
+       floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
+       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
+/*
+for(int f=0; f<mNumVertices; f++)
+  {
+  android.util.Log.e("mesh", "vert "+f+": "+mVertAttribs1[9*f]+" "+mVertAttribs1[9*f+1]+" "+mVertAttribs1[9*f+2]+"   "+mVertAttribs1[9*f+3]+" "+mVertAttribs1[9*f+4]+" "+mVertAttribs1[9*f+5]+"   "+mVertAttribs1[9*f+6]+" "+mVertAttribs1[9*f+7]+" "+mVertAttribs1[9*f+8]);
+  }
+*/
        TexComponent tex;
        int index, texComp;
        float x, y, z, w;
@@ -828,6 +871,8 @@ public abstract class MeshBase
          z= tmp[TEX_COMP_SIZE*texComp+3];
          w= tmp[TEX_COMP_SIZE*texComp+4];
 
+//android.util.Log.e("mesh", "tex comp "+texComp+" index="+index+" x="+x+" y="+y+" z="+z+" w="+w);
+
          tex = new TexComponent(index);
          tex.setMap(new Static4D(x,y,z,w));
 
@@ -838,10 +883,12 @@ public abstract class MeshBase
          {
          index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
          mEffComponent.add(index);
+
+
+//android.util.Log.e("mesh", "eff comp "+effComp+" index="+index);
+
          }
        }
-
-     file.close();
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -850,55 +897,48 @@ public abstract class MeshBase
 /**
  * Write the Mesh to a File.
  */
-   public void write(FileChannel file) throws IOException
+   public void write(DataOutputStream stream)
      {
      TexComponent tex;
 
      int numTex = mTexComponent.size();
      int numEff = mEffComponent.size();
 
-     ByteBuffer buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*(numEff+TEX_COMP_SIZE*numTex+4));
-     FloatBuffer buf  = buffer.asFloatBuffer();
-
-     buf.put(1);             // version
-     buf.put(mNumVertices);
-     buf.put(numTex);
-     buf.put(numEff);
-
-     for(int i=0; i<numTex; i++)
+     try
        {
-       tex = mTexComponent.get(i);
+       stream.writeInt(1);  // version
+       stream.writeInt(mNumVertices);
+       stream.writeInt(numTex);
+       stream.writeInt(numEff);
 
-       buf.put(tex.mEndIndex);
-       buf.put(tex.mTextureMap.get0());
-       buf.put(tex.mTextureMap.get1());
-       buf.put(tex.mTextureMap.get2());
-       buf.put(tex.mTextureMap.get3());
-       }
+       for(int i=0; i<numTex; i++)
+         {
+         tex = mTexComponent.get(i);
 
-     for(int i=0; i<numEff; i++)
-       {
-       buf.put(mEffComponent.get(i));
-       }
+         stream.writeFloat((float)tex.mEndIndex);
+         stream.writeFloat(tex.mTextureMap.get0());
+         stream.writeFloat(tex.mTextureMap.get1());
+         stream.writeFloat(tex.mTextureMap.get2());
+         stream.writeFloat(tex.mTextureMap.get3());
+         }
 
-     ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
-     vertBuf1.asFloatBuffer().put(mVertAttribs1);
-     ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
-     vertBuf2.asFloatBuffer().put(mVertAttribs2);
+       for(int i=0; i<numEff; i++)
+         {
+         stream.writeFloat((float)mEffComponent.get(i));
+         }
 
-     try
-       {
-       file.write(buffer);
-       file.write(vertBuf1);
-       file.write(vertBuf2);
+       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
+       vertBuf1.asFloatBuffer().put(mVertAttribs1);
+       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
+       vertBuf2.asFloatBuffer().put(mVertAttribs2);
+
+       stream.write(vertBuf1.array());
+       stream.write(vertBuf2.array());
        }
-     catch(IOException e)
+     catch(IOException ex)
        {
-       file.close();
-       throw e;
+       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
        }
-
-     file.close();
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/mesh/MeshFile.java b/src/main/java/org/distorted/library/mesh/MeshFile.java
index 3993552..1729a50 100644
--- a/src/main/java/org/distorted/library/mesh/MeshFile.java
+++ b/src/main/java/org/distorted/library/mesh/MeshFile.java
@@ -19,8 +19,7 @@
 
 package org.distorted.library.mesh;
 
-import java.io.IOException;
-import java.nio.channels.FileChannel;
+import java.io.DataInputStream;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -32,18 +31,11 @@ public class MeshFile extends MeshBase
  * <p>
  * File format: as written by MeshBase.write().
  */
-  public MeshFile(FileChannel file)
+  public MeshFile(DataInputStream stream)
     {
     super();
 
-    try
-      {
-      read(file);
-      }
-    catch(IOException ex)
-      {
-      android.util.Log.e("MeshFile", "exception reading file: "+ex.toString());
-      }
+    read(stream);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
