 
 ;; not bad!
 
-(def step-size 0.02)
+(def step-size 0.1)
 
 (def start
-  [(v3 0 0 0)])
+  {:trail [(v3 0 0 0)]})
 
-(defn update [trail]
-  (let [last-point (last trail)]
-    (conj trail (v3+ last-point (rand-point step-size)))))
+(defn constrain [trail length]
+  (->> trail
+       (take-last length)
+       vec))
 
-(defn draw [trail]
-  [t/basic-scene {:width 600 :height 600}
-   [line {:color 0xffffff} trail]])
+(defn grow
+  ([trail] (grow trail (last trail)))
+  ([trail p] (conj trail p))
+  ([trail p r] (conj trail (v3+ p r))))
 
-(defonce sketch (zajal/sketch start update draw))+(defn grow-random [trail]
+  (grow trail (first trail) (rand-point step-size)))
+
+(defn remap [v low1 high1 low2 high2]
+  (+ low2
+     (/ (* (- v low1)
+           (- high2 low2))
+        (- high1 low1))))
+
+(defn grow-on-mouse [trail {:keys [mouse]}]
+  (if (mouse :pressed?)
+    (-> trail (grow (v3 
+                      (remap (-> mouse :x) 0 600 -0.5 0.5)
+                      (remap (-> mouse :y) 0 600 0.5 -0.5)
+                      0)))
+    trail))
+
+(defn turn-trail [trail]
+  (let [axis (v3 0 1 0)]
+    (map #(apply-angle-axis % axis 0.01) trail)))
+
+(defn step [state input]
+  (-> state
+      (update :trail #(-> %
+                          grow-random
+                          ; (grow-on-mouse input)
+                          turn-trail
+                          ))))
+
+(defn draw [{:keys [trail]}]
+  [:div 
+   ; [:p {} (str (-> hello-triangle :fragment-shader :glsl))]
+   [t/basic-scene {:width 600 :height 600}
+    ; [line {:color 0xffffff} trail]
+    [shaded-line trail]
+    ]])
+
+(defonce sketch (zajal/sketch start #'step #'draw))
+


(defn v3+ [a b]
  (.. a clone (add b)))

(defn line [{:keys [color]
             :or {color 0xffffff}}

(ns zajal.brownian
  (:require [zajal.three :as t :refer [renderer euler scene camera v3]]
            [zajal.core :as zajal]
            [gamma.api :as g]
            [gamma.program :as p]))

(defn rrand [a b]
  (- (rand (- a b)) a))
