Skip to content

Commit

Permalink
[core] add frustum-planes fn and aabb-frustum intersection fn
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 18, 2015
1 parent 7ff25fd commit 19c2292
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
27 changes: 27 additions & 0 deletions geom-core/src/intersect.org
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ Also works for rect/circle...
(<= ds (* r r)))))
#+END_SRC

**** AABB - Frustum

#+BEGIN_SRC clojure
;; usage
(isec/intersect-aabb-frustum? box-p box-size (vals (mat/frustum-planes view-mat proj-mat)))
#+END_SRC

http://pastebin.com/PEeDKLET

#+BEGIN_SRC clojure :noweb-ref isec
(defn aabb-vertex-for-normal
[[x1 y1 z1] [x2 y2 z2] [nx ny nz]]
(vec3 (if (>= nx 0.0) x2 x1) (if (>= ny 0.0) y2 y1) (if (>= nz 0.0) z2 z1)))

(defn intersect-aabb-frustum?
[p size planes]
(let [q (g/+ p size)]
(reduce
(fn [res [n w]]
(if (neg? (+ (g/dot n (aabb-vertex-for-normal p q n)) w))
(reduced -1)
(if (neg? (+ (g/dot n (aabb-vertex-for-normal q p n)) w))
0
res)))
1 planes)))
#+END_SRC

** Sphere

*** Sphere - Sphere
Expand Down
31 changes: 27 additions & 4 deletions geom-core/src/matrix.org
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,29 @@ ordering (row major).
:top top
:bottom (- top)}))

(defn frustum-planes
[view proj]
(let [^Matrix44 pv (g/transpose (g/* proj view))
m30 (.-m30 pv)
m31 (.-m31 pv)
m32 (.-m32 pv)
m33 (.-m33 pv)
l [(+ m30 (.-m00 pv)) (+ m31 (.-m01 pv))
(+ m32 (.-m02 pv)) (+ m33 (.-m03 pv))]
r [(- m30 (.-m00 pv)) (- m31 (.-m01 pv))
(- m32 (.-m02 pv)) (- m33 (.-m03 pv))]
t [(- m30 (.-m10 pv)) (- m31 (.-m11 pv))
(- m32 (.-m12 pv)) (- m33 (.-m13 pv))]
b [(+ m30 (.-m10 pv)) (+ m31 (.-m11 pv))
(+ m32 (.-m12 pv)) (+ m33 (.-m13 pv))]
n [(+ m30 (.-m20 pv)) (+ m31 (.-m21 pv))
(+ m32 (.-m22 pv)) (+ m33 (.-m23 pv))]
f [(- m30 (.-m20 pv)) (- m31 (.-m21 pv))
(- m32 (.-m22 pv)) (- m33 (.-m23 pv))]]
(zipmap
[:left :right :top :bottom :near :far]
(map (fn [p] [(g/normalize (vec3 p)) (peek p)]) [l r t b n f]))))

(defn ortho
"Returns an orthographic projection matrix, in which objects are the
same size no matter how far away or nearby they are. This emulates
Expand Down Expand Up @@ -981,10 +1004,10 @@ ordering (row major).
z' (- (* z 2.0) 1)
p' (g/transform-vector inv-mat [x' y' z'])]
(g/* p' (/ (mm/madd
x' (.-m03 inv-mat)
y' (.-m13 inv-mat)
z' (.-m23 inv-mat)
(.-m33 inv-mat)))))))
x' (.-m03 inv-mat)
y' (.-m13 inv-mat)
z' (.-m23 inv-mat)
(.-m33 inv-mat)))))))
#+END_SRC

** Complete namespace definition
Expand Down

0 comments on commit 19c2292

Please sign in to comment.