1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * 3d Library
7 *
8 * PHP versions 5
9 *
10 * LICENSE:
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24 *
25 * @category  Image
26 * @package   Image_3D
27 * @author    Kore Nordmann <3d@kore-nordmann.de>
28 * @copyright 1997-2005 Kore Nordmann
29 * @license   http://www.gnu.org/licenses/lgpl.txt lgpl 2.1
30 * @version   CVS: $Id$
31 * @link      http://pear.php.net/package/PackageName
32 * @since     File available since Release 0.1.0
33 */
34
35require_once('Image/3D/Paintable/Object.php');
36require_once('Image/3D/Paintable/Object/3dsChunks.php');
37
38/**
39 * Image_3D_Object_3ds
40 *
41 * @category  Image
42 * @package   Image_3D
43 * @author    Kore Nordmann <3d@kore-nordmann.de>
44 * @copyright 1997-2005 Kore Nordmann
45 * @license   http://www.gnu.org/licenses/lgpl.txt lgpl 2.1
46 * @version   Release: @package_version@
47 * @link      http://pear.php.net/package/PackageName
48 * @since     Class available since Release 0.1.0
49 */
50class Image_3D_Object_3ds extends Image_3D_Object {
51
52    protected $_file;
53    protected $_fileSize;
54    protected $_objects;
55
56    protected $_chunks;
57
58    public function __construct($file) {
59        parent::__construct();
60        $this->_points = array();
61        $this->_chunks = array();
62        $this->_objects = array();
63
64        if (!is_file($file) || !is_readable($file)) {
65            throw new exception('3ds file could not be loaded.');
66        }
67        $this->_file = $file;
68
69        $this->_readChunks();
70    }
71
72    protected function _readChunks()
73    {
74        $this->_chunks = new Image_3D_Chunk(Image_3D_Chunk::MAIN3DS, substr(file_get_contents($this->_file), 6));
75        $this->_chunks->readChunks();
76
77        $editor = $this->_chunks->getFirstChunkByType(Image_3D_Chunk::EDIT3DS);
78        $editor->readChunks();
79
80        $objects = $editor->getChunksByType(Image_3D_Chunk::EDIT_OBJECT);
81        foreach ($objects as $object) {
82            $object = new Image_3D_Chunk_Object($object->getType(), $object->getContent());
83            $object->readChunks($this);
84        }
85    }
86
87    public function addObject($id)
88    {
89        $id = (string) $id;
90        $this->_objects[$id] = new Image_3D_Object_3ds_Object();
91        return $this->_objects[$id];
92    }
93
94    public function getObjectIDs() {
95        return array_keys($this->_objects);
96    }
97
98    public function getObject($id) {
99        if (!@isset($this->_objects[$id])) return false;
100        return $this->_objects[$id];
101    }
102
103    public function paint() {
104        foreach ($this->_objects as $object) $object->paint();
105    }
106
107    public function getPolygonCount()
108    {
109        $count = 0;
110        foreach ($this->_objects as $object) $count += $object->getPolygonCount();
111        return $count;
112    }
113
114    public function setColor(Image_3D_Color $color) {
115        foreach ($this->_objects as $object) $object->setColor($color);
116    }
117
118    public function setOption($option, $value) {
119        foreach ($this->_objects as $object) $object->setOption($option, $value);
120    }
121
122    public function transform(Image_3D_Matrix $matrix, $id = null) {
123        if ($id === null) $id = substr(md5(microtime()), 0, 8);
124        foreach ($this->_objects as $object) $object->transform($matrix, $id);
125    }
126
127    public function subdivideSurfaces($factor) {
128        foreach ($this->_objects as $object) $object->subdivideSurfaces($factor);
129    }
130
131    public function getPolygones()
132    {
133        $polygones = array();
134        foreach ($this->_objects as $object) $polygones = array_merge($polygones, $object->getPolygones());
135        return $polygones;
136    }
137}
138
139?>
140