diff org/eyes.org @ 112:128fa71ee188

working on retina design
author Robert McIntyre <rlm@mit.edu>
date Thu, 19 Jan 2012 11:29:46 -0700
parents b7a3ba5e879b
children a980462ebe76
line wrap: on
line diff
     1.1 --- a/org/eyes.org	Thu Jan 19 08:09:15 2012 -0700
     1.2 +++ b/org/eyes.org	Thu Jan 19 11:29:46 2012 -0700
     1.3 @@ -20,7 +20,7 @@
     1.4    {:author "Robert McIntyre"}
     1.5    (:use cortex.world)
     1.6    (:import com.jme3.post.SceneProcessor)
     1.7 -  (:import (com.jme3.util Screenshots BufferUtils))
     1.8 +  (:import (com.jme3.util BufferUtils))
     1.9    (:import java.nio.ByteBuffer)
    1.10    (:import java.awt.image.BufferedImage)
    1.11    (:import com.jme3.renderer.ViewPort)
    1.12 @@ -31,11 +31,10 @@
    1.13    continuation function. The SceneProcessor will take care of
    1.14    converting the rendered frame to a BufferedImage and passing that
    1.15    BufferedImage to the continuation.  The continuation should be a
    1.16 -  function that takes a BufferedImage."
    1.17 +  function that takes a ByteBuffer which represents the image."
    1.18    [continuation]
    1.19    (let [byte-buffer (atom nil)
    1.20 -	renderer (atom nil)
    1.21 -	image (atom nil)]
    1.22 +	renderer (atom nil)]
    1.23    (proxy [SceneProcessor] []
    1.24      (initialize
    1.25       [renderManager viewPort]
    1.26 @@ -45,10 +44,7 @@
    1.27         (reset! renderer (.getRenderer renderManager))
    1.28         (reset! byte-buffer
    1.29  	     (BufferUtils/createByteBuffer
    1.30 -	      (* width height 4)))
    1.31 -       (reset! image (BufferedImage.
    1.32 -                      width height
    1.33 -                      BufferedImage/TYPE_4BYTE_ABGR))))
    1.34 +	      (* width height 4)))))
    1.35      (isInitialized [] (not (nil? @byte-buffer)))
    1.36      (reshape [_ _ _])
    1.37      (preFrame [_])
    1.38 @@ -56,11 +52,17 @@
    1.39      (postFrame
    1.40       [#^FrameBuffer fb]
    1.41       (.clear @byte-buffer)
    1.42 -     (.readFrameBuffer @renderer fb @byte-buffer)
    1.43 -     (Screenshots/convertScreenShot @byte-buffer @image)
    1.44 -     (continuation @image))
    1.45 +     ;;(.readFrameBuffer @renderer fb @byte-buffer)
    1.46 +     (continuation @byte-buffer))
    1.47      (cleanup []))))
    1.48      
    1.49 +(defn buffer->image! [width height]
    1.50 +  (let [image (BufferedImage. width height
    1.51 +                              BufferedImage/TYPE_4BYTE_ABGR)]
    1.52 +    (fn [byte-buffer]
    1.53 +      (Screenshots/convertScreenShot byte-buffer image)
    1.54 +      image)))
    1.55 +
    1.56  (defn add-eye
    1.57    "Add an eye to the world, calling continuation on every frame
    1.58    produced." 
    1.59 @@ -71,11 +73,14 @@
    1.60  	viewport (.createMainView render-manager "eye-view" camera)]
    1.61      (doto viewport
    1.62        (.setClearFlags true true true)
    1.63 -      (.setBackgroundColor ColorRGBA/Gray)
    1.64 +      (.setBackgroundColor ColorRGBA/Black)
    1.65        (.addProcessor (scene-processor continuation))
    1.66        (.attachScene (.getRootNode world)))))
    1.67  #+end_src
    1.68  
    1.69 +#+results: eyes
    1.70 +: #'cortex.vision/add-eye
    1.71 +
    1.72  Note the use of continuation passing style for connecting the eye to a
    1.73  function to process the output. You can create any number of eyes, and
    1.74  each of them will see the world from their own =Camera=. Once every
    1.75 @@ -96,7 +101,8 @@
    1.76    (:import javax.swing.JFrame)
    1.77    (:import com.jme3.math.ColorRGBA)
    1.78    (:import com.jme3.scene.Node)
    1.79 -  (:import com.jme3.math.Vector3f))
    1.80 +  (:import com.jme3.math.Vector3f)
    1.81 +  (:import (com.jme3.util Screenshots)))
    1.82  
    1.83  (defn view-image
    1.84    "Initailizes a JPanel on which you may draw a BufferedImage.
    1.85 @@ -137,26 +143,37 @@
    1.86    []
    1.87    (let [candy
    1.88          (box 1 1 1 :physical? false :color ColorRGBA/Blue)]
    1.89 -    (world (doto (Node.)
    1.90 -             (.attachChild candy))
    1.91 -           {}
    1.92 -           (fn [world]
    1.93 -             (let [cam (.clone (.getCamera world))
    1.94 -                   width (.getWidth cam)
    1.95 -                   height (.getHeight cam)]
    1.96 -               (add-eye world cam (view-image))
    1.97 -               (add-eye world
    1.98 -                        (doto (.clone cam)
    1.99 -                          (.setLocation (Vector3f. -10 0 0))
   1.100 -                          (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))
   1.101 -                        (view-image))
   1.102 -                ;; This is here to restore the main view
   1.103 -               ;; after the other views have completed processing
   1.104 -               (add-eye world (.getCamera world) no-op)))
   1.105 -           (fn [world tpf]
   1.106 -             (.rotate candy (* tpf 0.2) 0 0)))))
   1.107 +    (world
   1.108 +     (doto (Node.)
   1.109 +       (.attachChild candy))
   1.110 +     {}
   1.111 +     (fn [world]
   1.112 +       (let [cam (.clone (.getCamera world))
   1.113 +             width (.getWidth cam)
   1.114 +             height (.getHeight cam)]
   1.115 +         (add-eye world cam 
   1.116 +                  no-op
   1.117 +                  ;;(comp (view-image) (buffer->image! width height))
   1.118 +                  )
   1.119 +         (add-eye world
   1.120 +                  (doto (.clone cam)
   1.121 +                    (.setLocation (Vector3f. -10 0 0))
   1.122 +                    (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))
   1.123 +                  no-op
   1.124 +                  ;;(comp (view-image) (buffer->image! width height))
   1.125 +                  )
   1.126 +         
   1.127 +         
   1.128 +         ;; This is here to restore the main view
   1.129 +         ;; after the other views have completed processing
   1.130 +         (add-eye world (.getCamera world) no-op)))
   1.131 +     (fn [world tpf]
   1.132 +       (.rotate candy (* tpf 0.2) 0 0)))))
   1.133  #+end_src
   1.134  
   1.135 +#+results: test-vision
   1.136 +: #'cortex.test.vision/test-two-eyes
   1.137 +
   1.138  The example code will create two videos of the same rotating object
   1.139  from different angles. It can be used both for stereoscopic vision
   1.140  simulation or for simulating multiple creatures, each with their own