rlm@458: rlm@458: #+begin_src clojure rlm@458: (ns cortex.depth-map rlm@458: "lol depth maps." rlm@458: {:author "Robert McIntyre"} rlm@458: (:use (cortex world util body sense vision touch)) rlm@458: (:import (com.jme3.scene Geometry Node Mesh)) rlm@458: (:import com.jme3.collision.CollisionResults) rlm@458: (:import com.jme3.scene.VertexBuffer$Type) rlm@458: rlm@458: (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f))) rlm@458: rlm@458: (cortex.import/mega-import-jme3) rlm@458: rlm@458: (defn convert-pixel-to-ray rlm@459: [camera ray x y] rlm@458: (let [pixel-arrow rlm@458: (.getWorldCoordinates rlm@458: camera rlm@458: (Vector2f. (float x) (float y)) rlm@458: ;; this is the "z depth" and can be anything, since rlm@458: ;; we only care about direction rlm@458: (float 1)) rlm@458: ;; now that we have the direction of a pixel, subtract rlm@458: ray (Ray.)] rlm@458: (set-ray ray Matrix4f/IDENTITY (.getLocation camera) rlm@458: pixel-arrow) ray)) rlm@458: rlm@458: (defn generate-depth-map-points rlm@458: "Generate a data structure representating the depth of the first rlm@458: object to collide with a ray from the camera's current position." rlm@458: [world camera] rlm@458: (let [width (.getWidth camera) rlm@458: height (.getHeight camera) rlm@458: pixels (for [x (range width) y (range height)] rlm@458: [x y]) rlm@459: temp-ray (Ray.) rlm@458: depths rlm@458: (for [[x y] pixels] rlm@459: (let [ray (convert-pixel-to-ray camera temp-ray x y) rlm@458: results (CollisionResults.)] rlm@458: (.collideWith world ray results) rlm@459: (if-let [closest (.getClosestCollision results)] rlm@459: (.hashCode (.getGeometry closest)) 0)))] rlm@459: rlm@458: (zipmap pixels depths))) rlm@458: rlm@458: (defn test-world [] rlm@458: (nodify [(floor*) (brick-wall*)])) rlm@458: rlm@459: (import com.aurellem.capture.RatchetTimer) rlm@459: rlm@459: (defn depth-map-test [] rlm@459: (let [the-world (test-world)] rlm@459: (world rlm@459: the-world rlm@459: ;;controls rlm@459: standard-debug-controls rlm@459: ;;init rlm@459: (fn [world] rlm@459: (let [timer (RatchetTimer. 60)] rlm@459: (.setTimer world timer) rlm@459: (display-dilated-time world timer))) rlm@459: ;; update rlm@459: no-op))) rlm@459: rlm@458: #+end_src