changeset 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
files org/eyes.org
diffstat 1 files changed, 39 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/eyes.org	Thu Jan 19 11:29:46 2012 -0700
     1.2 +++ b/org/eyes.org	Thu Jan 19 22:08:46 2012 -0700
     1.3 @@ -20,21 +20,23 @@
     1.4    {:author "Robert McIntyre"}
     1.5    (:use cortex.world)
     1.6    (:import com.jme3.post.SceneProcessor)
     1.7 -  (:import (com.jme3.util BufferUtils))
     1.8 +  (:import (com.jme3.util BufferUtils Screenshots))
     1.9    (:import java.nio.ByteBuffer)
    1.10    (:import java.awt.image.BufferedImage)
    1.11    (:import com.jme3.renderer.ViewPort)
    1.12 -  (:import com.jme3.math.ColorRGBA))
    1.13 +  (:import com.jme3.math.ColorRGBA)
    1.14 +  (:import com.jme3.renderer.Renderer))
    1.15  
    1.16 -(defn scene-processor
    1.17 +
    1.18 +(defn vision-pipeline
    1.19    "Create a SceneProcessor object which wraps a vision processing
    1.20 -  continuation function. The SceneProcessor will take care of
    1.21 -  converting the rendered frame to a BufferedImage and passing that
    1.22 -  BufferedImage to the continuation.  The continuation should be a
    1.23 -  function that takes a ByteBuffer which represents the image."
    1.24 +  continuation function. The continuation is a function that takes 
    1.25 +  [#^Renderer r #^FrameBuffer fb #^ByteBuffer b #^BufferedImage bi],
    1.26 +  each of which has already been appropiately sized."
    1.27    [continuation]
    1.28    (let [byte-buffer (atom nil)
    1.29 -	renderer (atom nil)]
    1.30 +	renderer (atom nil)
    1.31 +        image (atom nil)]
    1.32    (proxy [SceneProcessor] []
    1.33      (initialize
    1.34       [renderManager viewPort]
    1.35 @@ -44,7 +46,10 @@
    1.36         (reset! renderer (.getRenderer renderManager))
    1.37         (reset! byte-buffer
    1.38  	     (BufferUtils/createByteBuffer
    1.39 -	      (* width height 4)))))
    1.40 +	      (* width height 4)))
    1.41 +        (reset! image (BufferedImage.
    1.42 +                      width height
    1.43 +                      BufferedImage/TYPE_4BYTE_ABGR))))
    1.44      (isInitialized [] (not (nil? @byte-buffer)))
    1.45      (reshape [_ _ _])
    1.46      (preFrame [_])
    1.47 @@ -52,16 +57,27 @@
    1.48      (postFrame
    1.49       [#^FrameBuffer fb]
    1.50       (.clear @byte-buffer)
    1.51 -     ;;(.readFrameBuffer @renderer fb @byte-buffer)
    1.52 -     (continuation @byte-buffer))
    1.53 +     (continuation @renderer fb @byte-buffer @image))
    1.54      (cleanup []))))
    1.55      
    1.56 -(defn buffer->image! [width height]
    1.57 -  (let [image (BufferedImage. width height
    1.58 -                              BufferedImage/TYPE_4BYTE_ABGR)]
    1.59 -    (fn [byte-buffer]
    1.60 -      (Screenshots/convertScreenShot byte-buffer image)
    1.61 -      image)))
    1.62 +(defn frameBuffer->byteBuffer!
    1.63 +  "Transfer the data in the graphics card (Renderer, FrameBuffer) to
    1.64 +   the CPU (ByteBuffer)."  
    1.65 +  [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb]
    1.66 +  (.readFrameBuffer r fb bb) bb)
    1.67 +
    1.68 +(defn byteBuffer->bufferedImage!
    1.69 +  "Convert the C-style BGRA image data in the ByteBuffer bb to the AWT
    1.70 +   style ABGR image data and place it in BufferedImage bi."
    1.71 +  [#^ByteBuffer bb #^BufferedImage bi]
    1.72 +  (Screenshots/convertScreenShot bb bi) bi)
    1.73 +
    1.74 +(defn BufferedImage!
    1.75 +  "Continuation which will grab the buffered image from the materials
    1.76 +   provided by (vision-pipeline)."
    1.77 +  [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi]
    1.78 +  (byteBuffer->bufferedImage!
    1.79 +   (frameBuffer->byteBuffer! r fb bb) bi))
    1.80  
    1.81  (defn add-eye
    1.82    "Add an eye to the world, calling continuation on every frame
    1.83 @@ -74,7 +90,7 @@
    1.84      (doto viewport
    1.85        (.setClearFlags true true true)
    1.86        (.setBackgroundColor ColorRGBA/Black)
    1.87 -      (.addProcessor (scene-processor continuation))
    1.88 +      (.addProcessor (vision-pipeline continuation))
    1.89        (.attachScene (.getRootNode world)))))
    1.90  #+end_src
    1.91  
    1.92 @@ -101,8 +117,7 @@
    1.93    (:import javax.swing.JFrame)
    1.94    (:import com.jme3.math.ColorRGBA)
    1.95    (:import com.jme3.scene.Node)
    1.96 -  (:import com.jme3.math.Vector3f)
    1.97 -  (:import (com.jme3.util Screenshots)))
    1.98 +  (:import com.jme3.math.Vector3f))
    1.99  
   1.100  (defn view-image
   1.101    "Initailizes a JPanel on which you may draw a BufferedImage.
   1.102 @@ -152,18 +167,15 @@
   1.103               width (.getWidth cam)
   1.104               height (.getHeight cam)]
   1.105           (add-eye world cam 
   1.106 -                  no-op
   1.107 -                  ;;(comp (view-image) (buffer->image! width height))
   1.108 +                  ;;no-op
   1.109 +                  (comp (view-image) BufferedImage!)
   1.110                    )
   1.111           (add-eye world
   1.112                    (doto (.clone cam)
   1.113                      (.setLocation (Vector3f. -10 0 0))
   1.114                      (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))
   1.115 -                  no-op
   1.116 -                  ;;(comp (view-image) (buffer->image! width height))
   1.117 -                  )
   1.118 -         
   1.119 -         
   1.120 +                  ;;no-op
   1.121 +                  (comp (view-image) BufferedImage!))
   1.122           ;; This is here to restore the main view
   1.123           ;; after the other views have completed processing
   1.124           (add-eye world (.getCamera world) no-op)))