;; ==============================================================================
;; Amplifocus Art / WebGL Animation Library
;; ==============================================================================

(require "libs/webgl/src/webgl.coni" :all)
(require "utils.coni" :all)

(def document (js/global "document"))
(def window (js/global "window"))



(def *time* (atom 0.0))
(def *gl-state* (atom nil))
(def *num-pts-val* (atom 0))

(def default-fs-src
  "precision mediump float;\nvarying vec4 v_color;\nvoid main() {\n  vec2 coord = (gl_PointCoord - vec2(0.5, 0.5));\n  float dist = length(coord);\n  if ((dist > 0.5)) {\n    discard;\n  };\n  float glow = (1.0 - (dist * 2.0));\n  vec4 final_color = (v_color * glow);\n  gl_FragColor = final_color;\n}")



(defn init-webgl [canvas vs-src fs-src]
  (let [gl (.getContext canvas "webgl")]
    (if (not gl)
      nil
      (let [vs (gl-shader gl (.-VERTEX_SHADER gl) vs-src)
            fs (gl-shader gl (.-FRAGMENT_SHADER gl) fs-src)
            prog (gl-program gl vs fs)]
            
        (.useProgram gl prog)
        (.enable gl (.-BLEND gl))
        (.blendFunc gl (.-SRC_ALPHA gl) (.-ONE gl))
        
        (let [buf (.createBuffer gl)
              pos-loc (.getAttribLocation gl prog "a_pos")
              u-time-loc (.getUniformLocation gl prog "u_time")
              u-res-loc (.getUniformLocation gl prog "u_resolution")]
          (.bindBuffer gl (.-ARRAY_BUFFER gl) buf)
          
          {:gl gl
           :prog prog
           :pos-loc pos-loc
           :u-time-loc u-time-loc
           :u-res-loc u-res-loc
           :buf buf})))))

(defn upload-static-data [arr]
  (let [state @*gl-state*
        gl (:gl state)
        pos-loc (:pos-loc state)]
    (.bufferData gl (.-ARRAY_BUFFER gl) arr (.-STATIC_DRAW gl))
    (.enableVertexAttribArray gl pos-loc)
    (.vertexAttribPointer gl pos-loc 4 (.-FLOAT gl) false 16 0)))

(defn draw-webgl [state t num-pts w h]
  (let [gl (:gl state)
        u-time-loc (:u-time-loc state)
        u-res-loc (:u-res-loc state)]
    (.viewport gl 0 0 w h)
    (.clearColor gl 0.015 0.015 0.04 1.0)
    (.clear gl (.-COLOR_BUFFER_BIT gl))
    (.uniform1f gl u-time-loc t)
    (.uniform2f gl u-res-loc w h)
    (.drawArrays gl (.-POINTS gl) 0 num-pts)))

(defn render-loop []
  (let [w (.-innerWidth window)
        h (.-innerHeight window)
        t @*time*
        canvas (get-el "visual-canvas")]
        
    (when (not= (.-width canvas) w) (.-width canvas w))
    (when (not= (.-height canvas) h) (.-height canvas h))
    
    (draw-webgl @*gl-state* t @*num-pts-val* w h)
    (reset! *time* (+ t 0.01)))
  (.requestAnimationFrame window render-loop))

(defn start-scene! "Initializes the WebGL scene." [num-pts vs-src fs-src base-points-buf]
  (let [canvas (get-el "visual-canvas")]
    (reset! *num-pts-val* num-pts)
    (reset! *gl-state* (init-webgl canvas vs-src fs-src))
    (js/on-event canvas :click request-fullscreen)
    (upload-static-data base-points-buf)
    (.requestAnimationFrame window render-loop)))
