view 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
line wrap: on
line source

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