 (ns zajal.brownian
-  (:refer-clojure :exclude [update])
-  (:require [zajal.three :as t :refer [renderer scene camera v3]]
-            [zajal.core :as zajal]))
+  (: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))


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

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


;; not bad!

(def step-size 0.1)

(def start
  {:trail [(v3 0 0 0)]})

(defn constrain [trail length]
  (->> trail
       (take-last length)
       vec))

(defn grow
  ([trail] (grow trail (last trail)))
  ([trail p] (conj trail p))
  ([trail p r] (conj trail (v3+ p r))))

(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))

