Mercurial > cortex
view org/eyes.org @ 44:f3e33fd5f97d
working through blender tutorial
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 08 Nov 2011 01:38:22 -0700 |
parents | 99971a59b6ac |
children | f080f1e49fba |
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 * Vision12 I want to make creatures with eyes. Each eye can be independely moved13 and should see its own version of the world depending on where it is.14 #+srcname: eyes15 #+begin_src clojure16 (ns cortex.vision17 "Simulate the sense of vision in jMonkeyEngine3. Enables multiple18 eyes from different positions to observe the same world, and pass19 the observed data to any arbitray function."20 {:author "Robert McIntyre"}21 (:use cortex.world)22 (:import com.jme3.post.SceneProcessor)23 (:import (com.jme3.util Screenshots BufferUtils))24 (:import java.nio.ByteBuffer)25 (:import java.awt.image.BufferedImage)26 (:import com.jme3.renderer.ViewPort)27 (:import com.jme3.math.ColorRGBA))29 (defn scene-processor30 "Create a SceneProcessor object which wraps a vision processing31 continuation function. The SceneProcessor will take care of32 converting the rendered frame to a BufferedImage and passing that33 BufferedImage to the continuation. The continuation should be a34 function that takes a BufferedImage."35 [continuation]36 (let [byte-buffer (atom nil)37 renderer (atom nil)38 image (atom nil)]39 (proxy [SceneProcessor] []40 (initialize41 [renderManager viewPort]42 (let [cam (.getCamera viewPort)43 width (.getWidth cam)44 height (.getHeight cam)]45 (reset! renderer (.getRenderer renderManager))46 (reset! byte-buffer47 (BufferUtils/createByteBuffer48 (* width height 4)))49 (reset! image (BufferedImage.50 width height51 BufferedImage/TYPE_4BYTE_ABGR))))52 (isInitialized [] (not (nil? @byte-buffer)))53 (reshape [_ _ _])54 (preFrame [_])55 (postQueue [_])56 (postFrame57 [#^FrameBuffer fb]58 (.clear @byte-buffer)59 (.readFrameBuffer @renderer fb @byte-buffer)60 (Screenshots/convertScreenShot @byte-buffer @image)61 (continuation @image))62 (cleanup []))))64 (defn add-eye65 "Add an eye to the world, calling continuation on every frame66 produced."67 [world camera continuation]68 (let [width (.getWidth camera)69 height (.getHeight camera)70 render-manager (.getRenderManager world)71 viewport (.createMainView render-manager "eye-view" camera)]72 (doto viewport73 (.setClearFlags true true true)74 (.setBackgroundColor ColorRGBA/Gray)75 (.addProcessor (scene-processor continuation))76 (.attachScene (.getRootNode world)))))77 #+end_src79 Note the use of continuation passing style for connecting the eye to a80 function to process the output. You can create any number of eyes, and81 each of them will see the world from their own =Camera=. Once every82 frame, the rendered image is copied to a =BufferedImage=, and that83 data is sent off to the continuation function. Moving the =Camera=84 which was used to create the eye will change what the eye sees.86 * Example88 #+srcname: test-vision89 #+begin_src clojure90 (ns test.vision91 (:use (cortex world util vision))92 (:import java.awt.image.BufferedImage)93 (:import javax.swing.JPanel)94 (:import javax.swing.SwingUtilities)95 (:import java.awt.Dimension)96 (:import javax.swing.JFrame)97 (:import com.jme3.math.ColorRGBA)98 (:import com.jme3.scene.Node))100 (defn view-image101 "Initailizes a JPanel on which you may draw a BufferedImage of the102 given width and height. Returns a function that accepts a103 BufferedImage and draws it to the JPanel."104 [width height]105 (let [image106 (atom107 (BufferedImage. width height BufferedImage/TYPE_4BYTE_ABGR))108 panel109 (proxy [JPanel] []110 (paint111 [graphics]112 (proxy-super paintComponent graphics)113 (.drawImage graphics @image 0 0 nil)))]114 (SwingUtilities/invokeLater115 (fn []116 (.setPreferredSize panel (Dimension. width height))117 (doto (JFrame. "Eye Camera!")118 (-> (.getContentPane) (.add panel))119 (.pack)120 (.setLocationRelativeTo nil)121 (.setResizable false)122 (.setVisible true))))123 (fn [#^BufferedImage i]124 (reset! image i)125 (.repaint panel))))127 (defn test-two-eyes128 "Tests the vision system by creating two views of the same rotating129 object from different angles and displaying both of those views in130 JFrames."131 []132 (.start133 (let [candy134 (box 1 1 1 :physical? false :color ColorRGBA/Blue)]135 (world (doto (Node.)136 (.attachChild candy))137 {}138 (fn [world]139 (let [cam (.clone (.getCamera world))140 width (.getWidth cam)141 height (.getHeight cam)]142 (add-eye world cam (view-image width height))143 (add-eye world144 (doto (.clone cam)145 (.setLocation (Vector3f. -10 0 0))146 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))147 (view-image width height))148 ;; This is here to restore the main view149 ;; after the other views have completed processing150 (add-eye world (.getCamera world) no-op)))151 (fn [world tpf]152 (.rotate candy (* tpf 0.2) 0 0))))))153 #+end_src155 The example code will create two videos of the same rotating object156 from different angles. It can be used both for stereoscopic vision157 simulation or for simulating multiple creatures, each with their own158 sense of vision.160 - As a neat bonus, this idea behind simulated vision also enables one161 to [[../../cortex/html/capture-video.html][capture live video feeds from jMonkeyEngine]].164 * COMMENT code generation165 #+begin_src clojure :tangle ../src/cortex/vision.clj166 <<eyes>>167 #+end_src169 #+begin_src clojure :tangle ../src/test/vision.clj170 <<test-vision>>171 #+end_src