pouët.net

Go to bottom

worlds first: '3d-engine' in php

<?php
/*
	worlds first "3d-engine" written entirely in php
	copyright (c) 2002
	rasmus christian kaae kaae@daimi.au.dk
	
	note: this piece of simple code does not demonstrate 
	      anything else than the capabilities to render
	      images through serverside scripting (i.e. php).
	      it uses easy formulas and standard php-functions.
	      
	run:  to run this you need to have access to a php-server
	      with gd-lib installed. my web-account at uni has this
	      but since i figure that it would more or less kill
	      the webserver if i put it online for public-display
	      you'll need to install it yourself (or ask a friend).
*/

function getmicrotime()
{ 
    list($usec, $sec) = explode(" ",microtime()); 
    return ((float)$usec + (float)$sec); 
} 


$scale = 2.0;

$point[0][0] = -$scale; $point[0][1] = -$scale; $point[0][2] =  $scale;
$point[1][0] = -$scale; $point[1][1] =  $scale; $point[1][2] =  $scale;
$point[2][0] =  $scale; $point[2][1] =  $scale; $point[2][2] =  $scale;
$point[3][0] =  $scale; $point[3][1] = -$scale; $point[3][2] =  $scale;

$point[4][0] = -$scale; $point[4][1] = -$scale; $point[4][2] = -$scale;
$point[5][0] = -$scale; $point[5][1] =  $scale; $point[5][2] = -$scale;
$point[6][0] =  $scale; $point[6][1] =  $scale; $point[6][2] = -$scale;
$point[7][0] =  $scale; $point[7][1] = -$scale; $point[7][2] = -$scale;



$counter = getmicrotime();

$xpos = 5.0*sin($counter*0.25);
$ypos = 5.0*cos($counter*0.75);
$zpos = 10.0*sin($counter*0.5)-20.0;

$xrot = $counter;
$yrot = $counter*0.25;
$zrot = $counter*0.125;

$s1 = sin($xrot);
$s2 = sin($yrot);
$s3 = sin($zrot);
$c1 = cos($xrot);
$c2 = cos($yrot);
$c3 = cos($zrot);

for ($i=0; $i<8; $i++)
{

	$x = $point[$i][0];
	$y = $point[$i][1];
	$z = $point[$i][2];

	$point[$i][0] = $x*($s1*$s2*$s3 + $c1*$c3) + $y*($c2*$s3) + $z*($c1*$s2*$s3 - $c3*$s1);
	$point[$i][1] = $x*($c3*$s1*$s2 - $c1*$s3) + $y*($c2*$c3) + $z*($c1*$c3*$s2 + $s1*$s3);
	$point[$i][2] = $x*($c1*$s2*$s3 - $c3*$s1) + $y*(-$s2)  + $z*($c1*$c2);

	$point[$i][0] += $xpos;	
	$point[$i][1] += $ypos;
	$point[$i][2] += $zpos;
	
	if ($point[$i][2]==0) $point[$i][2]=0.000001;
	
	
	
	$point[$i][0] = 128+((256.0*$point[$i][0])/(float)$point[$i][2]);
	$point[$i][1] = 128+((256.0*$point[$i][1])/(float)$point[$i][2]);
}

header ("Content-type: image/png");
$im = @imagecreate (256, 256) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$wire_color = imagecolorallocate ($im, 0, 0, 0);

imageline($im, $point[0][0], $point[0][1], $point[1][0], $point[1][1], $wire_color);
imageline($im, $point[1][0], $point[1][1], $point[2][0], $point[2][1], $wire_color);
imageline($im, $point[2][0], $point[2][1], $point[3][0], $point[3][1], $wire_color);
imageline($im, $point[3][0], $point[3][1], $point[0][0], $point[0][1], $wire_color);

imageline($im, $point[4][0], $point[4][1], $point[5][0], $point[5][1], $wire_color);
imageline($im, $point[5][0], $point[5][1], $point[6][0], $point[6][1], $wire_color);
imageline($im, $point[6][0], $point[6][1], $point[7][0], $point[7][1], $wire_color);
imageline($im, $point[7][0], $point[7][1], $point[4][0], $point[4][1], $wire_color);

imageline($im, $point[4][0], $point[4][1], $point[0][0], $point[0][1], $wire_color);
imageline($im, $point[5][0], $point[5][1], $point[1][0], $point[1][1], $wire_color);
imageline($im, $point[6][0], $point[6][1], $point[2][0], $point[2][1], $wire_color);
imageline($im, $point[7][0], $point[7][1], $point[3][0], $point[3][1], $wire_color);



imagepng ($im);

?>
Go to top