Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Last active August 29, 2015 14:22
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 postspectacular/b62565c0e788e8f27dfd to your computer and use it in GitHub Desktop.
Save postspectacular/b62565c0e788e8f27dfd to your computer and use it in GitHub Desktop.
geom-webgl mesh handling with & without vertex attributes (temp. workaround until fully implemented)
(require
'[thi.ng.geom.core :as g]
'[thi.ng.geom.core.vector :as v :refer [vec2 vec3]]
'[thi.ng.geom.core.matrix :refer [M44]]
'[thi.ng.geom.core.utils :as gu]
'[thi.ng.geom.aabb :as a]
'[thi.ng.geom.plane :as pl]
'[thi.ng.geom.quad :as q]
'[thi.ng.geom.webgl.core :as gl]
'[thi.ng.geom.webgl.buffers :as buf]
'[thi.ng.math.core :as m]
'[thi.ng.typedarrays.core :as ta])
;; create ground plane...
;; option 1 - create a 3d quad
(def ground (-> (q/quad3 100) (g/center) (g/as-mesh)))
;; or use (initially) infinite plane as basis
;; (def ground (g/as-mesh (pl/plane-with-point (vec3) v/V3Y) {:size 100}))
;; create 2 boxes of different sizes/positions/orientations
(def box1 (g/as-mesh (a/aabb 10)))
(def box2
(-> (a/aabb 5)
(g/as-mesh)
(g/transform (-> M44 (g/translate -25 0 0) (g/rotate-y m/QUARTER_PI)))))
(defn scene-without-uvs
{:ground (-> ground
(gl/as-webgl-buffer-spec {:tessellate true :fnormals true})
;; TODO insert shader spec (see http://v.gd/ez7RHK )
(buf/make-attribute-buffers-in-spec gl gl/static-draw))
:boxes (-> box1
(g/into box2) ;; combine box meshes
(gl/as-webgl-buffer-spec {:tessellate true :fnormals true})
;; TODO insert shader spec (see http://v.gd/ez7RHK )
(buf/make-attribute-buffers-in-spec gl gl/static-draw))})
;; vertex attributes are planned, but not *yet* implemented by the geom mesh types...
;; the below is a hacky workaround (not v.usable for larger meshes)
;; calling g/faces on an AABB or Cuboid will always return faces (quadriliterals) in this order:
;; east, west, north, south, front, back (assuming +X as right & +Y as up axes)
;;
;; +---+
;; | 2 |
;; +---+---+---+
;; | 1 | 4 | 0 |
;; +---+---+---+
;; | 3 |
;; +---+
;; | 5 |
;; +---+
(defn vertex-attribs
[attribs]
(flatten (map gu/tessellate-3 attribs)))
(def box-faces (g/faces (a/aabb 10)))
(def webgl-box-spec
(-> {:attribs {:position {:data (ta/float32 (vertex-attribs box-faces))
:size 3}
:uvs {:data (->> [[0 0] [1 0] [1 1] [0 1]]
(repeat 6) ;; use same UVs for all faces
(vertex-attribs)
(ta/float32))
:size 2}
:normal {:data (->> [[1 0 0] [-1 0 0] [0 1 0] [0 -1 0] [0 0 1] [0 0 -1]]
(map #(repeat 4 %)) ;; repeat each normal 4x times (4 verts / face)
(vertex-attribs)
(ta/float32))
:size 3}}
:uniforms {:model M44
:view (mat/look-at (vec3 0 25 100) (vec3) (vec3 0 1 0))
:proj (gl/perspective 60 (/ 16 9) 0.1 100)}
:mode gl/triangles
:num-vertices (* 6 2 3)
:shader your-shader-object ;; see make-shader-from-spec (http://v.gd/Rs3ieR )
}
;; TODO insert shader spec (see http://v.gd/ez7RHK )
(buf/make-attribute-buffers-in-spec gl gl/static-draw)))
;; this model spec can then be drawn via:
;; (buf/draw-arrays gl webgl-box-spec) or
;; (buf/draw-arrays-with-shader gl webgl-box-spec)
;;
;; If using the former, you can ignore the shader & uniforms keys in
;; the above spec map, but then are responsible for setting all this yourself...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment