Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This script draws and outputs a Cube.
- *
- * The GD library is used to draw the cube and output it as a
- * PNG image.
- *
- * Developed by Leonel Machava <leonelmachava@gmail.com>
- * http://codentronix.com
- * http://codentronix.com/2011/06/06/how-to-draw-a-cube-using-php/
- *
- * This code is released under the "MIT License" available at
- * http://www.opensource.org/licenses/mit-license.php
- * http://codentronix.com/2011/06/06/how-to-draw-a-cube-using-php/
- */
- /* Represents points in 3D space. */
- class Point3D {
- public $x;
- public $y;
- public $z;
- public function __construct($x,$y,$z) {
- $this->x = $x;
- $this->y = $y;
- $this->z = $z;
- }
- public function rotateX($angle) {
- $rad = $angle * M_PI / 180;
- $cosa = cos($rad);
- $sina = sin($rad);
- $y = $this->y * $cosa - $this->z * $sina;
- $z = $this->y * $sina + $this->z * $cosa;
- return new Point3D($this->x, $y, $z);
- }
- public function rotateY($angle) {
- $rad = $angle * M_PI / 180;
- $cosa = cos($rad);
- $sina = sin($rad);
- $z = $this->z * $cosa - $this->x * $sina;
- $x = $this->z * $sina + $this->x * $cosa;
- return new Point3D($x, $this->y, $z);
- }
- public function rotateZ($angle) {
- $rad = $angle * M_PI / 180;
- $cosa = cos($rad);
- $sina = sin($rad);
- $x = $this->x * $cosa - $this->y * $sina;
- $y = $this->x * $sina + $this->y * $cosa;
- return new Point3D($x, $y, $this->z);
- }
- public function project($width,$height,$fov,$viewerDistance) {
- $factor = (float)($fov) / ($viewerDistance + $this->z);
- $x = $this->x * $factor + $width / 2;
- $y = -$this->y * $factor + $height / 2;
- return new Point3D($x,$y,$this->z);
- }
- }
- class Cube3DImage{
- public function __construct($width,$height,$depth){
- $this->width=$width;
- $this->height=$height;
- $this->depth=$depth;
- $this->rotate_x=0;
- $this->rotate_y=0;
- $this->rotate_z=0;
- $this->image_width=90;
- $this->image_height=90;
- $this->fov=256;
- $this->view_distance=4;
- $this->x_color=array(140,140,140);
- $this->y_color=array(192,192,192);
- $this->z_color=array(220,220,220);
- $this->bg_color=array(249, 249, 249);
- }
- function rotate($axis,$val){
- if($axis=='x'){$this->rotate_x=strtolower($val);}
- else if($axis=='y'){$this->rotate_y=strtolower($val);}
- else if($axis=='z'){$this->rotate_z=strtolower($val);}
- }
- function image(){
- /*
- Z (+1z)
- .
- g. . , h |
- : f_ _._ e .
- , | ,| |
- c; ,| . ;|<-d .
- ` ,|_ _ _| |
- b a .
- |
- .
- |
- .
- |
- .
- |
- ,
- . / * \ ,
- , / ` .
- / ` \ .
- / ` ` \ .
- (+1x) X Y (+1y)
- */
- $vertices = array(
- 'a'=>new Point3D((abs($this->width/2)*(-1)), (abs($this->depth/2)), (abs($this->height/2)*(-1))), //a -> 0 = right, front, bottom
- 'b'=>new Point3D((abs($this->width/2)), (abs($this->depth/2)), (abs($this->height/2)*(-1))), //b -> 1 = left, front, bottom
- 'c'=>new Point3D((abs($this->width/2)), (abs($this->depth/2)*(-1)), (abs($this->height/2)*(-1))), //c -> 2 = left, back, bottom
- 'd'=>new Point3D((abs($this->width/2)*(-1)), (abs($this->depth/2)*(-1)), (abs($this->height/2)*(-1))), //d -> 3 = right, back, bottom
- 'e'=>new Point3D((abs($this->width/2)*(-1)), (abs($this->depth/2)), (abs($this->height/2))), //e -> 4 = right, front, top
- 'f'=>new Point3D((abs($this->width/2)), (abs($this->depth/2)), (abs($this->height/2))), //f -> 5 = left, front, top
- 'g'=>new Point3D((abs($this->width/2)), (abs($this->depth/2)*(-1)), (abs($this->height/2))), //g -> 6 = left, back, top
- 'h'=>new Point3D((abs($this->width/2)*(-1)), (abs($this->depth/2)*(-1)), (abs($this->height/2))) //h -> 7 = right, back, top
- );
- $faces = array(
- 'bottom'=>array('a', 'b', 'c', 'd'),// BOTTOM SIDE (a,b,c,d)
- 'left'=>array('b', 'f', 'g', 'c'),// LEFT SIDE (b,f,g,c)
- 'top'=>array('f', 'e', 'h', 'g'),// TOP SIDE (f,e,h,g)
- 'right'=>array('e','a', 'd', 'h'),// RIGHT SIDE (e,a,d,h)
- 'front'=>array('a', 'e', 'f', 'b'),// FRONT SIDE (a,e,f,b)
- 'back'=>array( 'd', 'c', 'g', 'h') // BACK SIDE (d,c,g,h)
- );
- $im = imagecreatetruecolor($this->image_width, $this->image_height);
- imagefill($im , 0, 0, imagecolorallocate($im, $this->bg_color[0], $this->bg_color[1], $this->bg_color[2]));//background color
- $colors = array(
- 'bottom'=>$this->z_color,
- 'left'=>$this->x_color,
- 'top'=>$this->z_color,
- 'right'=>$this->x_color,
- 'front'=>$this->y_color,
- 'back'=>$this->y_color
- );
- foreach($colors as $k => $v){
- $im_colors[$k] = imagecolorallocate($im,$v[0],$v[1],$v[2]);
- }
- /* It will store transformed vertices. */
- $transform = array();
- foreach($vertices as $k => $v){
- $transform[$k] = $v->rotateX($this->rotate_x)->rotateY($this->rotate_y)->rotateZ($this->rotate_z)->project($this->image_width, $this->image_height, $this->fov, $this->view_distance);/* Transform all the vertices. */
- }
- $avgZ = array();
- /* Calculate the average Z value of each face. */
- //$faces['bottom'] $faces['left'] $faces['top'] $faces['right'] $faces['front'] $faces['back']
- foreach($faces as $k => $v){// as $k => $v
- $avgZ[$k] = ($transform[$v[0]]->z + $transform[$v[1]]->z + $transform[$v[2]]->z + $transform[$v[3]]->z) / $this->view_distance;//8/30/2011 ADJUSTED??!?
- }
- arsort($avgZ);
- foreach($avgZ as $k => $v){/* Draw the faces from back to front. */
- $_face = $faces[$k];//'bottom'=>, 'left'=>, 'top'=>, 'right'=>, 'front'=>, 'back'=>
- $points = array(
- $transform[$_face[0]]->x, $transform[$_face[0]]->y, // 0,1 => x1, y1
- $transform[$_face[1]]->x, $transform[$_face[1]]->y, // 2,3 => x2, y2
- $transform[$_face[2]]->x, $transform[$_face[2]]->y, // 4,5 => x3, y3
- $transform[$_face[3]]->x, $transform[$_face[3]]->y // 6,7 => x4, y4
- );
- imagefilledpolygon($im,$points,4,$im_colors[$k]);
- }
- header("Content-Type: image/png");/* Tell the browser/client we are outputing a PNG image. */
- imagepng($im);/* Output the image. */
- }
- }
- $dim = array();
- $dim['width']=14;//x cord
- $dim['height']=14;//z cord
- $dim['depth']=36;//y cord
- $cubeOBJ=new Cube3DImage($dim['width'],$dim['height'],$dim['depth']);
- $cubeOBJ->image_width=960;
- $cubeOBJ->image_height=960;
- $cubeOBJ->fov=256;
- $cubeOBJ->view_distance=4;
- if(@array_key_exists('fov',$_GET)){$cubeOBJ->fov=intVal($_GET['fov']);}
- if(@array_key_exists('dist',$_GET)){$cubeOBJ->view_distance=intVal($_GET['dist']);}
- /* Assign random values for the angles that describe the cube orientation. */
- $angleX = 40;// up and down horizontally
- $angleZ = 0;// side to side horizontally
- $angleY = 20;// tilt (tip/roll)
- $cubeOBJ->rotate('x', $angleX);
- $cubeOBJ->rotate('y', $angleY);
- $cubeOBJ->rotate('z', $angleZ);
- $cubeOBJ->image();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement