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

Source for file ZBuffer.php

Documentation is available at ZBuffer.php

  1. <?php
  2.  
  3.     
  4.     protected $_filetype;
  5.     protected $_points;
  6.     protected $_heigth;
  7.  
  8.     public function __construct({
  9.         parent::__construct();
  10.         
  11.         $this->_filetype = 'png';
  12.         $this->_points = array();
  13.         $this->_heigth = array();
  14.     }
  15.     
  16.     public function createImage($x$y{
  17.         $this->_image = imagecreatetruecolor($x$y);
  18.         imagealphablending($this->_imagetrue);
  19.         imageSaveAlpha($this->_imagetrue);
  20.     }
  21.     
  22.     protected function _getColor(Image_3D_Color $color$alpha = 1.{
  23.         $values $color->getValues();
  24.         
  25.         $values[0= (int) round($values[0* 255);
  26.         $values[1= (int) round($values[1* 255);
  27.         $values[2= (int) round($values[2* 255);
  28.         $values[3= (int) round((1 - ((1 - $values[3]$alpha)) * 127);
  29.  
  30.         if ($values[3> 0{
  31.             // Tranzparente Farbe allokieren
  32.             $color = imageColorExactAlpha($this->_image$values[0]$values[1]$values[2]$values[3]);
  33.             if ($color === -1{
  34.                 // Wenn nicht Farbe neu alloziieren
  35.                 $color = imageColorAllocateAlpha($this->_image$values[0]$values[1]$values[2]$values[3]);
  36.             }
  37.         else {
  38.             // Deckende Farbe allozieren
  39.             $color = imageColorExact($this->_image$values[0]$values[1]$values[2]);
  40.             if ($color === -1{
  41.                 // Wenn nicht Farbe neu alloziieren
  42.                 $color = imageColorAllocate($this->_image$values[0]$values[1]$values[2]);
  43.             }
  44.         }
  45.         
  46.         return $color;
  47.     }
  48.     
  49.     public function setBackground(Image_3D_Color $color{
  50.         $bg $this->_getColor($color);
  51.         imagefill($this->_image11$bg);
  52.     }
  53.     
  54.     protected function _drawLine(Image_3D_Point $p1Image_3D_Point $p2{
  55.         list($x1$y1$p1->getScreenCoordinates();
  56.         list($x2$y2$p2->getScreenCoordinates();
  57.         $z1 $p1->getZ()$z2 $p2->getZ();
  58.         
  59.         $steps ceil(max(abs($x1 $x2)abs($y1 $y2)));
  60.         
  61.         $xdiff ($x2 $x1$steps;
  62.         $ydiff ($y2 $y1$steps;
  63.         $zdiff ($z2 $z1$steps;
  64.         
  65.         $points = array('height' => array()'coverage' => array());
  66.         for ($i = 0; $i $steps$i++{
  67.             $x $x1 $i $xdiff;
  68.             $xFloor floor($x);
  69.             $xCeil ceil($x);
  70.             $xOffset $x $xFloor;
  71.             
  72.             $y $y1 $i $ydiff;
  73.             $yFloor floor($y);
  74.             $yCeil ceil($y);
  75.             $yOffset $y $yFloor;
  76.             
  77.             if (!isset($points['coverage'][(int) $xFloor][(int) $yCeil])) {
  78.                 $points['height'][(int) $xFloor][(int) $yCeil$z1 $i $zdiff;
  79.                 $points['coverage'][(int) $xFloor][(int) $yCeil(1 - $xOffset$yOffset;
  80.             else {
  81.                 $points['coverage'][(int) $xFloor][(int) $yCeil+= (1 - $xOffset$yOffset;
  82.             }
  83.             
  84.             if (!isset($points['coverage'][(int) $xFloor][(int) $yFloor])) {
  85.                 $points['height'][(int) $xFloor][(int) $yFloor$z1 $i $zdiff;
  86.                 $points['coverage'][(int) $xFloor][(int) $yFloor(1 - $xOffset(1 - $yOffset);
  87.             else {
  88.                 $points['coverage'][(int) $xFloor][(int) $yFloor+= (1 - $xOffset(1 - $yOffset);
  89.             }
  90.             
  91.             if (!isset($points['coverage'][(int) $xCeil][(int) $yCeil])) {
  92.                 $points['height'][(int) $xCeil][(int) $yCeil$z1 $i $zdiff;
  93.                 $points['coverage'][(int) $xCeil][(int) $yCeil$xOffset $yOffset;
  94.             else {
  95.                 $points['coverage'][(int) $xCeil][(int) $yCeil+= $xOffset $yOffset;
  96.             }
  97.             
  98.             if (!isset($points['coverage'][(int) $xCeil][(int) $yFloor])) {
  99.                 $points['height'][(int) $xCeil][(int) $yFloor$z1 $i $zdiff;
  100.                 $points['coverage'][(int) $xCeil][(int) $yFloor$xOffset (1 - $yOffset);
  101.             else {
  102.                 $points['coverage'][(int) $xCeil][(int) $yFloor+= $xOffset (1 - $yOffset);
  103.             }
  104.         }
  105.         return $points;
  106.     }
  107.     
  108.     protected function _getPolygonOutlines($pointArray{
  109.         $map = array('height' => array()'coverage' => array());
  110.         
  111.         $last end($pointArray);
  112.         foreach ($pointArray as $point{
  113.             $line $this->_drawLine($last$point);
  114.             $last $point;
  115.             // Merge line to map
  116.             foreach ($line['height'as $x => $row{
  117.                 foreach ($row as $y => $height{
  118.                     $map['height'][(int) $x][(int) $y$height;
  119.                     $map['coverage'][(int) $x][(int) $y$line['coverage'][(int) $x][(int) $y];
  120.                 }
  121.             }
  122.         }
  123.         
  124.         return $map;
  125.     }
  126.     
  127.     public function drawPolygon(Image_3D_Polygon $polygon{
  128.         $points $this->_getPolygonOutlines($polygon->getPoints());
  129.  
  130.         foreach ($points['coverage'as $x => $row{
  131.             if (count($row< 2continue;
  132.             
  133.             $start min(array_keys($row));
  134.             $end max(array_keys($row));
  135.             
  136.             $zStart $points['height'][$x][$start];
  137.             $zEnd $points['height'][$x][$end];
  138.             $zStep ($zEnd $zStart($end $start);
  139.  
  140.             // Starting point
  141.             $this->_heigth[$x][$start][(int) ($zStart * 100)$this->_getColor($polygon->getColor()$points['coverage'][$x][$start]);
  142.             
  143.             // the way between
  144.             for ($y $start + 1; $y $end$y++{
  145.                 $this->_heigth[$x][$y][(int) (($zStart $zStep ($y $start)) * 100)$this->_getColor($polygon->getColor());
  146.             }
  147.  
  148.             // Ending point
  149.             $this->_points[$x][$end][(int) ($zEnd * 100)$this->_getColor($polygon->getColor()$points['coverage'][$x][$end]);
  150.         }
  151.     }
  152.     
  153.     public function drawGradientPolygon(Image_3D_Polygon $polygon{
  154.         $this->drawPolygon($polygon);
  155.     }
  156.     
  157.     public function setFiletye($type{
  158.         $type strtolower($type);
  159.         if (in_array($typearray('png''jpeg'))) {
  160.             $this->_filetype = $type;
  161.             return true;
  162.         else {
  163.             return false;
  164.         }
  165.     }
  166.     
  167.     public function save($file{
  168.         
  169.         foreach ($this->_heigth as $x => $row{
  170.             foreach ($row as $y => $points{
  171.                 krsort($points);
  172.                 foreach ($points as $colorimagesetpixel($this->_image$x$y$color);
  173.             }
  174.         }
  175.         
  176.         switch ($this->_filetype{
  177.             case 'png':
  178.                 return imagepng($this->_image$file);
  179.             case 'jpeg':
  180.                 return imagejpeg($this->_image$file);
  181.         }
  182.     }
  183.  
  184.     public function getSupportedShading({
  185.         return array(    Image_3D_Renderer::SHADE_NO
  186.                         Image_3D_Renderer::SHADE_FLAT
  187. //                        Image_3D_Renderer::SHADE_GAUROUD,
  188.                         );
  189.     }
  190. }
  191.  
  192. ?>

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