游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6079|回复: 3

一点对m3g模型文件解析的工作,希望有帮助

[复制链接]

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2005-6-11 00:01:00 | 显示全部楼层 |阅读模式
因为最近不会在m3g文件上继续工作,把之前一点少少的工作放出来,希望可以有点帮助

  1. /*
  2. * Main.java
  3. *
  4. * Created on 2005年5月15日, 下午11:41
  5. */

  6. package m3gloader;
  7. import java.io.*;
  8. import java.util.zip.*;
  9. /**
  10. *
  11. * @author admin
  12. */
  13. public class Main {
  14.    
  15.     /** Creates a new instance of Main */
  16.     public Main() {
  17.     }
  18.    
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) throws Exception {
  23.         File f=new File("otokka_jump2.m3g");
  24.         System.out.println("\nFile \""+f.toString()+"\" exists? : "+f.exists());
  25.         if(!f.exists())
  26.             err("File "+f.toString()+" does not exists!");
  27.         FileInputStream fis=new FileInputStream(f);
  28.         M3GDataInputStream dis=new M3GDataInputStream(fis);
  29.         System.out.println("====== File identifier ======");
  30.         byte[] fileIdentifier=new byte[12];
  31.         if(dis.available()>=12){
  32.             dis.read(fileIdentifier);
  33.             System.out.println("File Identifier is: ");
  34.             for(int l=0;l<fileIdentifier.length;l++)
  35.                 System.out.print(fileIdentifier[l]+" ");
  36.             System.out.print("\nFile Identifier characters is: \n");
  37.             System.out.println(new String(fileIdentifier));
  38.             System.out.println("====== end of file identifier ======\n");
  39.         } else
  40.             err("File Identifier Error! ");
  41.         
  42.         /*
  43.         Section 0: Header Object
  44.          */
  45.         System.out.println("====== Section 0: Header Object ======");
  46.         if(dis.available()<5)
  47.             err("Section 0: Header error! ");
  48.         System.out.println("CompressionScheme: "+dis.readByte());
  49.         UInt32 totalSectionLength=dis.readUInt32();
  50.         //We have read 5 bytes.So we just check if (totalSectionLength.longValue()-5) is available.
  51.         if(dis.available()<totalSectionLength.longValue()-5)
  52.             err("Section 0: Content error! ");
  53.         System.out.println("TotalSectionLength: "+totalSectionLength);
  54.         System.out.println("UncompressedLength: "+dis.readUInt32());
  55.         System.out.println("$$$$$$ Objects $$$$$$");
  56.         System.out.println("ObjectType: "+dis.readByte());
  57.         //length1 is the length of 1st object we load, length2 is the 2nd.
  58.         UInt32 length1=dis.readUInt32();
  59.         System.out.println("Length: "+length1);
  60.         
  61.         System.out.print("VersionNumber: ");
  62.         System.out.print(dis.readByte()+".");
  63.         System.out.println(dis.readByte());
  64.         
  65.         System.out.println("hasExternalReferences: "+dis.readBoolean());
  66.         System.out.println("TotalFileSize: "+dis.readUInt32());
  67.         System.out.println("ApproximateContentSize: "+dis.readUInt32());
  68.         
  69.         /*
  70.         Head Object contain:
  71.         Byte[2] VersionNumber
  72.         Boolean hasExternalReferences
  73.         UInt32  TotalFileSize
  74.         UInt32  ApproximateContentSize
  75.         String  AuthoringField
  76.         So AuthoringField's length is object (length-11)
  77.          */
  78.         byte[] authoringField=new byte[length1.intValue()-11];
  79.         dis.readFully(authoringField);
  80.         System.out.println("------ AuthoringField ------");
  81.         System.out.println(new String(authoringField,"UTF-8"));
  82.         System.out.println("------ End of AuthoringField ------");
  83.         System.out.println("$$$$$$ End of Objects $$$$$$");
  84.         System.out.println("Checksum: "+dis.readUInt32());
  85.         System.out.println("====== End of Section 0: Header Object ======\n");
  86.         
  87.         
  88.         //new section
  89.         System.out.println(dis.readByte());
  90.         System.out.println(dis.readUInt32());
  91.         System.out.println(dis.readUInt32());
  92.         M3GInflaterInputStream iis=new M3GInflaterInputStream(fis);
  93.         System.out.println(iis.read());
  94.         System.out.println(iis.readUInt32());
  95.         /////////////////////a camera
  96.         //user id
  97.         System.out.println(iis.readUInt32());
  98.         //animation track
  99.         System.out.println(iis.readUInt32());
  100.         //user para
  101.         System.out.println(iis.readUInt32());
  102.         //hasComponentTransform;
  103.         System.out.println(iis.read());
  104.         //hasGeneralTransform;
  105.         System.out.println(iis.read());
  106.         //enable rendering
  107.         System.out.println(iis.read());
  108.         // enablePicking;
  109.         System.out.println(iis.read());
  110.         // alphaFactor;
  111.         System.out.println(iis.read());
  112.         //scope;
  113.         System.out.println(iis.readUInt32());   
  114.         //hasAlignment;
  115.         System.out.println(iis.read());
  116.         //h
  117.         System.out.println(iis.read());
  118.         System.out.println(iis.readUInt32());   
  119.         System.out.println(iis.readUInt32());   
  120.         System.out.println(iis.readUInt32());   
  121.         System.out.println(iis.readUInt32());   
  122. /////////////next object/////////////////////
  123.         //image 2d
  124.         System.out.println(iis.read());
  125.         System.out.println(iis.readUInt32());
  126.         //user id
  127.         System.out.println(iis.readUInt32());
  128.         //animation tracks
  129.         System.out.println(iis.readUInt32());
  130.         //user object
  131.         System.out.println(iis.readUInt32());
  132.         //format
  133.         System.out.println(iis.read());
  134.         //is mutable
  135.         System.out.println(iis.read());
  136.         //width
  137.         System.out.println(iis.readUInt32());
  138.         //height
  139.         System.out.println(iis.readUInt32());
  140.         //  palette;
  141.         System.out.println(iis.read());
  142.         //pixels;
  143.         System.out.println(iis.read());

  144.         /*
  145.         for(int l=0;l<4894;l++)
  146.             iis.read();
  147.         System.out.println(iis.read());
  148.         System.out.println(iis.readUInt32());
  149.         
  150.         for(int l=0;l<40;l++)
  151.             iis.read();
  152.         System.out.println(iis.read());
  153.         System.out.println(iis.readUInt32());
  154.         
  155.         for(int l=0;l<66;l++)
  156.             iis.read();
  157.         System.out.println(iis.read());
  158.         System.out.println(iis.readUInt32());
  159.         
  160.         for(int l=0;l<66;l++)
  161.             iis.read();
  162.         System.out.println(iis.read());
  163.         System.out.println(iis.readUInt32());
  164.         
  165.         for(int l=0;l<66;l++)
  166.             iis.read();
  167.         System.out.println(iis.read());
  168.         System.out.println(iis.readUInt32());
  169.         
  170.         for(int l=0;l<41;l++)
  171.             iis.read();
  172.         System.out.println(iis.read());
  173.         System.out.println(iis.readUInt32());
  174.         
  175.         for(int l=0;l<29;l++)
  176.             iis.read();
  177.         System.out.println(iis.read());
  178.         System.out.println(iis.readUInt32());
  179.         
  180.         for(int l=0;l<25;l++)
  181.             iis.read();
  182.         System.out.println(iis.read());
  183.         System.out.println(iis.readUInt32());
  184.         
  185.         for(int l=0;l<68;l++)
  186.             iis.read();
  187.         System.out.println(iis.read());
  188.         System.out.println(iis.readUInt32());
  189.     */
  190.         dis.close();
  191.         fis.close();
  192.     }
  193.    
  194.     public static void err(String msg){
  195.         System.out.println("\n**** An error has occured!!! The program was terminated!!! The errot infomation is: ");
  196.         System.out.println(msg+"\n");
  197.         System.exit(0);
  198.     }
  199.    
  200.     static class M3GDataInputStream extends DataInputStream{
  201.         
  202.         public M3GDataInputStream(InputStream in){
  203.             super(in);
  204.         }
  205.         
  206.         public UInt32 readUInt32() throws IOException {
  207.             int ch1 = in.read();
  208.             int ch2 = in.read();
  209.             int ch3 = in.read();
  210.             long ch4 = in.read();
  211.             //System.out.println(ch1+" "+ch2+" "+ch3+" "+ch4);
  212.             if ((ch1 | ch2 | ch3 | ch4) < 0)
  213.                 throw new EOFException();
  214.             return new UInt32((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24));
  215.         }
  216.     }
  217.    
  218.     static class M3GInflaterInputStream extends InflaterInputStream{
  219.         
  220.         public M3GInflaterInputStream(InputStream in){
  221.             super(in,new Inflater(),32);
  222.         }
  223.         
  224.         public UInt32 readUInt32() throws IOException {
  225.             int ch1 = super.read();
  226.             int ch2 = super.read();
  227.             int ch3 = super.read();
  228.             long ch4 = super.read();
  229.             //System.out.println(ch1+" "+ch2+" "+ch3+" "+ch4);
  230.             if ((ch1 | ch2 | ch3 | ch4) < 0)
  231.                 throw new EOFException();
  232.             return new UInt32((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24));
  233.         }
  234.     }
  235.     static class Float32{
  236.         
  237.     }
  238.     static class UInt32{
  239.         private long value;
  240.         
  241.         public UInt32(long value){
  242.             this.value=value;
  243.             Integer f=new Integer(2);
  244.             
  245.         }
  246.         
  247.         public long longValue(){
  248.             return value;
  249.         }
  250.         
  251.         public int intValue(){
  252.             if(value > 2147483647 && value < -2147483648)
  253.                 throw new ClassCastException();
  254.             return (int)value;
  255.         }
  256.         
  257.         public String toString(){
  258.             return Long.toString(value);
  259.         }
  260.     }
  261. }


复制代码
[em10] [em10]

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2005-6-11 00:02:00 | 显示全部楼层

Re: 一点对m3g模型文件解析的工作,希望有帮助

与之匹配的m3g模型
http://forweb.myrice.com/otokka_jump2.m3g

7

主题

188

帖子

198

积分

注册会员

Rank: 2

积分
198
发表于 2005-6-11 19:28:00 | 显示全部楼层

Re:一点对m3g模型文件解析的工作,希望有帮助

支持,有备无患,哈哈

1

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2005-6-16 22:30:00 | 显示全部楼层

Re:一点对m3g模型文件解析的工作,希望有帮助

为什么要做这个呢?似乎没什么大用吧 现在最新的3dmax可以导出.m3g了 然后直接放在手机上用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-26 00:38

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表