rlm@0: #+title: Simulated Senses rlm@0: #+author: Robert McIntyre rlm@0: #+email: rlm@mit.edu rlm@0: #+description: Simulating senses for AI research using JMonkeyEngine3 rlm@4: #+SETUPFILE: ../../aurellem/org/setup.org rlm@4: #+INCLUDE: ../../aurellem/org/level-0.org rlm@21: #+babel: :mkdirp yes :noweb yes :exports both rlm@0: rlm@0: rlm@0: * Simulation Base rlm@0: rlm@0: ** Imports rlm@21: jMonkeyEngine has a plethora of classes which can be overwhelming at rlm@21: first. So that I one can get right to coding, it's good to take the rlm@21: time right now and make a "import all" function which brings in all of rlm@21: the important jme3 classes. Once I'm happy with the general structure rlm@21: of a namespace I can deal with importing only the classes it actually rlm@21: needs. rlm@21: rlm@0: #+srcname: import rlm@0: #+begin_src clojure :results silent rlm@0: (ns cortex.import rlm@0: (:require swank.util.class-browse)) rlm@0: rlm@21: (defn permissive-import rlm@21: [classname] rlm@21: (eval `(try (import '~classname) rlm@21: (catch java.lang.Exception e# rlm@21: (println "couldn't import " '~classname)))) rlm@21: classname) rlm@0: rlm@21: (defn jme-class? [classname] rlm@21: (and rlm@21: (.startsWith classname "com.jme3.") rlm@21: ;; Don't import the Lwjgl stuff since it can throw exceptions rlm@21: ;; upon being loaded. rlm@21: (not (re-matches #".*Lwjgl.*" classname)))) rlm@0: rlm@21: (defn jme-classes rlm@21: "returns a list of all jme3 classes" rlm@21: [] rlm@21: (filter rlm@21: jme-class? rlm@21: (map :name rlm@21: swank.util.class-browse/available-classes))) rlm@0: rlm@0: (defn mega-import-jme3 rlm@21: "Import ALL the jme classes. For REPL use." rlm@0: [] rlm@21: (doall rlm@21: (map (comp permissive-import symbol) (jme-classes)))) rlm@0: #+end_src rlm@0: rlm@0: The =mega-import-jme3= is quite usefull for debugging purposes since rlm@21: it allows completion for almost all of JME's classes. rlm@21: rlm@21: Out of curiousity, let's see just how many classes =mega-import-jme3= rlm@21: imports: rlm@21: rlm@21: #+begin_src clojure :exports both rlm@21: (clojure.core/count (cortex.import/jme-classes)) rlm@21: #+end_src rlm@21: rlm@21: #+results: rlm@21: : 955 rlm@0: rlm@0: ** Simplification rlm@0: *** World rlm@0: rlm@0: It is comvienent to wrap the JME elements that deal with creating a rlm@0: world, creation of basic objects, and Keyboard input with a nicer rlm@0: interface (at least for my purposes). rlm@0: rlm@0: #+srcname: world-inputs rlm@0: #+begin_src clojure :results silent rlm@0: (ns cortex.world) rlm@0: (require 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (rlm.rlm-commands/help) rlm@0: (cortex.import/mega-import-jme3) rlm@0: rlm@0: (defvar *app-settings* rlm@0: (doto (AppSettings. true) rlm@0: (.setFullscreen false) rlm@0: (.setTitle "Aurellem.") rlm@0: ;; disable 32 bit stuff for now rlm@6: ;;(.setAudioRenderer "Send") rlm@0: ) rlm@0: "These settings control how the game is displayed on the screen for rlm@0: debugging purposes. Use binding forms to change this if desired. rlm@0: Full-screen mode does not work on some computers.") rlm@0: rlm@0: (defn asset-manager rlm@0: "returns a new, configured assetManager" [] rlm@0: (JmeSystem/newAssetManager rlm@0: (.getResource rlm@0: (.getContextClassLoader (Thread/currentThread)) rlm@0: "com/jme3/asset/Desktop.cfg"))) rlm@0: rlm@0: (defmacro no-exceptions rlm@0: "Sweet relief like I never knew." rlm@0: [& forms] rlm@0: `(try ~@forms (catch Exception e# (.printStackTrace e#)))) rlm@0: rlm@0: (defn thread-exception-removal [] rlm@0: (println "removing exceptions from " (Thread/currentThread)) rlm@0: (.setUncaughtExceptionHandler rlm@0: (Thread/currentThread) rlm@0: (proxy [Thread$UncaughtExceptionHandler] [] rlm@0: (uncaughtException rlm@0: [thread thrown] rlm@0: (println "uncaught-exception thrown in " thread) rlm@0: (println (.getMessage thrown)))))) rlm@0: rlm@0: (def println-repl (bound-fn [& args] (apply println args))) rlm@0: rlm@0: (use '[pokemon [lpsolve :only [constant-map]]]) rlm@0: rlm@0: (defn no-op [& _]) rlm@0: rlm@0: (defn all-keys rlm@0: "Construct a map of strings representing all the manual inputs from rlm@0: either the keyboard or mouse." rlm@0: [] rlm@0: (let [inputs (constant-map KeyInput)] rlm@0: (assoc rlm@0: (zipmap (map (fn [field] rlm@0: (.toLowerCase (re-gsub #"_" "-" field))) (vals inputs)) rlm@0: (map (fn [val] (KeyTrigger. val)) (keys inputs))) rlm@0: ;;explicitly add mouse controls rlm@0: "mouse-left" (MouseButtonTrigger. 0) rlm@0: "mouse-middle" (MouseButtonTrigger. 2) rlm@0: "mouse-right" (MouseButtonTrigger. 1)))) rlm@0: rlm@0: (defn initialize-inputs rlm@0: "more java-interop cruft to establish keybindings for a particular virtual world" rlm@0: [game input-manager key-map] rlm@0: (doall (map (fn [[name trigger]] rlm@0: (.addMapping ^InputManager input-manager rlm@0: name (into-array (class trigger) [trigger]))) key-map)) rlm@0: (doall (map (fn [name] rlm@0: (.addListener ^InputManager input-manager game rlm@0: (into-array String [name]))) (keys key-map)))) rlm@0: rlm@0: #+end_src rlm@0: rlm@0: These functions are all for debug controlling of the world through rlm@0: keyboard and mouse. rlm@0: rlm@0: We reuse =constant-map= from =pokemon.lpsolve= to get the numerical rlm@0: values for all the keys defined in the =KeyInput= class. The rlm@0: documentation for =constant-map= is: rlm@0: rlm@0: #+begin_src clojure :results output rlm@0: (doc pokemon.lpsolve/constant-map) rlm@0: #+end_src rlm@0: rlm@0: #+results: rlm@0: : ------------------------- rlm@0: : pokemon.lpsolve/constant-map rlm@0: : ([class]) rlm@0: : Takes a class and creates a map of the static constant integer rlm@0: : fields with their names. This helps with C wrappers where they have rlm@0: : just defined a bunch of integer constants instead of enums rlm@0: rlm@0: rlm@0: Then, =all-keys= converts the constant names like =KEY_J= to the more rlm@0: clojure-like =key-j=, and returns a map from these keys to rlm@0: jMonkeyEngine KeyTrigger objects, the use of which will soon become rlm@0: apparent. =all-keys= also adds the three mouse button controls to the rlm@0: map. rlm@0: rlm@0: #+srcname: world rlm@0: #+begin_src clojure :results silent rlm@0: (in-ns 'cortex.world) rlm@0: rlm@0: (defn traverse rlm@0: "apply f to every non-node, deeply" rlm@0: [f node] rlm@0: (if (isa? (class node) Node) rlm@0: (dorun (map (partial traverse f) (.getChildren node))) rlm@0: (f node))) rlm@0: rlm@0: (def gravity (Vector3f. 0 -9.81 0)) rlm@0: rlm@0: (defn world rlm@0: [root-node key-map setup-fn update-fn] rlm@0: (let [physics-manager (BulletAppState.) rlm@0: shadow-renderer (BasicShadowRenderer. (asset-manager) (int 256)) rlm@0: ;;maybe use a better shadow renderer someday! rlm@0: ;;shadow-renderer (PssmShadowRenderer. (asset-manager) 256 1) rlm@0: ] rlm@0: (doto rlm@0: (proxy [SimpleApplication ActionListener] [] rlm@0: (simpleInitApp rlm@0: [] rlm@0: (no-exceptions rlm@0: (.setTimer this (IsoTimer. 60)) rlm@0: ;; Create key-map. rlm@0: (.setFrustumFar (.getCamera this) 300) rlm@0: (initialize-inputs this (.getInputManager this) (all-keys)) rlm@0: ;; Don't take control of the mouse rlm@0: (org.lwjgl.input.Mouse/setGrabbed false) rlm@0: ;; add all objects to the world rlm@0: (.attachChild (.getRootNode this) root-node) rlm@0: ;; enable physics rlm@0: ;; add a physics manager rlm@0: (.attach (.getStateManager this) physics-manager) rlm@0: (.setGravity (.getPhysicsSpace physics-manager) gravity) rlm@0: rlm@0: rlm@0: ;; go through every object and add it to the physics manager rlm@0: ;; if relavant. rlm@0: (traverse (fn [geom] rlm@0: (dorun rlm@0: (for [n (range (.getNumControls geom))] rlm@0: (do rlm@0: (println-repl "adding control " (.getControl geom n)) rlm@0: (.add (.getPhysicsSpace physics-manager) rlm@0: (.getControl geom n)))))) rlm@0: (.getRootNode this)) rlm@0: ;;(.addAll (.getPhysicsSpace physics-manager) (.getRootNode this)) rlm@0: rlm@0: (setup-fn this) rlm@0: (.setDirection shadow-renderer rlm@0: (.normalizeLocal (Vector3f. -1 -1 -1))) rlm@0: (.addProcessor (.getViewPort this) shadow-renderer) rlm@0: (.setShadowMode (.getRootNode this) RenderQueue$ShadowMode/Off) rlm@0: )) rlm@0: (simpleUpdate rlm@0: [tpf] rlm@0: (no-exceptions rlm@0: (update-fn this tpf))) rlm@0: (onAction rlm@0: [binding value tpf] rlm@0: ;; whenever a key is pressed, call the function returned from rlm@0: ;; key-map. rlm@0: (no-exceptions rlm@0: (if-let [react (key-map binding)] rlm@0: (react this value))))) rlm@0: ;; don't show a menu to change options. rlm@0: rlm@0: (.setShowSettings false) rlm@0: (.setPauseOnLostFocus false) rlm@0: (.setSettings *app-settings*)))) rlm@0: rlm@0: (defn apply-map rlm@0: "Like apply, but works for maps and functions that expect an implicit map rlm@0: and nothing else as in (fn [& {}]). rlm@0: ------- Example ------- rlm@4: (defn jjj [& {:keys [www] :or {www \"oh yeah\"} :as env}] (println www)) rlm@0: (apply-map jjj {:www \"whatever\"}) rlm@0: -->\"whatever\"" rlm@0: [fn m] rlm@0: (apply fn (reduce #(into %1 %2) [] m))) rlm@0: rlm@0: #+end_src rlm@0: rlm@0: rlm@0: =world= is the most important function here. rlm@0: *** TODO more documentation rlm@0: rlm@0: #+srcname: world-shapes rlm@0: #+begin_src clojure :results silent rlm@0: (in-ns 'cortex.world) rlm@0: (defrecord shape-description rlm@0: [name rlm@0: color rlm@0: mass rlm@0: friction rlm@0: texture rlm@0: material rlm@0: position rlm@0: rotation rlm@0: shape rlm@0: physical?]) rlm@0: rlm@0: (def base-shape rlm@0: (shape-description. rlm@0: "default-shape" rlm@0: false rlm@0: ;;ColorRGBA/Blue rlm@0: 1.0 ;; mass rlm@0: 1.0 ;; friction rlm@0: ;; texture rlm@0: "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: ;; material rlm@0: "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: Vector3f/ZERO rlm@0: Quaternion/IDENTITY rlm@0: (Box. Vector3f/ZERO 0.5 0.5 0.5) rlm@0: true)) rlm@0: rlm@0: (defn make-shape rlm@0: [#^shape-description d] rlm@6: (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager)) rlm@6: mat (Material. asset-manager (:material d)) rlm@0: geom (Geometry. (:name d) (:shape d))] rlm@0: (if (:texture d) rlm@0: (let [key (TextureKey. (:texture d))] rlm@0: (.setGenerateMips key true) rlm@6: (.setTexture mat "ColorMap" (.loadTexture asset-manager key)))) rlm@0: (if (:color d) (.setColor mat "Color" (:color d))) rlm@0: (.setMaterial geom mat) rlm@0: (if-let [rotation (:rotation d)] (.rotate geom rotation)) rlm@0: (.setLocalTranslation geom (:position d)) rlm@0: (if (:physical? d) rlm@6: (let [impact-shape (doto (GImpactCollisionShape. rlm@6: (.getMesh geom)) (.setMargin 0)) rlm@0: physics-control (RigidBodyControl. rlm@6: ;;impact-shape ;; comment to disable rlm@0: (float (:mass d)))] rlm@0: (.createJmeMesh impact-shape) rlm@0: (.addControl geom physics-control) rlm@0: ;;(.setSleepingThresholds physics-control (float 0) (float 0)) rlm@0: (.setFriction physics-control (:friction d)))) rlm@0: ;;the default is to keep this node in the physics engine forever. rlm@0: ;;these commands must come after the control is added to the geometry. rlm@0: ;; rlm@0: geom)) rlm@0: rlm@0: (defn box rlm@0: ([l w h & {:as options}] rlm@0: (let [options (merge base-shape options)] rlm@0: (make-shape (assoc options rlm@0: :shape (Box. l w h))))) rlm@0: ([] (box 0.5 0.5 0.5))) rlm@0: rlm@0: (defn sphere rlm@0: ([r & {:as options}] rlm@0: (let [options (merge base-shape options)] rlm@0: (make-shape (assoc options rlm@0: :shape (Sphere. 32 32 (float r)))))) rlm@0: ([] (sphere 0.5))) rlm@0: rlm@15: (defn add-element rlm@15: ([game element node] rlm@0: (.addAll rlm@0: (.getPhysicsSpace rlm@0: (.getState rlm@0: (.getStateManager game) rlm@0: BulletAppState)) rlm@15: element) rlm@15: (.attachChild node element)) rlm@15: ([game element] rlm@15: (add-element game element (.getRootNode game)))) rlm@15: rlm@0: rlm@0: (defn set-gravity* rlm@0: [game gravity] rlm@0: (traverse rlm@0: (fn [geom] rlm@0: (if-let rlm@0: [control (.getControl geom RigidBodyControl)] rlm@0: (do rlm@0: (.setGravity control gravity) rlm@0: (.applyImpulse control Vector3f/ZERO Vector3f/ZERO) rlm@0: ))) rlm@0: (.getRootNode game))) rlm@0: rlm@0: #+end_src rlm@0: rlm@0: These are convienence functions for creating JME objects and rlm@0: manipulating a world. rlm@0: rlm@0: #+srcname: world-view rlm@0: #+begin_src clojure :results silent rlm@0: (in-ns 'cortex.world) rlm@0: rlm@0: (defprotocol Viewable rlm@0: (view [something])) rlm@0: rlm@0: (extend-type com.jme3.scene.Geometry rlm@0: Viewable rlm@0: (view [geo] rlm@0: (view (doto (Node.)(.attachChild geo))))) rlm@0: rlm@0: (extend-type com.jme3.scene.Node rlm@0: Viewable rlm@0: (view [node] rlm@0: (.start rlm@0: (world node rlm@0: {} rlm@0: (fn [world] rlm@0: (.enableDebug rlm@0: (.getPhysicsSpace rlm@0: (.getState rlm@0: (.getStateManager world) rlm@0: BulletAppState)) rlm@0: (asset-manager)) rlm@0: (set-gravity* world Vector3f/ZERO) rlm@0: ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0)) rlm@0: (let [sun (doto (DirectionalLight.) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 1 0 -2))) rlm@0: (.setColor ColorRGBA/White))] rlm@0: (.addLight (.getRootNode world) sun))) rlm@0: no-op)))) rlm@0: rlm@0: (defn position-camera [game] rlm@0: (doto (.getCamera game) rlm@0: (.setLocation (Vector3f. 0 6 6)) rlm@0: (.lookAt Vector3f/ZERO (Vector3f. 0 1 0)))) rlm@0: rlm@0: #+end_src rlm@0: rlm@0: Here I make the =Viewable= protocol and extend it to JME's types. Now rlm@0: hello-world can be written as easily as: rlm@0: rlm@0: #+begin_src clojure :results silent rlm@0: (cortex.world/view (cortex.world/box)) rlm@0: #+end_src rlm@0: rlm@0: ** Hello rlm@0: Here are the jmonkeyengine "Hello" programs translated to clojure. rlm@0: *** Hello Simple App rlm@0: Here is the hello world example for jme3 in clojure. rlm@0: It's a more or less direct translation from the java source rlm@0: from rlm@0: http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication. rlm@0: rlm@0: Of note is the fact that since we don't have access to the rlm@0: =AssetManager= via extendig =SimpleApplication=, we have to build one rlm@0: ourselves. rlm@0: rlm@0: #+srcname: hello-simple-app rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.hello-simple-app) rlm@0: (require 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (rlm.rlm-commands/help) rlm@21: (cortex.import/mega-import-jme3) rlm@0: (use 'cortex.world) rlm@0: rlm@0: rlm@0: (def cube (Box. Vector3f/ZERO 1 1 1)) rlm@0: rlm@0: (def geom (Geometry. "Box" cube)) rlm@0: rlm@0: (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md")) rlm@0: rlm@0: (.setColor mat "Color" ColorRGBA/Blue) rlm@0: rlm@0: (.setMaterial geom mat) rlm@0: rlm@0: (defn simple-app [] rlm@0: (doto rlm@0: (proxy [SimpleApplication] [] rlm@0: (simpleInitApp rlm@0: [] rlm@0: ;; Don't take control of the mouse rlm@0: (org.lwjgl.input.Mouse/setGrabbed false) rlm@0: (.attachChild (.getRootNode this) geom))) rlm@0: ;; don't show a menu to change options. rlm@0: (.setShowSettings false) rlm@0: (.setPauseOnLostFocus false) rlm@0: (.setSettings *app-settings*))) rlm@0: #+end_src rlm@0: rlm@0: Running this program will begin a new jMonkeyEngine game which rlm@0: displays a single blue cube. rlm@0: rlm@0: #+begin_src clojure :exports code :results silent rlm@0: (.start (hello.hello-simple-app/simple-app)) rlm@0: #+end_src rlm@0: rlm@0: #+caption: the simplest JME game. rlm@0: [[./images/simple-app.jpg]] rlm@0: rlm@0: rlm@0: rlm@0: *** Hello Physics rlm@0: From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics rlm@0: rlm@0: #+srcname: brick-wall-header rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.brick-wall) rlm@0: (require 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (rlm.rlm-commands/help) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (use '[pokemon [lpsolve :only [constant-map]]]) rlm@0: (use 'cortex.world) rlm@0: #+end_src rlm@0: rlm@0: #+srcname: brick-wall-body rlm@0: #+begin_src clojure :results silent rlm@0: (in-ns 'hello.brick-wall) rlm@0: rlm@0: (defn floor rlm@0: "make a sturdy, unmovable physical floor" rlm@0: [] rlm@0: (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0))) rlm@0: rlm@0: (def brick-length 0.48) rlm@0: (def brick-width 0.24) rlm@0: (def brick-height 0.12) rlm@0: rlm@0: rlm@0: (defn brick* [position] rlm@0: (doto (box brick-length brick-height brick-width rlm@0: :position position :name "brick" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: :mass 36) rlm@0: (-> rlm@0: (.getMesh) rlm@0: (.scaleTextureCoordinates (Vector2f. 1 0.5))) rlm@0: ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive) rlm@0: ) rlm@0: ) rlm@0: rlm@0: (defn inception-brick-wall rlm@0: "construct a physical brick wall" rlm@0: [] rlm@0: (let [node (Node. "brick-wall")] rlm@0: (dorun rlm@0: (map (comp #(.attachChild node %) brick*) rlm@0: (for rlm@0: [x (range 15) rlm@0: y (range 10) rlm@0: z (range 1)] rlm@0: (Vector3f. rlm@0: (* brick-length x 1.03) rlm@0: (* brick-width y y 10) rlm@0: (* brick-height z))))) rlm@0: node)) rlm@0: rlm@0: (defn gravity-toggle rlm@0: [new-value] rlm@0: (fn [game value] rlm@0: (println-repl "set gravity to " new-value) rlm@0: (if value rlm@0: (set-gravity* game new-value) rlm@0: (set-gravity* game gravity)))) rlm@0: rlm@15: (defn fire-cannon-ball rlm@15: ([node] rlm@15: (fn [game value] rlm@15: (if (not value) rlm@15: (let [camera (.getCamera game) rlm@15: cannon-ball rlm@15: (sphere 0.7 rlm@15: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@15: :texture "Textures/PokeCopper.jpg" rlm@15: :position rlm@15: (.add (.getLocation camera) rlm@15: (.mult (.getDirection camera) (float 1))) rlm@15: :mass 3)] ;200 0.05 rlm@15: (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive) rlm@15: (.setLinearVelocity rlm@15: (.getControl cannon-ball RigidBodyControl) rlm@15: (.mult (.getDirection camera) (float 50))) ;50 rlm@21: (add-element game cannon-ball (if node node (.getRootNode game))))))) rlm@15: ([] rlm@15: (fire-cannon-ball false))) rlm@15: rlm@0: rlm@0: (defn floor* [] rlm@0: (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240 rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/Pond/Pond.png" rlm@0: :position (Vector3f. 0 -0.1 0 ) rlm@0: :mass 0) rlm@0: (-> rlm@0: (.getMesh) rlm@0: (.scaleTextureCoordinates (Vector2f. 3 6)));64 64 rlm@0: (-> rlm@0: (.getMaterial) rlm@0: (.getTextureParam "ColorMap") rlm@0: (.getTextureValue) rlm@0: (.setWrap Texture$WrapMode/Repeat)) rlm@0: (.setShadowMode RenderQueue$ShadowMode/Receive) rlm@0: )) rlm@0: rlm@0: (defn brick-wall* [] rlm@0: (let [node (Node. "brick-wall")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) brick*) rlm@0: (for [y (range 15) rlm@0: x (range 4) rlm@0: z (range 1)] rlm@0: (Vector3f. rlm@0: (+ (* 2 x brick-length) rlm@0: (if (even? (+ y z)) rlm@0: (/ brick-length 4) (/ brick-length -4))) rlm@0: (+ (* brick-height (inc (* 2 y)))) rlm@0: (* 2 z brick-width) )))) rlm@0: (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive) rlm@0: node)) rlm@0: rlm@0: (defn brick-wall-game-run [] rlm@0: (doto rlm@0: (world rlm@0: (doto (Node.) (.attachChild (floor*)) rlm@0: (.attachChild (brick-wall*)) rlm@0: ) rlm@0: {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81)) rlm@0: "key-m" (gravity-toggle (Vector3f. 0 0 9.81)) rlm@0: "key-l" (gravity-toggle (Vector3f. 9.81 0 0)) rlm@0: "key-j" (gravity-toggle (Vector3f. -9.81 0 0)) rlm@0: "key-k" (gravity-toggle Vector3f/ZERO) rlm@0: "key-u" (gravity-toggle (Vector3f. 0 9.81 0)) rlm@0: "key-o" (gravity-toggle (Vector3f. 0 -9.81 0)) rlm@0: "key-f" (fn[game value] rlm@0: (if (not value) (add-element game (brick-wall*)))) rlm@0: "key-return" (fire-cannon-ball)} rlm@0: position-camera rlm@0: (fn [& _])) rlm@0: (.start))) rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :results silent rlm@0: (hello.brick-wall/brick-wall-game-run) rlm@0: #+end_src rlm@0: rlm@0: #+caption: the brick wall standing rlm@0: [[./images/brick-wall-standing.jpg]] rlm@0: rlm@0: #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball" rlm@0: [[./images/brick-wall-knocked-down.jpg]] rlm@0: rlm@0: *** Other Brick Games rlm@0: #+srcname: other-games rlm@0: #+begin_src clojure :results silent rlm@0: (ns cortex.other-games rlm@0: {:author "Dylan Holmes"}) rlm@0: (use 'cortex.world) rlm@0: (use 'hello.brick-wall) rlm@0: (use 'cortex.import) rlm@0: (cortex.import/mega-import-jme3) rlm@0: rlm@0: (defn scad [position] rlm@0: (doto (box 0.1 0.1 0.1 rlm@0: :position position :name "brick" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: :mass 20) rlm@0: (-> rlm@0: (.getMesh) rlm@0: (.scaleTextureCoordinates (Vector2f. 1 0.5)) rlm@0: ) rlm@0: (-> (.getControl RigidBodyControl) rlm@0: (.setLinearVelocity (Vector3f. 0 100 0)) rlm@0: ) rlm@0: rlm@0: ;;(.setShadowMode RenderQueue$ShadowMode/Cast) rlm@0: )) rlm@0: rlm@0: rlm@0: (defn shrapnel [] rlm@0: (let [node (Node. "explosion-day")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) scad) rlm@0: (for [y (range 15) rlm@0: x (range 4) rlm@0: z (range 1)] rlm@0: (Vector3f. rlm@0: (+ (* 2 x brick-height) rlm@0: (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4))) rlm@0: (+ (* brick-height (inc (* 2 y)))) rlm@0: (* 2 z brick-height) )))) rlm@0: node)) rlm@0: rlm@0: rlm@0: (def domino-height 0.48) rlm@0: (def domino-thickness 0.12) rlm@0: (def domino-width 0.24) rlm@0: rlm@0: (def domino-thickness 0.05) rlm@0: (def domino-width 0.5) rlm@0: (def domino-height 1) rlm@0: rlm@0: (defn domino rlm@0: ([position] rlm@0: (domino position (Quaternion/IDENTITY))) rlm@0: ([position rotation] rlm@0: (doto (box domino-width domino-height domino-thickness rlm@0: :position position :name "domino" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: :mass 1 rlm@0: :rotation rotation) rlm@0: (.setShadowMode RenderQueue$ShadowMode/CastAndReceive) rlm@0: ))) rlm@0: rlm@0: rlm@0: (defn domino-row [] rlm@0: (let [node (Node. "domino-row")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) domino) rlm@0: (for [ rlm@0: z (range 10) rlm@0: x (range 5) rlm@0: ] rlm@0: (Vector3f. rlm@0: (+ (* z domino-width) (* x 5 domino-width)) rlm@0: (/ domino-height 1) rlm@0: (* -5.5 domino-thickness z) )))) rlm@0: rlm@0: node)) rlm@0: rlm@0: (defn domino-cycle [] rlm@0: (let [node (Node. "domino-cycle")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) (partial apply domino) ) rlm@0: (for [n (range 720)] rlm@0: (let [space (* domino-height 5.5) rlm@0: r (fn[n] (* (+ n 3) domino-width 0.5)) rlm@0: t (fn[n] (reduce rlm@0: + rlm@0: (map rlm@0: (fn dt[n] (/ space (* 2 (Math/PI) (r n)))) rlm@0: (range n)))) rlm@0: t (t n) rlm@0: r (r n) rlm@0: ct (Math/cos t) rlm@0: st (Math/sin t) rlm@0: ] rlm@0: (list rlm@0: (Vector3f. rlm@0: (* -1 r st) rlm@0: (/ domino-height 1) rlm@0: (* r ct)) rlm@0: (.fromAngleAxis (Quaternion.) rlm@0: (- (/ 3.1415926 2) t) (Vector3f. 0 1 0)) rlm@0: ))) rlm@0: )) rlm@0: node)) rlm@0: rlm@0: rlm@0: (defn domino-game-run [] rlm@0: (doto rlm@0: (world rlm@0: (doto (Node.) (.attachChild (floor*)) rlm@0: ) rlm@0: {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81)) rlm@0: "key-m" (gravity-toggle (Vector3f. 0 0 9.81)) rlm@0: "key-l" (gravity-toggle (Vector3f. 9.81 0 0)) rlm@0: "key-j" (gravity-toggle (Vector3f. -9.81 0 0)) rlm@0: "key-k" (gravity-toggle (Vector3f. 0 9.81 0) ) rlm@0: "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true)) rlm@0: "key-o" (gravity-toggle (Vector3f. 0 -9.81 0)) rlm@0: rlm@0: "key-space" rlm@0: (fn[game value] rlm@0: rlm@0: (if (not value) rlm@0: (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0) rlm@0: (.fromAngleAxis (Quaternion.) rlm@0: (/ Math/PI 2) (Vector3f. 0 1 0)))] rlm@0: (add-element game d)))) rlm@0: "key-f" rlm@0: (fn[game value](if (not value) (add-element game (domino-cycle)))) rlm@0: "key-return" (fire-cannon-ball)} rlm@0: position-camera rlm@0: (fn [& _])) rlm@0: (.start))) rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :results silent rlm@0: (cortex.other-games/domino-game-run) rlm@0: #+end_src rlm@0: rlm@0: #+caption: floating dominos rlm@0: [[./images/dominos.jpg]] rlm@0: rlm@0: *** Hello Loop rlm@0: #+srcname: hello-loop rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.loop) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: rlm@0: (defn blue-cube [] rlm@0: (box 1 1 1 rlm@0: :color ColorRGBA/Blue rlm@0: :texture false rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :name "blue-cube" rlm@0: :physical? false)) rlm@0: rlm@0: (defn blue-cube-game [] rlm@0: (let [cube (blue-cube) rlm@0: root (doto (Node.) (.attachChild cube))] rlm@0: (world root rlm@0: {} rlm@0: no-op rlm@0: (fn [game tpf] rlm@0: (.rotate cube 0.0 (* 2 tpf) 0.0))))) rlm@0: #+end_src rlm@0: rlm@0: *** Hello Collision rlm@0: rlm@0: #+srcname: hello-collision rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.collision) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: rlm@0: rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]]) rlm@0: rlm@0: rlm@0: (defn environment [] rlm@0: (let rlm@0: [scene-model rlm@0: (doto rlm@0: (.loadModel rlm@0: (doto (asset-manager) rlm@0: (.registerLocator rlm@0: "/home/r/cortex/assets/zips/town.zip" ZipLocator)) rlm@0: "main.scene") rlm@0: (.setLocalScale (float 2.0))) rlm@0: collision-shape rlm@0: (CollisionShapeFactory/createMeshShape #^Node scene-model) rlm@0: landscape (RigidBodyControl. collision-shape 0)] rlm@0: (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive) rlm@0: (.addControl scene-model landscape) rlm@0: scene-model)) rlm@0: rlm@0: (defn player-fn [] rlm@0: (doto rlm@0: (CharacterControl. rlm@0: (CapsuleCollisionShape. (float 1.5) (float 6)(float 1)) rlm@0: (float 0.05)) rlm@0: (.setJumpSpeed 20) rlm@0: (.setFallSpeed 30) rlm@0: (.setGravity 30) ;30 rlm@0: (.setPhysicsLocation (Vector3f. 0 10 0)))) rlm@0: rlm@0: (defn lights [] rlm@0: [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1)))) rlm@0: (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1)))) rlm@0: (doto (DirectionalLight.) rlm@0: (.setColor (.mult ColorRGBA/White (float 0.9) )) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))]) rlm@0: rlm@0: (defn night-lights [] rlm@0: [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3)))) rlm@0: (doto (DirectionalLight.) rlm@0: (.setColor (.mult ColorRGBA/White (float 0.2) )) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))]) rlm@0: rlm@0: (def player (atom (player-fn))) rlm@0: rlm@0: (defn setup-fn [game] rlm@0: (dorun (map #(.addLight (.getRootNode game) %) (lights))) rlm@0: ;; set the color of the sky rlm@0: (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1)) rlm@0: ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1) rlm@0: (doto (.getFlyByCamera game) rlm@0: (.setMoveSpeed (float 100)) rlm@0: (.setRotationSpeed 3)) rlm@0: (.add rlm@0: (.getPhysicsSpace rlm@0: (.getState (.getStateManager game) BulletAppState)) rlm@0: @player) rlm@0: rlm@0: (doto (Node.) (.attachChild (.getRootNode game)) rlm@0: (.attachChild (brick-wall*)) rlm@0: ) rlm@0: rlm@0: ) rlm@0: rlm@0: rlm@0: (def walking-up? (atom false)) rlm@0: (def walking-down? (atom false)) rlm@0: (def walking-left? (atom false)) rlm@0: (def walking-right? (atom false)) rlm@0: rlm@0: (defn set-walk [walk-atom game value] rlm@0: ;;(println-repl "setting stuff to " value) rlm@0: (reset! walk-atom value)) rlm@0: rlm@0: (defn responses [] rlm@0: {"key-w" (partial set-walk walking-up?) rlm@0: "key-d" (partial set-walk walking-right?) rlm@0: "key-s" (partial set-walk walking-down?) rlm@0: "key-a" (partial set-walk walking-left?) rlm@0: "key-return" (fire-cannon-ball) rlm@0: "key-space" (fn [game value] (.jump @player)) rlm@0: }) rlm@0: rlm@0: (defn update-fn rlm@0: [game tpf] rlm@0: (let [camera (.getCamera game) rlm@0: cam-dir (.multLocal rlm@0: (.clone rlm@0: (.getDirection camera)) (float 0.6)) rlm@0: cam-left (.multLocal rlm@0: (.clone rlm@0: (.getLeft camera)) (float 0.4)) rlm@0: walk-direction (Vector3f. 0 0 0)] rlm@0: rlm@0: (cond rlm@0: @walking-up? (.addLocal walk-direction cam-dir) rlm@0: @walking-right? (.addLocal walk-direction (.negate cam-left)) rlm@0: @walking-down? (.addLocal walk-direction (.negate cam-dir)) rlm@0: @walking-left? (.addLocal walk-direction cam-left)) rlm@0: (.setWalkDirection @player walk-direction) rlm@0: (.setLocation camera (.getPhysicsLocation @player)))) rlm@0: rlm@0: (defn run-game [] rlm@0: (.start rlm@0: (world (environment) rlm@0: (responses) rlm@0: setup-fn rlm@0: update-fn))) rlm@0: #+end_src rlm@0: rlm@0: *** Hello Terrain rlm@0: #+srcname: hello-terrain rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.terrain) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (import jme3tools.converters.ImageToAwt) rlm@0: rlm@0: rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]]) rlm@0: rlm@0: rlm@0: (defn setup-fn [type game] rlm@0: (.setMoveSpeed (.getFlyByCamera game) 50) rlm@0: (.setFrustumFar (.getCamera game) 10000) rlm@0: (let [env (environment type) rlm@0: cameras [(.getCamera game)] rlm@0: control (TerrainLodControl. env cameras)] rlm@0: ;;(.addControl env control) rlm@0: (.attachChild (.getRootNode game) env))) rlm@0: rlm@0: (defn environment [type] rlm@0: (let rlm@0: [mat_terrain rlm@0: (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md") rlm@0: grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg") rlm@0: dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg") rlm@0: rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg") rlm@0: heightmap-image (.loadTexture (asset-manager) rlm@0: ({:mountain "Textures/Terrain/splat/mountains512.png" rlm@0: :fortress "Textures/Terrain/splat/fortress512.png" rlm@0: }type)) rlm@0: heightmap (ImageBasedHeightMap. rlm@0: (ImageToAwt/convert (.getImage heightmap-image) false true 0)) rlm@0: terrain (do (.load heightmap) rlm@0: (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap))) rlm@0: ] rlm@0: rlm@0: (dorun (map #(.setWrap % Texture$WrapMode/Repeat) rlm@0: [grass dirt rock])) rlm@0: rlm@0: (doto mat_terrain rlm@0: (.setTexture "Tex1" grass) rlm@0: (.setFloat "Tex1Scale" (float 64)) rlm@0: rlm@0: (.setTexture "Tex2" dirt) rlm@0: (.setFloat "Tex2Scale" (float 32)) rlm@0: rlm@0: (.setTexture "Tex3" rock) rlm@0: (.setFloat "Tex3Scale" (float 128)) rlm@0: rlm@0: (.setTexture "Alpha" rlm@0: (.loadTexture rlm@0: (asset-manager) rlm@0: ({:mountain "Textures/Terrain/splat/alphamap.png" rlm@0: :fortress "Textures/Terrain/splat/alphamap2.png"} type)))) rlm@0: rlm@0: (doto terrain rlm@0: (.setMaterial mat_terrain) rlm@0: (.setLocalTranslation 0 -100 0) rlm@0: (.setLocalScale 2 1 2)))) rlm@0: rlm@0: rlm@0: rlm@0: (defn run-terrain-game [type] rlm@0: (.start rlm@0: (world rlm@0: (Node.) rlm@0: {} rlm@0: (partial setup-fn type) rlm@0: no-op))) rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: #+srcname: hello-animation rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.animation) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (use '[hello [collision :only [lights]]]) rlm@0: rlm@0: (defn stand rlm@0: [channel] rlm@0: (doto channel rlm@0: (.setAnim "stand" (float 0.5)) rlm@0: (.setLoopMode LoopMode/DontLoop) rlm@0: (.setSpeed (float 1)))) rlm@0: rlm@0: (defn anim-listener [] rlm@0: (proxy [AnimEventListener] [] rlm@0: (onAnimChange rlm@0: [control channel animation-name] rlm@0: (println-repl "RLM --- onAnimChange")) rlm@0: (onAnimCycleDone rlm@0: [control channel animation-name] rlm@0: (if (= animation-name "Walk") rlm@0: (stand channel) rlm@0: )))) rlm@0: rlm@0: (defn setup-fn [channel game] rlm@0: (dorun (map #(.addLight (.getRootNode game) %) (lights))) rlm@0: ;; set the color of the sky rlm@0: (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1)) rlm@0: ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1) rlm@0: (.setAnim channel "stand") rlm@0: (doto (.getFlyByCamera game) rlm@0: (.setMoveSpeed (float 10)) rlm@0: (.setRotationSpeed 1))) rlm@0: rlm@0: (defn walk [channel] rlm@0: (println-repl "zzz") rlm@0: (doto channel rlm@0: (.setAnim "Walk" (float 0.5)) rlm@0: (.setLoopMode LoopMode/Loop))) rlm@0: rlm@0: rlm@0: (defn key-map [channel] rlm@0: {"key-space" (fn [game value] rlm@0: (if (not value) rlm@0: (walk channel)))}) rlm@0: rlm@0: (defn player [] rlm@0: (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml") rlm@0: control (.getControl model AnimControl)] rlm@0: (.setLocalScale model (float 0.5)) rlm@0: (.clearListeners control) rlm@0: (.addListener control (anim-control)) rlm@0: model)) rlm@0: rlm@0: rlm@0: rlm@0: (defn run-anim-game [] rlm@0: (let [ninja (player) rlm@0: control (.getControl ninja AnimControl) rlm@0: channel (.createChannel control)] rlm@0: (.start rlm@0: (world rlm@0: ninja rlm@0: (key-map channel) rlm@0: (partial setup-fn channel) rlm@0: no-op)))) rlm@0: #+end_src rlm@0: rlm@0: *** Hello Materials rlm@0: #+srcname: material rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.material) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: rlm@0: (defn simple-cube [] rlm@0: (box 1 1 1 rlm@0: :position (Vector3f. -3 1.1 0) rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Interface/Logo/Monkey.jpg" rlm@0: :physical? false)) rlm@0: rlm@0: (defn leaky-box [] rlm@0: (box 1 1 1 rlm@0: :position (Vector3f. 3 -1 0) rlm@0: :material "Common/MatDefs/Misc/ColoredTextured.j3md" rlm@0: :texture "Textures/ColoredTex/Monkey.png" rlm@0: :color (ColorRGBA. 1 0 1 1) rlm@0: :physical? false)) rlm@0: rlm@0: (defn transparent-box [] rlm@0: (doto rlm@0: (box 1 1 0.1 rlm@0: :position Vector3f/ZERO rlm@0: :name "window frame" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/ColoredTex/Monkey.png" rlm@0: :physical? false) rlm@0: (-> (.getMaterial) rlm@0: (.getAdditionalRenderState) rlm@0: (.setBlendMode RenderState$BlendMode/Alpha)) rlm@0: (.setQueueBucket RenderQueue$Bucket/Transparent))) rlm@0: rlm@0: (defn bumpy-sphere [] rlm@0: (doto rlm@0: (sphere 2 rlm@0: :position (Vector3f. 0 2 -2) rlm@0: :name "Shiny rock" rlm@0: :material "Common/MatDefs/Light/Lighting.j3md" rlm@0: :texture false rlm@0: :physical? false) rlm@0: (-> (.getMesh) rlm@0: (doto rlm@0: (.setTextureMode Sphere$TextureMode/Projected) rlm@0: (TangentBinormalGenerator/generate))) rlm@0: (-> (.getMaterial) rlm@0: (doto rlm@0: (.setTexture "DiffuseMap" (.loadTexture (asset-manager) rlm@0: "Textures/Terrain/Pond/Pond.png")) rlm@0: (.setTexture "NormalMap" (.loadTexture (asset-manager) rlm@0: "Textures/Terrain/Pond/Pond_normal.png")) rlm@0: (.setFloat "Shininess" (float 5)))) rlm@0: (.rotate (float 1.6) 0 0))) rlm@0: rlm@0: rlm@0: (defn start-game [] rlm@0: (.start rlm@0: (world rlm@0: (let [root (Node.)] rlm@0: (dorun (map #(.attachChild root %) rlm@0: [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)])) rlm@0: root) rlm@0: {} rlm@0: (fn [world] rlm@0: (let [sun (doto (DirectionalLight.) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 1 0 -2))) rlm@0: (.setColor ColorRGBA/White))] rlm@0: (.addLight (.getRootNode world) sun))) rlm@0: no-op rlm@0: ))) rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: * The Body rlm@0: ** Eyes rlm@0: rlm@0: Ultimately I want to make creatures with eyes. Each eye can be rlm@0: independely moved and should see its own version of the world rlm@0: depending on where it is. rlm@0: #+srcname: eyes rlm@0: #+begin_src clojure rlm@0: (ns body.eye) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (import java.nio.ByteBuffer) rlm@0: (import java.awt.image.BufferedImage) rlm@0: (import java.awt.Color) rlm@0: (import java.awt.Dimension) rlm@0: (import java.awt.Graphics) rlm@0: (import java.awt.Graphics2D) rlm@0: (import java.awt.event.WindowAdapter) rlm@0: (import java.awt.event.WindowEvent) rlm@0: (import java.awt.image.BufferedImage) rlm@0: (import java.nio.ByteBuffer) rlm@0: (import javax.swing.JFrame) rlm@0: (import javax.swing.JPanel) rlm@0: (import javax.swing.SwingUtilities) rlm@0: (import javax.swing.ImageIcon) rlm@0: (import javax.swing.JOptionPane) rlm@0: (import java.awt.image.ImageObserver) rlm@0: rlm@0: rlm@0: rlm@0: (defn scene-processor rlm@0: "deals with converting FrameBuffers to BufferedImages so rlm@0: that the continuation function can be defined only in terms rlm@0: of what it does with BufferedImages" rlm@0: [continuation] rlm@0: (let [byte-buffer (atom nil) rlm@0: renderer (atom nil) rlm@0: image (atom nil)] rlm@0: (proxy [SceneProcessor] [] rlm@0: (initialize rlm@0: [renderManager viewPort] rlm@0: (let [cam (.getCamera viewPort) rlm@0: width (.getWidth cam) rlm@0: height (.getHeight cam)] rlm@0: (reset! renderer (.getRenderer renderManager)) rlm@0: (reset! byte-buffer rlm@0: (BufferUtils/createByteBuffer rlm@0: (* width height 4))) rlm@0: (reset! image (BufferedImage. width height rlm@0: BufferedImage/TYPE_4BYTE_ABGR)))) rlm@0: (isInitialized [] (not (nil? @byte-buffer))) rlm@0: (reshape [_ _ _]) rlm@0: (preFrame [_]) rlm@0: (postQueue [_]) rlm@0: (postFrame rlm@0: [#^FrameBuffer fb] rlm@0: (.clear @byte-buffer) rlm@0: (.readFrameBuffer @renderer fb @byte-buffer) rlm@0: (Screenshots/convertScreenShot @byte-buffer @image) rlm@0: (continuation @image)) rlm@0: (cleanup [])))) rlm@0: rlm@0: (defn add-eye rlm@0: "Add an eye to the world, and call continuation on rlm@0: every frame produced" rlm@0: [world camera continuation] rlm@0: (let [width (.getWidth camera) rlm@0: height (.getHeight camera) rlm@0: render-manager (.getRenderManager world) rlm@0: viewport (.createMainView render-manager "eye-view" camera)] rlm@0: (doto viewport rlm@0: (.setBackgroundColor ColorRGBA/Black) rlm@0: (.setClearFlags true true true) rlm@0: (.addProcessor (scene-processor continuation)) rlm@0: (.attachScene (.getRootNode world))))) rlm@0: rlm@0: (defn make-display-frame [display width height] rlm@0: (SwingUtilities/invokeLater rlm@0: (fn [] rlm@0: (.setPreferredSize display (Dimension. width height)) rlm@0: (doto (JFrame. "Eye Camera!") rlm@0: (-> (.getContentPane) (.add display)) rlm@0: (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE) rlm@0: (.pack) rlm@0: (.setLocationRelativeTo nil) rlm@0: (.setResizable false) rlm@0: (.setVisible true))))) rlm@0: rlm@0: (defn image-monitor [#^BufferedImage image] rlm@0: (proxy [JPanel] [] rlm@0: (paintComponent rlm@0: [g] rlm@0: (proxy-super paintComponent g) rlm@0: (locking image rlm@0: (.drawImage g image 0 0 rlm@0: (proxy [ImageObserver] rlm@0: [] rlm@0: (imageUpdate rlm@0: [] rlm@0: (proxy-super imageUpdate)))))))) rlm@0: rlm@0: (defn movie-image [] rlm@0: (let [setup rlm@0: (runonce rlm@0: (fn [#^BufferedImage image] rlm@0: (let [width (.getWidth image) rlm@0: height (.getHeight image) rlm@0: display (image-monitor image) rlm@0: frame (make-display-frame display width height)] rlm@0: display)))] rlm@0: (fn [#^BufferedImage image] rlm@0: (.repaint (setup image))))) rlm@0: rlm@0: rlm@0: (defn observer rlm@0: "place thy eye!" rlm@0: [world camera] rlm@0: (let [eye camera rlm@0: width (.getWidth eye) rlm@0: height (.getHeight eye)] rlm@0: (no-exceptions rlm@0: (add-eye rlm@0: world rlm@0: eye rlm@0: (movie-image))))) rlm@0: #+end_src rlm@0: rlm@0: #+srcname: test-vision rlm@0: #+begin_src clojure rlm@0: rlm@0: (ns test.vision) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (use 'body.eye) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (import java.nio.ByteBuffer) rlm@0: (import java.awt.image.BufferedImage) rlm@0: (import java.awt.Color) rlm@0: (import java.awt.Dimension) rlm@0: (import java.awt.Graphics) rlm@0: (import java.awt.Graphics2D) rlm@0: (import java.awt.event.WindowAdapter) rlm@0: (import java.awt.event.WindowEvent) rlm@0: (import java.awt.image.BufferedImage) rlm@0: (import java.nio.ByteBuffer) rlm@0: (import javax.swing.JFrame) rlm@0: (import javax.swing.JPanel) rlm@0: (import javax.swing.SwingUtilities) rlm@0: (import javax.swing.ImageIcon) rlm@0: (import javax.swing.JOptionPane) rlm@0: (import java.awt.image.ImageObserver) rlm@0: rlm@0: rlm@0: (def width 200) rlm@0: (def height 200) rlm@0: rlm@0: (defn camera [] rlm@0: (doto (Camera. width height) rlm@0: (.setFrustumPerspective 45 1 1 1000) rlm@0: (.setLocation (Vector3f. -3 0 -5)) rlm@0: (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))) rlm@0: rlm@0: (defn camera2 [] rlm@0: (doto (Camera. width height) rlm@0: (.setFrustumPerspective 45 1 1 1000) rlm@0: (.setLocation (Vector3f. 3 0 -5)) rlm@0: (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))) rlm@0: rlm@0: (defn setup-fn [world] rlm@0: (let [eye (camera) rlm@0: width (.getWidth eye) rlm@0: height (.getHeight eye)] rlm@0: (no-exceptions rlm@0: (add-eye rlm@0: world rlm@0: eye rlm@0: (runonce visual)) rlm@0: (add-eye rlm@0: world rlm@0: (camera2) rlm@0: (runonce visual))))) rlm@0: rlm@0: (defn spider-eye [position] rlm@0: (doto (Camera. 200 200 ) rlm@0: (.setFrustumPerspective 45 1 1 1000) rlm@0: (.setLocation position) rlm@0: (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))) rlm@0: rlm@0: (defn setup-fn* [world] rlm@0: (let [eye (camera) rlm@0: width (.getWidth eye) rlm@0: height (.getHeight eye)] rlm@0: ;;(.setClearFlags (.getViewPort world) true true true) rlm@0: (observer world (.getCamera world)) rlm@0: (observer world (spider-eye (Vector3f. 3 0 -5))) rlm@0: ;;(observer world (spider-eye (Vector3f. 0 0 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. -3 0 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. 0 3 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. 0 -3 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. 3 3 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. -3 3 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. 3 -3 -5))) rlm@0: ;; (observer world (spider-eye (Vector3f. -3 -3 -5))) rlm@0: rlm@0: ) rlm@0: world) rlm@0: rlm@0: (defn test-world [] rlm@0: (let [thing (box 1 1 1 :physical? false)] rlm@0: (world rlm@0: (doto (Node.) rlm@0: (.attachChild thing)) rlm@0: {} rlm@0: setup-fn rlm@0: (fn [world tpf] rlm@0: (.rotate thing (* tpf 0.2) 0 0) rlm@0: )))) rlm@0: rlm@0: rlm@0: #+end_src rlm@0: rlm@0: rlm@0: #+results: eyes rlm@0: : #'body.eye/test-world rlm@0: rlm@0: Note the use of continuation passing style for connecting the eye to a rlm@0: function to process the output. The example code will create two rlm@0: videos of the same rotating cube from different angles, sutiable for rlm@0: stereoscopic vision. rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: * COMMENT code generation rlm@0: #+begin_src clojure :tangle ../src/cortex/import.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/brick_wall.clj rlm@0: <> rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/cortex/world.clj rlm@0: <> rlm@0: <> rlm@0: <> rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/cortex/other_games.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/loop.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/collision.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/terrain.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/animation.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/material.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/body/eye.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/test/vision.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: rlm@0: