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

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