Skip to content

Commit

Permalink
[core] optimize intersect-aabb-frustum?, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 19, 2015
1 parent 225dce2 commit 48134ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 20 additions & 15 deletions geom-core/src/intersect.org
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,32 @@ Also works for rect/circle...

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

http://pastebin.com/PEeDKLET
https://groups.google.com/forum/#!topic/comp.graphics.algorithms/M6lvyWC6PqU

#+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)]
"Takes 2 vectors defining an AABB (min-p & size) and a seq of plane
parameters (each element [normal w]). The plane normals must be
pointing *inwards*. Returns :inside, :intersect or :outside"
[[px py pz :as p] size planes]
(let [[qx qy qz] (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)))
(fn [res [[nx ny nz] w]]
(let [vx (if (pos? nx) px qx)
vy (if (pos? ny) py qy)
vz (if (pos? nz) pz qz)]
(if (pos? (mm/madd nx vx ny vy nz vz w))
(reduced :outside)
(let [vx (if (pos? nx) qx px)
vy (if (pos? ny) qy py)
vz (if (pos? nz) qz pz)]
(if (pos? (mm/madd nx vx ny vy nz vz w))
:intersect
res)))))
:inside planes)))
#+END_SRC

** Sphere
Expand Down
2 changes: 2 additions & 0 deletions geom-core/src/matrix.org
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,8 @@ ordering (row major).
** Matrix functions
*** Frustum helpers

https://github.com/stefanmirea/view-frustum-culling/blob/master/lab_camera.hpp#L163

#+BEGIN_SRC clojure :noweb-ref helpers
(defn frustum
"Sets up a viewing frustum, shaped like a truncated pyramid with the
Expand Down

0 comments on commit 48134ef

Please sign in to comment.