| 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>
|
Initial support for a writing a Mesh to a file and restoring it from a file (new class MeshFile).
Untested!