How to Draw A Cube Using PHP

PHP is one of the most used languages for web development, and it is often used to generate HTML pages. Today, I will show you how to use it to make something different. We will make a script to draw a 3D cube.

The image below shows an output from the PHP script. Click the image to draw another cube.

NOTE: PHP is often used to output text content, however it can also output images (really, it can output pretty much anything). To draw images I relied on the GD library. If you have never used the GD library, consider reading the tutorial Generating Images with PHP.

Code

Before checking the code let me remember you that a cube is made of 8 vertices and 6 faces. I guess the code is pretty much self-explanatory, so I will not give too much detail.

When drawing the cube, we must draw only the faces that are visible. In 3D computer graphics theory, this is called the “visibility problem”, and there are some algorithms, called hidden surface algorithms, which solve this problem. The Painter’s Algorithm is one of them, and solves the problem drawing the faces from back to front. To do this, we calculate the average Z value of each face, then we sort them, and finally we draw them in descending order of the Z value.

Hey this is great…
The code is prettier. Good job.
I tried it and it is fine…

Well, I had to run it with the dist parameter set to 35. The default value (of 4) made the cube appear drastically distorted. This happens because after the transformations some parts of the cube fall out of the image (screen space). It is very cool, though.
To test this out don’t forget to set dist parameter to something greater or equal to 35. Doing something like test.php?dist=35

Ya, I was trying to make sure the changes I made were consistent. I was trying to solve the distortion issue but you can do some funky stuff to cameras so I wasn’t entirely sure how to deal with it.

For what I was using it for… it was working very well.

Any reason for 35? I saw it was a ‘factor’ between distance and field of view. So any combinations of numbers could cause distortion, I found distortion only happened in low numbers (I used numbers in the 1000′s).

The distortion happens because you are defining the vertices of the cube with high numbers. In the original code I used 1 and -1 to define the vertices coordinates. You are using 14 and 36.
So, the view distance must be higher in order for the object to be seen perfectly. Otherwise, when the vertices are transformed they end up with negative coordinates or with coordinates higher than the width or height of the image. Another solution would be to lower the value of field of view.

Using 14 and 36 for vertex definition you don’t get a cube as a result. The result is more a prism or parallelepiped. To get a cube you should use (14, -14) or (36, -36).

Lines 149-150: the division should be by 4.0 and not by view_distance.

I’ve been exploring for a bit for any high quality articles or blog posts in this sort of house . Exploring in Yahoo I ultimately stumbled upon this site. Studying this info So i am glad to exhibit that I’ve a very just right uncanny feeling I discovered just what I needed. I such a lot without a doubt will make sure to don?t overlook this web site and give it a glance regularly.