Mercurial > cortex
view org/eyes.org @ 24:e965675ec4d0
moving things around
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 24 Oct 2011 01:19:15 -0700 |
parents | cab2da252494 |
children | 775d97247dd0 |
line wrap: on
line source
1 #+title: Eyes2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulating senses for AI research using JMonkeyEngine35 #+SETUPFILE: ../../aurellem/org/setup.org6 #+INCLUDE: ../../aurellem/org/level-0.org7 #+babel: :mkdirp yes :noweb yes :exports both11 ** Eyes13 Ultimately I want to make creatures with eyes. Each eye can be14 independely moved and should see its own version of the world15 depending on where it is.16 #+srcname: eyes17 #+begin_src clojure18 (ns body.eye)19 (use 'cortex.world)20 (use 'cortex.import)21 (use 'clojure.contrib.def)22 (cortex.import/mega-import-jme3)23 (rlm.rlm-commands/help)24 (import java.nio.ByteBuffer)25 (import java.awt.image.BufferedImage)26 (import java.awt.Color)27 (import java.awt.Dimension)28 (import java.awt.Graphics)29 (import java.awt.Graphics2D)30 (import java.awt.event.WindowAdapter)31 (import java.awt.event.WindowEvent)32 (import java.awt.image.BufferedImage)33 (import java.nio.ByteBuffer)34 (import javax.swing.JFrame)35 (import javax.swing.JPanel)36 (import javax.swing.SwingUtilities)37 (import javax.swing.ImageIcon)38 (import javax.swing.JOptionPane)39 (import java.awt.image.ImageObserver)43 (defn scene-processor44 "deals with converting FrameBuffers to BufferedImages so45 that the continuation function can be defined only in terms46 of what it does with BufferedImages"47 [continuation]48 (let [byte-buffer (atom nil)49 renderer (atom nil)50 image (atom nil)]51 (proxy [SceneProcessor] []52 (initialize53 [renderManager viewPort]54 (let [cam (.getCamera viewPort)55 width (.getWidth cam)56 height (.getHeight cam)]57 (reset! renderer (.getRenderer renderManager))58 (reset! byte-buffer59 (BufferUtils/createByteBuffer60 (* width height 4)))61 (reset! image (BufferedImage. width height62 BufferedImage/TYPE_4BYTE_ABGR))))63 (isInitialized [] (not (nil? @byte-buffer)))64 (reshape [_ _ _])65 (preFrame [_])66 (postQueue [_])67 (postFrame68 [#^FrameBuffer fb]69 (.clear @byte-buffer)70 (.readFrameBuffer @renderer fb @byte-buffer)71 (Screenshots/convertScreenShot @byte-buffer @image)72 (continuation @image))73 (cleanup []))))75 (defn add-eye76 "Add an eye to the world, and call continuation on77 every frame produced"78 [world camera continuation]79 (let [width (.getWidth camera)80 height (.getHeight camera)81 render-manager (.getRenderManager world)82 viewport (.createMainView render-manager "eye-view" camera)]83 (doto viewport84 (.setBackgroundColor ColorRGBA/Black)85 (.setClearFlags true true true)86 (.addProcessor (scene-processor continuation))87 (.attachScene (.getRootNode world)))))89 (defn make-display-frame [display width height]90 (SwingUtilities/invokeLater91 (fn []92 (.setPreferredSize display (Dimension. width height))93 (doto (JFrame. "Eye Camera!")94 (-> (.getContentPane) (.add display))95 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)96 (.pack)97 (.setLocationRelativeTo nil)98 (.setResizable false)99 (.setVisible true)))))101 (defn image-monitor [#^BufferedImage image]102 (proxy [JPanel] []103 (paintComponent104 [g]105 (proxy-super paintComponent g)106 (locking image107 (.drawImage g image 0 0108 (proxy [ImageObserver]109 []110 (imageUpdate111 []112 (proxy-super imageUpdate))))))))114 (defn movie-image []115 (let [setup116 (runonce117 (fn [#^BufferedImage image]118 (let [width (.getWidth image)119 height (.getHeight image)120 display (image-monitor image)121 frame (make-display-frame display width height)]122 display)))]123 (fn [#^BufferedImage image]124 (.repaint (setup image)))))127 (defn observer128 "place thy eye!"129 [world camera]130 (let [eye camera131 width (.getWidth eye)132 height (.getHeight eye)]133 (no-exceptions134 (add-eye135 world136 eye137 (movie-image)))))138 #+end_src140 #+srcname: test-vision141 #+begin_src clojure143 (ns test.vision)144 (use 'cortex.world)145 (use 'cortex.import)146 (use 'clojure.contrib.def)147 (use 'body.eye)148 (cortex.import/mega-import-jme3)149 (rlm.rlm-commands/help)150 (import java.nio.ByteBuffer)151 (import java.awt.image.BufferedImage)152 (import java.awt.Color)153 (import java.awt.Dimension)154 (import java.awt.Graphics)155 (import java.awt.Graphics2D)156 (import java.awt.event.WindowAdapter)157 (import java.awt.event.WindowEvent)158 (import java.awt.image.BufferedImage)159 (import java.nio.ByteBuffer)160 (import javax.swing.JFrame)161 (import javax.swing.JPanel)162 (import javax.swing.SwingUtilities)163 (import javax.swing.ImageIcon)164 (import javax.swing.JOptionPane)165 (import java.awt.image.ImageObserver)168 (def width 200)169 (def height 200)171 (defn camera []172 (doto (Camera. width height)173 (.setFrustumPerspective 45 1 1 1000)174 (.setLocation (Vector3f. -3 0 -5))175 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))177 (defn camera2 []178 (doto (Camera. width height)179 (.setFrustumPerspective 45 1 1 1000)180 (.setLocation (Vector3f. 3 0 -5))181 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))183 (defn setup-fn [world]184 (let [eye (camera)185 width (.getWidth eye)186 height (.getHeight eye)]187 (no-exceptions188 (add-eye189 world190 eye191 (runonce visual))192 (add-eye193 world194 (camera2)195 (runonce visual)))))197 (defn spider-eye [position]198 (doto (Camera. 200 200 )199 (.setFrustumPerspective 45 1 1 1000)200 (.setLocation position)201 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))203 (defn setup-fn* [world]204 (let [eye (camera)205 width (.getWidth eye)206 height (.getHeight eye)]207 ;;(.setClearFlags (.getViewPort world) true true true)208 (observer world (.getCamera world))209 (observer world (spider-eye (Vector3f. 3 0 -5)))210 ;;(observer world (spider-eye (Vector3f. 0 0 -5)))211 ;; (observer world (spider-eye (Vector3f. -3 0 -5)))212 ;; (observer world (spider-eye (Vector3f. 0 3 -5)))213 ;; (observer world (spider-eye (Vector3f. 0 -3 -5)))214 ;; (observer world (spider-eye (Vector3f. 3 3 -5)))215 ;; (observer world (spider-eye (Vector3f. -3 3 -5)))216 ;; (observer world (spider-eye (Vector3f. 3 -3 -5)))217 ;; (observer world (spider-eye (Vector3f. -3 -3 -5)))219 )220 world)222 (defn test-world []223 (let [thing (box 1 1 1 :physical? false)]224 (world225 (doto (Node.)226 (.attachChild thing))227 {}228 setup-fn229 (fn [world tpf]230 (.rotate thing (* tpf 0.2) 0 0)231 ))))234 #+end_src237 #+results: eyes238 : #'body.eye/test-world240 Note the use of continuation passing style for connecting the eye to a241 function to process the output. The example code will create two242 videos of the same rotating cube from different angles, sutiable for243 stereoscopic vision.247 * COMMENT code generation248 #+begin_src clojure :tangle ../src/body/eye.clj249 <<eyes>>250 #+end_src252 #+begin_src clojure :tangle ../src/test/vision.clj253 <<test-vision>>254 #+end_src