Skip to content

Instantly share code, notes, and snippets.

@hyperized
Created September 14, 2013 16:13
Show Gist options
  • Save hyperized/6563252 to your computer and use it in GitHub Desktop.
Save hyperized/6563252 to your computer and use it in GitHub Desktop.
A small PHP implementation of Kirupa.coms 3D 'engine'. It does the basic math and lacks commenting.
<?PHP
class ThreeD
{
static public function Transform2Dto3D(array $points, array $axis, $focalLength, $center_x, $center_y)
{
$pointarray = array();
$sx = sin($axis['x']);
$cx = cos($axis['x']);
$sy = sin($axis['y']);
$cy = cos($axis['y']);
$sz = sin($axis['z']);
$cz = cos($axis['z']);
$i = count($points);
while($i--)
{
//For each point, build defaults
$x = $points[$i]['x'];
$y = $points[$i]['y'];
$z = $points[$i]['z'];
// rotation around x
$xy = $cx*$y - $sx*$z;
$xz = $sx*$y + $cx*$z;
// rotation around y
$yz = $cy*$xz - $sy*$x;
$yx = $sy*$xz + $cy*$x;
// rotation around z
$zx = $cz*$yx - $sz*$xy;
$zy = $sz*$yx + $cz*$xy;
$scaleFactor = $focalLength / ($focalLength + $yz);
$x = ($zx*$scaleFactor) + $center_x;
$y = ($zy*$scaleFactor) + $center_y;
$z = $yz;
$pointarray[$i] = ThreeD::make3DPoint($x, $y, $z);
}
return $pointarray;
}
static public function make3DPoint($x = null, $y = null, $z = null)
{
$point = array();
$point['x'] = $x;
$point['y'] = $y;
$point['z'] = $z;
return $point;
}
static public function getRotation($x_rotate, $y_rotate, $z_rotate)
{
$x_rad = ( $x_rotate / ( 180/pi() ) );
$y_rad = ( $y_rotate / ( 180/pi() ) );
$z_rad = ( $z_rotate / ( 180/pi() ) );
return ThreeD::make3DPoint($x_rad, $y_rad, $z_rad);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment