This is a program to generate object descriptions (written to the standard output). Depending on the argument passed to it, shape generates a cylinder, sphere, or "fractal" mountain slope. The program takes two arguments: the shape to be approximated and an integer specifying the level of approximation. For example:
$ ./shape cylinder 64 > cylinder.obj
$ ./shape sphere 64 > sphere.obj
$ ./shape fractal 6 > fractal.obj
generates object descriptions with a fairly high level of approximation for all shapes and saves their object descriptions to a corresponding file. Since there's not much to be said about the sphere and cylinder, I'll say some stuff about the fractal landscape.
First of all, I'm not so pleased with the way it turned out. Had I found a better method to create unique vertices, I think it would have turned out a bit better. That said, here's what it's doing. Basically, an equilateral triangle is subdivided n times by drawing lines between the midpoints of the sides of the triangle and then between the midpoints of the sides of the new triangles creates by those lines, etc. During each subdivision, however, the vertical component of each vertex is wiggled slightly up.
In addition, the general "roughness" of the terrain (that is, the height of the wiggle) is different for each terrain generated. To create the effect of a mountain, a point in the (x,y) plane is picked at random as the location of the "peak". The distance of the peak from the midpoints of the triangles that make up the terrain determines how that each particular triangle is scaled. Those vertices that are closer to the peak are scaled up more than those farther away. In addition, the height of the peak is picked at random each time the program is run. My only compaint is that the recursion in the program leaves a bit of a residue in the mountain. Sometimes, if you look hard enough, it's easy to see the sections of the mountain that were the product of more deeply nested recursive calls. See the picture for an example:
For the artistic fun, I added one more feature. The triangles that make up the mesh are either oriented upward or downward. The program flips a coin each time it is run for each orientation to see if those triangles are shaded on their upward-facing side or their downward-facing side. Sometimes all triangles are shaded to form a mountain, sometimes none are shaded to form a crater when the mesh is turned over, and sometimes it's half and half for a neat effect:
Anyway, it's fun to play with. I wouldn't go past 7 levels of recursion though unless you're the patient type (to guarantee unique, ordered vertices I had to do a bit of sorting and didn't feel inspired to quicksort them...)
dorito.c
I chose flatshading over a wireframe rendering for my postscript output, since hidden surface removal was then easier. The faces are ordered according to depth such that those faces farthest from the camera are drawn first and those closer to the camera are drawn next until the closest face has been drawn. Since the faces aren't transparent and the projection is orthogonal, those faces blocked by the the faces in front of them are drawn but not visible. In addition, it is assumed that polygons do not intersect (the al model has some slightly intersecting polygons on the right side of his head that my approach does not address). Again, I shunned quicksort for this since, well, the computer is fast.
Cheers.
|