Project

General

Profile

« Previous | Next » 

Revision bc2ab8c5

Added by Leszek Koltunski over 4 years ago

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

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.java
30 30
import org.distorted.library.program.DistortedProgram;
31 31
import org.distorted.library.type.Static4D;
32 32

  
33
import java.io.DataOutputStream;
34
import java.io.FileNotFoundException;
35
import java.io.FileOutputStream;
33 36
import java.io.IOException;
37
import java.io.DataInputStream;
34 38
import java.nio.ByteBuffer;
35 39
import java.nio.ByteOrder;
36 40
import java.nio.FloatBuffer;
37
import java.nio.channels.FileChannel;
38 41
import java.util.ArrayList;
39 42

  
40 43
///////////////////////////////////////////////////////////////////////////////////////////////////
......
716 719
     if( mJobNode[0]!=null )
717 720
       {
718 721
       mJobNode[0].execute();  // this will set itself to null
722
/*
723
       try
724
         {
725
         String name = "/sdcard/"+mNumVertices+".dmesh";
726
         DataOutputStream dos = new DataOutputStream(new FileOutputStream(name));
727
         write(dos);
728
         android.util.Log.e("mesh", "file wrritten: "+name);
729
         }
730
       catch(FileNotFoundException ex)
731
         {
732
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
733
         }
734
 */
719 735
       }
720 736

  
721 737
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
......
770 786

  
771 787
///////////////////////////////////////////////////////////////////////////////////////////////////
772 788

  
773
   void read(FileChannel file) throws IOException
789
   void read(DataInputStream stream)
774 790
     {
775
     ByteBuffer buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*4);
776
     int version, numVertices=0, numEff=0, numTex=0;
791
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
792
     int version, numEff, numTex;
777 793

  
778 794
     try
779 795
       {
780
       file.read(buffer);
796
       stream.read(buffer);
797
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
781 798

  
782
       version = buffer.getInt(0);
799
       version = byteBuf.getInt(0);
783 800

  
784 801
       if( version==1 )
785 802
         {
786
         numVertices = buffer.getInt(4);
787
         numTex      = buffer.getInt(8);
788
         numEff      = buffer.getInt(12);
803
         mNumVertices= byteBuf.getInt(4);
804
         numTex      = byteBuf.getInt(8);
805
         numEff      = byteBuf.getInt(12);
806

  
807
    //     android.util.Log.e("mesh", "version: "+version+" vert: "+mNumVertices+" numTex: "+numTex+" numEff: "+numEff);
789 808
         }
790 809
       else
791 810
         {
792
         android.util.Log.e("mesh", "Error: unknown mesh file version "+version);
793
         file.close();
811
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
794 812
         return;
795 813
         }
796 814
       }
797 815
     catch(IOException e)
798 816
       {
799
       file.close();
800
       throw e;
817
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
818
       return;
801 819
       }
802 820

  
803
     if( numVertices>0 && numEff>0 && numTex>0 )
821
     if( mNumVertices>0 && numEff>0 && numTex>0 )
804 822
       {
805 823
       int size = numEff+TEX_COMP_SIZE*numTex;
806 824
       float[] tmp = new float[size];
807
       mVertAttribs1 = new float[VERT1_SIZE*mNumVertices];
808
       mVertAttribs2 = new float[VERT2_SIZE*mNumVertices];
825
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
826
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
809 827

  
810
       buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*size + mNumVertices*(VERT1_SIZE+VERT2_SIZE));
828
       buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(VERT1_ATTRIBS+VERT2_ATTRIBS))];
811 829

  
812
       file.read(buffer);
830
       try
831
         {
832
         stream.read(buffer);
833
         }
834
       catch(IOException e)
835
         {
836
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
837
         return;
838
         }
839

  
840

  
841
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
842
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
813 843

  
814
       buffer.asFloatBuffer().get(tmp,0,size);
815
       buffer.asFloatBuffer().get(mVertAttribs1, 0, VERT1_SIZE*mNumVertices);
816
       buffer.asFloatBuffer().get(mVertAttribs2, 0, VERT2_SIZE*mNumVertices);
844
       floatBuf.get(tmp,0,size);
817 845

  
846
/*
847
for(int f=0; f<size; f++)
848
  {
849
  android.util.Log.e("mesh", "tmp "+f+": "+tmp[f]);
850
  }
851
*/
852

  
853
       floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
854
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
855
/*
856
for(int f=0; f<mNumVertices; f++)
857
  {
858
  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]);
859
  }
860
*/
818 861
       TexComponent tex;
819 862
       int index, texComp;
820 863
       float x, y, z, w;
......
828 871
         z= tmp[TEX_COMP_SIZE*texComp+3];
829 872
         w= tmp[TEX_COMP_SIZE*texComp+4];
830 873

  
874
//android.util.Log.e("mesh", "tex comp "+texComp+" index="+index+" x="+x+" y="+y+" z="+z+" w="+w);
875

  
831 876
         tex = new TexComponent(index);
832 877
         tex.setMap(new Static4D(x,y,z,w));
833 878

  
......
838 883
         {
839 884
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
840 885
         mEffComponent.add(index);
886

  
887

  
888
//android.util.Log.e("mesh", "eff comp "+effComp+" index="+index);
889

  
841 890
         }
842 891
       }
843

  
844
     file.close();
845 892
     }
846 893

  
847 894
///////////////////////////////////////////////////////////////////////////////////////////////////
......
850 897
/**
851 898
 * Write the Mesh to a File.
852 899
 */
853
   public void write(FileChannel file) throws IOException
900
   public void write(DataOutputStream stream)
854 901
     {
855 902
     TexComponent tex;
856 903

  
857 904
     int numTex = mTexComponent.size();
858 905
     int numEff = mEffComponent.size();
859 906

  
860
     ByteBuffer buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*(numEff+TEX_COMP_SIZE*numTex+4));
861
     FloatBuffer buf  = buffer.asFloatBuffer();
862

  
863
     buf.put(1);             // version
864
     buf.put(mNumVertices);
865
     buf.put(numTex);
866
     buf.put(numEff);
867

  
868
     for(int i=0; i<numTex; i++)
907
     try
869 908
       {
870
       tex = mTexComponent.get(i);
909
       stream.writeInt(1);  // version
910
       stream.writeInt(mNumVertices);
911
       stream.writeInt(numTex);
912
       stream.writeInt(numEff);
871 913

  
872
       buf.put(tex.mEndIndex);
873
       buf.put(tex.mTextureMap.get0());
874
       buf.put(tex.mTextureMap.get1());
875
       buf.put(tex.mTextureMap.get2());
876
       buf.put(tex.mTextureMap.get3());
877
       }
914
       for(int i=0; i<numTex; i++)
915
         {
916
         tex = mTexComponent.get(i);
878 917

  
879
     for(int i=0; i<numEff; i++)
880
       {
881
       buf.put(mEffComponent.get(i));
882
       }
918
         stream.writeFloat((float)tex.mEndIndex);
919
         stream.writeFloat(tex.mTextureMap.get0());
920
         stream.writeFloat(tex.mTextureMap.get1());
921
         stream.writeFloat(tex.mTextureMap.get2());
922
         stream.writeFloat(tex.mTextureMap.get3());
923
         }
883 924

  
884
     ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
885
     vertBuf1.asFloatBuffer().put(mVertAttribs1);
886
     ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
887
     vertBuf2.asFloatBuffer().put(mVertAttribs2);
925
       for(int i=0; i<numEff; i++)
926
         {
927
         stream.writeFloat((float)mEffComponent.get(i));
928
         }
888 929

  
889
     try
890
       {
891
       file.write(buffer);
892
       file.write(vertBuf1);
893
       file.write(vertBuf2);
930
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
931
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
932
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
933
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
934

  
935
       stream.write(vertBuf1.array());
936
       stream.write(vertBuf2.array());
894 937
       }
895
     catch(IOException e)
938
     catch(IOException ex)
896 939
       {
897
       file.close();
898
       throw e;
940
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
899 941
       }
900

  
901
     file.close();
902 942
     }
903 943

  
904 944
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/mesh/MeshFile.java
19 19

  
20 20
package org.distorted.library.mesh;
21 21

  
22
import java.io.IOException;
23
import java.nio.channels.FileChannel;
22
import java.io.DataInputStream;
24 23

  
25 24
///////////////////////////////////////////////////////////////////////////////////////////////////
26 25

  
......
32 31
 * <p>
33 32
 * File format: as written by MeshBase.write().
34 33
 */
35
  public MeshFile(FileChannel file)
34
  public MeshFile(DataInputStream stream)
36 35
    {
37 36
    super();
38 37

  
39
    try
40
      {
41
      read(file);
42
      }
43
    catch(IOException ex)
44
      {
45
      android.util.Log.e("MeshFile", "exception reading file: "+ex.toString());
46
      }
38
    read(stream);
47 39
    }
48 40

  
49 41
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff