The first transformation we can do with a matrix. It's simply a matter multiplying every point in the world by a translation matrix generated from the inverse of the viewer's location. The rotation of the world to align with the orientation of the viewer can be done by taking advantage of a useful result from linear algebra, which tells us how to simply make a matrix from three mutually orthoganal unit vectors which will align each of those unit vectors with one of the three axies, bringing the rest of the world along with it.
While it is not necessary for this projection, it is useful to scale everything visible to be within a unit cube, the cannonical view volume. When all points are in this coordinate system, clipping becomes both simple and fast. Getting the points into this coordinate system is a matter of multiplying them by appropriate scale and translate matricies.
To add perspective we cannot use only matricies. We would need a single matrix that could be used on all points and would modify each point's x and y coordinates on the basis of their z coordinate. What we can do instead is modify the fourth or homogeneous coordinate of our points with a matrix and then later, when it comes time to draw the point or the structure containing that point, divide the x and y coordinates by the homogeneous coordinate.
All of these manipulations are matrix manipulations, with the exception of part of the perspective calculation, and they can (almost) all be combined into one matrix by which every point is multipled. This is pretty efficient, as we need calculate only one matrix and need do only on matrix-vector multiplication for each point. In hardware this would be better, as matrix multiplications parallelize well.
Module_addLineCube(Module* md, double
center_x, double center_y, double
center_z, double radius);
with an accompanying command wirecube x y z :
radius
for the module file format. I also wrote a
function for adding spheres:
Module_addLineSphere(Module* md, double
center_x, double center_y, double
center_z, double radius, double resoltion);
with an accompanying file format command command
wiresphere x y z : radius : resolution
. This
sphere is made by starting with a tetrahedron and recursively
breaking the lines at their midpoints, pushing these
midpoints out to the surface of the sphere, and connecting
the midpoints to make new lines. The
resolution
argument indicates how many breaks
to make. The images below, with links to movies, show the
sphere approximations for resolution
values
of 1 through 5.