annotate org/cortex.org @ 4:50c92af2018e

fixed headers, make compat symlink
author Robert McIntyre <rlm@mit.edu>
date Thu, 20 Oct 2011 15:10:38 -0700
parents 92f8d83b5d0b
children e3c6d1c1cb00
rev   line source
rlm@0 1 #+title: Simulated Senses
rlm@0 2 #+author: Robert McIntyre
rlm@0 3 #+email: rlm@mit.edu
rlm@0 4 #+description: Simulating senses for AI research using JMonkeyEngine3
rlm@4 5 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@4 6 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@4 7
rlm@0 8
rlm@0 9 * Background
rlm@0 10 Artificial Intelligence has tried and failed for more than half a
rlm@0 11 century to produce programs as flexible, creative, and “intelligent”
rlm@0 12 as the human mind itself. Clearly, we are still missing some important
rlm@0 13 ideas concerning intelligent programs or we would have strong AI
rlm@0 14 already. What idea could be missing?
rlm@0 15
rlm@0 16 When Turing first proposed his famous “Turing Test” in the
rlm@0 17 groundbreaking paper [[./sources/turing.pdf][/Computing Machines and Intelligence/]], he gave
rlm@0 18 little importance to how a computer program might interact with the
rlm@0 19 world:
rlm@0 20
rlm@0 21 #+BEGIN_QUOTE
rlm@0 22 \ldquo{}We need not be too concerned about the legs, eyes, etc. The example of
rlm@0 23 Miss Helen Keller shows that education can take place provided that
rlm@0 24 communication in both directions between teacher and pupil can take
rlm@0 25 place by some means or other.\rdquo{}
rlm@0 26 #+END_QUOTE
rlm@0 27
rlm@0 28 And from the example of Hellen Keller he went on to assume that the
rlm@0 29 only thing a fledgling AI program could need by way of communication
rlm@0 30 is a teletypewriter. But Hellen Keller did possess vision and hearing
rlm@0 31 for the first few months of her life, and her tactile sense was far
rlm@0 32 more rich than any text-stream could hope to achieve. She possessed a
rlm@0 33 body she could move freely, and had continual access to the real world
rlm@0 34 to learn from her actions.
rlm@0 35
rlm@0 36 I believe that our programs are suffering from too little sensory
rlm@0 37 input to become really intelligent. Imagine for a moment that you
rlm@0 38 lived in a world completely cut off form all sensory stimulation. You
rlm@0 39 have no eyes to see, no ears to hear, no mouth to speak. No body, no
rlm@0 40 taste, no feeling whatsoever. The only sense you get at all is a
rlm@0 41 single point of light, flickering on and off in the void. If this was
rlm@0 42 your life from birth, you would never learn anything, and could never
rlm@0 43 become intelligent. Actual humans placed in sensory deprivation
rlm@0 44 chambers experience hallucinations and can begin to loose their sense
rlm@0 45 of reality in as little as 15 minutes[sensory-deprivation]. Most of
rlm@0 46 the time, the programs we write are in exactly this situation. They do
rlm@0 47 not interface with cameras and microphones, and they do not control a
rlm@0 48 real or simulated body or interact with any sort of world.
rlm@0 49
rlm@0 50
rlm@0 51 * Simulation vs. Reality
rlm@0 52 I want demonstrate that multiple senses are what enable
rlm@0 53 intelligence. There are two ways of playing around with senses and
rlm@0 54 computer programs:
rlm@0 55
rlm@0 56 The first is to go entirely with simulation: virtual world, virtual
rlm@0 57 character, virtual senses. The advantages are that when everything is
rlm@0 58 a simulation, experiments in that simulation are absolutely
rlm@0 59 reproducible. It's also easier to change the character and world to
rlm@0 60 explore new situations and different sensory combinations.
rlm@0 61
rlm@0 62
rlm@0 63 ** Issues with Simulation
rlm@0 64
rlm@0 65 If the world is to be simulated on a computer, then not only do you
rlm@0 66 have to worry about whether the character's senses are rich enough to
rlm@0 67 learn from the world, but whether the world itself is rendered with
rlm@0 68 enough detail and realism to give enough working material to the
rlm@0 69 character's senses. To name just a few difficulties facing modern
rlm@0 70 physics simulators: destructibility of the environment, simulation of
rlm@0 71 water/other fluids, large areas, nonrigid bodies, lots of objects,
rlm@0 72 smoke. I don't know of any computer simulation that would allow a
rlm@0 73 character to take a rock and grind it into fine dust, then use that
rlm@0 74 dust to make a clay sculpture, at least not without spending years
rlm@0 75 calculating the interactions of every single small grain of
rlm@0 76 dust. Maybe a simulated world with today's limitations doesn't provide
rlm@0 77 enough richness for real intelligence to evolve.
rlm@0 78
rlm@0 79 ** Issues with Reality
rlm@0 80
rlm@0 81 The other approach for playing with senses is to hook your software up
rlm@0 82 to real cameras, microphones, robots, etc., and let it loose in the
rlm@0 83 real world. This has the advantage of eliminating concerns about
rlm@0 84 simulating the world at the expense of increasing the complexity of
rlm@0 85 implementing the senses. Instead of just grabbing the current rendered
rlm@0 86 frame for processing, you have to use an actual camera with real
rlm@0 87 lenses and interact with photons to get an image. It is much harder to
rlm@0 88 change the character, which is now partly a physical robot of some
rlm@0 89 sort, since doing so involves changing things around in the real world
rlm@0 90 instead of modifying lines of code. While the real world is very rich
rlm@0 91 and definitely provides enough stimulation for intelligence to develop
rlm@0 92 as evidenced by our own existence, it is also uncontrollable in the
rlm@0 93 sense that a particular situation cannot be recreated perfectly or
rlm@0 94 saved for later use. It is harder to conduct science because it is
rlm@0 95 harder to repeat an experiment. The worst thing about using the real
rlm@0 96 world instead of a simulation is the matter of time. Instead of
rlm@0 97 simulated time you get the constant and unstoppable flow of real
rlm@0 98 time. This severely limits the sorts of software you can use to
rlm@0 99 program the AI because all sense inputs must be handled in real
rlm@0 100 time. Complicated ideas may have to be implemented in hardware or may
rlm@0 101 simply be impossible given the current speed of our
rlm@0 102 processors. Contrast this with a simulation, in which the flow of time
rlm@0 103 in the simulated world can be slowed down to accommodate the
rlm@0 104 limitations of the character's programming. In terms of cost, doing
rlm@0 105 everything in software is far cheaper than building custom real-time
rlm@0 106 hardware. All you need is a laptop and some patience.
rlm@0 107
rlm@0 108 * Choose a Simulation Engine
rlm@0 109
rlm@0 110 Mainly because of issues with controlling the flow of time, I chose to
rlm@0 111 simulate both the world and the character. I set out to make a minimal
rlm@0 112 world in which I could embed a character with multiple senses. My main
rlm@0 113 goal is to make an environment where I can perform further experiments
rlm@0 114 in simulated senses.
rlm@0 115
rlm@0 116 As Carl Sagan once said, "If you wish to make an apple pie from
rlm@0 117 scratch, you must first invent the universe.” I examined many
rlm@0 118 different 3D environments to try and find something I would use as the
rlm@0 119 base for my simulation; eventually the choice came down to three
rlm@0 120 engines: the Quake II engine, the Source Engine, and jMonkeyEngine.
rlm@0 121
rlm@0 122 ** Quake II/Jake2
rlm@0 123
rlm@0 124 I spent a bit more than a month working with the Quake II Engine from
rlm@0 125 ID software to see if I could use it for my purposes. All the source
rlm@0 126 code was released by ID software into the Public Domain several years
rlm@0 127 ago, and as a result it has been ported and modified for many
rlm@0 128 different reasons. This engine was famous for its advanced use of
rlm@0 129 realistic shading and had decent and fast physics
rlm@0 130 simulation. Researchers at Princeton [[http://www.nature.com/nature/journal/v461/n7266/pdf/nature08499.pdf][used this code]] to study spatial
rlm@0 131 information encoding in the hippocampal cells of rats. Those
rlm@0 132 researchers created a special Quake II level that simulated a maze,
rlm@0 133 and added an interface where a mouse could run around inside a ball in
rlm@0 134 various directions to move the character in the simulated maze. They
rlm@0 135 measured hippocampal activity during this exercise to try and tease
rlm@0 136 out the method in which spatial data was stored in that area of the
rlm@0 137 brain. I find this promising because if a real living rat can interact
rlm@0 138 with a computer simulation of a maze in the same way as it interacts
rlm@0 139 with a real-world maze, then maybe that simulation is close enough to
rlm@0 140 reality that a simulated sense of vision and motor control interacting
rlm@0 141 with that simulation could reveal useful information about the real
rlm@0 142 thing. It happens that there is a Java port of the original C source
rlm@0 143 code called Jake2. The port demonstrates Java's OpenGL bindings and
rlm@0 144 runs anywhere from 90% to 105% as fast as the C version. After
rlm@0 145 reviewing much of the source of Jake2, I eventually rejected it
rlm@0 146 because the engine is too tied to the concept of a first-person
rlm@0 147 shooter game. One of the problems I had was that there does not seem
rlm@0 148 to be any easy way to attach multiple cameras to a single
rlm@0 149 character. There are also several physics clipping issues that are
rlm@0 150 corrected in a way that only applies to the main character and does
rlm@0 151 not apply to arbitrary objects. While there is a large community of
rlm@0 152 level modders, I couldn't find a community to support using the engine
rlm@0 153 to make new things.
rlm@0 154
rlm@0 155 ** Source Engine
rlm@0 156
rlm@0 157 The Source Engine evolved from the Quake II and Quake I engines and is
rlm@0 158 used by Valve in the Half-Life series of games. The physics simulation
rlm@0 159 in the Source Engine is quite accurate and probably the best out of
rlm@0 160 all the engines I investigated. There is also an extensive community
rlm@0 161 actively working with the engine. However, applications that use the
rlm@0 162 Source Engine must be written in C++, the code is not open, it only
rlm@0 163 runs on Windows, and the tools that come with the SDK to handle models
rlm@0 164 and textures are complicated and awkward to use.
rlm@0 165
rlm@0 166 ** jMonkeyEngine
rlm@0 167
rlm@0 168 jMonkeyEngine is a new library for creating games in Java. It uses
rlm@0 169 OpenGL to render to the screen and uses screengraphs to avoid drawing
rlm@0 170 things that do not appear on the screen. It has an active community
rlm@0 171 and several games in the pipeline. The engine was not built to serve
rlm@0 172 any particular game but is instead meant to be used for any 3D
rlm@0 173 game. After experimenting with each of these three engines and a few
rlm@0 174 others for about 2 months I settled on jMonkeyEngine. I chose it
rlm@0 175 because it had the most features out of all the open projects I looked
rlm@0 176 at, and because I could then write my code in Clojure, an
rlm@0 177 implementation of LISP that runs on the JVM.
rlm@0 178
rlm@0 179 * Setup
rlm@0 180
rlm@0 181 First, I checked out the source to jMonkeyEngine:
rlm@0 182
rlm@0 183 #+srcname: checkout
rlm@0 184 #+begin_src sh :results verbatim
rlm@0 185 svn checkout http://jmonkeyengine.googlecode.com/svn/trunk/engine jme3
rlm@0 186 #+end_src
rlm@0 187
rlm@0 188 #+results: checkout
rlm@0 189 : Checked out revision 7975.
rlm@0 190
rlm@0 191
rlm@0 192 Building jMonkeyEngine is easy enough:
rlm@0 193
rlm@0 194 #+srcname: build
rlm@0 195 #+begin_src sh :results verbatim
rlm@0 196 cd jme3
rlm@0 197 ant jar | tail -n 2
rlm@0 198 #+end_src
rlm@0 199
rlm@0 200 #+results: build
rlm@0 201 : BUILD SUCCESSFUL
rlm@0 202 : Total time: 15 seconds
rlm@0 203
rlm@0 204
rlm@0 205 Also build the javadoc:
rlm@0 206
rlm@0 207 #+srcname: javadoc
rlm@0 208 #+begin_src sh :results verbatim
rlm@0 209 cd jme3
rlm@0 210 ant javadoc | tail -n 2
rlm@0 211 #+end_src
rlm@0 212
rlm@0 213 #+results: javadoc
rlm@0 214 : BUILD SUCCESSFUL
rlm@0 215 : Total time: 12 seconds
rlm@0 216
rlm@0 217 Now, move the jars from the compilation into the project's lib folder.
rlm@0 218
rlm@0 219 #+srcname: move-jars
rlm@0 220 #+begin_src sh :results verbatim
rlm@0 221 mkdir -p lib
rlm@0 222 mkdir -p src
rlm@0 223 cp jme3/dist/jMonkeyEngine3.jar lib/
rlm@0 224 cp jme3/dist/lib/* lib/
rlm@0 225 ls lib
rlm@0 226 #+end_src
rlm@0 227
rlm@0 228 #+results: move-jars
rlm@0 229 #+begin_example
rlm@0 230 eventbus-1.4.jar
rlm@0 231 jbullet.jar
rlm@0 232 jheora-jst-debug-0.6.0.jar
rlm@0 233 jinput.jar
rlm@0 234 jME3-jbullet.jar
rlm@0 235 jME3-lwjgl-natives.jar
rlm@0 236 jME3-testdata.jar
rlm@0 237 jME3-test.jar
rlm@0 238 jMonkeyEngine3.jar
rlm@0 239 j-ogg-oggd.jar
rlm@0 240 j-ogg-vorbisd.jar
rlm@0 241 lwjgl.jar
rlm@0 242 nifty-1.3.jar
rlm@0 243 nifty-default-controls-1.3.jar
rlm@0 244 nifty-examples-1.3.jar
rlm@0 245 nifty-lwjgl-renderer-1.3.jar
rlm@0 246 nifty-openal-soundsystem-1.0.jar
rlm@0 247 nifty-style-black-1.3.jar
rlm@0 248 nifty-style-grey-1.0.jar
rlm@0 249 noise-0.0.1-SNAPSHOT.jar
rlm@0 250 stack-alloc.jar
rlm@0 251 vecmath.jar
rlm@0 252 xmlpull-xpp3-1.1.4c.jar
rlm@0 253 #+end_example
rlm@0 254
rlm@0 255 It's good to create a =assets= directory in the style that the
rlm@0 256 =AssetManager= will like.
rlm@0 257
rlm@0 258 #+srcname: create-assets
rlm@0 259 #+begin_src sh :results verbatim
rlm@0 260 mkdir -p assets
rlm@0 261 mkdir -p assets/Interface
rlm@0 262 mkdir -p assets/Materials
rlm@0 263 mkdir -p assets/MatDefs
rlm@0 264 mkdir -p assets/Models
rlm@0 265 mkdir -p assets/Scenes
rlm@0 266 mkdir -p assets/Shaders
rlm@0 267 mkdir -p assets/Sounds
rlm@0 268 mkdir -p assets/Textures
rlm@0 269 tree -L 1 assets
rlm@0 270 #+end_src
rlm@0 271
rlm@0 272 #+results: create-assets
rlm@0 273 #+begin_example
rlm@0 274 assets
rlm@0 275 |-- Interface
rlm@0 276 |-- MatDefs
rlm@0 277 |-- Materials
rlm@0 278 |-- Models
rlm@0 279 |-- Scenes
rlm@0 280 |-- Shaders
rlm@0 281 |-- Sounds
rlm@0 282 `-- Textures
rlm@0 283
rlm@0 284 8 directories, 0 files
rlm@0 285 #+end_example
rlm@0 286
rlm@0 287
rlm@0 288 The java classpath should have all the jars contained in the =lib=
rlm@0 289 directory as well as the src directory.
rlm@0 290
rlm@0 291 For example, here is the file I use to run my REPL for clojure.
rlm@0 292
rlm@0 293 #+include: "~/swank-all" src sh :exports code
rlm@0 294
rlm@0 295 The important thing here is that =cortex/lib/*=, =cortex/src=, and
rlm@0 296 =cortex/assets= appear on the classpath. (=cortex= is the base
rlm@0 297 directory of this project.)
rlm@0 298
rlm@0 299 #+srcname: pwd
rlm@0 300 #+begin_src sh
rlm@0 301 pwd
rlm@0 302 #+end_src
rlm@0 303
rlm@0 304 #+results: pwd
rlm@0 305 : /home/r/cortex
rlm@0 306
rlm@0 307
rlm@0 308 * Simulation Base
rlm@0 309
rlm@0 310 ** Imports
rlm@0 311 First, I'll import jme core classes.
rlm@0 312 #+srcname: import
rlm@0 313 #+begin_src clojure :results silent
rlm@0 314 (ns cortex.import
rlm@0 315 (:require swank.util.class-browse))
rlm@0 316
rlm@0 317 (defn import-jme3 []
rlm@0 318 (import '[com.jme3.system AppSettings JmeSystem])
rlm@0 319 (import '[com.jme3.app Application SimpleApplication])
rlm@0 320 (import 'com.jme3.material.Material)
rlm@0 321 (import '[com.jme3.math Vector3f ColorRGBA Quaternion Transform])
rlm@0 322 (import '[com.jme3.scene Node Geometry])
rlm@0 323 (import '[com.jme3.scene.shape Box Sphere Sphere$TextureMode])
rlm@0 324 (import 'com.jme3.font.BitmapText)
rlm@0 325 (import '[com.jme3.input KeyInput InputManager])
rlm@0 326 (import '[com.jme3.input.controls
rlm@0 327 ActionListener AnalogListener KeyTrigger MouseButtonTrigger])
rlm@0 328 (import '[com.jme3.asset AssetManager DesktopAssetManager] )
rlm@0 329 (import '[com.jme3.asset.plugins HttpZipLocator ZipLocator])
rlm@0 330 (import '[com.jme3.light PointLight DirectionalLight])
rlm@0 331 (import '[com.jme3.animation AnimControl Skeleton Bone])
rlm@0 332 (import '[com.jme3.bullet.collision.shapes
rlm@0 333 MeshCollisionShape SphereCollisionShape BoxCollisionShape])
rlm@0 334 (import 'com.jme3.renderer.queue.RenderQueue$ShadowMode)
rlm@0 335 (import 'jme3test.TestChooser)
rlm@0 336 (import '[com.jme3.bullet PhysicsTickListener PhysicsSpace])
rlm@0 337 (import '[com.jme3.bullet.joints SixDofJoint HingeJoint
rlm@0 338 SliderJoint Point2PointJoint ConeJoint]))
rlm@0 339
rlm@0 340
rlm@0 341 (defmacro permissive-import* [class-symbol]
rlm@0 342 `(try
rlm@0 343 (import ~class-symbol)
rlm@0 344 (catch Exception e#
rlm@0 345 (println "can't import " ~class-symbol))))
rlm@0 346
rlm@0 347 (defn permissive-import [class-symbol]
rlm@0 348 (eval (list 'cortex.import/permissive-import* class-symbol)))
rlm@0 349
rlm@0 350 (defn selection-import [selection-fn]
rlm@0 351 (dorun
rlm@0 352 (map (comp permissive-import symbol)
rlm@0 353 (filter selection-fn
rlm@0 354 (map :name
rlm@0 355 swank.util.class-browse/available-classes)))))
rlm@0 356
rlm@0 357 (defn mega-import-jme3
rlm@0 358 "ALL the jme classes. For REPL use."
rlm@0 359 []
rlm@0 360 (selection-import
rlm@0 361 #(and
rlm@0 362 (.startsWith % "com.jme3.")
rlm@0 363 ;; Don't import the Lwjgl stuff since it can throw exceptions
rlm@0 364 ;; upon being loaded.
rlm@0 365 (not (re-matches #".*Lwjgl.*" %)))))
rlm@0 366 #+end_src
rlm@0 367
rlm@0 368 The =mega-import-jme3= is quite usefull for debugging purposes since
rlm@0 369 it allows completion for almost all of JME's classes
rlm@0 370
rlm@0 371 ** Simplification
rlm@0 372 *** World
rlm@0 373
rlm@0 374 It is comvienent to wrap the JME elements that deal with creating a
rlm@0 375 world, creation of basic objects, and Keyboard input with a nicer
rlm@0 376 interface (at least for my purposes).
rlm@0 377
rlm@0 378 #+srcname: world-inputs
rlm@0 379 #+begin_src clojure :results silent
rlm@0 380 (ns cortex.world)
rlm@0 381 (require 'cortex.import)
rlm@0 382 (use 'clojure.contrib.def)
rlm@0 383 (rlm.rlm-commands/help)
rlm@0 384 (cortex.import/mega-import-jme3)
rlm@0 385
rlm@0 386 (defvar *app-settings*
rlm@0 387 (doto (AppSettings. true)
rlm@0 388 (.setFullscreen false)
rlm@0 389 (.setTitle "Aurellem.")
rlm@0 390 ;; disable 32 bit stuff for now
rlm@4 391 (.setAudioRenderer "Send")
rlm@0 392 )
rlm@0 393 "These settings control how the game is displayed on the screen for
rlm@0 394 debugging purposes. Use binding forms to change this if desired.
rlm@0 395 Full-screen mode does not work on some computers.")
rlm@0 396
rlm@0 397 (defn asset-manager
rlm@0 398 "returns a new, configured assetManager" []
rlm@0 399 (JmeSystem/newAssetManager
rlm@0 400 (.getResource
rlm@0 401 (.getContextClassLoader (Thread/currentThread))
rlm@0 402 "com/jme3/asset/Desktop.cfg")))
rlm@0 403
rlm@0 404 (defmacro no-exceptions
rlm@0 405 "Sweet relief like I never knew."
rlm@0 406 [& forms]
rlm@0 407 `(try ~@forms (catch Exception e# (.printStackTrace e#))))
rlm@0 408
rlm@0 409 (defn thread-exception-removal []
rlm@0 410 (println "removing exceptions from " (Thread/currentThread))
rlm@0 411 (.setUncaughtExceptionHandler
rlm@0 412 (Thread/currentThread)
rlm@0 413 (proxy [Thread$UncaughtExceptionHandler] []
rlm@0 414 (uncaughtException
rlm@0 415 [thread thrown]
rlm@0 416 (println "uncaught-exception thrown in " thread)
rlm@0 417 (println (.getMessage thrown))))))
rlm@0 418
rlm@0 419 (def println-repl (bound-fn [& args] (apply println args)))
rlm@0 420
rlm@0 421 (use '[pokemon [lpsolve :only [constant-map]]])
rlm@0 422
rlm@0 423 (defn no-op [& _])
rlm@0 424
rlm@0 425 (defn all-keys
rlm@0 426 "Construct a map of strings representing all the manual inputs from
rlm@0 427 either the keyboard or mouse."
rlm@0 428 []
rlm@0 429 (let [inputs (constant-map KeyInput)]
rlm@0 430 (assoc
rlm@0 431 (zipmap (map (fn [field]
rlm@0 432 (.toLowerCase (re-gsub #"_" "-" field))) (vals inputs))
rlm@0 433 (map (fn [val] (KeyTrigger. val)) (keys inputs)))
rlm@0 434 ;;explicitly add mouse controls
rlm@0 435 "mouse-left" (MouseButtonTrigger. 0)
rlm@0 436 "mouse-middle" (MouseButtonTrigger. 2)
rlm@0 437 "mouse-right" (MouseButtonTrigger. 1))))
rlm@0 438
rlm@0 439 (defn initialize-inputs
rlm@0 440 "more java-interop cruft to establish keybindings for a particular virtual world"
rlm@0 441 [game input-manager key-map]
rlm@0 442 (doall (map (fn [[name trigger]]
rlm@0 443 (.addMapping ^InputManager input-manager
rlm@0 444 name (into-array (class trigger) [trigger]))) key-map))
rlm@0 445 (doall (map (fn [name]
rlm@0 446 (.addListener ^InputManager input-manager game
rlm@0 447 (into-array String [name]))) (keys key-map))))
rlm@0 448
rlm@0 449 #+end_src
rlm@0 450
rlm@0 451 These functions are all for debug controlling of the world through
rlm@0 452 keyboard and mouse.
rlm@0 453
rlm@0 454 We reuse =constant-map= from =pokemon.lpsolve= to get the numerical
rlm@0 455 values for all the keys defined in the =KeyInput= class. The
rlm@0 456 documentation for =constant-map= is:
rlm@0 457
rlm@0 458 #+begin_src clojure :results output
rlm@0 459 (doc pokemon.lpsolve/constant-map)
rlm@0 460 #+end_src
rlm@0 461
rlm@0 462 #+results:
rlm@0 463 : -------------------------
rlm@0 464 : pokemon.lpsolve/constant-map
rlm@0 465 : ([class])
rlm@0 466 : Takes a class and creates a map of the static constant integer
rlm@0 467 : fields with their names. This helps with C wrappers where they have
rlm@0 468 : just defined a bunch of integer constants instead of enums
rlm@0 469
rlm@0 470
rlm@0 471 Then, =all-keys= converts the constant names like =KEY_J= to the more
rlm@0 472 clojure-like =key-j=, and returns a map from these keys to
rlm@0 473 jMonkeyEngine KeyTrigger objects, the use of which will soon become
rlm@0 474 apparent. =all-keys= also adds the three mouse button controls to the
rlm@0 475 map.
rlm@0 476
rlm@0 477 #+srcname: world
rlm@0 478 #+begin_src clojure :results silent
rlm@0 479 (in-ns 'cortex.world)
rlm@0 480
rlm@0 481 (defn traverse
rlm@0 482 "apply f to every non-node, deeply"
rlm@0 483 [f node]
rlm@0 484 (if (isa? (class node) Node)
rlm@0 485 (dorun (map (partial traverse f) (.getChildren node)))
rlm@0 486 (f node)))
rlm@0 487
rlm@0 488 (def gravity (Vector3f. 0 -9.81 0))
rlm@0 489
rlm@0 490 (defn world
rlm@0 491 [root-node key-map setup-fn update-fn]
rlm@0 492 (let [physics-manager (BulletAppState.)
rlm@0 493 shadow-renderer (BasicShadowRenderer. (asset-manager) (int 256))
rlm@0 494 ;;maybe use a better shadow renderer someday!
rlm@0 495 ;;shadow-renderer (PssmShadowRenderer. (asset-manager) 256 1)
rlm@0 496 ]
rlm@0 497 (doto
rlm@0 498 (proxy [SimpleApplication ActionListener] []
rlm@0 499 (simpleInitApp
rlm@0 500 []
rlm@0 501 (no-exceptions
rlm@0 502 (.setTimer this (IsoTimer. 60))
rlm@0 503 ;; Create key-map.
rlm@0 504 (.setFrustumFar (.getCamera this) 300)
rlm@0 505 (initialize-inputs this (.getInputManager this) (all-keys))
rlm@0 506 ;; Don't take control of the mouse
rlm@0 507 (org.lwjgl.input.Mouse/setGrabbed false)
rlm@0 508 ;; add all objects to the world
rlm@0 509 (.attachChild (.getRootNode this) root-node)
rlm@0 510 ;; enable physics
rlm@0 511 ;; add a physics manager
rlm@0 512 (.attach (.getStateManager this) physics-manager)
rlm@0 513 (.setGravity (.getPhysicsSpace physics-manager) gravity)
rlm@0 514
rlm@0 515
rlm@0 516 ;; go through every object and add it to the physics manager
rlm@0 517 ;; if relavant.
rlm@0 518 (traverse (fn [geom]
rlm@0 519 (dorun
rlm@0 520 (for [n (range (.getNumControls geom))]
rlm@0 521 (do
rlm@0 522 (println-repl "adding control " (.getControl geom n))
rlm@0 523 (.add (.getPhysicsSpace physics-manager)
rlm@0 524 (.getControl geom n))))))
rlm@0 525 (.getRootNode this))
rlm@0 526 ;;(.addAll (.getPhysicsSpace physics-manager) (.getRootNode this))
rlm@0 527
rlm@0 528 (setup-fn this)
rlm@0 529 (.setDirection shadow-renderer
rlm@0 530 (.normalizeLocal (Vector3f. -1 -1 -1)))
rlm@0 531 (.addProcessor (.getViewPort this) shadow-renderer)
rlm@0 532 (.setShadowMode (.getRootNode this) RenderQueue$ShadowMode/Off)
rlm@0 533 ))
rlm@0 534 (simpleUpdate
rlm@0 535 [tpf]
rlm@0 536 (no-exceptions
rlm@0 537 (update-fn this tpf)))
rlm@0 538 (onAction
rlm@0 539 [binding value tpf]
rlm@0 540 ;; whenever a key is pressed, call the function returned from
rlm@0 541 ;; key-map.
rlm@0 542 (no-exceptions
rlm@0 543 (if-let [react (key-map binding)]
rlm@0 544 (react this value)))))
rlm@0 545 ;; don't show a menu to change options.
rlm@0 546
rlm@0 547 (.setShowSettings false)
rlm@0 548 (.setPauseOnLostFocus false)
rlm@0 549 (.setSettings *app-settings*))))
rlm@0 550
rlm@0 551 (defn apply-map
rlm@0 552 "Like apply, but works for maps and functions that expect an implicit map
rlm@0 553 and nothing else as in (fn [& {}]).
rlm@0 554 ------- Example -------
rlm@4 555 (defn jjj [& {:keys [www] :or {www \"oh yeah\"} :as env}] (println www))
rlm@0 556 (apply-map jjj {:www \"whatever\"})
rlm@0 557 -->\"whatever\""
rlm@0 558 [fn m]
rlm@0 559 (apply fn (reduce #(into %1 %2) [] m)))
rlm@0 560
rlm@0 561 #+end_src
rlm@0 562
rlm@0 563
rlm@0 564 =world= is the most important function here.
rlm@0 565 *** TODO more documentation
rlm@0 566
rlm@0 567 #+srcname: world-shapes
rlm@0 568 #+begin_src clojure :results silent
rlm@0 569 (in-ns 'cortex.world)
rlm@0 570 (defrecord shape-description
rlm@0 571 [name
rlm@0 572 color
rlm@0 573 mass
rlm@0 574 friction
rlm@0 575 texture
rlm@0 576 material
rlm@0 577 position
rlm@0 578 rotation
rlm@0 579 shape
rlm@0 580 physical?])
rlm@0 581
rlm@0 582 (def base-shape
rlm@0 583 (shape-description.
rlm@0 584 "default-shape"
rlm@0 585 false
rlm@0 586 ;;ColorRGBA/Blue
rlm@0 587 1.0 ;; mass
rlm@0 588 1.0 ;; friction
rlm@0 589 ;; texture
rlm@0 590 "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 591 ;; material
rlm@0 592 "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 593 Vector3f/ZERO
rlm@0 594 Quaternion/IDENTITY
rlm@0 595 (Box. Vector3f/ZERO 0.5 0.5 0.5)
rlm@0 596 true))
rlm@0 597
rlm@0 598 (defn make-shape
rlm@0 599 [#^shape-description d]
rlm@0 600 (let [mat (Material. (asset-manager) (:material d))
rlm@0 601 geom (Geometry. (:name d) (:shape d))]
rlm@0 602 (if (:texture d)
rlm@0 603 (let [key (TextureKey. (:texture d))]
rlm@0 604 (.setGenerateMips key true)
rlm@0 605 (.setTexture mat "ColorMap" (.loadTexture (asset-manager) key))))
rlm@0 606 (if (:color d) (.setColor mat "Color" (:color d)))
rlm@0 607 (.setMaterial geom mat)
rlm@0 608 (if-let [rotation (:rotation d)] (.rotate geom rotation))
rlm@0 609 (.setLocalTranslation geom (:position d))
rlm@0 610 (if (:physical? d)
rlm@0 611 (let [impact-shape (doto (GImpactCollisionShape. (.getMesh geom)) (.setMargin 0))
rlm@0 612 physics-control (RigidBodyControl.
rlm@0 613 impact-shape
rlm@0 614 (float (:mass d)))]
rlm@0 615 (.createJmeMesh impact-shape)
rlm@0 616 (.addControl geom physics-control)
rlm@0 617 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
rlm@0 618 (.setFriction physics-control (:friction d))))
rlm@0 619 ;;the default is to keep this node in the physics engine forever.
rlm@0 620 ;;these commands must come after the control is added to the geometry.
rlm@0 621 ;;
rlm@0 622 geom))
rlm@0 623
rlm@0 624 (defn box
rlm@0 625 ([l w h & {:as options}]
rlm@0 626 (let [options (merge base-shape options)]
rlm@0 627 (make-shape (assoc options
rlm@0 628 :shape (Box. l w h)))))
rlm@0 629 ([] (box 0.5 0.5 0.5)))
rlm@0 630
rlm@0 631 (defn sphere
rlm@0 632 ([r & {:as options}]
rlm@0 633 (let [options (merge base-shape options)]
rlm@0 634 (make-shape (assoc options
rlm@0 635 :shape (Sphere. 32 32 (float r))))))
rlm@0 636 ([] (sphere 0.5)))
rlm@0 637
rlm@0 638 (defn add-element [game node]
rlm@0 639 (.addAll
rlm@0 640 (.getPhysicsSpace
rlm@0 641 (.getState
rlm@0 642 (.getStateManager game)
rlm@0 643 BulletAppState))
rlm@0 644 node)
rlm@0 645 (.attachChild (.getRootNode game) node))
rlm@0 646
rlm@0 647 (defn set-gravity*
rlm@0 648 [game gravity]
rlm@0 649 (traverse
rlm@0 650 (fn [geom]
rlm@0 651 (if-let
rlm@0 652 [control (.getControl geom RigidBodyControl)]
rlm@0 653 (do
rlm@0 654 (.setGravity control gravity)
rlm@0 655 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
rlm@0 656 )))
rlm@0 657 (.getRootNode game)))
rlm@0 658
rlm@0 659 #+end_src
rlm@0 660
rlm@0 661 These are convienence functions for creating JME objects and
rlm@0 662 manipulating a world.
rlm@0 663
rlm@0 664 #+srcname: world-view
rlm@0 665 #+begin_src clojure :results silent
rlm@0 666 (in-ns 'cortex.world)
rlm@0 667
rlm@0 668 (defprotocol Viewable
rlm@0 669 (view [something]))
rlm@0 670
rlm@0 671 (extend-type com.jme3.scene.Geometry
rlm@0 672 Viewable
rlm@0 673 (view [geo]
rlm@0 674 (view (doto (Node.)(.attachChild geo)))))
rlm@0 675
rlm@0 676 (extend-type com.jme3.scene.Node
rlm@0 677 Viewable
rlm@0 678 (view [node]
rlm@0 679 (.start
rlm@0 680 (world node
rlm@0 681 {}
rlm@0 682 (fn [world]
rlm@0 683 (.enableDebug
rlm@0 684 (.getPhysicsSpace
rlm@0 685 (.getState
rlm@0 686 (.getStateManager world)
rlm@0 687 BulletAppState))
rlm@0 688 (asset-manager))
rlm@0 689 (set-gravity* world Vector3f/ZERO)
rlm@0 690 ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0))
rlm@0 691 (let [sun (doto (DirectionalLight.)
rlm@0 692 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 693 (.setColor ColorRGBA/White))]
rlm@0 694 (.addLight (.getRootNode world) sun)))
rlm@0 695 no-op))))
rlm@0 696
rlm@0 697 (defn position-camera [game]
rlm@0 698 (doto (.getCamera game)
rlm@0 699 (.setLocation (Vector3f. 0 6 6))
rlm@0 700 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
rlm@0 701
rlm@0 702 #+end_src
rlm@0 703
rlm@0 704 Here I make the =Viewable= protocol and extend it to JME's types. Now
rlm@0 705 hello-world can be written as easily as:
rlm@0 706
rlm@0 707 #+begin_src clojure :results silent
rlm@0 708 (cortex.world/view (cortex.world/box))
rlm@0 709 #+end_src
rlm@0 710
rlm@0 711 ** Hello
rlm@0 712 Here are the jmonkeyengine "Hello" programs translated to clojure.
rlm@0 713 *** Hello Simple App
rlm@0 714 Here is the hello world example for jme3 in clojure.
rlm@0 715 It's a more or less direct translation from the java source
rlm@0 716 from
rlm@0 717 http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication.
rlm@0 718
rlm@0 719 Of note is the fact that since we don't have access to the
rlm@0 720 =AssetManager= via extendig =SimpleApplication=, we have to build one
rlm@0 721 ourselves.
rlm@0 722
rlm@0 723 #+srcname: hello-simple-app
rlm@0 724 #+begin_src clojure :results silent
rlm@0 725 (ns hello.hello-simple-app)
rlm@0 726 (require 'cortex.import)
rlm@0 727 (use 'clojure.contrib.def)
rlm@0 728 (rlm.rlm-commands/help)
rlm@0 729 (cortex.import/import-jme3)
rlm@0 730 (use 'cortex.world)
rlm@0 731
rlm@0 732
rlm@0 733 (def cube (Box. Vector3f/ZERO 1 1 1))
rlm@0 734
rlm@0 735 (def geom (Geometry. "Box" cube))
rlm@0 736
rlm@0 737 (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
rlm@0 738
rlm@0 739 (.setColor mat "Color" ColorRGBA/Blue)
rlm@0 740
rlm@0 741 (.setMaterial geom mat)
rlm@0 742
rlm@0 743 (defn simple-app []
rlm@0 744 (doto
rlm@0 745 (proxy [SimpleApplication] []
rlm@0 746 (simpleInitApp
rlm@0 747 []
rlm@0 748 ;; Don't take control of the mouse
rlm@0 749 (org.lwjgl.input.Mouse/setGrabbed false)
rlm@0 750 (.attachChild (.getRootNode this) geom)))
rlm@0 751 ;; don't show a menu to change options.
rlm@0 752 (.setShowSettings false)
rlm@0 753 (.setPauseOnLostFocus false)
rlm@0 754 (.setSettings *app-settings*)))
rlm@0 755 #+end_src
rlm@0 756
rlm@0 757 Running this program will begin a new jMonkeyEngine game which
rlm@0 758 displays a single blue cube.
rlm@0 759
rlm@0 760 #+begin_src clojure :exports code :results silent
rlm@0 761 (.start (hello.hello-simple-app/simple-app))
rlm@0 762 #+end_src
rlm@0 763
rlm@0 764 #+caption: the simplest JME game.
rlm@0 765 [[./images/simple-app.jpg]]
rlm@0 766
rlm@0 767
rlm@0 768
rlm@0 769 *** Hello Physics
rlm@0 770 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
rlm@0 771
rlm@0 772 #+srcname: brick-wall-header
rlm@0 773 #+begin_src clojure :results silent
rlm@0 774 (ns hello.brick-wall)
rlm@0 775 (require 'cortex.import)
rlm@0 776 (use 'clojure.contrib.def)
rlm@0 777 (rlm.rlm-commands/help)
rlm@0 778 (cortex.import/mega-import-jme3)
rlm@0 779 (use '[pokemon [lpsolve :only [constant-map]]])
rlm@0 780 (use 'cortex.world)
rlm@0 781 #+end_src
rlm@0 782
rlm@0 783 #+srcname: brick-wall-body
rlm@0 784 #+begin_src clojure :results silent
rlm@0 785 (in-ns 'hello.brick-wall)
rlm@0 786
rlm@0 787 (defn floor
rlm@0 788 "make a sturdy, unmovable physical floor"
rlm@0 789 []
rlm@0 790 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
rlm@0 791
rlm@0 792 (def brick-length 0.48)
rlm@0 793 (def brick-width 0.24)
rlm@0 794 (def brick-height 0.12)
rlm@0 795
rlm@0 796
rlm@0 797 (defn brick* [position]
rlm@0 798 (doto (box brick-length brick-height brick-width
rlm@0 799 :position position :name "brick"
rlm@0 800 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 801 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 802 :mass 36)
rlm@0 803 (->
rlm@0 804 (.getMesh)
rlm@0 805 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@0 806 ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 807 )
rlm@0 808 )
rlm@0 809
rlm@0 810 (defn inception-brick-wall
rlm@0 811 "construct a physical brick wall"
rlm@0 812 []
rlm@0 813 (let [node (Node. "brick-wall")]
rlm@0 814 (dorun
rlm@0 815 (map (comp #(.attachChild node %) brick*)
rlm@0 816 (for
rlm@0 817 [x (range 15)
rlm@0 818 y (range 10)
rlm@0 819 z (range 1)]
rlm@0 820 (Vector3f.
rlm@0 821 (* brick-length x 1.03)
rlm@0 822 (* brick-width y y 10)
rlm@0 823 (* brick-height z)))))
rlm@0 824 node))
rlm@0 825
rlm@0 826 (defn gravity-toggle
rlm@0 827 [new-value]
rlm@0 828 (fn [game value]
rlm@0 829 (println-repl "set gravity to " new-value)
rlm@0 830 (if value
rlm@0 831 (set-gravity* game new-value)
rlm@0 832 (set-gravity* game gravity))))
rlm@0 833
rlm@0 834 (defn fire-cannon-ball []
rlm@0 835 (fn [game value]
rlm@0 836 (if (not value)
rlm@0 837 (let [camera (.getCamera game)
rlm@0 838 cannon-ball
rlm@0 839 (sphere 0.7
rlm@0 840 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 841 :texture "Textures/PokeCopper.jpg"
rlm@0 842 :position
rlm@0 843 (.add (.getLocation camera)
rlm@0 844 (.mult (.getDirection camera) (float 1)))
rlm@0 845 :mass 3)] ;200 0.05
rlm@0 846 (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive)
rlm@0 847 (.setLinearVelocity
rlm@0 848 (.getControl cannon-ball RigidBodyControl)
rlm@0 849 (.mult (.getDirection camera) (float 50))) ;50
rlm@0 850 (add-element game cannon-ball)))))
rlm@0 851
rlm@0 852 (defn floor* []
rlm@0 853 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@0 854 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 855 :texture "Textures/Terrain/Pond/Pond.png"
rlm@0 856 :position (Vector3f. 0 -0.1 0 )
rlm@0 857 :mass 0)
rlm@0 858 (->
rlm@0 859 (.getMesh)
rlm@0 860 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@0 861 (->
rlm@0 862 (.getMaterial)
rlm@0 863 (.getTextureParam "ColorMap")
rlm@0 864 (.getTextureValue)
rlm@0 865 (.setWrap Texture$WrapMode/Repeat))
rlm@0 866 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@0 867 ))
rlm@0 868
rlm@0 869 (defn brick-wall* []
rlm@0 870 (let [node (Node. "brick-wall")]
rlm@0 871 (dorun
rlm@0 872 (map
rlm@0 873 (comp #(.attachChild node %) brick*)
rlm@0 874 (for [y (range 15)
rlm@0 875 x (range 4)
rlm@0 876 z (range 1)]
rlm@0 877 (Vector3f.
rlm@0 878 (+ (* 2 x brick-length)
rlm@0 879 (if (even? (+ y z))
rlm@0 880 (/ brick-length 4) (/ brick-length -4)))
rlm@0 881 (+ (* brick-height (inc (* 2 y))))
rlm@0 882 (* 2 z brick-width) ))))
rlm@0 883 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@0 884 node))
rlm@0 885
rlm@0 886 (defn brick-wall-game-run []
rlm@0 887 (doto
rlm@0 888 (world
rlm@0 889 (doto (Node.) (.attachChild (floor*))
rlm@0 890 (.attachChild (brick-wall*))
rlm@0 891 )
rlm@0 892 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 893 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 894 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 895 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 896 "key-k" (gravity-toggle Vector3f/ZERO)
rlm@0 897 "key-u" (gravity-toggle (Vector3f. 0 9.81 0))
rlm@0 898 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 899 "key-f" (fn[game value]
rlm@0 900 (if (not value) (add-element game (brick-wall*))))
rlm@0 901 "key-return" (fire-cannon-ball)}
rlm@0 902 position-camera
rlm@0 903 (fn [& _]))
rlm@0 904 (.start)))
rlm@0 905 #+end_src
rlm@0 906
rlm@0 907 #+begin_src clojure :results silent
rlm@0 908 (hello.brick-wall/brick-wall-game-run)
rlm@0 909 #+end_src
rlm@0 910
rlm@0 911 #+caption: the brick wall standing
rlm@0 912 [[./images/brick-wall-standing.jpg]]
rlm@0 913
rlm@0 914 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
rlm@0 915 [[./images/brick-wall-knocked-down.jpg]]
rlm@0 916
rlm@0 917 *** Other Brick Games
rlm@0 918 #+srcname: other-games
rlm@0 919 #+begin_src clojure :results silent
rlm@0 920 (ns cortex.other-games
rlm@0 921 {:author "Dylan Holmes"})
rlm@0 922 (use 'cortex.world)
rlm@0 923 (use 'hello.brick-wall)
rlm@0 924 (use 'cortex.import)
rlm@0 925 (cortex.import/mega-import-jme3)
rlm@0 926
rlm@0 927 (defn scad [position]
rlm@0 928 (doto (box 0.1 0.1 0.1
rlm@0 929 :position position :name "brick"
rlm@0 930 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 931 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 932 :mass 20)
rlm@0 933 (->
rlm@0 934 (.getMesh)
rlm@0 935 (.scaleTextureCoordinates (Vector2f. 1 0.5))
rlm@0 936 )
rlm@0 937 (-> (.getControl RigidBodyControl)
rlm@0 938 (.setLinearVelocity (Vector3f. 0 100 0))
rlm@0 939 )
rlm@0 940
rlm@0 941 ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
rlm@0 942 ))
rlm@0 943
rlm@0 944
rlm@0 945 (defn shrapnel []
rlm@0 946 (let [node (Node. "explosion-day")]
rlm@0 947 (dorun
rlm@0 948 (map
rlm@0 949 (comp #(.attachChild node %) scad)
rlm@0 950 (for [y (range 15)
rlm@0 951 x (range 4)
rlm@0 952 z (range 1)]
rlm@0 953 (Vector3f.
rlm@0 954 (+ (* 2 x brick-height)
rlm@0 955 (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
rlm@0 956 (+ (* brick-height (inc (* 2 y))))
rlm@0 957 (* 2 z brick-height) ))))
rlm@0 958 node))
rlm@0 959
rlm@0 960
rlm@0 961 (def domino-height 0.48)
rlm@0 962 (def domino-thickness 0.12)
rlm@0 963 (def domino-width 0.24)
rlm@0 964
rlm@0 965 (def domino-thickness 0.05)
rlm@0 966 (def domino-width 0.5)
rlm@0 967 (def domino-height 1)
rlm@0 968
rlm@0 969 (defn domino
rlm@0 970 ([position]
rlm@0 971 (domino position (Quaternion/IDENTITY)))
rlm@0 972 ([position rotation]
rlm@0 973 (doto (box domino-width domino-height domino-thickness
rlm@0 974 :position position :name "domino"
rlm@0 975 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 976 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 977 :mass 1
rlm@0 978 :rotation rotation)
rlm@0 979 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 980 )))
rlm@0 981
rlm@0 982
rlm@0 983 (defn domino-row []
rlm@0 984 (let [node (Node. "domino-row")]
rlm@0 985 (dorun
rlm@0 986 (map
rlm@0 987 (comp #(.attachChild node %) domino)
rlm@0 988 (for [
rlm@0 989 z (range 10)
rlm@0 990 x (range 5)
rlm@0 991 ]
rlm@0 992 (Vector3f.
rlm@0 993 (+ (* z domino-width) (* x 5 domino-width))
rlm@0 994 (/ domino-height 1)
rlm@0 995 (* -5.5 domino-thickness z) ))))
rlm@0 996
rlm@0 997 node))
rlm@0 998
rlm@0 999 (defn domino-cycle []
rlm@0 1000 (let [node (Node. "domino-cycle")]
rlm@0 1001 (dorun
rlm@0 1002 (map
rlm@0 1003 (comp #(.attachChild node %) (partial apply domino) )
rlm@0 1004 (for [n (range 720)]
rlm@0 1005 (let [space (* domino-height 5.5)
rlm@0 1006 r (fn[n] (* (+ n 3) domino-width 0.5))
rlm@0 1007 t (fn[n] (reduce
rlm@0 1008 +
rlm@0 1009 (map
rlm@0 1010 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
rlm@0 1011 (range n))))
rlm@0 1012 t (t n)
rlm@0 1013 r (r n)
rlm@0 1014 ct (Math/cos t)
rlm@0 1015 st (Math/sin t)
rlm@0 1016 ]
rlm@0 1017 (list
rlm@0 1018 (Vector3f.
rlm@0 1019 (* -1 r st)
rlm@0 1020 (/ domino-height 1)
rlm@0 1021 (* r ct))
rlm@0 1022 (.fromAngleAxis (Quaternion.)
rlm@0 1023 (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
rlm@0 1024 )))
rlm@0 1025 ))
rlm@0 1026 node))
rlm@0 1027
rlm@0 1028
rlm@0 1029 (defn domino-game-run []
rlm@0 1030 (doto
rlm@0 1031 (world
rlm@0 1032 (doto (Node.) (.attachChild (floor*))
rlm@0 1033 )
rlm@0 1034 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 1035 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 1036 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 1037 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 1038 "key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
rlm@0 1039 "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
rlm@0 1040 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 1041
rlm@0 1042 "key-space"
rlm@0 1043 (fn[game value]
rlm@0 1044
rlm@0 1045 (if (not value)
rlm@0 1046 (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
rlm@0 1047 (.fromAngleAxis (Quaternion.)
rlm@0 1048 (/ Math/PI 2) (Vector3f. 0 1 0)))]
rlm@0 1049 (add-element game d))))
rlm@0 1050 "key-f"
rlm@0 1051 (fn[game value](if (not value) (add-element game (domino-cycle))))
rlm@0 1052 "key-return" (fire-cannon-ball)}
rlm@0 1053 position-camera
rlm@0 1054 (fn [& _]))
rlm@0 1055 (.start)))
rlm@0 1056 #+end_src
rlm@0 1057
rlm@0 1058 #+begin_src clojure :results silent
rlm@0 1059 (cortex.other-games/domino-game-run)
rlm@0 1060 #+end_src
rlm@0 1061
rlm@0 1062 #+caption: floating dominos
rlm@0 1063 [[./images/dominos.jpg]]
rlm@0 1064
rlm@0 1065 *** Hello Loop
rlm@0 1066 #+srcname: hello-loop
rlm@0 1067 #+begin_src clojure :results silent
rlm@0 1068 (ns hello.loop)
rlm@0 1069 (use 'cortex.world)
rlm@0 1070 (use 'cortex.import)
rlm@0 1071 (cortex.import/mega-import-jme3)
rlm@0 1072 (rlm.rlm-commands/help)
rlm@0 1073
rlm@0 1074 (defn blue-cube []
rlm@0 1075 (box 1 1 1
rlm@0 1076 :color ColorRGBA/Blue
rlm@0 1077 :texture false
rlm@0 1078 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1079 :name "blue-cube"
rlm@0 1080 :physical? false))
rlm@0 1081
rlm@0 1082 (defn blue-cube-game []
rlm@0 1083 (let [cube (blue-cube)
rlm@0 1084 root (doto (Node.) (.attachChild cube))]
rlm@0 1085 (world root
rlm@0 1086 {}
rlm@0 1087 no-op
rlm@0 1088 (fn [game tpf]
rlm@0 1089 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
rlm@0 1090 #+end_src
rlm@0 1091
rlm@0 1092 *** Hello Collision
rlm@0 1093
rlm@0 1094 #+srcname: hello-collision
rlm@0 1095 #+begin_src clojure :results silent
rlm@0 1096 (ns hello.collision)
rlm@0 1097 (use 'cortex.world)
rlm@0 1098 (use 'cortex.import)
rlm@0 1099 (use 'clojure.contrib.def)
rlm@0 1100
rlm@0 1101
rlm@0 1102 (cortex.import/mega-import-jme3)
rlm@0 1103 (rlm.rlm-commands/help)
rlm@0 1104 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 1105
rlm@0 1106
rlm@0 1107 (defn environment []
rlm@0 1108 (let
rlm@0 1109 [scene-model
rlm@0 1110 (doto
rlm@0 1111 (.loadModel
rlm@0 1112 (doto (asset-manager)
rlm@0 1113 (.registerLocator
rlm@0 1114 "/home/r/cortex/assets/zips/town.zip" ZipLocator))
rlm@0 1115 "main.scene")
rlm@0 1116 (.setLocalScale (float 2.0)))
rlm@0 1117 collision-shape
rlm@0 1118 (CollisionShapeFactory/createMeshShape #^Node scene-model)
rlm@0 1119 landscape (RigidBodyControl. collision-shape 0)]
rlm@0 1120 (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
rlm@0 1121 (.addControl scene-model landscape)
rlm@0 1122 scene-model))
rlm@0 1123
rlm@0 1124 (defn player-fn []
rlm@0 1125 (doto
rlm@0 1126 (CharacterControl.
rlm@0 1127 (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
rlm@0 1128 (float 0.05))
rlm@0 1129 (.setJumpSpeed 20)
rlm@0 1130 (.setFallSpeed 30)
rlm@0 1131 (.setGravity 30) ;30
rlm@0 1132 (.setPhysicsLocation (Vector3f. 0 10 0))))
rlm@0 1133
rlm@0 1134 (defn lights []
rlm@0 1135 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
rlm@0 1136 (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
rlm@0 1137 (doto (DirectionalLight.)
rlm@0 1138 (.setColor (.mult ColorRGBA/White (float 0.9) ))
rlm@0 1139 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 1140
rlm@0 1141 (defn night-lights []
rlm@0 1142 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
rlm@0 1143 (doto (DirectionalLight.)
rlm@0 1144 (.setColor (.mult ColorRGBA/White (float 0.2) ))
rlm@0 1145 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 1146
rlm@0 1147 (def player (atom (player-fn)))
rlm@0 1148
rlm@0 1149 (defn setup-fn [game]
rlm@0 1150 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 1151 ;; set the color of the sky
rlm@0 1152 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 1153 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 1154 (doto (.getFlyByCamera game)
rlm@0 1155 (.setMoveSpeed (float 100))
rlm@0 1156 (.setRotationSpeed 3))
rlm@0 1157 (.add
rlm@0 1158 (.getPhysicsSpace
rlm@0 1159 (.getState (.getStateManager game) BulletAppState))
rlm@0 1160 @player)
rlm@0 1161
rlm@0 1162 (doto (Node.) (.attachChild (.getRootNode game))
rlm@0 1163 (.attachChild (brick-wall*))
rlm@0 1164 )
rlm@0 1165
rlm@0 1166 )
rlm@0 1167
rlm@0 1168
rlm@0 1169 (def walking-up? (atom false))
rlm@0 1170 (def walking-down? (atom false))
rlm@0 1171 (def walking-left? (atom false))
rlm@0 1172 (def walking-right? (atom false))
rlm@0 1173
rlm@0 1174 (defn set-walk [walk-atom game value]
rlm@0 1175 ;;(println-repl "setting stuff to " value)
rlm@0 1176 (reset! walk-atom value))
rlm@0 1177
rlm@0 1178 (defn responses []
rlm@0 1179 {"key-w" (partial set-walk walking-up?)
rlm@0 1180 "key-d" (partial set-walk walking-right?)
rlm@0 1181 "key-s" (partial set-walk walking-down?)
rlm@0 1182 "key-a" (partial set-walk walking-left?)
rlm@0 1183 "key-return" (fire-cannon-ball)
rlm@0 1184 "key-space" (fn [game value] (.jump @player))
rlm@0 1185 })
rlm@0 1186
rlm@0 1187 (defn update-fn
rlm@0 1188 [game tpf]
rlm@0 1189 (let [camera (.getCamera game)
rlm@0 1190 cam-dir (.multLocal
rlm@0 1191 (.clone
rlm@0 1192 (.getDirection camera)) (float 0.6))
rlm@0 1193 cam-left (.multLocal
rlm@0 1194 (.clone
rlm@0 1195 (.getLeft camera)) (float 0.4))
rlm@0 1196 walk-direction (Vector3f. 0 0 0)]
rlm@0 1197
rlm@0 1198 (cond
rlm@0 1199 @walking-up? (.addLocal walk-direction cam-dir)
rlm@0 1200 @walking-right? (.addLocal walk-direction (.negate cam-left))
rlm@0 1201 @walking-down? (.addLocal walk-direction (.negate cam-dir))
rlm@0 1202 @walking-left? (.addLocal walk-direction cam-left))
rlm@0 1203 (.setWalkDirection @player walk-direction)
rlm@0 1204 (.setLocation camera (.getPhysicsLocation @player))))
rlm@0 1205
rlm@0 1206 (defn run-game []
rlm@0 1207 (.start
rlm@0 1208 (world (environment)
rlm@0 1209 (responses)
rlm@0 1210 setup-fn
rlm@0 1211 update-fn)))
rlm@0 1212 #+end_src
rlm@0 1213
rlm@0 1214 *** Hello Terrain
rlm@0 1215 #+srcname: hello-terrain
rlm@0 1216 #+begin_src clojure :results silent
rlm@0 1217 (ns hello.terrain)
rlm@0 1218 (use 'cortex.world)
rlm@0 1219 (use 'cortex.import)
rlm@0 1220 (use 'clojure.contrib.def)
rlm@0 1221 (import jme3tools.converters.ImageToAwt)
rlm@0 1222
rlm@0 1223
rlm@0 1224 (cortex.import/mega-import-jme3)
rlm@0 1225 (rlm.rlm-commands/help)
rlm@0 1226 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 1227
rlm@0 1228
rlm@0 1229 (defn setup-fn [type game]
rlm@0 1230 (.setMoveSpeed (.getFlyByCamera game) 50)
rlm@0 1231 (.setFrustumFar (.getCamera game) 10000)
rlm@0 1232 (let [env (environment type)
rlm@0 1233 cameras [(.getCamera game)]
rlm@0 1234 control (TerrainLodControl. env cameras)]
rlm@0 1235 ;;(.addControl env control)
rlm@0 1236 (.attachChild (.getRootNode game) env)))
rlm@0 1237
rlm@0 1238 (defn environment [type]
rlm@0 1239 (let
rlm@0 1240 [mat_terrain
rlm@0 1241 (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
rlm@0 1242 grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
rlm@0 1243 dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
rlm@0 1244 rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
rlm@0 1245 heightmap-image (.loadTexture (asset-manager)
rlm@0 1246 ({:mountain "Textures/Terrain/splat/mountains512.png"
rlm@0 1247 :fortress "Textures/Terrain/splat/fortress512.png"
rlm@0 1248 }type))
rlm@0 1249 heightmap (ImageBasedHeightMap.
rlm@0 1250 (ImageToAwt/convert (.getImage heightmap-image) false true 0))
rlm@0 1251 terrain (do (.load heightmap)
rlm@0 1252 (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
rlm@0 1253 ]
rlm@0 1254
rlm@0 1255 (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
rlm@0 1256 [grass dirt rock]))
rlm@0 1257
rlm@0 1258 (doto mat_terrain
rlm@0 1259 (.setTexture "Tex1" grass)
rlm@0 1260 (.setFloat "Tex1Scale" (float 64))
rlm@0 1261
rlm@0 1262 (.setTexture "Tex2" dirt)
rlm@0 1263 (.setFloat "Tex2Scale" (float 32))
rlm@0 1264
rlm@0 1265 (.setTexture "Tex3" rock)
rlm@0 1266 (.setFloat "Tex3Scale" (float 128))
rlm@0 1267
rlm@0 1268 (.setTexture "Alpha"
rlm@0 1269 (.loadTexture
rlm@0 1270 (asset-manager)
rlm@0 1271 ({:mountain "Textures/Terrain/splat/alphamap.png"
rlm@0 1272 :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
rlm@0 1273
rlm@0 1274 (doto terrain
rlm@0 1275 (.setMaterial mat_terrain)
rlm@0 1276 (.setLocalTranslation 0 -100 0)
rlm@0 1277 (.setLocalScale 2 1 2))))
rlm@0 1278
rlm@0 1279
rlm@0 1280
rlm@0 1281 (defn run-terrain-game [type]
rlm@0 1282 (.start
rlm@0 1283 (world
rlm@0 1284 (Node.)
rlm@0 1285 {}
rlm@0 1286 (partial setup-fn type)
rlm@0 1287 no-op)))
rlm@0 1288 #+end_src
rlm@0 1289
rlm@0 1290
rlm@0 1291
rlm@0 1292 #+srcname: hello-animation
rlm@0 1293 #+begin_src clojure :results silent
rlm@0 1294 (ns hello.animation)
rlm@0 1295 (use 'cortex.world)
rlm@0 1296 (use 'cortex.import)
rlm@0 1297 (use 'clojure.contrib.def)
rlm@0 1298 (cortex.import/mega-import-jme3)
rlm@0 1299 (rlm.rlm-commands/help)
rlm@0 1300 (use '[hello [collision :only [lights]]])
rlm@0 1301
rlm@0 1302 (defn stand
rlm@0 1303 [channel]
rlm@0 1304 (doto channel
rlm@0 1305 (.setAnim "stand" (float 0.5))
rlm@0 1306 (.setLoopMode LoopMode/DontLoop)
rlm@0 1307 (.setSpeed (float 1))))
rlm@0 1308
rlm@0 1309 (defn anim-listener []
rlm@0 1310 (proxy [AnimEventListener] []
rlm@0 1311 (onAnimChange
rlm@0 1312 [control channel animation-name]
rlm@0 1313 (println-repl "RLM --- onAnimChange"))
rlm@0 1314 (onAnimCycleDone
rlm@0 1315 [control channel animation-name]
rlm@0 1316 (if (= animation-name "Walk")
rlm@0 1317 (stand channel)
rlm@0 1318 ))))
rlm@0 1319
rlm@0 1320 (defn setup-fn [channel game]
rlm@0 1321 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 1322 ;; set the color of the sky
rlm@0 1323 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 1324 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 1325 (.setAnim channel "stand")
rlm@0 1326 (doto (.getFlyByCamera game)
rlm@0 1327 (.setMoveSpeed (float 10))
rlm@0 1328 (.setRotationSpeed 1)))
rlm@0 1329
rlm@0 1330 (defn walk [channel]
rlm@0 1331 (println-repl "zzz")
rlm@0 1332 (doto channel
rlm@0 1333 (.setAnim "Walk" (float 0.5))
rlm@0 1334 (.setLoopMode LoopMode/Loop)))
rlm@0 1335
rlm@0 1336
rlm@0 1337 (defn key-map [channel]
rlm@0 1338 {"key-space" (fn [game value]
rlm@0 1339 (if (not value)
rlm@0 1340 (walk channel)))})
rlm@0 1341
rlm@0 1342 (defn player []
rlm@0 1343 (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
rlm@0 1344 control (.getControl model AnimControl)]
rlm@0 1345 (.setLocalScale model (float 0.5))
rlm@0 1346 (.clearListeners control)
rlm@0 1347 (.addListener control (anim-control))
rlm@0 1348 model))
rlm@0 1349
rlm@0 1350
rlm@0 1351
rlm@0 1352 (defn run-anim-game []
rlm@0 1353 (let [ninja (player)
rlm@0 1354 control (.getControl ninja AnimControl)
rlm@0 1355 channel (.createChannel control)]
rlm@0 1356 (.start
rlm@0 1357 (world
rlm@0 1358 ninja
rlm@0 1359 (key-map channel)
rlm@0 1360 (partial setup-fn channel)
rlm@0 1361 no-op))))
rlm@0 1362 #+end_src
rlm@0 1363
rlm@0 1364 *** Hello Materials
rlm@0 1365 #+srcname: material
rlm@0 1366 #+begin_src clojure :results silent
rlm@0 1367 (ns hello.material)
rlm@0 1368 (use 'cortex.world)
rlm@0 1369 (use 'cortex.import)
rlm@0 1370 (use 'clojure.contrib.def)
rlm@0 1371 (cortex.import/mega-import-jme3)
rlm@0 1372 (rlm.rlm-commands/help)
rlm@0 1373
rlm@0 1374 (defn simple-cube []
rlm@0 1375 (box 1 1 1
rlm@0 1376 :position (Vector3f. -3 1.1 0)
rlm@0 1377 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1378 :texture "Interface/Logo/Monkey.jpg"
rlm@0 1379 :physical? false))
rlm@0 1380
rlm@0 1381 (defn leaky-box []
rlm@0 1382 (box 1 1 1
rlm@0 1383 :position (Vector3f. 3 -1 0)
rlm@0 1384 :material "Common/MatDefs/Misc/ColoredTextured.j3md"
rlm@0 1385 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 1386 :color (ColorRGBA. 1 0 1 1)
rlm@0 1387 :physical? false))
rlm@0 1388
rlm@0 1389 (defn transparent-box []
rlm@0 1390 (doto
rlm@0 1391 (box 1 1 0.1
rlm@0 1392 :position Vector3f/ZERO
rlm@0 1393 :name "window frame"
rlm@0 1394 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1395 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 1396 :physical? false)
rlm@0 1397 (-> (.getMaterial)
rlm@0 1398 (.getAdditionalRenderState)
rlm@0 1399 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 1400 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 1401
rlm@0 1402 (defn bumpy-sphere []
rlm@0 1403 (doto
rlm@0 1404 (sphere 2
rlm@0 1405 :position (Vector3f. 0 2 -2)
rlm@0 1406 :name "Shiny rock"
rlm@0 1407 :material "Common/MatDefs/Light/Lighting.j3md"
rlm@0 1408 :texture false
rlm@0 1409 :physical? false)
rlm@0 1410 (-> (.getMesh)
rlm@0 1411 (doto
rlm@0 1412 (.setTextureMode Sphere$TextureMode/Projected)
rlm@0 1413 (TangentBinormalGenerator/generate)))
rlm@0 1414 (-> (.getMaterial)
rlm@0 1415 (doto
rlm@0 1416 (.setTexture "DiffuseMap" (.loadTexture (asset-manager)
rlm@0 1417 "Textures/Terrain/Pond/Pond.png"))
rlm@0 1418 (.setTexture "NormalMap" (.loadTexture (asset-manager)
rlm@0 1419 "Textures/Terrain/Pond/Pond_normal.png"))
rlm@0 1420 (.setFloat "Shininess" (float 5))))
rlm@0 1421 (.rotate (float 1.6) 0 0)))
rlm@0 1422
rlm@0 1423
rlm@0 1424 (defn start-game []
rlm@0 1425 (.start
rlm@0 1426 (world
rlm@0 1427 (let [root (Node.)]
rlm@0 1428 (dorun (map #(.attachChild root %)
rlm@0 1429 [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
rlm@0 1430 root)
rlm@0 1431 {}
rlm@0 1432 (fn [world]
rlm@0 1433 (let [sun (doto (DirectionalLight.)
rlm@0 1434 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 1435 (.setColor ColorRGBA/White))]
rlm@0 1436 (.addLight (.getRootNode world) sun)))
rlm@0 1437 no-op
rlm@0 1438 )))
rlm@0 1439 #+end_src
rlm@0 1440
rlm@0 1441
rlm@0 1442
rlm@0 1443 * The Body
rlm@0 1444 ** Eyes
rlm@0 1445
rlm@0 1446 Ultimately I want to make creatures with eyes. Each eye can be
rlm@0 1447 independely moved and should see its own version of the world
rlm@0 1448 depending on where it is.
rlm@0 1449 #+srcname: eyes
rlm@0 1450 #+begin_src clojure
rlm@0 1451 (ns body.eye)
rlm@0 1452 (use 'cortex.world)
rlm@0 1453 (use 'cortex.import)
rlm@0 1454 (use 'clojure.contrib.def)
rlm@0 1455 (cortex.import/mega-import-jme3)
rlm@0 1456 (rlm.rlm-commands/help)
rlm@0 1457 (import java.nio.ByteBuffer)
rlm@0 1458 (import java.awt.image.BufferedImage)
rlm@0 1459 (import java.awt.Color)
rlm@0 1460 (import java.awt.Dimension)
rlm@0 1461 (import java.awt.Graphics)
rlm@0 1462 (import java.awt.Graphics2D)
rlm@0 1463 (import java.awt.event.WindowAdapter)
rlm@0 1464 (import java.awt.event.WindowEvent)
rlm@0 1465 (import java.awt.image.BufferedImage)
rlm@0 1466 (import java.nio.ByteBuffer)
rlm@0 1467 (import javax.swing.JFrame)
rlm@0 1468 (import javax.swing.JPanel)
rlm@0 1469 (import javax.swing.SwingUtilities)
rlm@0 1470 (import javax.swing.ImageIcon)
rlm@0 1471 (import javax.swing.JOptionPane)
rlm@0 1472 (import java.awt.image.ImageObserver)
rlm@0 1473
rlm@0 1474
rlm@0 1475
rlm@0 1476 (defn scene-processor
rlm@0 1477 "deals with converting FrameBuffers to BufferedImages so
rlm@0 1478 that the continuation function can be defined only in terms
rlm@0 1479 of what it does with BufferedImages"
rlm@0 1480 [continuation]
rlm@0 1481 (let [byte-buffer (atom nil)
rlm@0 1482 renderer (atom nil)
rlm@0 1483 image (atom nil)]
rlm@0 1484 (proxy [SceneProcessor] []
rlm@0 1485 (initialize
rlm@0 1486 [renderManager viewPort]
rlm@0 1487 (let [cam (.getCamera viewPort)
rlm@0 1488 width (.getWidth cam)
rlm@0 1489 height (.getHeight cam)]
rlm@0 1490 (reset! renderer (.getRenderer renderManager))
rlm@0 1491 (reset! byte-buffer
rlm@0 1492 (BufferUtils/createByteBuffer
rlm@0 1493 (* width height 4)))
rlm@0 1494 (reset! image (BufferedImage. width height
rlm@0 1495 BufferedImage/TYPE_4BYTE_ABGR))))
rlm@0 1496 (isInitialized [] (not (nil? @byte-buffer)))
rlm@0 1497 (reshape [_ _ _])
rlm@0 1498 (preFrame [_])
rlm@0 1499 (postQueue [_])
rlm@0 1500 (postFrame
rlm@0 1501 [#^FrameBuffer fb]
rlm@0 1502 (.clear @byte-buffer)
rlm@0 1503 (.readFrameBuffer @renderer fb @byte-buffer)
rlm@0 1504 (Screenshots/convertScreenShot @byte-buffer @image)
rlm@0 1505 (continuation @image))
rlm@0 1506 (cleanup []))))
rlm@0 1507
rlm@0 1508 (defn add-eye
rlm@0 1509 "Add an eye to the world, and call continuation on
rlm@0 1510 every frame produced"
rlm@0 1511 [world camera continuation]
rlm@0 1512 (let [width (.getWidth camera)
rlm@0 1513 height (.getHeight camera)
rlm@0 1514 render-manager (.getRenderManager world)
rlm@0 1515 viewport (.createMainView render-manager "eye-view" camera)]
rlm@0 1516 (doto viewport
rlm@0 1517 (.setBackgroundColor ColorRGBA/Black)
rlm@0 1518 (.setClearFlags true true true)
rlm@0 1519 (.addProcessor (scene-processor continuation))
rlm@0 1520 (.attachScene (.getRootNode world)))))
rlm@0 1521
rlm@0 1522 (defn make-display-frame [display width height]
rlm@0 1523 (SwingUtilities/invokeLater
rlm@0 1524 (fn []
rlm@0 1525 (.setPreferredSize display (Dimension. width height))
rlm@0 1526 (doto (JFrame. "Eye Camera!")
rlm@0 1527 (-> (.getContentPane) (.add display))
rlm@0 1528 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
rlm@0 1529 (.pack)
rlm@0 1530 (.setLocationRelativeTo nil)
rlm@0 1531 (.setResizable false)
rlm@0 1532 (.setVisible true)))))
rlm@0 1533
rlm@0 1534 (defn image-monitor [#^BufferedImage image]
rlm@0 1535 (proxy [JPanel] []
rlm@0 1536 (paintComponent
rlm@0 1537 [g]
rlm@0 1538 (proxy-super paintComponent g)
rlm@0 1539 (locking image
rlm@0 1540 (.drawImage g image 0 0
rlm@0 1541 (proxy [ImageObserver]
rlm@0 1542 []
rlm@0 1543 (imageUpdate
rlm@0 1544 []
rlm@0 1545 (proxy-super imageUpdate))))))))
rlm@0 1546
rlm@0 1547 (defn movie-image []
rlm@0 1548 (let [setup
rlm@0 1549 (runonce
rlm@0 1550 (fn [#^BufferedImage image]
rlm@0 1551 (let [width (.getWidth image)
rlm@0 1552 height (.getHeight image)
rlm@0 1553 display (image-monitor image)
rlm@0 1554 frame (make-display-frame display width height)]
rlm@0 1555 display)))]
rlm@0 1556 (fn [#^BufferedImage image]
rlm@0 1557 (.repaint (setup image)))))
rlm@0 1558
rlm@0 1559
rlm@0 1560 (defn observer
rlm@0 1561 "place thy eye!"
rlm@0 1562 [world camera]
rlm@0 1563 (let [eye camera
rlm@0 1564 width (.getWidth eye)
rlm@0 1565 height (.getHeight eye)]
rlm@0 1566 (no-exceptions
rlm@0 1567 (add-eye
rlm@0 1568 world
rlm@0 1569 eye
rlm@0 1570 (movie-image)))))
rlm@0 1571 #+end_src
rlm@0 1572
rlm@0 1573 #+srcname: test-vision
rlm@0 1574 #+begin_src clojure
rlm@0 1575
rlm@0 1576 (ns test.vision)
rlm@0 1577 (use 'cortex.world)
rlm@0 1578 (use 'cortex.import)
rlm@0 1579 (use 'clojure.contrib.def)
rlm@0 1580 (use 'body.eye)
rlm@0 1581 (cortex.import/mega-import-jme3)
rlm@0 1582 (rlm.rlm-commands/help)
rlm@0 1583 (import java.nio.ByteBuffer)
rlm@0 1584 (import java.awt.image.BufferedImage)
rlm@0 1585 (import java.awt.Color)
rlm@0 1586 (import java.awt.Dimension)
rlm@0 1587 (import java.awt.Graphics)
rlm@0 1588 (import java.awt.Graphics2D)
rlm@0 1589 (import java.awt.event.WindowAdapter)
rlm@0 1590 (import java.awt.event.WindowEvent)
rlm@0 1591 (import java.awt.image.BufferedImage)
rlm@0 1592 (import java.nio.ByteBuffer)
rlm@0 1593 (import javax.swing.JFrame)
rlm@0 1594 (import javax.swing.JPanel)
rlm@0 1595 (import javax.swing.SwingUtilities)
rlm@0 1596 (import javax.swing.ImageIcon)
rlm@0 1597 (import javax.swing.JOptionPane)
rlm@0 1598 (import java.awt.image.ImageObserver)
rlm@0 1599
rlm@0 1600
rlm@0 1601 (def width 200)
rlm@0 1602 (def height 200)
rlm@0 1603
rlm@0 1604 (defn camera []
rlm@0 1605 (doto (Camera. width height)
rlm@0 1606 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1607 (.setLocation (Vector3f. -3 0 -5))
rlm@0 1608 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1609
rlm@0 1610 (defn camera2 []
rlm@0 1611 (doto (Camera. width height)
rlm@0 1612 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1613 (.setLocation (Vector3f. 3 0 -5))
rlm@0 1614 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1615
rlm@0 1616 (defn setup-fn [world]
rlm@0 1617 (let [eye (camera)
rlm@0 1618 width (.getWidth eye)
rlm@0 1619 height (.getHeight eye)]
rlm@0 1620 (no-exceptions
rlm@0 1621 (add-eye
rlm@0 1622 world
rlm@0 1623 eye
rlm@0 1624 (runonce visual))
rlm@0 1625 (add-eye
rlm@0 1626 world
rlm@0 1627 (camera2)
rlm@0 1628 (runonce visual)))))
rlm@0 1629
rlm@0 1630 (defn spider-eye [position]
rlm@0 1631 (doto (Camera. 200 200 )
rlm@0 1632 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1633 (.setLocation position)
rlm@0 1634 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1635
rlm@0 1636 (defn setup-fn* [world]
rlm@0 1637 (let [eye (camera)
rlm@0 1638 width (.getWidth eye)
rlm@0 1639 height (.getHeight eye)]
rlm@0 1640 ;;(.setClearFlags (.getViewPort world) true true true)
rlm@0 1641 (observer world (.getCamera world))
rlm@0 1642 (observer world (spider-eye (Vector3f. 3 0 -5)))
rlm@0 1643 ;;(observer world (spider-eye (Vector3f. 0 0 -5)))
rlm@0 1644 ;; (observer world (spider-eye (Vector3f. -3 0 -5)))
rlm@0 1645 ;; (observer world (spider-eye (Vector3f. 0 3 -5)))
rlm@0 1646 ;; (observer world (spider-eye (Vector3f. 0 -3 -5)))
rlm@0 1647 ;; (observer world (spider-eye (Vector3f. 3 3 -5)))
rlm@0 1648 ;; (observer world (spider-eye (Vector3f. -3 3 -5)))
rlm@0 1649 ;; (observer world (spider-eye (Vector3f. 3 -3 -5)))
rlm@0 1650 ;; (observer world (spider-eye (Vector3f. -3 -3 -5)))
rlm@0 1651
rlm@0 1652 )
rlm@0 1653 world)
rlm@0 1654
rlm@0 1655 (defn test-world []
rlm@0 1656 (let [thing (box 1 1 1 :physical? false)]
rlm@0 1657 (world
rlm@0 1658 (doto (Node.)
rlm@0 1659 (.attachChild thing))
rlm@0 1660 {}
rlm@0 1661 setup-fn
rlm@0 1662 (fn [world tpf]
rlm@0 1663 (.rotate thing (* tpf 0.2) 0 0)
rlm@0 1664 ))))
rlm@0 1665
rlm@0 1666
rlm@0 1667 #+end_src
rlm@0 1668
rlm@0 1669
rlm@0 1670 #+results: eyes
rlm@0 1671 : #'body.eye/test-world
rlm@0 1672
rlm@0 1673 Note the use of continuation passing style for connecting the eye to a
rlm@0 1674 function to process the output. The example code will create two
rlm@0 1675 videos of the same rotating cube from different angles, sutiable for
rlm@0 1676 stereoscopic vision.
rlm@0 1677
rlm@0 1678
rlm@0 1679
rlm@0 1680
rlm@0 1681
rlm@0 1682
rlm@0 1683 * COMMENT code generation
rlm@0 1684 #+begin_src clojure :tangle ../src/cortex/import.clj
rlm@0 1685 <<import>>
rlm@0 1686 #+end_src
rlm@0 1687
rlm@0 1688 #+begin_src clojure :tangle ../src/hello/brick_wall.clj
rlm@0 1689 <<brick-wall-header>>
rlm@0 1690 <<brick-wall-body>>
rlm@0 1691 #+end_src
rlm@0 1692
rlm@0 1693 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
rlm@0 1694 <<hello-simple-app>>
rlm@0 1695 #+end_src
rlm@0 1696
rlm@0 1697 #+begin_src clojure :tangle ../src/cortex/world.clj
rlm@0 1698 <<world-inputs>>
rlm@0 1699 <<world>>
rlm@0 1700 <<world-shapes>>
rlm@0 1701 <<world-view>>
rlm@0 1702 #+end_src
rlm@0 1703
rlm@0 1704 #+begin_src clojure :tangle ../src/cortex/other_games.clj
rlm@0 1705 <<other-games>>
rlm@0 1706 #+end_src
rlm@0 1707
rlm@0 1708 #+begin_src clojure :tangle ../src/hello/loop.clj
rlm@0 1709 <<hello-loop>>
rlm@0 1710 #+end_src
rlm@0 1711
rlm@0 1712 #+begin_src clojure :tangle ../src/hello/collision.clj
rlm@0 1713 <<hello-collision>>
rlm@0 1714 #+end_src
rlm@0 1715
rlm@0 1716 #+begin_src clojure :tangle ../src/hello/terrain.clj
rlm@0 1717 <<hello-terrain>>
rlm@0 1718 #+end_src
rlm@0 1719
rlm@0 1720 #+begin_src clojure :tangle ../src/hello/animation.clj
rlm@0 1721 <<hello-animation>>
rlm@0 1722 #+end_src
rlm@0 1723
rlm@0 1724 #+begin_src clojure :tangle ../src/hello/material.clj
rlm@0 1725 <<material>>
rlm@0 1726 #+end_src
rlm@0 1727
rlm@0 1728 #+begin_src clojure :tangle ../src/body/eye.clj
rlm@0 1729 <<eyes>>
rlm@0 1730 #+end_src
rlm@0 1731
rlm@0 1732 #+begin_src clojure :tangle ../src/test/vision.clj
rlm@0 1733 <<test-vision>>
rlm@0 1734 #+end_src
rlm@0 1735
rlm@0 1736
rlm@0 1737