Image_3D
[ class tree: Image_3D ] [ index: Image_3D ] [ all elements ]

Source for file 3dsChunks.php

Documentation is available at 3dsChunks.php

  1. <?php
  2.  
  3. class Image_3D_Chunk {
  4.     
  5.     protected $type;
  6.     protected $content;
  7.     protected $size;
  8.     
  9.     protected $chunks;
  10.     
  11.  //>------ Primary chunk
  12.     const    MAIN3DS            = 0x4D4D;
  13.  
  14.  //>------ Main Chunks
  15.      const    EDIT3DS            = 0x3D3D;  // this is the start of the editor config
  16.      const    KEYF3DS            = 0xB000;  // this is the start of the keyframer config
  17.  
  18.  //>------ sub defines of EDIT3DS
  19.      const    EDIT_MATERIAL    = 0xAFFF;
  20.      const    EDIT_CONFIG1    = 0x0100;
  21.      const    EDIT_CONFIG2    = 0x3E3D;
  22.      const    EDIT_VIEW_P1    = 0x7012;
  23.      const    EDIT_VIEW_P2    = 0x7011;
  24.      const    EDIT_VIEW_P3    = 0x7020;
  25.      const    EDIT_VIEW1        = 0x7001;
  26.      const    EDIT_BACKGR        = 0x1200;
  27.      const    EDIT_AMBIENT    = 0x2100;
  28.      const    EDIT_OBJECT        = 0x4000;
  29.  
  30.  //>------ sub defines of EDIT_OBJECT
  31.      const    OBJ_TRIMESH        = 0x4100;
  32.      const    OBJ_LIGHT        = 0x4600;
  33.      const    OBJ_CAMERA        = 0x4700;
  34.  
  35.      const    OBJ_UNKNWN01    = 0x4010;
  36.      const    OBJ_UNKNWN02    = 0x4012;
  37.  
  38.  //>------ sub defines of OBJ_CAMERA
  39.      const    CAM_UNKNWN01    = 0x4710; 
  40.      const    CAM_UNKNWN02    = 0x4720; 
  41.  
  42.  //>------ sub defines of OBJ_LIGHT
  43.      const    LIT_OFF            = 0x4620;
  44.      const    LIT_SPOT        = 0x4610;
  45.      const    LIT_UNKNWN01    = 0x465A;
  46.  
  47.  //>------ sub defines of OBJ_TRIMESH
  48.      const    TRI_VERTEXL        = 0x4110;
  49.      const    TRI_FACEL2        = 0x4111; 
  50.      const    TRI_FACEL1        = 0x4120;
  51.      const    TRI_SMOOTH        = 0x4150;
  52.      const    TRI_LOCAL        = 0x4160;
  53.      const    TRI_VISIBLE        = 0x4165;
  54.  
  55.  //>>------ sub defs of KEYF3DS
  56.      const    KEYF_UNKNWN01    = 0xB009;
  57.      const    KEYF_UNKNWN02    = 0xB00A;
  58.      const    KEYF_FRAMES        = 0xB008;
  59.      const    KEYF_OBJDES        = 0xB002;
  60.  
  61.  //>>------  these define the different color chunk types
  62.      const    COL_RGB            = 0x0010;
  63.      const    COL_TRU            = 0x0011;
  64.      const    COL_UNK            = 0x0013;
  65.  
  66.  //>>------ defines for viewport chunks
  67.      const    TOP                = 0x0001;
  68.      const    BOTTOM            = 0x0002;
  69.      const    LEFT            = 0x0003;
  70.      const    RIGHT            = 0x0004;
  71.      const    FRONT            = 0x0005;
  72.      const    BACK            = 0x0006;
  73.      const    USER            = 0x0007;
  74.      const    CAMERA            = 0x0008;
  75.      const    LIGHT            = 0x0009;
  76.      const    DISABLED        = 0x0010;
  77.      const    BOGUS            = 0x0011;
  78.      
  79.     public function __construct($type$content{
  80.         $this->type = (int) $type;
  81.         $this->size strlen($content);
  82.         $this->content $content;
  83.     }
  84.     
  85.     public function readChunks({
  86.         
  87.         if (count($this->chunks|| ($this->size < 6)) return false;
  88.         
  89.         $position = 0;
  90.         $string $this->content;
  91.         $length $this->size - 6;
  92.         
  93.         while ($position <= $length{
  94.             $type $this->getWord(substr($string$position2));
  95.             $position += 2;
  96.             $chunkLength $this->getDWord(substr($string$position4)) - 6;
  97.             $position += 4;
  98.             
  99.             $this->chunks[= new Image_3D_Chunk($typesubstr($string$position$chunkLength));
  100.             $position += $chunkLength;
  101.         }
  102.     }
  103.     
  104.     public function debug({
  105.         printf("Typ: %6d (0x%04x) (%6d bytes) | Objects:%4d | Content:%6d\n"$this->type$this->type$this->sizecount($this->chunks)strlen($this->content));
  106.     }
  107.     
  108.     protected function getWord($string{
  109.         return (ord($string{1}<< 8ord($string{0});
  110.     }
  111.     
  112.     protected function getDWord($string{
  113.         return ord($string{0}(ord($string{1}<< 8(ord($string{2}<< 16(ord($string{3}<< 32);
  114.     }
  115.     
  116.     protected function getUnsignedInt($string{
  117.         return (ord($string{0}<< 8ord($string{1});
  118.     }
  119.     
  120.     protected function getFloat($string{
  121.         // Convert C-Float to PHP-Float
  122.         return (ord($string{3}128 ? -1 : 1(1 + (float) (ord($string{2}127/ 127 + (float) (ord($string{1})) / 256 / 127 + (float) (ord($string{0})) / 256 / 256 / 127pow(2.((((ord($string{3}127<< 1(ord($string{2}>> 7)) - 127));
  123.     }
  124.     
  125.     public function getChunks({
  126.         return $this->chunks;
  127.     }
  128.     
  129.     public function getType({
  130.         return $this->type;
  131.     }
  132.     
  133.     public function getContent({
  134.         return $this->content;
  135.     }
  136.     
  137.     public function getFirstChunkByType($type{
  138.         if (!is_int($type)) $type hexdec($type);
  139.         
  140.         foreach ($this->chunks as $chunkif ($chunk->getType(=== $typereturn $chunk;
  141.         return false;
  142.     }
  143.     
  144.     public function getChunksByType($type{
  145.         if (!is_int($type)) $type hexdec($type);
  146.         
  147.         $chunks = array();
  148.         foreach ($this->chunks as $chunkif ($chunk->getType(=== $type$chunks[$chunk;
  149.         return $chunks;
  150.     }
  151. }
  152.  
  153. class Image_3D_Chunk_Object extends Image_3D_Chunk {
  154.     
  155.     protected $name;
  156.     
  157.     public function __construct($type$content{
  158.         parent::__construct($type$content);
  159.         $this->getName();
  160.     }
  161.     
  162.     protected function getName({
  163.         $i = 0;
  164.         while ((ord($this->content{$i}!== 0&& ($i $this->size)) $this->name .= $this->content{$i++};
  165.         $this->content = substr($this->content$i + 1);
  166.     }
  167.     
  168.     public function readChunks(Image_3D_Object_3ds $k3ds{
  169.         $subtype $this->getWord(substr($this->content02));
  170.         $subcontent substr($this->content6);
  171.         
  172.         switch ($subtype{
  173.             case self::OBJ_TRIMESH:
  174.                 $object $k3ds->addObject($this->name);
  175.                 $this->chunks[= new Image_3D_Chunk_TriMesh($subtype$subcontent$object);
  176.             break;
  177.         }
  178.     }
  179.     
  180.     public function debug({
  181.         echo 'Object: '$this->name"\n";
  182.         parent::debug();
  183.     }
  184. }
  185.  
  186. class Image_3D_Chunk_TriMesh extends Image_3D_Chunk {
  187.     
  188.     protected $matrix;
  189.     
  190.     protected $object;
  191.     
  192.     public function __construct($type$content$object{
  193.         parent::__construct($type$content);
  194.         
  195.         $this->object $object;
  196.         
  197.         $this->readChunks();
  198.         
  199.         $this->getPoints();
  200.         $this->getFaces();
  201.     }
  202.     
  203.     protected function getPoints({
  204.         $vertexlists $this->getChunksByType(Image_3D_Chunk::TRI_VERTEXL);
  205.         foreach ($vertexlists as $vertexlist{
  206.             $points $vertexlist->getContent();
  207.             $count $this->getWord(substr($points02));
  208.             $points substr($points2);
  209.             
  210.             for ($i = 0; $i $count$i++{
  211.                 $x $this->getFloat(substr($points04));
  212.                 $y $this->getFloat(substr($points44));
  213.                 $z $this->getFloat(substr($points84));
  214.                 $this->object->newPoint($x$y$z);
  215.                 $points substr($points12);
  216.             }
  217.         }
  218.     }
  219.     
  220.     protected function getFaces({
  221.         $facelists $this->getChunksByType(Image_3D_Chunk::TRI_FACEL1);
  222.         foreach ($facelists as $facelist{
  223.             $faces $facelist->getContent();
  224.             $count $this->getWord(substr($faces02));
  225.             $faces substr($faces2);
  226.             
  227.             for ($i = 0; $i $count$i++{
  228.                 $p1 $this->getWord(substr($faces02));
  229.                 $p2 $this->getWord(substr($faces22));
  230.                 $p3 $this->getWord(substr($faces42));
  231.                 $this->object->newPolygon($p1$p2$p3);
  232.                 $faces substr($faces8);
  233.             }
  234.         }
  235.     }
  236.     
  237.     protected function getTranslations({
  238.         $translists $this->getChunksByType(Image_3D_Chunk::TRI_LOCAL);
  239.         foreach ($translists as $translist{
  240.             $trans $translist->getContent();
  241.  
  242.             echo "Trans: " strlen($trans)"\n";
  243.         }
  244.     }
  245.     
  246.     public function debug({
  247.         parent::debug();
  248.         printf("Trimesh with %d (0x%04x) points - Pointsize: %.2f\n"$this->pointCount$this->pointCount$this->size $this->pointCount);
  249.     }
  250. }
  251.  
  252.     
  253.     protected $_points;
  254.     
  255.     public function __construct({
  256.         parent::__construct();
  257.         
  258.         $this->_points = array();
  259.     }
  260.     
  261.     public function newPoint($x$y$z{
  262.         $this->_points[= new Image_3D_Point($x$y$z);
  263. //        echo "New Point: $x, $y, $z -> ", count($this->_points), "\n";
  264.     }
  265.     
  266.     public function newPolygon($p1$p2$p3{
  267.         if (!isset($this->_points[$p1]|| !isset($this->_points[$p2]|| !isset($this->_points[$p3])) {
  268. //            printf("ERROR: Unknown point (%d, %d, %d of %d)\n", $p1, $p2, $p3, count($this->_points) - 1);
  269.             return false;
  270.         }
  271.         $this->_addPolygon(new Image_3D_Polygon($this->_points[$p1]$this->_points[$p2]$this->_points[$p3]));
  272. //        echo "New Polygon: $p1, $p2, $p3 -> ", count($this->_polygones), "\n";
  273.     }
  274.     
  275.     public function debug({
  276.         printf("Points: %d | Polygones: %d (%d)\n"count($this->_points)count($this->_polygones)$this->_polygonCount);
  277.     }
  278. }
  279.  
  280. ?>

Documentation generated on Mon, 11 Mar 2019 14:17:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.