view org/eyes.org @ 23:cab2da252494

split off the rest of cortex.org
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 23:54:26 -0700
parents
children e965675ec4d0
line wrap: on
line source
1 #+title: Eyes
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulating senses for AI research using JMonkeyEngine3
5 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org
7 #+babel: :mkdirp yes :noweb yes :exports both
11 ** Eyes
13 Ultimately I want to make creatures with eyes. Each eye can be
14 independely moved and should see its own version of the world
15 depending on where it is.
16 #+srcname: eyes
17 #+begin_src clojure
18 (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-processor
44 "deals with converting FrameBuffers to BufferedImages so
45 that the continuation function can be defined only in terms
46 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 (initialize
53 [renderManager viewPort]
54 (let [cam (.getCamera viewPort)
55 width (.getWidth cam)
56 height (.getHeight cam)]
57 (reset! renderer (.getRenderer renderManager))
58 (reset! byte-buffer
59 (BufferUtils/createByteBuffer
60 (* width height 4)))
61 (reset! image (BufferedImage. width height
62 BufferedImage/TYPE_4BYTE_ABGR))))
63 (isInitialized [] (not (nil? @byte-buffer)))
64 (reshape [_ _ _])
65 (preFrame [_])
66 (postQueue [_])
67 (postFrame
68 [#^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-eye
76 "Add an eye to the world, and call continuation on
77 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 viewport
84 (.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/invokeLater
91 (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 (paintComponent
104 [g]
105 (proxy-super paintComponent g)
106 (locking image
107 (.drawImage g image 0 0
108 (proxy [ImageObserver]
109 []
110 (imageUpdate
111 []
112 (proxy-super imageUpdate))))))))
114 (defn movie-image []
115 (let [setup
116 (runonce
117 (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 observer
128 "place thy eye!"
129 [world camera]
130 (let [eye camera
131 width (.getWidth eye)
132 height (.getHeight eye)]
133 (no-exceptions
134 (add-eye
135 world
136 eye
137 (movie-image)))))
138 #+end_src
140 #+srcname: test-vision
141 #+begin_src clojure
143 (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-exceptions
188 (add-eye
189 world
190 eye
191 (runonce visual))
192 (add-eye
193 world
194 (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 (world
225 (doto (Node.)
226 (.attachChild thing))
227 {}
228 setup-fn
229 (fn [world tpf]
230 (.rotate thing (* tpf 0.2) 0 0)
231 ))))
234 #+end_src
237 #+results: eyes
238 : #'body.eye/test-world
240 Note the use of continuation passing style for connecting the eye to a
241 function to process the output. The example code will create two
242 videos of the same rotating cube from different angles, sutiable for
243 stereoscopic vision.