Skip to content

Instantly share code, notes, and snippets.

@sgrove
Created June 16, 2015 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgrove/835e5f74a234ebbc47ea to your computer and use it in GitHub Desktop.
Save sgrove/835e5f74a234ebbc47ea to your computer and use it in GitHub Desktop.
bufferSubData Success!
(defn tick
"Takes the old world value and produces a new world value, suitable
for rendering"
[state time]
;; We get the elapsed time since the last render to compensate for
;; lag, etc.
(let [time-now (.getTime (js/Date.))
elapsed (- time-now (:last-rendered state))
pyramid-diff (/ (* 50 elapsed) 100000)
cube-diff (/ (* 75 elapsed) 100000)
driver (get-in state [:live :driver])
gl (:gl driver)
array-buffer (-> (:backing-driver driver)
:resource-state
deref
:monkey-instance-offsets
:array-buffer)
monkey-idx (- (get-in state [:scene :selected-id]) 2)
monkey-offsets (-> (get-in state [:scene :monkeys :offsets])
(assoc-in [(inc monkey-idx)] (* 5 (js/Math.sin (/ time-now 1000)))))
offset (+ (* monkey-idx 3 4) 4)
new-y (get-in monkey-offsets [(inc monkey-idx)])
value (js/Float32Array. #js[new-y])]
;; Make sure we have an object selected before we try to buffer
;; data at some offset
(when (and (not (zero? (:last-rendered state)))
array-buffer
(pos? (get-in state [:scene :selected-id]))
monkey-idx)
(.bindBuffer gl ggl/ARRAY_BUFFER array-buffer)
(.bufferSubData gl ggl/ARRAY_BUFFER offset value)
(.bindBuffer gl ggl/ARRAY_BUFFER nil))
(-> state
(update-in [:scene :pyramid-rotation] + pyramid-diff)
(update-in [:scene :cube-rotation] + cube-diff)
(assoc-in [:scene :monkeys :offsets] monkey-offsets)
(assoc-in [:last-rendered] time-now))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment