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@459
|
16 [camera ray 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 (defn generate-depth-map-points
|
rlm@458
|
30 "Generate a data structure representating the depth of the first
|
rlm@458
|
31 object to collide with a ray from the camera's current position."
|
rlm@458
|
32 [world camera]
|
rlm@458
|
33 (let [width (.getWidth camera)
|
rlm@458
|
34 height (.getHeight camera)
|
rlm@458
|
35 pixels (for [x (range width) y (range height)]
|
rlm@458
|
36 [x y])
|
rlm@459
|
37 temp-ray (Ray.)
|
rlm@458
|
38 depths
|
rlm@458
|
39 (for [[x y] pixels]
|
rlm@459
|
40 (let [ray (convert-pixel-to-ray camera temp-ray x y)
|
rlm@458
|
41 results (CollisionResults.)]
|
rlm@458
|
42 (.collideWith world ray results)
|
rlm@459
|
43 (if-let [closest (.getClosestCollision results)]
|
rlm@459
|
44 (.hashCode (.getGeometry closest)) 0)))]
|
rlm@459
|
45
|
rlm@458
|
46 (zipmap pixels depths)))
|
rlm@458
|
47
|
rlm@458
|
48 (defn test-world []
|
rlm@458
|
49 (nodify [(floor*) (brick-wall*)]))
|
rlm@458
|
50
|
rlm@459
|
51 (import com.aurellem.capture.RatchetTimer)
|
rlm@459
|
52
|
rlm@459
|
53 (defn depth-map-test []
|
rlm@459
|
54 (let [the-world (test-world)]
|
rlm@459
|
55 (world
|
rlm@459
|
56 the-world
|
rlm@459
|
57 ;;controls
|
rlm@459
|
58 standard-debug-controls
|
rlm@459
|
59 ;;init
|
rlm@459
|
60 (fn [world]
|
rlm@459
|
61 (let [timer (RatchetTimer. 60)]
|
rlm@459
|
62 (.setTimer world timer)
|
rlm@459
|
63 (display-dilated-time world timer)))
|
rlm@459
|
64 ;; update
|
rlm@459
|
65 no-op)))
|
rlm@459
|
66
|
rlm@458
|
67 #+end_src |