4

目前我想从具有PHP功能的.blend文件中读取一些数据(元数据,场景名称,网格数,顶点数......),参考文档:unpack()Blender SDNA

http://www.atmind.nl/blender/blender-sdna-256.html

是否有一些简单的解决方案可以使用一些现有的类或库来读取所有这些信息,或者我是否必须从文件中逐块读取并编写我自己的函数/类/库(这样我就可以创建类似对象的东西)?

4

1 回答 1

2

在咨询了 php 手册后,我可以告诉你 php 只是不提供读取二进制文件的方法,但我认为有很好的方法来做到这一点(受c 和 fread的启发)

class BinaryReader {
    const FLOAT_SIZE = 4;

    protected $fp = null; // file pointer
    ...

    public function readFloat() {
         $data = fread( $fp, self::FLOAT_SIZE);
         $array = unpack( 'f', $data);
         return $array[0];
    }

     // Reading unsigned short int
     public function readUint16( $endian = null){
          if( $endian === null){
               $endian = $this->getDefaultEndian();
          }

          // Assuming _fread handles EOF and similar things
          $data = $this->_fread( 2);
          $array = unapack( ($endian == BIG_ENDIAN ? 'n' : 'v'), $data);
          return $array[0];
     }

    // ... All other binary type functions

    // You may also write it more general:
    public function readByReference( &$variable){
        switch( get_type( $variable)){
            case 'double':
                return $this->readDouble();
            ...
        }
    }
}

如果您有任何改进或提示,请将它们发布在评论中,我很乐意扩展答案。

于 2012-01-20T13:48:14.923 回答