Mercurial > cortex
comparison org/eyes.org @ 167:9e6a30b8c99a
renamed some functions in eyes.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 04:07:25 -0700 |
parents | aaacf087504c |
children |
comparison
equal
deleted
inserted
replaced
166:e4c2cc79a171 | 167:9e6a30b8c99a |
---|---|
46 the eye marker. The eye marker will contain the metadata for the | 46 the eye marker. The eye marker will contain the metadata for the |
47 eye, and will be moved by it's bound geometry. The dimensions of | 47 eye, and will be moved by it's bound geometry. The dimensions of |
48 the eye's camera are equal to the dimensions of the eye's "UV" | 48 the eye's camera are equal to the dimensions of the eye's "UV" |
49 map. | 49 map. |
50 | 50 |
51 | |
52 | |
53 | |
54 | |
55 #+name: eyes | 51 #+name: eyes |
56 #+begin_src clojure | 52 #+begin_src clojure |
57 (ns cortex.vision | 53 (ns cortex.vision |
58 "Simulate the sense of vision in jMonkeyEngine3. Enables multiple | 54 "Simulate the sense of vision in jMonkeyEngine3. Enables multiple |
59 eyes from different positions to observe the same world, and pass | 55 eyes from different positions to observe the same world, and pass |
60 the observed data to any arbitray function." | 56 the observed data to any arbitray function." |
61 {:author "Robert McIntyre"} | 57 {:author "Robert McIntyre"} |
62 (:use (cortex world sense util)) | 58 (:use (cortex world sense util)) |
59 (:use clojure.contrib.def) | |
63 (:import com.jme3.post.SceneProcessor) | 60 (:import com.jme3.post.SceneProcessor) |
64 (:import (com.jme3.util BufferUtils Screenshots)) | 61 (:import (com.jme3.util BufferUtils Screenshots)) |
65 (:import java.nio.ByteBuffer) | 62 (:import java.nio.ByteBuffer) |
66 (:import java.awt.image.BufferedImage) | 63 (:import java.awt.image.BufferedImage) |
67 (:import com.jme3.renderer.ViewPort) | 64 (:import com.jme3.renderer.ViewPort) |
68 (:import com.jme3.math.ColorRGBA) | 65 (:import com.jme3.math.ColorRGBA) |
69 (:import com.jme3.renderer.Renderer) | 66 (:import com.jme3.renderer.Renderer) |
70 (:import jme3tools.converters.ImageToAwt) | |
71 (:import com.jme3.scene.Node)) | 67 (:import com.jme3.scene.Node)) |
72 | 68 |
73 (cortex.import/mega-import-jme3) | 69 (cortex.import/mega-import-jme3) |
74 | 70 |
75 | 71 |
122 provided by (vision-pipeline)." | 118 provided by (vision-pipeline)." |
123 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi] | 119 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi] |
124 (byteBuffer->bufferedImage! | 120 (byteBuffer->bufferedImage! |
125 (frameBuffer->byteBuffer! r fb bb) bi)) | 121 (frameBuffer->byteBuffer! r fb bb) bi)) |
126 | 122 |
127 (defn add-eye | 123 (defn add-eye! |
128 "Add an eye to the world, calling continuation on every frame | 124 "Add an eye to the world, calling continuation on every frame |
129 produced." | 125 produced." |
130 [world camera continuation] | 126 [#^Application world camera continuation] |
131 (let [width (.getWidth camera) | 127 (let [width (.getWidth camera) |
132 height (.getHeight camera) | 128 height (.getHeight camera) |
133 render-manager (.getRenderManager world) | 129 render-manager (.getRenderManager world) |
134 viewport (.createMainView render-manager "eye-view" camera)] | 130 viewport (.createMainView render-manager "eye-view" camera)] |
135 (doto viewport | 131 (doto viewport |
146 defined as extracting the red green blue and average components | 142 defined as extracting the red green blue and average components |
147 respectively." | 143 respectively." |
148 [#^Spatial eye] | 144 [#^Spatial eye] |
149 (if-let [eye-map (meta-data eye "eye")] | 145 (if-let [eye-map (meta-data eye "eye")] |
150 (map-vals | 146 (map-vals |
151 #(ImageToAwt/convert | 147 load-image |
152 (.getImage (.loadTexture (asset-manager) %)) | |
153 false false 0) | |
154 (eval (read-string eye-map))))) | 148 (eval (read-string eye-map))))) |
155 | 149 |
156 (defn eye-dimensions | 150 (defn eye-dimensions |
157 "returns the width and height specified in the metadata of the eye" | 151 "returns the width and height specified in the metadata of the eye" |
158 [#^Spatial eye] | 152 [#^Spatial eye] |
160 (map #(vector (.getWidth %) (.getHeight %)) | 154 (map #(vector (.getWidth %) (.getHeight %)) |
161 (vals (retina-sensor-image eye)))] | 155 (vals (retina-sensor-image eye)))] |
162 [(apply max (map first dimensions)) | 156 [(apply max (map first dimensions)) |
163 (apply max (map second dimensions))])) | 157 (apply max (map second dimensions))])) |
164 | 158 |
165 (defn creature-eyes | 159 (defvar |
166 ;;dylan | 160 ^{:arglists '([creature])} |
167 "Return the children of the creature's \"eyes\" node." | 161 eyes |
168 ;;"The eye nodes which are children of the \"eyes\" node in the | 162 (sense-nodes "eyes") |
169 ;;creature." | 163 "Return the children of the creature's \"eyes\" node.") |
170 [#^Node creature] | |
171 (if-let [eye-node (.getChild creature "eyes")] | |
172 (seq (.getChildren eye-node)) | |
173 (do (println-repl "could not find eyes node") []))) | |
174 | |
175 | 164 |
176 (defn attach-eye | 165 (defn attach-eye |
177 "Attach a Camera to the appropiate area and return the Camera." | 166 "Attach a Camera to the appropiate area and return the Camera." |
178 [#^Node creature #^Spatial eye] | 167 [#^Node creature #^Spatial eye] |
179 (let [target (closest-node creature eye) | 168 (let [target (closest-node creature eye) |
202 (atom | 191 (atom |
203 (BufferedImage. (.getWidth camera) | 192 (BufferedImage. (.getWidth camera) |
204 (.getHeight camera) | 193 (.getHeight camera) |
205 BufferedImage/TYPE_BYTE_BINARY))] | 194 BufferedImage/TYPE_BYTE_BINARY))] |
206 [(fn [world] | 195 [(fn [world] |
207 (add-eye | 196 (add-eye! |
208 world camera | 197 world camera |
209 (let [counter (atom 0)] | 198 (let [counter (atom 0)] |
210 (fn [r fb bb bi] | 199 (fn [r fb bb bi] |
211 (if (zero? (rem (swap! counter inc) (inc skip))) | 200 (if (zero? (rem (swap! counter inc) (inc skip))) |
212 (reset! vision-image (BufferedImage! r fb bb bi))))))) | 201 (reset! vision-image (BufferedImage! r fb bb bi))))))) |
231 (fn [[init-a senses-a] | 220 (fn [[init-a senses-a] |
232 [init-b senses-b]] | 221 [init-b senses-b]] |
233 [(conj init-a init-b) | 222 [(conj init-a init-b) |
234 (into senses-a senses-b)]) | 223 (into senses-a senses-b)]) |
235 [[][]] | 224 [[][]] |
236 (for [eye (creature-eyes creature)] | 225 (for [eye (eyes creature)] |
237 (enable-vision creature eye)))) | 226 (enable-vision creature eye)))) |
238 | 227 |
239 | 228 |
240 #+end_src | 229 #+end_src |
241 | 230 |
279 {} | 268 {} |
280 (fn [world] | 269 (fn [world] |
281 (let [cam (.clone (.getCamera world)) | 270 (let [cam (.clone (.getCamera world)) |
282 width (.getWidth cam) | 271 width (.getWidth cam) |
283 height (.getHeight cam)] | 272 height (.getHeight cam)] |
284 (add-eye world cam | 273 (add-eye! world cam |
285 ;;no-op | 274 ;;no-op |
286 (comp (view-image) BufferedImage!) | 275 (comp (view-image) BufferedImage!) |
287 ) | 276 ) |
288 (add-eye world | 277 (add-eye! world |
289 (doto (.clone cam) | 278 (doto (.clone cam) |
290 (.setLocation (Vector3f. -10 0 0)) | 279 (.setLocation (Vector3f. -10 0 0)) |
291 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)) | 280 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)) |
292 ;;no-op | 281 ;;no-op |
293 (comp (view-image) BufferedImage!)) | 282 (comp (view-image) BufferedImage!)) |
294 ;; This is here to restore the main view | 283 ;; This is here to restore the main view |
295 ;; after the other views have completed processing | 284 ;; after the other views have completed processing |
296 (add-eye world (.getCamera world) no-op))) | 285 (add-eye! world (.getCamera world) no-op))) |
297 (fn [world tpf] | 286 (fn [world tpf] |
298 (.rotate candy (* tpf 0.2) 0 0))))) | 287 (.rotate candy (* tpf 0.2) 0 0))))) |
299 #+end_src | 288 #+end_src |
300 | 289 |
301 #+results: test-vision | 290 #+results: test-vision |