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@458: [camera 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: 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@458: rlm@458: depths rlm@458: (for [[x y] pixels] rlm@458: (let [ray (convert-pixel-to-ray camera x y) rlm@458: results (CollisionResults.)] rlm@458: (.collideWith world ray results) rlm@458: (.getDistance (.getClosestCollision results))))] rlm@458: (zipmap pixels depths))) rlm@458: rlm@458: (defn test-world [] rlm@458: (nodify [(floor*) (brick-wall*)])) rlm@458: rlm@458: (defn run-depth-map-test [] rlm@458: #+end_src