Project

General

Profile

« Previous | Next » 

Revision 22422a76

Added by Leszek Koltunski almost 4 years ago

Initial support for a writing a Mesh to a file and restoring it from a file (new class MeshFile).
Untested!

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.InputStream;
34
import java.io.OutputStream;
33
import java.io.IOException;
35 34
import java.nio.ByteBuffer;
36 35
import java.nio.ByteOrder;
37 36
import java.nio.FloatBuffer;
37
import java.nio.channels.FileChannel;
38 38
import java.util.ArrayList;
39 39

  
40 40
///////////////////////////////////////////////////////////////////////////////////////////////////
......
92 92
   private static int[] mEquAssociationH = new int[EffectQueue.MAIN_VARIANTS];
93 93
   private static int[] mAndAssociationH = new int[EffectQueue.MAIN_VARIANTS];
94 94

  
95
   private static final int TEX_COMP_SIZE = 5; // 5 four-bytes entities inside the component
96

  
95 97
   private static class TexComponent
96 98
     {
97 99
     private int mEndIndex;
......
767 769
     }
768 770

  
769 771
///////////////////////////////////////////////////////////////////////////////////////////////////
770
// PUBLIC API
771
///////////////////////////////////////////////////////////////////////////////////////////////////
772
/**
773
 * Write tthe Mesh to an OutputStream.
774
 */
775
   public void write(OutputStream stream)
772

  
773
   void read(FileChannel file) throws IOException
776 774
     {
775
     ByteBuffer buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*4);
776
     int version, numVertices=0, numEff=0, numTex=0;
777

  
778
     try
779
       {
780
       file.read(buffer);
781

  
782
       version = buffer.getInt(0);
783

  
784
       if( version==1 )
785
         {
786
         numVertices = buffer.getInt(4);
787
         numTex      = buffer.getInt(8);
788
         numEff      = buffer.getInt(12);
789
         }
790
       else
791
         {
792
         android.util.Log.e("mesh", "Error: unknown mesh file version "+version);
793
         file.close();
794
         return;
795
         }
796
       }
797
     catch(IOException e)
798
       {
799
       file.close();
800
       throw e;
801
       }
802

  
803
     if( numVertices>0 && numEff>0 && numTex>0 )
804
       {
805
       int size = numEff+TEX_COMP_SIZE*numTex;
806
       float[] tmp = new float[size];
807
       mVertAttribs1 = new float[VERT1_SIZE*mNumVertices];
808
       mVertAttribs2 = new float[VERT2_SIZE*mNumVertices];
809

  
810
       buffer = ByteBuffer.allocate(BYTES_PER_FLOAT*size + mNumVertices*(VERT1_SIZE+VERT2_SIZE));
811

  
812
       file.read(buffer);
813

  
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);
817

  
818
       TexComponent tex;
819
       int index, texComp;
820
       float x, y, z, w;
821

  
822
       for(texComp=0; texComp<numTex; texComp++)
823
         {
824
         index = (int)tmp[TEX_COMP_SIZE*texComp];
825

  
826
         x= tmp[TEX_COMP_SIZE*texComp+1];
827
         y= tmp[TEX_COMP_SIZE*texComp+2];
828
         z= tmp[TEX_COMP_SIZE*texComp+3];
829
         w= tmp[TEX_COMP_SIZE*texComp+4];
777 830

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

  
834
         mTexComponent.add(tex);
835
         }
836

  
837
       for(int effComp=0; effComp<numEff; effComp++)
838
         {
839
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
840
         mEffComponent.add(index);
841
         }
842
       }
843

  
844
     file.close();
778 845
     }
779 846

  
847
///////////////////////////////////////////////////////////////////////////////////////////////////
848
// PUBLIC API
780 849
///////////////////////////////////////////////////////////////////////////////////////////////////
781 850
/**
782
 * Read the mesh from an InputStream.
851
 * Write the Mesh to a File.
783 852
 */
784
   public void read(InputStream stream)
853
   public void write(FileChannel file) throws IOException
785 854
     {
855
     TexComponent tex;
856

  
857
     int numTex = mTexComponent.size();
858
     int numEff = mEffComponent.size();
786 859

  
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++)
869
       {
870
       tex = mTexComponent.get(i);
871

  
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
       }
878

  
879
     for(int i=0; i<numEff; i++)
880
       {
881
       buf.put(mEffComponent.get(i));
882
       }
883

  
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);
888

  
889
     try
890
       {
891
       file.write(buffer);
892
       file.write(vertBuf1);
893
       file.write(vertBuf2);
894
       }
895
     catch(IOException e)
896
       {
897
       file.close();
898
       throw e;
899
       }
900

  
901
     file.close();
787 902
     }
788 903

  
789 904
///////////////////////////////////////////////////////////////////////////////////////////////////
790

  
791 905
/**
792 906
 * When rendering this Mesh, do we want to render the Normal vectors as well?
793 907
 * <p>
src/main/java/org/distorted/library/mesh/MeshFile.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.library.mesh;
21

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

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
public class MeshFile extends MeshBase
28
{
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * Read mesh description from a file.
32
 * <p>
33
 * File format: as written by MeshBase.write().
34
 */
35
  public MeshFile(FileChannel file)
36
    {
37
    super();
38

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

  
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
/**
51
 * Copy constructor.
52
 */
53
  public MeshFile(MeshFile mesh, boolean deep)
54
   {
55
   super(mesh,deep);
56
   }
57

  
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
/**
60
 * Copy the Mesh.
61
 *
62
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
63
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
64
 *             coordinates and effect associations, is always deep copied)
65
 */
66
  public MeshFile copy(boolean deep)
67
   {
68
   return new MeshFile(this,deep);
69
   }
70
}

Also available in: Unified diff