Mercurial > cortex
view org/vision.org @ 548:0b891e0dd809
version 0.2 of thesis complete.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 01 May 2014 23:41:41 -0400 |
parents | 3401053124b0 |
children | 819968c8a391 |
line wrap: on
line source
1 #+title: Simulated Sense of Sight2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulated sight for AI research using JMonkeyEngine3 and clojure5 #+keywords: computer vision, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org8 #+babel: :mkdirp yes :noweb yes :exports both10 * JMonkeyEngine natively supports multiple views of the same world.12 Vision is one of the most important senses for humans, so I need to13 build a simulated sense of vision for my AI. I will do this with14 simulated eyes. Each eye can be independently moved and should see its15 own version of the world depending on where it is.17 Making these simulated eyes a reality is simple because jMonkeyEngine18 already contains extensive support for multiple views of the same 3D19 simulated world. The reason jMonkeyEngine has this support is because20 the support is necessary to create games with split-screen21 views. Multiple views are also used to create efficient22 pseudo-reflections by rendering the scene from a certain perspective23 and then projecting it back onto a surface in the 3D world.25 #+caption: jMonkeyEngine supports multiple views to enable split-screen games, like GoldenEye, which was one of the first games to use split-screen views.26 [[../images/goldeneye-4-player.png]]28 ** =ViewPorts=, =SceneProcessors=, and the =RenderManager=.29 # =ViewPorts= are cameras; =RenderManger= takes snapshots each frame.30 #* A Brief Description of jMonkeyEngine's Rendering Pipeline32 jMonkeyEngine allows you to create a =ViewPort=, which represents a33 view of the simulated world. You can create as many of these as you34 want. Every frame, the =RenderManager= iterates through each35 =ViewPort=, rendering the scene in the GPU. For each =ViewPort= there36 is a =FrameBuffer= which represents the rendered image in the GPU.38 #+caption: =ViewPorts= are cameras in the world. During each frame, the =RenderManager= records a snapshot of what each view is currently seeing; these snapshots are =FrameBuffer= objects.39 #+ATTR_HTML: width="400"40 [[../images/diagram_rendermanager2.png]]42 Each =ViewPort= can have any number of attached =SceneProcessor=43 objects, which are called every time a new frame is rendered. A44 =SceneProcessor= receives its =ViewPort's= =FrameBuffer= and can do45 whatever it wants to the data. Often this consists of invoking GPU46 specific operations on the rendered image. The =SceneProcessor= can47 also copy the GPU image data to RAM and process it with the CPU.49 ** From Views to Vision50 # Appropriating Views for Vision.52 Each eye in the simulated creature needs its own =ViewPort= so that53 it can see the world from its own perspective. To this =ViewPort=, I54 add a =SceneProcessor= that feeds the visual data to any arbitrary55 continuation function for further processing. That continuation56 function may perform both CPU and GPU operations on the data. To make57 this easy for the continuation function, the =SceneProcessor=58 maintains appropriately sized buffers in RAM to hold the data. It does59 not do any copying from the GPU to the CPU itself because it is a slow60 operation.62 #+name: pipeline-163 #+begin_src clojure64 (defn vision-pipeline65 "Create a SceneProcessor object which wraps a vision processing66 continuation function. The continuation is a function that takes67 [#^Renderer r #^FrameBuffer fb #^ByteBuffer b #^BufferedImage bi],68 each of which has already been appropriately sized."69 [continuation]70 (let [byte-buffer (atom nil)71 renderer (atom nil)72 image (atom nil)]73 (proxy [SceneProcessor] []74 (initialize75 [renderManager viewPort]76 (let [cam (.getCamera viewPort)77 width (.getWidth cam)78 height (.getHeight cam)]79 (reset! renderer (.getRenderer renderManager))80 (reset! byte-buffer81 (BufferUtils/createByteBuffer82 (* width height 4)))83 (reset! image (BufferedImage.84 width height85 BufferedImage/TYPE_4BYTE_ABGR))))86 (isInitialized [] (not (nil? @byte-buffer)))87 (reshape [_ _ _])88 (preFrame [_])89 (postQueue [_])90 (postFrame91 [#^FrameBuffer fb]92 (.clear @byte-buffer)93 (continuation @renderer fb @byte-buffer @image))94 (cleanup []))))95 #+end_src97 The continuation function given to =vision-pipeline= above will be98 given a =Renderer= and three containers for image data. The99 =FrameBuffer= references the GPU image data, but the pixel data can100 not be used directly on the CPU. The =ByteBuffer= and =BufferedImage=101 are initially "empty" but are sized to hold the data in the102 =FrameBuffer=. I call transferring the GPU image data to the CPU103 structures "mixing" the image data. I have provided three functions to104 do this mixing.106 #+name: pipeline-2107 #+begin_src clojure108 (defn frameBuffer->byteBuffer!109 "Transfer the data in the graphics card (Renderer, FrameBuffer) to110 the CPU (ByteBuffer)."111 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb]112 (.readFrameBuffer r fb bb) bb)114 (defn byteBuffer->bufferedImage!115 "Convert the C-style BGRA image data in the ByteBuffer bb to the AWT116 style ABGR image data and place it in BufferedImage bi."117 [#^ByteBuffer bb #^BufferedImage bi]118 (Screenshots/convertScreenShot bb bi) bi)120 (defn BufferedImage!121 "Continuation which will grab the buffered image from the materials122 provided by (vision-pipeline)."123 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi]124 (byteBuffer->bufferedImage!125 (frameBuffer->byteBuffer! r fb bb) bi))126 #+end_src128 Note that it is possible to write vision processing algorithms129 entirely in terms of =BufferedImage= inputs. Just compose that130 =BufferedImage= algorithm with =BufferedImage!=. However, a vision131 processing algorithm that is entirely hosted on the GPU does not have132 to pay for this convenience.134 * Optical sensor arrays are described with images and referenced with metadata135 The vision pipeline described above handles the flow of rendered136 images. Now, we need simulated eyes to serve as the source of these137 images.139 An eye is described in blender in the same way as a joint. They are140 zero dimensional empty objects with no geometry whose local coordinate141 system determines the orientation of the resulting eye. All eyes are142 children of a parent node named "eyes" just as all joints have a143 parent named "joints". An eye binds to the nearest physical object144 with =bind-sense=.146 #+name: add-eye147 #+begin_src clojure148 (in-ns 'cortex.vision)150 (defn add-eye!151 "Create a Camera centered on the current position of 'eye which152 follows the closest physical node in 'creature. The camera will153 point in the X direction and use the Z vector as up as determined154 by the rotation of these vectors in blender coordinate space. Use155 XZY rotation for the node in blender."156 [#^Node creature #^Spatial eye]157 (let [target (closest-node creature eye)158 [cam-width cam-height]159 ;;[640 480] ;; graphics card on laptop doesn't support160 ;; arbitray dimensions.161 (eye-dimensions eye)162 cam (Camera. cam-width cam-height)163 rot (.getWorldRotation eye)]164 (.setLocation cam (.getWorldTranslation eye))165 (.lookAtDirection166 cam ; this part is not a mistake and167 (.mult rot Vector3f/UNIT_X) ; is consistent with using Z in168 (.mult rot Vector3f/UNIT_Y)) ; blender as the UP vector.169 (.setFrustumPerspective170 cam (float 45)171 (float (/ (.getWidth cam) (.getHeight cam)))172 (float 1)173 (float 1000))174 (bind-sense target cam) cam))175 #+end_src177 Here, the camera is created based on metadata on the eye-node and178 attached to the nearest physical object with =bind-sense=179 ** The Retina181 An eye is a surface (the retina) which contains many discrete sensors182 to detect light. These sensors can have different light-sensing183 properties. In humans, each discrete sensor is sensitive to red, blue,184 green, or gray. These different types of sensors can have different185 spatial distributions along the retina. In humans, there is a fovea in186 the center of the retina which has a very high density of color187 sensors, and a blind spot which has no sensors at all. Sensor density188 decreases in proportion to distance from the fovea.190 I want to be able to model any retinal configuration, so my eye-nodes191 in blender contain metadata pointing to images that describe the192 precise position of the individual sensors using white pixels. The193 meta-data also describes the precise sensitivity to light that the194 sensors described in the image have. An eye can contain any number of195 these images. For example, the metadata for an eye might look like196 this:198 #+begin_src clojure199 {0xFF0000 "Models/test-creature/retina-small.png"}200 #+end_src202 #+caption: The retinal profile image "Models/test-creature/retina-small.png". White pixels are photo-sensitive elements. The distribution of white pixels is denser in the middle and falls off at the edges and is inspired by the human retina.203 [[../assets/Models/test-creature/retina-small.png]]205 Together, the number 0xFF0000 and the image image above describe the206 placement of red-sensitive sensory elements.208 Meta-data to very crudely approximate a human eye might be something209 like this:211 #+begin_src clojure212 (let [retinal-profile "Models/test-creature/retina-small.png"]213 {0xFF0000 retinal-profile214 0x00FF00 retinal-profile215 0x0000FF retinal-profile216 0xFFFFFF retinal-profile})217 #+end_src219 The numbers that serve as keys in the map determine a sensor's220 relative sensitivity to the channels red, green, and blue. These221 sensitivity values are packed into an integer in the order =|_|R|G|B|=222 in 8-bit fields. The RGB values of a pixel in the image are added223 together with these sensitivities as linear weights. Therefore,224 0xFF0000 means sensitive to red only while 0xFFFFFF means sensitive to225 all colors equally (gray).227 For convenience I've defined a few symbols for the more common228 sensitivity values.230 #+name: sensitivity231 #+begin_src clojure232 (def sensitivity-presets233 "Retinal sensitivity presets for sensors that extract one channel234 (:red :blue :green) or average all channels (:all)"235 {:all 0xFFFFFF236 :red 0xFF0000237 :blue 0x0000FF238 :green 0x00FF00})239 #+end_src241 ** Metadata Processing243 =retina-sensor-profile= extracts a map from the eye-node in the same244 format as the example maps above. =eye-dimensions= finds the245 dimensions of the smallest image required to contain all the retinal246 sensor maps.248 #+name: retina249 #+begin_src clojure250 (defn retina-sensor-profile251 "Return a map of pixel sensitivity numbers to BufferedImages252 describing the distribution of light-sensitive components of this253 eye. :red, :green, :blue, :gray are already defined as extracting254 the red, green, blue, and average components respectively."255 [#^Spatial eye]256 (if-let [eye-map (meta-data eye "eye")]257 (map-vals258 load-image259 (eval (read-string eye-map)))))261 (defn eye-dimensions262 "Returns [width, height] determined by the metadata of the eye."263 [#^Spatial eye]264 (let [dimensions265 (map #(vector (.getWidth %) (.getHeight %))266 (vals (retina-sensor-profile eye)))]267 [(apply max (map first dimensions))268 (apply max (map second dimensions))]))269 #+end_src271 * Importing and parsing descriptions of eyes.272 First off, get the children of the "eyes" empty node to find all the273 eyes the creature has.274 #+name: eye-node275 #+begin_src clojure276 (def277 ^{:doc "Return the children of the creature's \"eyes\" node."278 :arglists '([creature])}279 eyes280 (sense-nodes "eyes"))281 #+end_src283 Then, add the camera created by =add-eye!= to the simulation by284 creating a new viewport.286 #+name: add-camera287 #+begin_src clojure288 (in-ns 'cortex.vision)289 (defn add-camera!290 "Add a camera to the world, calling continuation on every frame291 produced."292 [#^Application world camera continuation]293 (let [width (.getWidth camera)294 height (.getHeight camera)295 render-manager (.getRenderManager world)296 viewport (.createMainView render-manager "eye-view" camera)]297 (doto viewport298 (.setClearFlags true true true)299 (.setBackgroundColor ColorRGBA/Black)300 (.addProcessor (vision-pipeline continuation))301 (.attachScene (.getRootNode world)))))302 #+end_src304 #+results: add-camera305 : #'cortex.vision/add-camera!308 The eye's continuation function should register the viewport with the309 simulation the first time it is called, use the CPU to extract the310 appropriate pixels from the rendered image and weight them by each311 sensor's sensitivity. I have the option to do this processing in312 native code for a slight gain in speed. I could also do it in the GPU313 for a massive gain in speed. =vision-kernel= generates a list of314 such continuation functions, one for each channel of the eye.316 #+name: kernel317 #+begin_src clojure318 (in-ns 'cortex.vision)320 (defrecord attached-viewport [vision-fn viewport-fn]321 clojure.lang.IFn322 (invoke [this world] (vision-fn world))323 (applyTo [this args] (apply vision-fn args)))325 (defn pixel-sense [sensitivity pixel]326 (let [s-r (bit-shift-right (bit-and 0xFF0000 sensitivity) 16)327 s-g (bit-shift-right (bit-and 0x00FF00 sensitivity) 8)328 s-b (bit-and 0x0000FF sensitivity)330 p-r (bit-shift-right (bit-and 0xFF0000 pixel) 16)331 p-g (bit-shift-right (bit-and 0x00FF00 pixel) 8)332 p-b (bit-and 0x0000FF pixel)334 total-sensitivity (* 255 (+ s-r s-g s-b))]335 (float (/ (+ (* s-r p-r)336 (* s-g p-g)337 (* s-b p-b))338 total-sensitivity))))340 (defn vision-kernel341 "Returns a list of functions, each of which will return a color342 channel's worth of visual information when called inside a running343 simulation."344 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]345 (let [retinal-map (retina-sensor-profile eye)346 camera (add-eye! creature eye)347 vision-image348 (atom349 (BufferedImage. (.getWidth camera)350 (.getHeight camera)351 BufferedImage/TYPE_BYTE_BINARY))352 register-eye!353 (runonce354 (fn [world]355 (add-camera!356 world camera357 (let [counter (atom 0)]358 (fn [r fb bb bi]359 (if (zero? (rem (swap! counter inc) (inc skip)))360 (reset! vision-image361 (BufferedImage! r fb bb bi))))))))]362 (vec363 (map364 (fn [[key image]]365 (let [whites (white-coordinates image)366 topology (vec (collapse whites))367 sensitivity (sensitivity-presets key key)]368 (attached-viewport.369 (fn [world]370 (register-eye! world)371 (vector372 topology373 (vec374 (for [[x y] whites]375 (pixel-sense376 sensitivity377 (.getRGB @vision-image x y))))))378 register-eye!)))379 retinal-map))))381 (defn gen-fix-display382 "Create a function to call to restore a simulation's display when it383 is disrupted by a Viewport."384 []385 (runonce386 (fn [world]387 (add-camera! world (.getCamera world) no-op))))388 #+end_src390 Note that since each of the functions generated by =vision-kernel=391 shares the same =register-eye!= function, the eye will be registered392 only once the first time any of the functions from the list returned393 by =vision-kernel= is called. Each of the functions returned by394 =vision-kernel= also allows access to the =Viewport= through which395 it receives images.397 The in-game display can be disrupted by all the ViewPorts that the398 functions generated by =vision-kernel= add. This doesn't affect the399 simulation or the simulated senses, but can be annoying.400 =gen-fix-display= restores the in-simulation display.402 ** The =vision!= function creates sensory probes.404 All the hard work has been done; all that remains is to apply405 =vision-kernel= to each eye in the creature and gather the results406 into one list of functions.408 #+name: main409 #+begin_src clojure410 (defn vision!411 "Returns a list of functions, each of which returns visual sensory412 data when called inside a running simulation."413 [#^Node creature & {skip :skip :or {skip 0}}]414 (reduce415 concat416 (for [eye (eyes creature)]417 (vision-kernel creature eye))))418 #+end_src420 ** Displaying visual data for debugging.421 # Visualization of Vision. Maybe less alliteration would be better.422 It's vital to have a visual representation for each sense. Here I use423 =view-sense= to construct a function that will create a display for424 visual data.426 #+name: display427 #+begin_src clojure428 (in-ns 'cortex.vision)430 (defn view-vision431 "Creates a function which accepts a list of visual sensor-data and432 displays each element of the list to the screen."433 []434 (view-sense435 (fn436 [[coords sensor-data]]437 (let [image (points->image coords)]438 (dorun439 (for [i (range (count coords))]440 (.setRGB image ((coords i) 0) ((coords i) 1)441 (gray (int (* 255 (sensor-data i)))))))442 image))))443 #+end_src445 * Demonstrations446 ** Demonstrating the vision pipeline.448 This is a basic test for the vision system. It only tests the449 vision-pipeline and does not deal with loading eyes from a blender450 file. The code creates two videos of the same rotating cube from451 different angles.453 #+name: test-1454 #+begin_src clojure455 (in-ns 'cortex.test.vision)457 (defn test-pipeline458 "Testing vision:459 Tests the vision system by creating two views of the same rotating460 object from different angles and displaying both of those views in461 JFrames.463 You should see a rotating cube, and two windows,464 each displaying a different view of the cube."465 ([] (test-pipeline false))466 ([record?]467 (let [candy468 (box 1 1 1 :physical? false :color ColorRGBA/Blue)]469 (world470 (doto (Node.)471 (.attachChild candy))472 {}473 (fn [world]474 (let [cam (.clone (.getCamera world))475 width (.getWidth cam)476 height (.getHeight cam)]477 (add-camera! world cam478 (comp479 (view-image480 (if record?481 (File. "/home/r/proj/cortex/render/vision/1")))482 BufferedImage!))483 (add-camera! world484 (doto (.clone cam)485 (.setLocation (Vector3f. -10 0 0))486 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))487 (comp488 (view-image489 (if record?490 (File. "/home/r/proj/cortex/render/vision/2")))491 BufferedImage!))492 (let [timer (IsoTimer. 60)]493 (.setTimer world timer)494 (display-dilated-time world timer))495 ;; This is here to restore the main view496 ;; after the other views have completed processing497 (add-camera! world (.getCamera world) no-op)))498 (fn [world tpf]499 (.rotate candy (* tpf 0.2) 0 0))))))500 #+end_src502 #+results: test-1503 : #'cortex.test.vision/test-pipeline505 #+begin_html506 <div class="figure">507 <video controls="controls" width="755">508 <source src="../video/spinning-cube.ogg" type="video/ogg"509 preload="none" poster="../images/aurellem-1280x480.png" />510 </video>511 <br> <a href="http://youtu.be/r5Bn2aG7MO0"> YouTube </a>512 <p>A rotating cube viewed from two different perspectives.</p>513 </div>514 #+end_html516 Creating multiple eyes like this can be used for stereoscopic vision517 simulation in a single creature or for simulating multiple creatures,518 each with their own sense of vision.519 ** Demonstrating eye import and parsing.521 To the worm from the last post, I add a new node that describes its522 eyes.524 #+attr_html: width=755525 #+caption: The worm with newly added empty nodes describing a single eye.526 [[../images/worm-with-eye.png]]528 The node highlighted in yellow is the root level "eyes" node. It has529 a single child, highlighted in orange, which describes a single530 eye. This is the "eye" node. It is placed so that the worm will have531 an eye located in the center of the flat portion of its lower532 hemispherical section.534 The two nodes which are not highlighted describe the single joint of535 the worm.537 The metadata of the eye-node is:539 #+begin_src clojure :results verbatim :exports both540 (cortex.sense/meta-data541 (.getChild (.getChild (cortex.test.body/worm) "eyes") "eye") "eye")542 #+end_src544 #+results:545 : "(let [retina \"Models/test-creature/retina-small.png\"]546 : {:all retina :red retina :green retina :blue retina})"548 This is the approximation to the human eye described earlier.550 #+name: test-2551 #+begin_src clojure552 (in-ns 'cortex.test.vision)554 (defn change-color [obj color]555 ;;(println-repl obj)556 (if obj557 (.setColor (.getMaterial obj) "Color" color)))559 (defn colored-cannon-ball [color]560 (comp #(change-color % color)561 (fire-cannon-ball)))563 (defn gen-worm564 "create a creature acceptable for testing as a replacement for the565 worm."566 []567 (nodify568 "worm"569 [(nodify570 "eyes"571 [(doto572 (Node. "eye1")573 (.setLocalTranslation (Vector3f. 0 -1.1 0))574 (.setUserData576 "eye"577 "(let [retina578 \"Models/test-creature/retina-small.png\"]579 {:all retina :red retina580 :green retina :blue retina})"))])581 (box582 0.2 0.2 0.2583 :name "worm-segment"584 :position (Vector3f. 0 0 0)585 :color ColorRGBA/Orange)]))589 (defn test-worm-vision590 "Testing vision:591 You should see the worm suspended in mid-air, looking down at a592 table. There are four small displays, one each for red, green blue,593 and gray channels. You can fire balls of various colors, and the594 four channels should react accordingly.596 Keys:597 r : fire red-ball598 b : fire blue-ball599 g : fire green-ball600 <space> : fire white ball"602 ([] (test-worm-vision false))603 ([record?]604 (let [the-worm (doto (worm)(body!))605 vision (vision! the-worm)606 vision-display (view-vision)607 fix-display (gen-fix-display)608 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)609 x-axis610 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red611 :position (Vector3f. 0 -5 0))612 y-axis613 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green614 :position (Vector3f. 0 -5 0))615 z-axis616 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue617 :position (Vector3f. 0 -5 0))619 ]621 (world622 (nodify [(floor) the-worm x-axis y-axis z-axis me])623 (merge standard-debug-controls624 {"key-r" (colored-cannon-ball ColorRGBA/Red)625 "key-b" (colored-cannon-ball ColorRGBA/Blue)626 "key-g" (colored-cannon-ball ColorRGBA/Green)})628 (fn [world]629 (light-up-everything world)630 (speed-up world)631 (let [timer (IsoTimer. 60)]632 (.setTimer world timer)633 (display-dilated-time world timer))634 ;; add a view from the worm's perspective635 (if record?636 (Capture/captureVideo637 world638 (File.639 "/home/r/proj/cortex/render/worm-vision/main-view")))641 (add-camera!642 world643 (add-eye! the-worm (first (eyes the-worm)))644 (comp645 (view-image646 (if record?647 (File.648 "/home/r/proj/cortex/render/worm-vision/worm-view")))649 BufferedImage!))651 (set-gravity world Vector3f/ZERO)652 (add-camera! world (.getCamera world) no-op))654 (fn [world _]655 (.setLocalTranslation me (.getLocation (.getCamera world)))656 (vision-display657 (map #(% world) vision)658 (if record?659 (File. "/home/r/proj/cortex/render/worm-vision")))660 (fix-display world)661 )))))662 #+end_src664 #+RESULTS: test-2665 : #'cortex.test.vision/test-worm-vision668 The world consists of the worm and a flat gray floor. I can shoot red,669 green, blue and white cannonballs at the worm. The worm is initially670 looking down at the floor, and there is no gravity. My perspective671 (the Main View), the worm's perspective (Worm View) and the 4 sensor672 channels that comprise the worm's eye are all saved frame-by-frame to673 disk.675 * Demonstration of Vision676 #+begin_html677 <div class="figure">678 <video controls="controls" width="755">679 <source src="../video/worm-vision.ogg" type="video/ogg"680 preload="none" poster="../images/aurellem-1280x480.png" />681 </video>682 <br> <a href="http://youtu.be/J3H3iB_2NPQ"> YouTube </a>683 <p>Simulated Vision in a Virtual Environment</p>684 </div>685 #+end_html687 ** Generate the Worm Video from Frames688 #+name: magick2689 #+begin_src clojure690 (ns cortex.video.magick2691 (:import java.io.File)692 (:use clojure.java.shell))694 (defn images [path]695 (sort (rest (file-seq (File. path)))))697 (def base "/home/r/proj/cortex/render/worm-vision/")699 (defn pics [file]700 (images (str base file)))702 (defn combine-images []703 (let [main-view (pics "main-view")704 worm-view (pics "worm-view")705 blue (pics "0")706 green (pics "1")707 red (pics "2")708 gray (pics "3")709 blender (let [b-pics (pics "blender")]710 (concat b-pics (repeat 9001 (last b-pics))))711 background (repeat 9001 (File. (str base "background.png")))712 targets (map713 #(File. (str base "out/" (format "%07d.png" %)))714 (range 0 (count main-view)))]715 (dorun716 (pmap717 (comp718 (fn [[background main-view worm-view red green blue gray blender target]]719 (println target)720 (sh "convert"721 background722 main-view "-geometry" "+18+17" "-composite"723 worm-view "-geometry" "+677+17" "-composite"724 green "-geometry" "+685+430" "-composite"725 red "-geometry" "+788+430" "-composite"726 blue "-geometry" "+894+430" "-composite"727 gray "-geometry" "+1000+430" "-composite"728 blender "-geometry" "+0+0" "-composite"729 target))730 (fn [& args] (map #(.getCanonicalPath %) args)))731 background main-view worm-view red green blue gray blender targets))))732 #+end_src734 #+begin_src sh :results silent735 cd /home/r/proj/cortex/render/worm-vision736 ffmpeg -r 25 -b 9001k -i out/%07d.png -vcodec libtheora worm-vision.ogg737 #+end_src739 * Onward!740 - As a neat bonus, this idea behind simulated vision also enables one741 to [[../../cortex/html/capture-video.html][capture live video feeds from jMonkeyEngine]].742 - Now that we have vision, it's time to tackle [[./hearing.org][hearing]].743 #+appendix745 * Headers747 #+name: vision-header748 #+begin_src clojure749 (ns cortex.vision750 "Simulate the sense of vision in jMonkeyEngine3. Enables multiple751 eyes from different positions to observe the same world, and pass752 the observed data to any arbitrary function. Automatically reads753 eye-nodes from specially prepared blender files and instantiates754 them in the world as actual eyes."755 {:author "Robert McIntyre"}756 (:use (cortex world sense util))757 (:import com.jme3.post.SceneProcessor)758 (:import (com.jme3.util BufferUtils Screenshots))759 (:import java.nio.ByteBuffer)760 (:import java.awt.image.BufferedImage)761 (:import (com.jme3.renderer ViewPort Camera))762 (:import (com.jme3.math ColorRGBA Vector3f Matrix3f))763 (:import com.jme3.renderer.Renderer)764 (:import com.jme3.app.Application)765 (:import com.jme3.texture.FrameBuffer)766 (:import (com.jme3.scene Node Spatial)))767 #+end_src769 #+name: test-header770 #+begin_src clojure771 (ns cortex.test.vision772 (:use (cortex world sense util body vision))773 (:use cortex.test.body)774 (:import java.awt.image.BufferedImage)775 (:import javax.swing.JPanel)776 (:import javax.swing.SwingUtilities)777 (:import java.awt.Dimension)778 (:import javax.swing.JFrame)779 (:import com.jme3.math.ColorRGBA)780 (:import com.jme3.scene.Node)781 (:import com.jme3.math.Vector3f)782 (:import java.io.File)783 (:import (com.aurellem.capture Capture RatchetTimer IsoTimer)))784 #+end_src786 #+results: test-header787 : com.aurellem.capture.IsoTimer789 * Source Listing790 - [[../src/cortex/vision.clj][cortex.vision]]791 - [[../src/cortex/test/vision.clj][cortex.test.vision]]792 - [[../src/cortex/video/magick2.clj][cortex.video.magick2]]793 - [[../assets/Models/subtitles/worm-vision-subtitles.blend][worm-vision-subtitles.blend]]794 #+html: <ul> <li> <a href="../org/sense.org">This org file</a> </li> </ul>795 - [[http://hg.bortreb.com ][source-repository]]798 * Next799 I find some [[./hearing.org][ears]] for the creature while exploring the guts of800 jMonkeyEngine's sound system.802 * COMMENT Generate Source803 #+begin_src clojure :tangle ../src/cortex/vision.clj804 <<vision-header>>805 <<pipeline-1>>806 <<pipeline-2>>807 <<retina>>808 <<add-eye>>809 <<sensitivity>>810 <<eye-node>>811 <<add-camera>>812 <<kernel>>813 <<main>>814 <<display>>815 #+end_src817 #+begin_src clojure :tangle ../src/cortex/test/vision.clj818 <<test-header>>819 <<test-1>>820 <<test-2>>821 #+end_src823 #+begin_src clojure :tangle ../src/cortex/video/magick2.clj824 <<magick2>>825 #+end_src