Skip to content

Instantly share code, notes, and snippets.

@Madsy
Created July 6, 2012 19:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Madsy/3062242 to your computer and use it in GitHub Desktop.
(ns example.hello)
(def vertexShader (atom 0))
(def fragmentShader (atom 0))
(def shaderProgram (atom 0))
(def bufferObject (atom 0))
(defn clear [gl]
(do
(. gl clearColor 1.0 0.0 0.0 1.0)
(. gl clear (.-COLOR_BUFFER_BIT gl))))
(defn initShaders [gl]
(let [vertexShaderStr "attribute vec3 vertexPos;\n\nvoid main(void)\n{\ngl_Position = vec4(vertexPos, 1.0);\n}\n"
fragmtShaderStr "precision mediump float;\n\nvoid main(void)\n{\ngl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n}\n"
psh (. gl createProgram)
vsh (. gl createShader (.-VERTEX_SHADER gl))
fsh (. gl createShader (.-FRAGMENT_SHADER gl))]
(do
(reset! shaderProgram psh)
(reset! vertexShader vsh)
(reset! fragmentShader vsh)
(. gl shaderSource vsh vertexShaderStr)
(. gl compileShader vsh)
(. gl attachShader psh vsh)
(. gl shaderSource fsh fragmtShaderStr)
(. gl compileShader fsh)
(. gl attachShader psh fsh)
(. gl linkProgram psh)
(. gl useProgram psh)
(let [attribPos (. gl getAttribLocation psh "vertexPos")]
(if (= attribPos -1)
(.log js/console "Invalid attribute")
(do
(.log js/console "Valid attribute" (str attribPos))
(. gl enableVertexAttribArray attribPos))
)))))
(defn initBuffers [gl]
(let [vbo (. gl createBuffer)
vertices (array 1.0,-1.0, 0.0
0.0, 1.0, 0.0
-1.0,-1.0, 0.0)]
(do
(. gl bindBuffer (.-ARRAY_BUFFER gl) vbo)
(. gl bufferData (.-ARRAY_BUFFER gl) (new js/Float32Array vertices) (.-STATIC_DRAW gl))
(reset! bufferObject vbo))))
(defn render [gl]
(do
(clear gl)
(. gl bindBuffer (.-ARRAY_BUFFER gl) @bufferObject)
(let [attribPos (. gl getAttribLocation @shaderProgram "vertexPos")]
(. gl vertexAttribPointer attribPos 3 (.-FLOAT gl) false 0 0))
(. gl drawArrays (.-TRIANGLES gl) 0 3)))
(defn webGLStart []
(let [canvas (.getElementById js/document "myWebGLCanvas")
gl (. canvas getContext "experimental-webgl")]
(do
(initShaders gl)
(initBuffers gl)
(render gl))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment