view org/eyes.org @ 25:775d97247dd0

cleaning up world.org
author Robert McIntyre <rlm@mit.edu>
date Mon, 24 Oct 2011 05:25:01 -0700
parents e965675ec4d0
children 183744c179e6
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)
41 (defn scene-processor
42 "deals with converting FrameBuffers to BufferedImages so
43 that the continuation function can be defined only in terms
44 of what it does with BufferedImages"
45 [continuation]
46 (let [byte-buffer (atom nil)
47 renderer (atom nil)
48 image (atom nil)]
49 (proxy [SceneProcessor] []
50 (initialize
51 [renderManager viewPort]
52 (let [cam (.getCamera viewPort)
53 width (.getWidth cam)
54 height (.getHeight cam)]
55 (reset! renderer (.getRenderer renderManager))
56 (reset! byte-buffer
57 (BufferUtils/createByteBuffer
58 (* width height 4)))
59 (reset! image (BufferedImage. width height
60 BufferedImage/TYPE_4BYTE_ABGR))))
61 (isInitialized [] (not (nil? @byte-buffer)))
62 (reshape [_ _ _])
63 (preFrame [_])
64 (postQueue [_])
65 (postFrame
66 [#^FrameBuffer fb]
67 (.clear @byte-buffer)
68 (.readFrameBuffer @renderer fb @byte-buffer)
69 (Screenshots/convertScreenShot @byte-buffer @image)
70 (continuation @image))
71 (cleanup []))))
73 (defn add-eye
74 "Add an eye to the world, and call continuation on
75 every frame produced"
76 [world camera continuation]
77 (let [width (.getWidth camera)
78 height (.getHeight camera)
79 render-manager (.getRenderManager world)
80 viewport (.createMainView render-manager "eye-view" camera)]
81 (doto viewport
82 (.setBackgroundColor ColorRGBA/Black)
83 (.setClearFlags true true true)
84 (.addProcessor (scene-processor continuation))
85 (.attachScene (.getRootNode world)))))
87 (defn make-display-frame [display width height]
88 (SwingUtilities/invokeLater
89 (fn []
90 (.setPreferredSize display (Dimension. width height))
91 (doto (JFrame. "Eye Camera!")
92 (-> (.getContentPane) (.add display))
93 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
94 (.pack)
95 (.setLocationRelativeTo nil)
96 (.setResizable false)
97 (.setVisible true)))))
99 (defn image-monitor [#^BufferedImage image]
100 (proxy [JPanel] []
101 (paintComponent
102 [g]
103 (proxy-super paintComponent g)
104 (locking image
105 (.drawImage g image 0 0
106 (proxy [ImageObserver]
107 []
108 (imageUpdate
109 []
110 (proxy-super imageUpdate))))))))
112 (defn movie-image []
113 (let [setup
114 (runonce
115 (fn [#^BufferedImage image]
116 (let [width (.getWidth image)
117 height (.getHeight image)
118 display (image-monitor image)
119 frame (make-display-frame display width height)]
120 display)))]
121 (fn [#^BufferedImage image]
122 (.repaint (setup image)))))
125 (defn observer
126 "place thy eye!"
127 [world camera]
128 (let [eye camera
129 width (.getWidth eye)
130 height (.getHeight eye)]
131 (no-exceptions
132 (add-eye
133 world
134 eye
135 (movie-image)))))
136 #+end_src
138 #+srcname: test-vision
139 #+begin_src clojure
141 (ns test.vision)
142 (use 'cortex.world)
143 (use 'cortex.import)
144 (use 'clojure.contrib.def)
145 (use 'body.eye)
146 (cortex.import/mega-import-jme3)
147 (rlm.rlm-commands/help)
148 (import java.nio.ByteBuffer)
149 (import java.awt.image.BufferedImage)
150 (import java.awt.Color)
151 (import java.awt.Dimension)
152 (import java.awt.Graphics)
153 (import java.awt.Graphics2D)
154 (import java.awt.event.WindowAdapter)
155 (import java.awt.event.WindowEvent)
156 (import java.awt.image.BufferedImage)
157 (import java.nio.ByteBuffer)
158 (import javax.swing.JFrame)
159 (import javax.swing.JPanel)
160 (import javax.swing.SwingUtilities)
161 (import javax.swing.ImageIcon)
162 (import javax.swing.JOptionPane)
163 (import java.awt.image.ImageObserver)
166 (def width 200)
167 (def height 200)
169 (defn camera []
170 (doto (Camera. width height)
171 (.setFrustumPerspective 45 1 1 1000)
172 (.setLocation (Vector3f. -3 0 -5))
173 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
175 (defn camera2 []
176 (doto (Camera. width height)
177 (.setFrustumPerspective 45 1 1 1000)
178 (.setLocation (Vector3f. 3 0 -5))
179 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
181 (defn setup-fn [world]
182 (let [eye (camera)
183 width (.getWidth eye)
184 height (.getHeight eye)]
185 (no-exceptions
186 (add-eye
187 world
188 eye
189 (runonce visual))
190 (add-eye
191 world
192 (camera2)
193 (runonce visual)))))
195 (defn spider-eye [position]
196 (doto (Camera. 200 200 )
197 (.setFrustumPerspective 45 1 1 1000)
198 (.setLocation position)
199 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
201 (defn setup-fn* [world]
202 (let [eye (camera)
203 width (.getWidth eye)
204 height (.getHeight eye)]
205 ;;(.setClearFlags (.getViewPort world) true true true)
206 (observer world (.getCamera world))
207 (observer world (spider-eye (Vector3f. 3 0 -5)))
208 ;;(observer world (spider-eye (Vector3f. 0 0 -5)))
209 ;; (observer world (spider-eye (Vector3f. -3 0 -5)))
210 ;; (observer world (spider-eye (Vector3f. 0 3 -5)))
211 ;; (observer world (spider-eye (Vector3f. 0 -3 -5)))
212 ;; (observer world (spider-eye (Vector3f. 3 3 -5)))
213 ;; (observer world (spider-eye (Vector3f. -3 3 -5)))
214 ;; (observer world (spider-eye (Vector3f. 3 -3 -5)))
215 ;; (observer world (spider-eye (Vector3f. -3 -3 -5)))
217 )
218 world)
220 (defn test-world []
221 (let [thing (box 1 1 1 :physical? false)]
222 (world
223 (doto (Node.)
224 (.attachChild thing))
225 {}
226 setup-fn
227 (fn [world tpf]
228 (.rotate thing (* tpf 0.2) 0 0)
229 ))))
232 #+end_src
235 #+results: eyes
236 : #'body.eye/test-world
238 Note the use of continuation passing style for connecting the eye to a
239 function to process the output. The example code will create two
240 videos of the same rotating cube from different angles, sutiable for
241 stereoscopic vision.
245 * COMMENT code generation
246 #+begin_src clojure :tangle ../src/body/eye.clj
247 <<eyes>>
248 #+end_src
250 #+begin_src clojure :tangle ../src/test/vision.clj
251 <<test-vision>>
252 #+end_src