rendering a sierpinski pyramid in clojure

A three-dimensional Sierpinski triangle ( http://en.wikipedia.org/wiki/Sierpinski_triangle ) is created recursively, by replacing each pyramid with five sub-pyramids. To create one, we use Penumbra ( http://github.com/ztellman/penumbra/tree/master ), which is a thin but idiomatic wrapper for OpenGL.

First, we create a pyramid.

(defn draw-pyramid [] 
  (material 0.8 0.2 0.2 1) 
  (draw-triangle-fan 
   (vertex 0 1 0) 
   (dotimes [_ 5] 
    (rotate 90 0 1 0) 
    (normal 1 0.5 1) 
    (vertex 0.5 0 0.5))) 
  (draw-quads 
   (normal 0 -1 0) 
   (dotimes [_ 4] 
    (rotate -90 0 1 0) 
    (vertex 0.5 0 0.5)))) 
The pyramid is defined in two parts: a fan of triangles around the tip, and a square at the bottom. Both are defined by rotating iteratively around the central axis.
 (draw-pyramid) 

Next, we turn one pyramid into five smaller pyramids. 

(defn subdivide [display-list] 
  (push-matrix 
   (scale 0.5 0.5 0.5) 
   (push-matrix 
    (translate 0 1 0) 
    (call-display-list display-list)) 
   (dotimes [_ 4] 
    (rotate 90 0 1 0) 
    (push-matrix 
     (translate 0.5 0 0.5) 
     (call-display-list display-list)))))
The subdivide function takes a display list as its argument, which is a cached list of OpenGL calls. This list is called five times, first at the top and then four times around the central axis.
(subdivide (get-display-list (draw-pyramid)))

We can call subdivide as many times as we like, but Clojure gives us a superior approach, by way of lazy sequences. 



(defn sierpinski [] 
  (iterate 
    #(get-display-list (subdivide %)) 
    (get-display-list (draw-pyramid)))) 
The 'iterate' function creates a lazy sequence based on a function that takes n and returns n+1. On each iteration, the previous display list is shrunk to half its size on all dimensions, and drawn five times. This lets us generate as much geometry as we need, and no more.
(call-display-list (nth (sierpinski) 2))

(call-display-list (nth (sierpinski) 6))

Complete code can be found in the /examples subfolder in the Penumbra repository.

saturday morning gestalts

       

becoming eloi, becoming morlock

Technology begets technologists. The technologist is created by the realization that the easy cause and effect presented by technology - the press of a key placing a letter on the screen, the push of the button carrying the hero across the screen - hides something more. He teases apart the complexities, and discovers that the pieces are themselves abstractions, waiting to be understood. Suddenly, unexpectedly, there is no limit to what he does not know. 

And so, he begins to delve into the unknown, trying to find what lies at the bottom. But early on, he discovers that what he can put together is not limited to what he has taken apart. Even with incomplete knowledge, standing on convenient half-truths, he can create something entirely new. 

Fundamental truths become less important in the act of creation. The lesser abstractions congeal, become atomic, and are combined into something greater than their sum. Technology is the creation of an abstraction from concrete pieces. 

In H.G. Wells' The Time Machine, the future is populated by two races. There are the Eloi, who live on the surface in a garden paradise, and the Morlock, who toil below to provide the Eloi everything they need. And sometimes, at night, the Morlock ascend to the surface and feed upon the Eloi. The Eloi are the consumers of technology, living in a world of false simplicity. The Morlock are the producers of technology, laboring to create an illusion because it gives them something they need. 

But technology is not a discrete thing, it is a continuum. The technologist creates new abstractions, but to do so he must build upon existing foundations. To produce, he must consume. He must inhabit both roles, become Eloi and Morlock, but he cannot be both at once. There is a tension between knowledge and creation: by pursuing one, he retreats from the other. At first, this doesn't seem to present a problem; the avenues are endless, but so is the time to pursue them. But inevitably, the choices made early on shape those that follow. Time begins to feel increasingly scarce, familiarity is conflated with aptitude, and what was once an open landscape becomes a long, narrow path. 

The technologist is defined by his limitations. To create anything, he must understand the breadth of his ignorance, and how to work within the boundaries that he has constructed around himself. But understanding these limitations does not lessen the desire to escape them. The technologist spends his entire life in pursuit of moments of clarity. He is at his happiest and most productive when he holds the entire schema of his creation in his head, and nothing else exists. It is a toy universe, but it is crystalline and whole and in that instant the technologist's dream is realized: he understands his own creation, completely. These moments are fleeting, existing only as long as he can deny the existence of everything he doesn't understand. But they are enough, driving him forward towards something he will never achieve, and giving him comfort in what he creates along the way.

testing, testing

About