micheal19840929 发表于 2013-2-7 10:57:50

一点对m3g模型文件解析的工作

因为最近不会在m3g文件上继续工作,把之前一点少少的工作放出来,希望可以有点帮助
/* * Main.java * * Created on 2005年5月15日, 下午11:41 */package m3gloader;import java.io.*;import java.util.zip.*;/** * * @author admin */public class Main {      /** Creates a new instance of Main */    public Main() {    }      /**   * @param args the command line arguments   */    public static void main(String[] args) throws Exception {      File f=new File("otokka_jump2.m3g");      System.out.println("\nFile \""+f.toString()+"\" exists? : "+f.exists());      if(!f.exists())            err("File "+f.toString()+" does not exists!");      FileInputStream fis=new FileInputStream(f);      M3GDataInputStream dis=new M3GDataInputStream(fis);      System.out.println("====== File identifier ======");      byte[] fileIdentifier=new byte;      if(dis.available()>=12){            dis.read(fileIdentifier);            System.out.println("File Identifier is: ");            for(int l=0;l<fileIdentifier.length;l++)                System.out.print(fileIdentifier+" ");            System.out.print("\nFile Identifier characters is: \n");            System.out.println(new String(fileIdentifier));            System.out.println("====== end of file identifier ======\n");      } else            err("File Identifier Error! ");                /*      Section 0: Header Object         */      System.out.println("====== Section 0: Header Object ======");      if(dis.available()<5)            err("Section 0: Header error! ");      System.out.println("CompressionScheme: "+dis.readByte());      UInt32 totalSectionLength=dis.readUInt32();      //We have read 5 bytes.So we just check if (totalSectionLength.longvalue()-5) is available.      if(dis.available()<totalSectionLength.longvalue()-5)            err("Section 0: Content error! ");      System.out.println("TotalSectionLength: "+totalSectionLength);      System.out.println("UncompressedLength: "+dis.readUInt32());      System.out.println("$$$$$$ Objects $$$$$$");      System.out.println("ObjectType: "+dis.readByte());      //length1 is the length of 1st object we load, length2 is the 2nd.      UInt32 length1=dis.readUInt32();      System.out.println("Length: "+length1);                System.out.print("VersionNumber: ");      System.out.print(dis.readByte()+".");      System.out.println(dis.readByte());                System.out.println("hasExternalReferences: "+dis.readBoolean());      System.out.println("TotalFileSize: "+dis.readUInt32());      System.out.println("ApproximateContentSize: "+dis.readUInt32());                /*      Head Object contain:      Byte VersionNumber      Boolean hasExternalReferences      UInt32TotalFileSize      UInt32ApproximateContentSize      StringAuthoringField      So AuthoringField's length is object (length-11)         */      byte[] authoringField=new byte;      dis.readFully(authoringField);      System.out.println("------ AuthoringField ------");      System.out.println(new String(authoringField,"UTF-8"));      System.out.println("------ End of AuthoringField ------");      System.out.println("$$$$$$ End of Objects $$$$$$");      System.out.println("Checksum: "+dis.readUInt32());      System.out.println("====== End of Section 0: Header Object ======\n");                        //new section      System.out.println(dis.readByte());      System.out.println(dis.readUInt32());      System.out.println(dis.readUInt32());      M3GInflaterInputStream iis=new M3GInflaterInputStream(fis);      System.out.println(iis.read());      System.out.println(iis.readUInt32());      /////////////////////a camera      //user id      System.out.println(iis.readUInt32());      //animation track      System.out.println(iis.readUInt32());      //user para      System.out.println(iis.readUInt32());      //hasComponentTransform;      System.out.println(iis.read());      //hasGeneralTransform;      System.out.println(iis.read());      //enable rendering      System.out.println(iis.read());      // enablePicking;      System.out.println(iis.read());      // alphaFactor;      System.out.println(iis.read());      //scope;      System.out.println(iis.readUInt32());            //hasAlignment;      System.out.println(iis.read());      //h      System.out.println(iis.read());      System.out.println(iis.readUInt32());            System.out.println(iis.readUInt32());            System.out.println(iis.readUInt32());            System.out.println(iis.readUInt32());    /////////////next object/////////////////////      //image 2d      System.out.println(iis.read());      System.out.println(iis.readUInt32());      //user id      System.out.println(iis.readUInt32());      //animation tracks      System.out.println(iis.readUInt32());      //user object      System.out.println(iis.readUInt32());      //format      System.out.println(iis.read());      //is mutable      System.out.println(iis.read());      //width      System.out.println(iis.readUInt32());      //height      System.out.println(iis.readUInt32());      //palette;      System.out.println(iis.read());      //pixels;      System.out.println(iis.read());      /*      for(int l=0;l<4894;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<40;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<66;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<66;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<66;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<41;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<29;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<25;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());                for(int l=0;l<68;l++)            iis.read();      System.out.println(iis.read());      System.out.println(iis.readUInt32());    */      dis.close();      fis.close();    }      public static void err(String msg){      System.out.println("\n**** An error has occured!!! The program was terminated!!! The errot infomation is: ");      System.out.println(msg+"\n");      System.exit(0);    }      static class M3GDataInputStream extends DataInputStream{                public M3GDataInputStream(InputStream in){            super(in);      }                public UInt32 readUInt32() throws IOException {            int ch1 = in.read();            int ch2 = in.read();            int ch3 = in.read();            long ch4 = in.read();            //System.out.println(ch1+" "+ch2+" "+ch3+" "+ch4);            if ((ch1 | ch2 | ch3 | ch4) < 0)                throw new EOFException();            return new UInt32((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24));      }    }      static class M3GInflaterInputStream extends InflaterInputStream{                public M3GInflaterInputStream(InputStream in){            super(in,new Inflater(),32);      }                public UInt32 readUInt32() throws IOException {            int ch1 = super.read();            int ch2 = super.read();            int ch3 = super.read();            long ch4 = super.read();            //System.out.println(ch1+" "+ch2+" "+ch3+" "+ch4);            if ((ch1 | ch2 | ch3 | ch4) < 0)                throw new EOFException();            return new UInt32((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24));      }    }    static class Float32{            }    static class UInt32{      private long value;                public UInt32(long value){            this.value=value;            Integer f=new Integer(2);                  }                public long longvalue(){            return value;      }                public int intvalue(){            if(value > 2147483647 && value < -2147483648)                throw new ClassCastException();            return (int)value;      }                public String toString(){            return Long.toString(value);      }    }} 
页: [1]
查看完整版本: 一点对m3g模型文件解析的工作