Mercurial > cortex
comparison org/eyes.org @ 113:a980462ebe76
more lazy/functional version of eyes
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 19 Jan 2012 22:08:46 -0700 |
parents | 128fa71ee188 |
children | 9d0fe7f54e14 |
comparison
equal
deleted
inserted
replaced
112:128fa71ee188 | 113:a980462ebe76 |
---|---|
18 eyes from different positions to observe the same world, and pass | 18 eyes from different positions to observe the same world, and pass |
19 the observed data to any arbitray function." | 19 the observed data to any arbitray function." |
20 {:author "Robert McIntyre"} | 20 {:author "Robert McIntyre"} |
21 (:use cortex.world) | 21 (:use cortex.world) |
22 (:import com.jme3.post.SceneProcessor) | 22 (:import com.jme3.post.SceneProcessor) |
23 (:import (com.jme3.util BufferUtils)) | 23 (:import (com.jme3.util BufferUtils Screenshots)) |
24 (:import java.nio.ByteBuffer) | 24 (:import java.nio.ByteBuffer) |
25 (:import java.awt.image.BufferedImage) | 25 (:import java.awt.image.BufferedImage) |
26 (:import com.jme3.renderer.ViewPort) | 26 (:import com.jme3.renderer.ViewPort) |
27 (:import com.jme3.math.ColorRGBA)) | 27 (:import com.jme3.math.ColorRGBA) |
28 | 28 (:import com.jme3.renderer.Renderer)) |
29 (defn scene-processor | 29 |
30 | |
31 (defn vision-pipeline | |
30 "Create a SceneProcessor object which wraps a vision processing | 32 "Create a SceneProcessor object which wraps a vision processing |
31 continuation function. The SceneProcessor will take care of | 33 continuation function. The continuation is a function that takes |
32 converting the rendered frame to a BufferedImage and passing that | 34 [#^Renderer r #^FrameBuffer fb #^ByteBuffer b #^BufferedImage bi], |
33 BufferedImage to the continuation. The continuation should be a | 35 each of which has already been appropiately sized." |
34 function that takes a ByteBuffer which represents the image." | |
35 [continuation] | 36 [continuation] |
36 (let [byte-buffer (atom nil) | 37 (let [byte-buffer (atom nil) |
37 renderer (atom nil)] | 38 renderer (atom nil) |
39 image (atom nil)] | |
38 (proxy [SceneProcessor] [] | 40 (proxy [SceneProcessor] [] |
39 (initialize | 41 (initialize |
40 [renderManager viewPort] | 42 [renderManager viewPort] |
41 (let [cam (.getCamera viewPort) | 43 (let [cam (.getCamera viewPort) |
42 width (.getWidth cam) | 44 width (.getWidth cam) |
43 height (.getHeight cam)] | 45 height (.getHeight cam)] |
44 (reset! renderer (.getRenderer renderManager)) | 46 (reset! renderer (.getRenderer renderManager)) |
45 (reset! byte-buffer | 47 (reset! byte-buffer |
46 (BufferUtils/createByteBuffer | 48 (BufferUtils/createByteBuffer |
47 (* width height 4))))) | 49 (* width height 4))) |
50 (reset! image (BufferedImage. | |
51 width height | |
52 BufferedImage/TYPE_4BYTE_ABGR)))) | |
48 (isInitialized [] (not (nil? @byte-buffer))) | 53 (isInitialized [] (not (nil? @byte-buffer))) |
49 (reshape [_ _ _]) | 54 (reshape [_ _ _]) |
50 (preFrame [_]) | 55 (preFrame [_]) |
51 (postQueue [_]) | 56 (postQueue [_]) |
52 (postFrame | 57 (postFrame |
53 [#^FrameBuffer fb] | 58 [#^FrameBuffer fb] |
54 (.clear @byte-buffer) | 59 (.clear @byte-buffer) |
55 ;;(.readFrameBuffer @renderer fb @byte-buffer) | 60 (continuation @renderer fb @byte-buffer @image)) |
56 (continuation @byte-buffer)) | |
57 (cleanup [])))) | 61 (cleanup [])))) |
58 | 62 |
59 (defn buffer->image! [width height] | 63 (defn frameBuffer->byteBuffer! |
60 (let [image (BufferedImage. width height | 64 "Transfer the data in the graphics card (Renderer, FrameBuffer) to |
61 BufferedImage/TYPE_4BYTE_ABGR)] | 65 the CPU (ByteBuffer)." |
62 (fn [byte-buffer] | 66 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb] |
63 (Screenshots/convertScreenShot byte-buffer image) | 67 (.readFrameBuffer r fb bb) bb) |
64 image))) | 68 |
69 (defn byteBuffer->bufferedImage! | |
70 "Convert the C-style BGRA image data in the ByteBuffer bb to the AWT | |
71 style ABGR image data and place it in BufferedImage bi." | |
72 [#^ByteBuffer bb #^BufferedImage bi] | |
73 (Screenshots/convertScreenShot bb bi) bi) | |
74 | |
75 (defn BufferedImage! | |
76 "Continuation which will grab the buffered image from the materials | |
77 provided by (vision-pipeline)." | |
78 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi] | |
79 (byteBuffer->bufferedImage! | |
80 (frameBuffer->byteBuffer! r fb bb) bi)) | |
65 | 81 |
66 (defn add-eye | 82 (defn add-eye |
67 "Add an eye to the world, calling continuation on every frame | 83 "Add an eye to the world, calling continuation on every frame |
68 produced." | 84 produced." |
69 [world camera continuation] | 85 [world camera continuation] |
72 render-manager (.getRenderManager world) | 88 render-manager (.getRenderManager world) |
73 viewport (.createMainView render-manager "eye-view" camera)] | 89 viewport (.createMainView render-manager "eye-view" camera)] |
74 (doto viewport | 90 (doto viewport |
75 (.setClearFlags true true true) | 91 (.setClearFlags true true true) |
76 (.setBackgroundColor ColorRGBA/Black) | 92 (.setBackgroundColor ColorRGBA/Black) |
77 (.addProcessor (scene-processor continuation)) | 93 (.addProcessor (vision-pipeline continuation)) |
78 (.attachScene (.getRootNode world))))) | 94 (.attachScene (.getRootNode world))))) |
79 #+end_src | 95 #+end_src |
80 | 96 |
81 #+results: eyes | 97 #+results: eyes |
82 : #'cortex.vision/add-eye | 98 : #'cortex.vision/add-eye |
99 (:import javax.swing.SwingUtilities) | 115 (:import javax.swing.SwingUtilities) |
100 (:import java.awt.Dimension) | 116 (:import java.awt.Dimension) |
101 (:import javax.swing.JFrame) | 117 (:import javax.swing.JFrame) |
102 (:import com.jme3.math.ColorRGBA) | 118 (:import com.jme3.math.ColorRGBA) |
103 (:import com.jme3.scene.Node) | 119 (:import com.jme3.scene.Node) |
104 (:import com.jme3.math.Vector3f) | 120 (:import com.jme3.math.Vector3f)) |
105 (:import (com.jme3.util Screenshots))) | |
106 | 121 |
107 (defn view-image | 122 (defn view-image |
108 "Initailizes a JPanel on which you may draw a BufferedImage. | 123 "Initailizes a JPanel on which you may draw a BufferedImage. |
109 Returns a function that accepts a BufferedImage and draws it to the | 124 Returns a function that accepts a BufferedImage and draws it to the |
110 JPanel." | 125 JPanel." |
150 (fn [world] | 165 (fn [world] |
151 (let [cam (.clone (.getCamera world)) | 166 (let [cam (.clone (.getCamera world)) |
152 width (.getWidth cam) | 167 width (.getWidth cam) |
153 height (.getHeight cam)] | 168 height (.getHeight cam)] |
154 (add-eye world cam | 169 (add-eye world cam |
155 no-op | 170 ;;no-op |
156 ;;(comp (view-image) (buffer->image! width height)) | 171 (comp (view-image) BufferedImage!) |
157 ) | 172 ) |
158 (add-eye world | 173 (add-eye world |
159 (doto (.clone cam) | 174 (doto (.clone cam) |
160 (.setLocation (Vector3f. -10 0 0)) | 175 (.setLocation (Vector3f. -10 0 0)) |
161 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)) | 176 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)) |
162 no-op | 177 ;;no-op |
163 ;;(comp (view-image) (buffer->image! width height)) | 178 (comp (view-image) BufferedImage!)) |
164 ) | |
165 | |
166 | |
167 ;; This is here to restore the main view | 179 ;; This is here to restore the main view |
168 ;; after the other views have completed processing | 180 ;; after the other views have completed processing |
169 (add-eye world (.getCamera world) no-op))) | 181 (add-eye world (.getCamera world) no-op))) |
170 (fn [world tpf] | 182 (fn [world tpf] |
171 (.rotate candy (* tpf 0.2) 0 0))))) | 183 (.rotate candy (* tpf 0.2) 0 0))))) |