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@4: rlm@0: rlm@0: * Background rlm@0: Artificial Intelligence has tried and failed for more than half a rlm@0: century to produce programs as flexible, creative, and “intelligent” rlm@0: as the human mind itself. Clearly, we are still missing some important rlm@0: ideas concerning intelligent programs or we would have strong AI rlm@0: already. What idea could be missing? rlm@0: rlm@0: When Turing first proposed his famous “Turing Test” in the rlm@0: groundbreaking paper [[./sources/turing.pdf][/Computing Machines and Intelligence/]], he gave rlm@0: little importance to how a computer program might interact with the rlm@0: world: rlm@0: rlm@0: #+BEGIN_QUOTE rlm@0: \ldquo{}We need not be too concerned about the legs, eyes, etc. The example of rlm@0: Miss Helen Keller shows that education can take place provided that rlm@0: communication in both directions between teacher and pupil can take rlm@0: place by some means or other.\rdquo{} rlm@0: #+END_QUOTE rlm@0: rlm@0: And from the example of Hellen Keller he went on to assume that the rlm@0: only thing a fledgling AI program could need by way of communication rlm@0: is a teletypewriter. But Hellen Keller did possess vision and hearing rlm@0: for the first few months of her life, and her tactile sense was far rlm@0: more rich than any text-stream could hope to achieve. She possessed a rlm@0: body she could move freely, and had continual access to the real world rlm@0: to learn from her actions. rlm@0: rlm@0: I believe that our programs are suffering from too little sensory rlm@0: input to become really intelligent. Imagine for a moment that you rlm@0: lived in a world completely cut off form all sensory stimulation. You rlm@0: have no eyes to see, no ears to hear, no mouth to speak. No body, no rlm@0: taste, no feeling whatsoever. The only sense you get at all is a rlm@0: single point of light, flickering on and off in the void. If this was rlm@0: your life from birth, you would never learn anything, and could never rlm@0: become intelligent. Actual humans placed in sensory deprivation rlm@0: chambers experience hallucinations and can begin to loose their sense rlm@0: of reality in as little as 15 minutes[sensory-deprivation]. Most of rlm@0: the time, the programs we write are in exactly this situation. They do rlm@0: not interface with cameras and microphones, and they do not control a rlm@0: real or simulated body or interact with any sort of world. rlm@0: rlm@0: rlm@0: * Simulation vs. Reality rlm@0: I want demonstrate that multiple senses are what enable rlm@0: intelligence. There are two ways of playing around with senses and rlm@0: computer programs: rlm@0: rlm@0: The first is to go entirely with simulation: virtual world, virtual rlm@0: character, virtual senses. The advantages are that when everything is rlm@0: a simulation, experiments in that simulation are absolutely rlm@0: reproducible. It's also easier to change the character and world to rlm@0: explore new situations and different sensory combinations. rlm@0: rlm@0: rlm@0: ** Issues with Simulation rlm@0: rlm@0: If the world is to be simulated on a computer, then not only do you rlm@0: have to worry about whether the character's senses are rich enough to rlm@0: learn from the world, but whether the world itself is rendered with rlm@0: enough detail and realism to give enough working material to the rlm@0: character's senses. To name just a few difficulties facing modern rlm@0: physics simulators: destructibility of the environment, simulation of rlm@0: water/other fluids, large areas, nonrigid bodies, lots of objects, rlm@0: smoke. I don't know of any computer simulation that would allow a rlm@0: character to take a rock and grind it into fine dust, then use that rlm@0: dust to make a clay sculpture, at least not without spending years rlm@0: calculating the interactions of every single small grain of rlm@0: dust. Maybe a simulated world with today's limitations doesn't provide rlm@0: enough richness for real intelligence to evolve. rlm@0: rlm@0: ** Issues with Reality rlm@0: rlm@0: The other approach for playing with senses is to hook your software up rlm@0: to real cameras, microphones, robots, etc., and let it loose in the rlm@0: real world. This has the advantage of eliminating concerns about rlm@0: simulating the world at the expense of increasing the complexity of rlm@0: implementing the senses. Instead of just grabbing the current rendered rlm@0: frame for processing, you have to use an actual camera with real rlm@0: lenses and interact with photons to get an image. It is much harder to rlm@0: change the character, which is now partly a physical robot of some rlm@0: sort, since doing so involves changing things around in the real world rlm@0: instead of modifying lines of code. While the real world is very rich rlm@0: and definitely provides enough stimulation for intelligence to develop rlm@0: as evidenced by our own existence, it is also uncontrollable in the rlm@0: sense that a particular situation cannot be recreated perfectly or rlm@0: saved for later use. It is harder to conduct science because it is rlm@0: harder to repeat an experiment. The worst thing about using the real rlm@0: world instead of a simulation is the matter of time. Instead of rlm@0: simulated time you get the constant and unstoppable flow of real rlm@0: time. This severely limits the sorts of software you can use to rlm@0: program the AI because all sense inputs must be handled in real rlm@0: time. Complicated ideas may have to be implemented in hardware or may rlm@0: simply be impossible given the current speed of our rlm@0: processors. Contrast this with a simulation, in which the flow of time rlm@0: in the simulated world can be slowed down to accommodate the rlm@0: limitations of the character's programming. In terms of cost, doing rlm@0: everything in software is far cheaper than building custom real-time rlm@0: hardware. All you need is a laptop and some patience. rlm@0: rlm@0: * Choose a Simulation Engine rlm@0: rlm@0: Mainly because of issues with controlling the flow of time, I chose to rlm@0: simulate both the world and the character. I set out to make a minimal rlm@0: world in which I could embed a character with multiple senses. My main rlm@0: goal is to make an environment where I can perform further experiments rlm@0: in simulated senses. rlm@0: rlm@0: As Carl Sagan once said, "If you wish to make an apple pie from rlm@0: scratch, you must first invent the universe.” I examined many rlm@0: different 3D environments to try and find something I would use as the rlm@0: base for my simulation; eventually the choice came down to three rlm@0: engines: the Quake II engine, the Source Engine, and jMonkeyEngine. rlm@0: rlm@0: ** Quake II/Jake2 rlm@0: rlm@0: I spent a bit more than a month working with the Quake II Engine from rlm@0: ID software to see if I could use it for my purposes. All the source rlm@0: code was released by ID software into the Public Domain several years rlm@0: ago, and as a result it has been ported and modified for many rlm@0: different reasons. This engine was famous for its advanced use of rlm@0: realistic shading and had decent and fast physics rlm@0: simulation. Researchers at Princeton [[http://www.nature.com/nature/journal/v461/n7266/pdf/nature08499.pdf][used this code]] to study spatial rlm@0: information encoding in the hippocampal cells of rats. Those rlm@0: researchers created a special Quake II level that simulated a maze, rlm@0: and added an interface where a mouse could run around inside a ball in rlm@0: various directions to move the character in the simulated maze. They rlm@0: measured hippocampal activity during this exercise to try and tease rlm@0: out the method in which spatial data was stored in that area of the rlm@0: brain. I find this promising because if a real living rat can interact rlm@0: with a computer simulation of a maze in the same way as it interacts rlm@0: with a real-world maze, then maybe that simulation is close enough to rlm@0: reality that a simulated sense of vision and motor control interacting rlm@0: with that simulation could reveal useful information about the real rlm@0: thing. It happens that there is a Java port of the original C source rlm@0: code called Jake2. The port demonstrates Java's OpenGL bindings and rlm@0: runs anywhere from 90% to 105% as fast as the C version. After rlm@0: reviewing much of the source of Jake2, I eventually rejected it rlm@0: because the engine is too tied to the concept of a first-person rlm@0: shooter game. One of the problems I had was that there does not seem rlm@0: to be any easy way to attach multiple cameras to a single rlm@0: character. There are also several physics clipping issues that are rlm@0: corrected in a way that only applies to the main character and does rlm@0: not apply to arbitrary objects. While there is a large community of rlm@0: level modders, I couldn't find a community to support using the engine rlm@0: to make new things. rlm@0: rlm@0: ** Source Engine rlm@0: rlm@0: The Source Engine evolved from the Quake II and Quake I engines and is rlm@0: used by Valve in the Half-Life series of games. The physics simulation rlm@0: in the Source Engine is quite accurate and probably the best out of rlm@0: all the engines I investigated. There is also an extensive community rlm@0: actively working with the engine. However, applications that use the rlm@0: Source Engine must be written in C++, the code is not open, it only rlm@0: runs on Windows, and the tools that come with the SDK to handle models rlm@0: and textures are complicated and awkward to use. rlm@0: rlm@0: ** jMonkeyEngine rlm@0: rlm@0: jMonkeyEngine is a new library for creating games in Java. It uses rlm@0: OpenGL to render to the screen and uses screengraphs to avoid drawing rlm@0: things that do not appear on the screen. It has an active community rlm@0: and several games in the pipeline. The engine was not built to serve rlm@0: any particular game but is instead meant to be used for any 3D rlm@0: game. After experimenting with each of these three engines and a few rlm@0: others for about 2 months I settled on jMonkeyEngine. I chose it rlm@0: because it had the most features out of all the open projects I looked rlm@0: at, and because I could then write my code in Clojure, an rlm@0: implementation of LISP that runs on the JVM. rlm@0: rlm@0: * Setup rlm@0: rlm@0: First, I checked out the source to jMonkeyEngine: rlm@0: rlm@0: #+srcname: checkout rlm@0: #+begin_src sh :results verbatim rlm@0: svn checkout http://jmonkeyengine.googlecode.com/svn/trunk/engine jme3 rlm@0: #+end_src rlm@0: rlm@0: #+results: checkout rlm@0: : Checked out revision 7975. rlm@0: rlm@0: rlm@0: Building jMonkeyEngine is easy enough: rlm@0: rlm@0: #+srcname: build rlm@0: #+begin_src sh :results verbatim rlm@0: cd jme3 rlm@0: ant jar | tail -n 2 rlm@0: #+end_src rlm@0: rlm@0: #+results: build rlm@0: : BUILD SUCCESSFUL rlm@0: : Total time: 15 seconds rlm@0: rlm@0: rlm@0: Also build the javadoc: rlm@0: rlm@0: #+srcname: javadoc rlm@0: #+begin_src sh :results verbatim rlm@0: cd jme3 rlm@0: ant javadoc | tail -n 2 rlm@0: #+end_src rlm@0: rlm@0: #+results: javadoc rlm@0: : BUILD SUCCESSFUL rlm@0: : Total time: 12 seconds rlm@0: rlm@0: Now, move the jars from the compilation into the project's lib folder. rlm@0: rlm@0: #+srcname: move-jars rlm@0: #+begin_src sh :results verbatim rlm@0: mkdir -p lib rlm@0: mkdir -p src rlm@0: cp jme3/dist/jMonkeyEngine3.jar lib/ rlm@0: cp jme3/dist/lib/* lib/ rlm@0: ls lib rlm@0: #+end_src rlm@0: rlm@0: #+results: move-jars rlm@0: #+begin_example rlm@0: eventbus-1.4.jar rlm@0: jbullet.jar rlm@0: jheora-jst-debug-0.6.0.jar rlm@0: jinput.jar rlm@0: jME3-jbullet.jar rlm@0: jME3-lwjgl-natives.jar rlm@0: jME3-testdata.jar rlm@0: jME3-test.jar rlm@0: jMonkeyEngine3.jar rlm@0: j-ogg-oggd.jar rlm@0: j-ogg-vorbisd.jar rlm@0: lwjgl.jar rlm@0: nifty-1.3.jar rlm@0: nifty-default-controls-1.3.jar rlm@0: nifty-examples-1.3.jar rlm@0: nifty-lwjgl-renderer-1.3.jar rlm@0: nifty-openal-soundsystem-1.0.jar rlm@0: nifty-style-black-1.3.jar rlm@0: nifty-style-grey-1.0.jar rlm@0: noise-0.0.1-SNAPSHOT.jar rlm@0: stack-alloc.jar rlm@0: vecmath.jar rlm@0: xmlpull-xpp3-1.1.4c.jar rlm@0: #+end_example rlm@0: rlm@0: It's good to create a =assets= directory in the style that the rlm@0: =AssetManager= will like. rlm@0: rlm@0: #+srcname: create-assets rlm@0: #+begin_src sh :results verbatim rlm@0: mkdir -p assets rlm@0: mkdir -p assets/Interface rlm@0: mkdir -p assets/Materials rlm@0: mkdir -p assets/MatDefs rlm@0: mkdir -p assets/Models rlm@0: mkdir -p assets/Scenes rlm@0: mkdir -p assets/Shaders rlm@0: mkdir -p assets/Sounds rlm@0: mkdir -p assets/Textures rlm@0: tree -L 1 assets rlm@0: #+end_src rlm@0: rlm@0: #+results: create-assets rlm@0: #+begin_example rlm@0: assets rlm@0: |-- Interface rlm@0: |-- MatDefs rlm@0: |-- Materials rlm@0: |-- Models rlm@0: |-- Scenes rlm@0: |-- Shaders rlm@0: |-- Sounds rlm@0: `-- Textures rlm@0: rlm@0: 8 directories, 0 files rlm@0: #+end_example rlm@0: rlm@0: rlm@0: The java classpath should have all the jars contained in the =lib= rlm@0: directory as well as the src directory. rlm@0: rlm@0: For example, here is the file I use to run my REPL for clojure. rlm@0: rlm@0: #+include: "~/swank-all" src sh :exports code rlm@0: rlm@0: The important thing here is that =cortex/lib/*=, =cortex/src=, and rlm@0: =cortex/assets= appear on the classpath. (=cortex= is the base rlm@0: directory of this project.) rlm@0: rlm@0: #+srcname: pwd rlm@0: #+begin_src sh rlm@0: pwd rlm@0: #+end_src rlm@0: rlm@0: #+results: pwd rlm@0: : /home/r/cortex rlm@0: rlm@0: rlm@0: * Simulation Base rlm@0: rlm@0: ** Imports rlm@0: First, I'll import jme core classes. 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@0: (defn import-jme3 [] rlm@0: (import '[com.jme3.system AppSettings JmeSystem]) rlm@0: (import '[com.jme3.app Application SimpleApplication]) rlm@0: (import 'com.jme3.material.Material) rlm@0: (import '[com.jme3.math Vector3f ColorRGBA Quaternion Transform]) rlm@0: (import '[com.jme3.scene Node Geometry]) rlm@0: (import '[com.jme3.scene.shape Box Sphere Sphere$TextureMode]) rlm@0: (import 'com.jme3.font.BitmapText) rlm@0: (import '[com.jme3.input KeyInput InputManager]) rlm@0: (import '[com.jme3.input.controls rlm@0: ActionListener AnalogListener KeyTrigger MouseButtonTrigger]) rlm@0: (import '[com.jme3.asset AssetManager DesktopAssetManager] ) rlm@0: (import '[com.jme3.asset.plugins HttpZipLocator ZipLocator]) rlm@0: (import '[com.jme3.light PointLight DirectionalLight]) rlm@0: (import '[com.jme3.animation AnimControl Skeleton Bone]) rlm@0: (import '[com.jme3.bullet.collision.shapes rlm@0: MeshCollisionShape SphereCollisionShape BoxCollisionShape]) rlm@0: (import 'com.jme3.renderer.queue.RenderQueue$ShadowMode) rlm@0: (import 'jme3test.TestChooser) rlm@0: (import '[com.jme3.bullet PhysicsTickListener PhysicsSpace]) rlm@0: (import '[com.jme3.bullet.joints SixDofJoint HingeJoint rlm@0: SliderJoint Point2PointJoint ConeJoint])) rlm@0: rlm@0: rlm@0: (defmacro permissive-import* [class-symbol] rlm@0: `(try rlm@0: (import ~class-symbol) rlm@0: (catch Exception e# rlm@0: (println "can't import " ~class-symbol)))) rlm@0: rlm@0: (defn permissive-import [class-symbol] rlm@0: (eval (list 'cortex.import/permissive-import* class-symbol))) rlm@0: rlm@0: (defn selection-import [selection-fn] rlm@0: (dorun rlm@0: (map (comp permissive-import symbol) rlm@0: (filter selection-fn rlm@0: (map :name rlm@0: swank.util.class-browse/available-classes))))) rlm@0: rlm@0: (defn mega-import-jme3 rlm@0: "ALL the jme classes. For REPL use." rlm@0: [] rlm@0: (selection-import rlm@0: #(and rlm@0: (.startsWith % "com.jme3.") rlm@0: ;; Don't import the Lwjgl stuff since it can throw exceptions rlm@0: ;; upon being loaded. rlm@0: (not (re-matches #".*Lwjgl.*" %))))) rlm@0: #+end_src rlm@0: rlm@0: The =mega-import-jme3= is quite usefull for debugging purposes since rlm@0: it allows completion for almost all of JME's classes 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@4: (.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@0: (let [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@0: (.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@0: (let [impact-shape (doto (GImpactCollisionShape. (.getMesh geom)) (.setMargin 0)) rlm@0: physics-control (RigidBodyControl. rlm@0: impact-shape 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@0: (defn add-element [game node] rlm@0: (.addAll rlm@0: (.getPhysicsSpace rlm@0: (.getState rlm@0: (.getStateManager game) rlm@0: BulletAppState)) rlm@0: node) rlm@0: (.attachChild (.getRootNode game) node)) 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@0: (cortex.import/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@0: (defn fire-cannon-ball [] rlm@0: (fn [game value] rlm@0: (if (not value) rlm@0: (let [camera (.getCamera game) rlm@0: cannon-ball rlm@0: (sphere 0.7 rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/PokeCopper.jpg" rlm@0: :position rlm@0: (.add (.getLocation camera) rlm@0: (.mult (.getDirection camera) (float 1))) rlm@0: :mass 3)] ;200 0.05 rlm@0: (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive) rlm@0: (.setLinearVelocity rlm@0: (.getControl cannon-ball RigidBodyControl) rlm@0: (.mult (.getDirection camera) (float 50))) ;50 rlm@0: (add-element game cannon-ball))))) 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: