Skip to content

Instantly share code, notes, and snippets.

Created March 8, 2017 03:11
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 anonymous/e5a9075a8f9c4f20eba7be22c8770915 to your computer and use it in GitHub Desktop.
Save anonymous/e5a9075a8f9c4f20eba7be22c8770915 to your computer and use it in GitHub Desktop.
(ns c.g.scratch
(:require-macros [cljs.core.async.macros :refer [go go-loop]]
[c.u.q :refer [defq qi qn]])
(:require [clojure.string]
[oops.core :refer [ocall oget oset!]]
[cljs.core.async :as a]
[c.g.shader]
[c.u.spec]
[cljsjs.opentype]
[dirac.runtime]
[devtools.core]))
(dirac.runtime/install!)
(devtools.core/install!)
(let [v2 (qn :vec2 20 20)
d2 (qn :dim2 23 58)
bb (qn :bbox v2 d2)
x (qi bb [:bbox/vec2])])
(def vertex-source
(clojure.string/join
"\n"
["#version 300 es"
"#define POSITION_LOCATION 0"
"precision highp float;"
"precision highp int;"
"layout(location = POSITION_LOCATION) in vec2 position;"
"void main() {"
" gl_Position = vec4(position, 0.0, 1.0);"
"}"]))
(def fragment-source
(clojure.string/join
"\n"
[
"#version 300 es"
"precision highp float;"
"precision highp int;"
"uniform sampler2D diffuse;"
"uniform vec2 u_imageSize;"
"out vec4 color;"
"void main() {"
" color = vec4(1, 0, 1, 0.5);"
"}"]))
;//texture(diffuse, vec2(gl_FragCoord.x, 1.0 - gl_FragCoord.y) / u_imageSize);
;; (def font-chan (a/chan))
;;
;; (defn load-opentype [fname c]
;; (js/opentype.load
;; fname
;; (fn [err font]
;; (if err
;; (go (a/>! c [:err err]))
;; (go (a/>! c [:font font]))
;; ;; (op-on-font font)
;; ) )))
;; (load-opentype "kafonts/KaTeX_Typewriter-Regular.ttf" font-chan)
(defn gen-canvas [;; font
]
(let [;; glyphs (js->clj (oget font "glyphs.glyphs"))
canvas (ocall js/document "createElement" "canvas")
_ (oset! canvas "width" 1024)
_ (oset! canvas "height" 1024)
ctx (ocall canvas "getContext" "2d")]
;; (doseq [[i [asc glyph]] (keep-indexed vector (sort glyphs ))
;; :let [y (quot i 32)
;; x (rem i 32)]]
;; (ocall glyph "draw" ctx (* x 64) (* y 128) 64))
canvas))
(defn run []
(go
(let [;; [t font] (a/<! font-chan)
canvas (ocall js/document "getElementById" "c")
_ (oset! canvas "width" 2048)
_ (oset! canvas "height" 2048)
gl (ocall canvas "getContext" "webgl2")
_ (assert gl)
vertex-shader (c.g.shader/create-shader gl (.-VERTEX_SHADER gl) vertex-source)
fragment-shader (c.g.shader/create-shader gl (.-FRAGMENT_SHADER gl) fragment-source)
program (c.g.shader/create-program gl vertex-shader fragment-shader)
diffuse-location (ocall gl "getUniformLocation" program "diffuse")
image-size-location (ocall gl "getUniformLocation" program "u_imageSize")
vertex-pos-buffer (ocall gl "createBuffer")
_ (ocall gl "bindBuffer" (oget gl "ARRAY_BUFFER") vertex-pos-buffer)
vertex-count 12
vertices (js/Float32Array.
(clj->js
[-0.8, -0.8,
0.8, -0.8,
0.8, 0.8,
0.8, 0.8,
-0.8, 0.8,
-0.8, -0.8,
-0.5, -0.5,
0.5, -0.5,
0.5, 0.5,
0.5, 0.5,
-0.5, 0.5,
-0.5, -0.5,]))
_ (ocall gl "bufferData"
(oget gl "ARRAY_BUFFER")
vertices
(oget gl "STATIC_DRAW"))
vertex-array (ocall gl "createVertexArray")
_ (ocall gl "bindVertexArray" vertex-array)]
(ocall gl "bindVertexArray" vertex-array)
(ocall gl "bindVertexArray" nil)
(let [texture (ocall gl "createTexture")
canvas (gen-canvas ;; font
)
vertex-pos-location 0]
(ocall gl "activeTexture" (oget gl "TEXTURE0"))
(ocall gl "bindTexture" (oget gl "TEXTURE_2D") texture)
(ocall gl "pixelStorei" (oget gl "UNPACK_FLIP_Y_WEBGL") false)
(ocall gl "texImage2D" (oget gl "TEXTURE_2D") 0 (oget gl "RGBA") (oget gl "RGBA") (oget gl "UNSIGNED_BYTE") canvas)
(ocall gl "texParameteri" (oget gl "TEXTURE_2D") (oget gl "TEXTURE_MAG_FILTER") (oget gl "LINEAR"))
(ocall gl "texParameteri" (oget gl "TEXTURE_2D") (oget gl "TEXTURE_MIN_FILTER") (oget gl "LINEAR"))
(ocall gl "clearColor" 0 0 0 1)
(ocall gl "clear" (oget gl "COLOR_BUFFER_BIT"))
(ocall gl "useProgram" program)
(ocall gl "uniform1i" diffuse-location 0)
(ocall gl "uniform2f" image-size-location 1024 1024)
(ocall gl "enableVertexAttribArray" vertex-pos-location)
(ocall gl "vertexAttribPointer" vertex-pos-location 2 (oget gl "FLOAT") false 0 0)
(ocall gl "bindVertexArray" nil )
(ocall gl "bindVertexArray" vertex-array)
(ocall gl "bindVertexArray" vertex-array)
(ocall gl "drawArrays" (oget gl "TRIANGLES") 0 (/ vertex-count 2))
(ocall gl "deleteTexture" texture)
(ocall gl "deleteProgram" program)
(ocall gl "deleteVertexArray" vertex-array)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment