<?php
require_once('Image/3D.php');
require_once('Image/3D/Color/Metal.php');
require_once('Image/3D/Paintable/Light/Ambient.php');
// Create the blank three-dimensional space
// RJM Programming
// September, 2019
// Thanks to https://pear.php.net/package/Image_3D and https://www6.software.ibm.com/developerworks/education/os-php-3d/os-php-3d-a4.pdf
$textx="";
$shape="cone";
$metal=null;
if (isset($_GET['shape'])) {
  $shape=strtolower(urldecode($_GET['shape']));
} else if (isset($_POST['shape'])) { 
  $shape=strtolower(urldecode($_POST['shape']));
} else if (isset($_GET['text'])) {
  $textx=str_replace("+"," ",urldecode($_GET['text']));
  $shape="";
} else if (isset($_POST['text'])) { 
  $textx=str_replace("+"," ",urldecode($_POST['text']));
  $shape="";
}
if (isset($_GET['metal'])) {
  $metal=new Image_3D_Color_Metal(255,255,255);
  if (strlen($_GET['metal']) == 0) {
  $metal->setMetal(1.25);
  } else {
  $metal->setMetal(strtolower(urldecode($_GET['metal'])));
  }
} else if (isset($_POST['metal'])) { 
  $metal=new Image_3D_Color_Metal(255,255,255);
  if (strlen($_POST['metal']) == 0) {
  $metal->setMetal(1.25);
  } else {
  $metal->setMetal(strtolower(urldecode($_POST['metal'])));
  }
} else {
  $metal=null;
}

if (!$metal && ($shape == "cone" || $shape == "") && $textx == "") {
$delim="";
if ($arg) {
foreach ($argv as $arg) {
    $e=explode("=",$arg);
    if (count($e) == 2) {
        $textx.=$delim . $e[1];
        
switch (strtolower(trim($textx))) {
    case "cone":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "torus":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "sphere":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
    
    case "cube":
    $shape=strtolower(trim($textx));
    $textx="";
    break;
}

    } else {   
        $textx.=$delim . $e[0];
    }
    if ($delim == "") {
      $textx="";
    }
    $delim=" ";
}
if ($textx != "") {
    $textx=trim($textx);
    if ($textx != "") { $shape=""; }
}
}
}

$world = new Image_3D();
$world->setColor(new Image_3D_Color(255,255,255));
// A blue light from the left
$light1 = $world->createLight('Light',array(-300,0,-300));
$light1->setColor(new Image_3D_Color(100,100,255));
// A green light from the upper-right
$light2 = $world->createLight('Light',array(300,-300,-300));
$light2->setColor(new Image_3D_Color(100,255,100));
switch ($shape) {
    case "cone":
$cone = $world->createObject('cone',array('detail' => 64));
if ($metal) {
$cone->setColor($metal);
} else { 
$cone->setColor(new Image_3D_Color(255,255,255));
}
$cone->transform($world->createMatrix('scale', array(70, 220, 70)));
$cone->transform($world->createMatrix('rotation',array(-45, -120, -10)));
$cone->transform($world->createMatrix('move', array(-50, -30, 10)));
        break;
    case "torus":
$torus = $world->createObject('torus', array('inner_radius' => 90,'outer_radius' => 120,'detail_1' => 10,'detail_2' => 10));
if ($metal) {
$torus->setColor($metal);
} else {
$torus->setColor(new Image_3D_Color(255, 255, 255));
}
$torus->transform($world->createMatrix('Rotation', array(-45,0,-30)));
$torus->transform($world->createMatrix('Move', array(0,-20,0)));
        break;
    case "cube":
$cube = $world->createObject('cube',array(100, 100, 100));
if ($metal) {
$cube->setColor($metal);
} else {
$cube->setColor(new Image_3D_Color(255,255,255));
}
$cube->transform($world->createMatrix('rotation',array(-60,60,40)));
        break;
        
    case "sphere":
$sphere = $world->createObject('sphere',array('r' => 85, 'detail' => 5));
if ($metal) {
$sphere->setColor($metal);
} else {
$sphere->setColor(new Image_3D_Color(255, 255, 255));
}
        break;
        
    default:
        if ($textx != "") {
        if (file_exists('Image/3D/Paintable/Object/TextData.dat')) {
        file_put_contents('Image/3D/Paintable/Object/TextData.dat', $textx);
        }
$text = $world->createObject('text', $textx);
if ($metal) {
$text->setColor($metal);
} else {
$text->setColor(new Image_3D_Color(255, 255, 255));
}
$text->transform($world->createMatrix('Scale', array(6, 6, 6)));
$text->transform($world->createMatrix('Rotation',array(-35, 30, -15)));
$text->transform($world->createMatrix('Move', array(-150, 10, 20)));        
		}
        
        break;

}
// Render and save the 2-D image
$world->createRenderer('Perspectively');
$world->createDriver('gd');
$world->render(400, 400, 'object' . $shape . '.png');
header ('Content-type:image/png');
echo file_get_contents('object' . $shape . '.png');
?>
