diff org/test-creature.org @ 117:94c005f7f9dd

saving progress
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Jan 2012 05:47:56 -0700
parents 947bef5d6670
children 1261444da2c7
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Fri Jan 20 01:07:46 2012 -0700
     1.2 +++ b/org/test-creature.org	Fri Jan 20 05:47:56 2012 -0700
     1.3 @@ -622,7 +622,9 @@
     1.4  ;; http://en.wikipedia.org/wiki/Retina
     1.5  
     1.6  (defn test-eye  []
     1.7 -  (.getChild (worm-model) "worm-11"))
     1.8 +  (.getChild 
     1.9 +   (.getChild (worm-model) "eyes")
    1.10 +   "eye"))
    1.11  
    1.12  
    1.13  (defn retina-sensor-image
    1.14 @@ -632,7 +634,7 @@
    1.15     values found in the pixel. :red, :green, :blue, :gray are already
    1.16     defined as extracting the red green blue and average components
    1.17     respectively."
    1.18 -   [#^Geometry eye]
    1.19 +   [#^Spatial eye]
    1.20     (if-let [eye-map (meta-data eye "eye")]
    1.21       (map-vals
    1.22        #(ImageToAwt/convert
    1.23 @@ -641,6 +643,16 @@
    1.24        (read-string
    1.25         eye-map))))
    1.26  
    1.27 +(defn eye-dimensions
    1.28 +  "returns the width and height specified in the metadata of the eye"
    1.29 +  [#^Spatial eye]
    1.30 +  (let [dimensions
    1.31 +          (map #(vector (.getWidth %) (.getHeight %))
    1.32 +               (vals (retina-sensor-image eye)))]
    1.33 +    [(apply max (map first dimensions))
    1.34 +     (apply max (map second dimensions))]))
    1.35 +
    1.36 +
    1.37  (defn creature-eyes
    1.38    "The eye nodes which are children of the \"eyes\" node in the
    1.39    creature."
    1.40 @@ -700,14 +712,45 @@
    1.41          (.getGeometry target)
    1.42          (recur (float (* 2 radius)))))))
    1.43  
    1.44 +(defn bind-camera
    1.45 +  "Bind the camera to the Spatial such that it will maintain its
    1.46 +   current position relative to the Spatial no matter how the spatial
    1.47 +   moves."
    1.48 +  [#^Spatial obj #^Camera cam]
    1.49 +  (let [cam-offset (.subtract (.getLocation cam)
    1.50 +                              (.getWorldTranslation obj))
    1.51 +        initial-cam-rotation (Quaternion. (.getRotation cam))
    1.52 +        base-anti-rotation (.inverse (.getWorldRotation obj))]
    1.53 +    (.addControl
    1.54 +     obj
    1.55 +     (proxy [AbstractControl] []
    1.56 +       (controlUpdate [tpf]
    1.57 +         (let [total-rotation
    1.58 +               (.mult base-anti-rotation (.getWorldRotation obj))]
    1.59 +           (.setLocation cam
    1.60 +                         (.add
    1.61 +                          (.mult total-rotation cam-offset)
    1.62 +                          (.getWorldTranslation obj)))
    1.63 +           (.setRotation cam
    1.64 +                         (.mult total-rotation initial-cam-rotation))))
    1.65 +       (controlRender [_ _])))))
    1.66 +
    1.67 +
    1.68  (defn attach-eyes
    1.69 -  "For each eye in the creature, attach a CameraNode to the appropiate
    1.70 +  "For each eye in the creature, attach a Camera to the appropiate
    1.71     area and return the Camera."
    1.72    [#^Node creature]
    1.73    (for [eye (creature-eyes creature)]
    1.74 -    (let [target (eye-target creature eye)]
    1.75 -      (CameraNode
    1.76 -)
    1.77 +    (let [target (eye-target creature eye)
    1.78 +          [cam-width cam-height] (eye-dimensions eye)
    1.79 +          cam (Camera. cam-width cam-height)]
    1.80 +      (.setLocation cam (.getWorldTranslation eye))
    1.81 +      (.setRotation cam (.getWorldRotation eye))
    1.82 +      
    1.83 +      )
    1.84 +      
    1.85 +      
    1.86 +))
    1.87    
    1.88  (defn vision
    1.89  
    1.90 @@ -866,33 +909,10 @@
    1.91  ;; the camera will stay in its initial position/rotation with relation
    1.92  ;; to the spatial.
    1.93  
    1.94 -(defn bind-camera [#^Spatial obj #^Camera cam]
    1.95 -  (let [cam-offset (.subtract (.getLocation cam)
    1.96 -                              (.getWorldTranslation obj))
    1.97 -        initial-cam-rotation (.getRotation cam)
    1.98 -        base-anti-rotation (.inverse (.getWorldRotation obj))]
    1.99 -    (.addControl
   1.100 -     obj
   1.101 -     (proxy [AbstractControl] []
   1.102 -       (controlUpdate [tpf]
   1.103 -         (let [total-rotation
   1.104 -               (.mult base-anti-rotation (.getWorldRotation obj))]
   1.105 -                          
   1.106 -         (.setLocation cam
   1.107 -                       (.add
   1.108 -                        (.mult total-rotation cam-offset)
   1.109 -                        (.getWorldTranslation obj)))
   1.110 -         (.setRotation cam
   1.111 -                       initial-cam-rotation)
   1.112 -                       ;;(.mult total-rotation initial-cam-rotation)
   1.113 -                       
   1.114 -         ))
   1.115  
   1.116 -       (controlRender [_ _])))))
   1.117 -    
   1.118 -
   1.119 -
   1.120 -(defn follow-test []
   1.121 +(defn follow-test
   1.122 +  "show a camera that stays in the same relative position to a blue cube."
   1.123 +  []
   1.124    (let [camera-pos (Vector3f. 0 30 0)
   1.125          rock (box 1 1 1 :color ColorRGBA/Blue
   1.126                       :position (Vector3f. 0 10 0)