Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGL context as dynamic var #19

Open
skrat opened this issue Apr 17, 2015 · 3 comments
Open

WebGL context as dynamic var #19

skrat opened this issue Apr 17, 2015 · 3 comments

Comments

@skrat
Copy link

skrat commented Apr 17, 2015

Currently I have to manage the reference to GL context in every namespace that uses thi.ng.geom.webgl. It would clean the user code a lot if we could remove all the occurrences of gl symbol that are not context creation. Having written a small WebGL library in CLJS myself, I used a dynamic var in the core module, to allow users bind it to their WebGL context somewhere in the app initialization. Then all other namespaces that need GL context, can simply use the symbol required. Eg.

;; core namespace
(ns thi.ng.geom.webgl.core)
(def ^dynamic *gl* nil)

;; any other thi.ng namespace
(ns thi.ng.geom.webgl.buffers
  (:require [thi.ng.geom.webgl.core :refer [*gl*]]))
(defn make-attribute-buffer
  [target draw-type data]
  (let [buffer (.createBuffer *gl*)]
    (.bindBuffer *gl* target buffer)
    (.bufferData *gl* target data draw-type)
    buffer))

;; user code
(ns example.gfx
  (:require [thi.ng.geom.webgl.core :as gl :refer [*gl*]]))
(defn -main [canvas]
  (binding [*gl* (gl/gl-context canvas)]
    ;; very interesting graphics goes here
    ))
@skrat
Copy link
Author

skrat commented Apr 17, 2015

I volunteer to do the refactoring if you like the idea.

@postspectacular
Copy link
Member

Thanks, @skrat - I will have to think about this a bit more how this might impact scenarios using multiple GL contexts (apart from having to wrap all calls in binding forms). Generally I'm with others who argue to stay away from dynamic vars and I personally quite like the explicit nature of knowing what goes into which fn call. Also, adopting a central state pattern as proposed by re-frame makes these things trivial and more flexible...

@skrat
Copy link
Author

skrat commented Apr 18, 2015

Multiple contexts is a no-brainer, even though in most cases you deal with just one, you can nest binding forms and it will always do the right, intuitive thing. It also plays nice with core.async processes. The idea is not to wrap every form in binding, but do it once at the top level. It's the same concept that many clojure.core functions use, eg. the printer. Thank you for accidentally introducing me to re-frame, I'm using reagent, but I somehow missed this, definitely looks interesting. Although I'm not sure how this issue is related to it. Could you elaborate a bit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants