Skip to content

Instantly share code, notes, and snippets.

@sortofsleepy
Created April 4, 2016 04:48
Show Gist options
  • Save sortofsleepy/b209c3a32f826a9f1574264eca09ca7c to your computer and use it in GitHub Desktop.
Save sortofsleepy/b209c3a32f826a9f1574264eca09ca7c to your computer and use it in GitHub Desktop.
thi.ng demo - just saving some test code for reference
(ns ribbon-attractor.demo
(:require
[matchstick.Geometry :as mgeo]
[matchstick.custom-geometry.Plane :as pgeo]
[matchstick.SimpleGeometry :as sgeo]
[thi.ng.geom.mesh.polyhedra :as poly]
[thi.ng.glsl.core :as glsl :include-macros true :refer-macros [defglsl]]
[thi.ng.math.core :as m :refer [PI HALF_PI TWO_PI]]
[thi.ng.geom.webgl.constants :as glc]
[thi.ng.geom.webgl.animator :as anim]
[thi.ng.geom.webgl.buffers :as buf]
[thi.ng.geom.webgl.shaders :as sh]
[thi.ng.geom.webgl.utils :as glu]
[thi.ng.geom.webgl.glmesh :as glm]
[thi.ng.geom.webgl.camera :as cam]
[thi.ng.geom.core :as g]
[thi.ng.geom.vector :as v :refer [vec2 vec3]]
[thi.ng.geom.matrix :as mat :refer [M44]]
[thi.ng.geom.aabb :as a]
[thi.ng.geom.circle :as c]
[thi.ng.geom.ptf :as ptf]
[thi.ng.glsl.core :as glsl :include-macros true]
[thi.ng.glsl.vertex :as vertex]
[thi.ng.glsl.lighting :as light]
[thi.ng.glsl.fog :as fog]
[thi.ng.color.gradients :as grad]
[thi.ng.geom.attribs :as attr]
[thi.ng.typedarrays.core :as arrays]
[thi.ng.color.core :as col]
[thi.ng.geom.webgl.core :as gl]
[thi.ng.geom.webgl.shaders.basic :as basic]
[thi.ng.geom.rect :as r]
[thi.ng.geom.utils :as gu]
[matchstick.camera :as mcam]))
(def gl (gl/gl-context "CANVAS"))
(def view-rect (gl/get-viewport-rect gl))
(def canvas (.querySelector js/document "#CANVAS"))
(set! view-rect (r/rect 0 0 js/window.innerWidth js/window.innerHeight))
(def camera (cam/perspective-camera {:aspect view-rect
:near 1.0
:far 1000.0
:fov 75}))
(defn updateCameraMatrices []
"resize the camera and canvas to make sure everything looks right"
(aset canvas "width" js/window.innerWidth)
(aset canvas "height" js/window.innerHeight)
(set! view-rect (r/rect 0 0 js/window.innerWidth js/window.innerHeight))
(let [ucam (mcam/camera-updateprojection camera (r/rect 0 0 js/window.innerWidth js/window.innerHeight))]
(set! camera ucam)) )
(.addEventListener js/window "resize" updateCameraMatrices)
(updateCameraMatrices)
(defglsl
vertex
[]
"void main() {
vec3 c = color;
gl_Position = proj * view * model * vec4(position, 1.0);
}")
(defglsl fragment
[]
"void main() {
gl_FragColor = vec4(bcolor,1.0);
}")
(def shader-spec
{:vs (str "void main() {
vec3 c = color;
vec3 n = normal;
vec2 u = uv;
gl_Position = proj * view * model * vec4(position, 1.0);
}")
:fs (glsl/assemble fragment)
:uniforms {:proj :mat4
:view :mat4
:time :float
:bcolor :vec3
:model :mat4}
:attribs {:position :vec3
:normal :vec3
:color :vec3
:uv :vec2}
:varying {}
:state {}})
(def vertices [[1.0 1.0 0.0] [-1.0 1.0 0.0] [1.0 -1.0 0.0] [-1.0 -1.0 0.0]])
(defn MatchstickGeoTest []
(let [geo (-> (mgeo/MakeGeometry {:position {:data (arrays/float32 (flatten vertices)) :size 3}})
(mgeo/setMode glc/triangle-strip)
(mgeo/setShaderSpec shader-spec)
(mgeo/compile))]
(anim/animate
(fn [t frame]
(doto gl
(gl/set-viewport view-rect)
(gl/clear-color-and-depth-buffer col/WHITE 1)
(gl/enable glc/depth-test)
(gl/draw-with-shader
(-> (aget geo "model")
(cam/apply camera)
(assoc-in [:uniforms :model] (-> M44 (g/rotate-y (* t 2))))))
)
true))
))
(defn demo2 []
(let [ucam (cam/set-view camera {:eye (v/vec3 0 0 20)})]
(set! camera ucam))
(let [modelPrep (-> {:attribs {:position {:data (arrays/float32 (flatten vertices)) :size 3}}
:mode glc/triangle-strip
:num-vertices (count vertices)
:shader (sh/make-shader-from-spec gl shader-spec)})
model (-> modelPrep (gl/make-buffers-in-spec gl glc/static-draw))]
(anim/animate
(fn [t frame]
(doto gl
(gl/set-viewport view-rect)
(gl/clear-color-and-depth-buffer col/WHITE 1)
(gl/enable glc/depth-test)
(gl/draw-with-shader
(-> model
(cam/apply camera)
(assoc-in [:uniforms :model] (-> M44 (g/rotate-y (* t 2))))))
)
true))
))
(def isoshape (poly/icosahedron 1))
(defn icoDemo []
(let [iso (sgeo/MakeIcosahedron shader-spec 3)]
(anim/animate
(fn [t frame]
(doto gl
(gl/set-viewport view-rect)
(gl/clear-color-and-depth-buffer col/WHITE 1)
(gl/enable glc/depth-test)
(gl/draw-with-shader
(-> iso
(cam/apply camera)
(assoc-in [:uniforms :model] (-> M44 (g/rotate-y (* t 2)))))))
true))))
(defn PlaneDemo []
(let [plane (pgeo/CreatePlaneVertices js/window.innerWidth js/window.innerHeight 1 1)]
(let [model (->
(glm/IndexedGLMesh.
(:vertices plane)
(:normals plane)
(:normals plane)
(:uvs plane)
(arrays/float32 (* 28))
(:indices plane)
#{:uvs :fnorm :vnorm}
{} (aget (:vertices plane) "length") (aget (:indices plane) "length"))
(gl/as-webgl-buffer-spec {:mode glc/triangles})
(cam/apply (cam/perspective-camera {:aspect view-rect}))
(assoc :shader (sh/make-shader-from-spec gl shader-spec))
(update :uniforms merge {:bcolor (v/vec3 255 255 0 )})
(assoc-in [:uniforms :model] (-> M44 (g/translate 0 0 0) ))
(gl/make-buffers-in-spec gl glc/dynamic-draw)
)
]
(anim/animate
(fn [t frame]
(doto gl
(gl/set-viewport view-rect)
(gl/clear-color-and-depth-buffer col/WHITE 1)
(gl/enable glc/depth-test)
(gl/draw-with-shader
(-> model
(cam/apply camera)
)
)
)
true)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment