annotate org/depth-map.org @ 458:42ddfe406c0a

working on depth maps for the lulz.
author Robert McIntyre <rlm@mit.edu>
date Tue, 11 Jun 2013 07:13:42 -0400
parents
children a86555b02916
rev   line source
rlm@458 1
rlm@458 2 #+begin_src clojure
rlm@458 3 (ns cortex.depth-map
rlm@458 4 "lol depth maps."
rlm@458 5 {:author "Robert McIntyre"}
rlm@458 6 (:use (cortex world util body sense vision touch))
rlm@458 7 (:import (com.jme3.scene Geometry Node Mesh))
rlm@458 8 (:import com.jme3.collision.CollisionResults)
rlm@458 9 (:import com.jme3.scene.VertexBuffer$Type)
rlm@458 10
rlm@458 11 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
rlm@458 12
rlm@458 13 (cortex.import/mega-import-jme3)
rlm@458 14
rlm@458 15 (defn convert-pixel-to-ray
rlm@458 16 [camera x y]
rlm@458 17 (let [pixel-arrow
rlm@458 18 (.getWorldCoordinates
rlm@458 19 camera
rlm@458 20 (Vector2f. (float x) (float y))
rlm@458 21 ;; this is the "z depth" and can be anything, since
rlm@458 22 ;; we only care about direction
rlm@458 23 (float 1))
rlm@458 24 ;; now that we have the direction of a pixel, subtract
rlm@458 25 ray (Ray.)]
rlm@458 26 (set-ray ray Matrix4f/IDENTITY (.getLocation camera)
rlm@458 27 pixel-arrow) ray))
rlm@458 28
rlm@458 29
rlm@458 30 (defn generate-depth-map-points
rlm@458 31 "Generate a data structure representating the depth of the first
rlm@458 32 object to collide with a ray from the camera's current position."
rlm@458 33 [world camera]
rlm@458 34 (let [width (.getWidth camera)
rlm@458 35 height (.getHeight camera)
rlm@458 36 pixels (for [x (range width) y (range height)]
rlm@458 37 [x y])
rlm@458 38
rlm@458 39 depths
rlm@458 40 (for [[x y] pixels]
rlm@458 41 (let [ray (convert-pixel-to-ray camera x y)
rlm@458 42 results (CollisionResults.)]
rlm@458 43 (.collideWith world ray results)
rlm@458 44 (.getDistance (.getClosestCollision results))))]
rlm@458 45 (zipmap pixels depths)))
rlm@458 46
rlm@458 47 (defn test-world []
rlm@458 48 (nodify [(floor*) (brick-wall*)]))
rlm@458 49
rlm@458 50 (defn run-depth-map-test []
rlm@458 51 #+end_src