annotate org/cortex.org @ 7:9ccfbcbed90a

refactored to help fix ray casting problem
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 00:49:33 -0700
parents e3c6d1c1cb00
children c32f3eb9fdeb
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@6 7 #+babel: :mkdirp yes :noweb yes
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@6 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@6 600 (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))
rlm@6 601 mat (Material. asset-manager (:material d))
rlm@0 602 geom (Geometry. (:name d) (:shape d))]
rlm@0 603 (if (:texture d)
rlm@0 604 (let [key (TextureKey. (:texture d))]
rlm@0 605 (.setGenerateMips key true)
rlm@6 606 (.setTexture mat "ColorMap" (.loadTexture asset-manager key))))
rlm@0 607 (if (:color d) (.setColor mat "Color" (:color d)))
rlm@0 608 (.setMaterial geom mat)
rlm@0 609 (if-let [rotation (:rotation d)] (.rotate geom rotation))
rlm@0 610 (.setLocalTranslation geom (:position d))
rlm@0 611 (if (:physical? d)
rlm@6 612 (let [impact-shape (doto (GImpactCollisionShape.
rlm@6 613 (.getMesh geom)) (.setMargin 0))
rlm@0 614 physics-control (RigidBodyControl.
rlm@6 615 ;;impact-shape ;; comment to disable
rlm@0 616 (float (:mass d)))]
rlm@0 617 (.createJmeMesh impact-shape)
rlm@0 618 (.addControl geom physics-control)
rlm@0 619 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
rlm@0 620 (.setFriction physics-control (:friction d))))
rlm@0 621 ;;the default is to keep this node in the physics engine forever.
rlm@0 622 ;;these commands must come after the control is added to the geometry.
rlm@0 623 ;;
rlm@0 624 geom))
rlm@0 625
rlm@0 626 (defn box
rlm@0 627 ([l w h & {:as options}]
rlm@0 628 (let [options (merge base-shape options)]
rlm@0 629 (make-shape (assoc options
rlm@0 630 :shape (Box. l w h)))))
rlm@0 631 ([] (box 0.5 0.5 0.5)))
rlm@0 632
rlm@0 633 (defn sphere
rlm@0 634 ([r & {:as options}]
rlm@0 635 (let [options (merge base-shape options)]
rlm@0 636 (make-shape (assoc options
rlm@0 637 :shape (Sphere. 32 32 (float r))))))
rlm@0 638 ([] (sphere 0.5)))
rlm@0 639
rlm@0 640 (defn add-element [game node]
rlm@0 641 (.addAll
rlm@0 642 (.getPhysicsSpace
rlm@0 643 (.getState
rlm@0 644 (.getStateManager game)
rlm@0 645 BulletAppState))
rlm@0 646 node)
rlm@0 647 (.attachChild (.getRootNode game) node))
rlm@0 648
rlm@0 649 (defn set-gravity*
rlm@0 650 [game gravity]
rlm@0 651 (traverse
rlm@0 652 (fn [geom]
rlm@0 653 (if-let
rlm@0 654 [control (.getControl geom RigidBodyControl)]
rlm@0 655 (do
rlm@0 656 (.setGravity control gravity)
rlm@0 657 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
rlm@0 658 )))
rlm@0 659 (.getRootNode game)))
rlm@0 660
rlm@0 661 #+end_src
rlm@0 662
rlm@0 663 These are convienence functions for creating JME objects and
rlm@0 664 manipulating a world.
rlm@0 665
rlm@0 666 #+srcname: world-view
rlm@0 667 #+begin_src clojure :results silent
rlm@0 668 (in-ns 'cortex.world)
rlm@0 669
rlm@0 670 (defprotocol Viewable
rlm@0 671 (view [something]))
rlm@0 672
rlm@0 673 (extend-type com.jme3.scene.Geometry
rlm@0 674 Viewable
rlm@0 675 (view [geo]
rlm@0 676 (view (doto (Node.)(.attachChild geo)))))
rlm@0 677
rlm@0 678 (extend-type com.jme3.scene.Node
rlm@0 679 Viewable
rlm@0 680 (view [node]
rlm@0 681 (.start
rlm@0 682 (world node
rlm@0 683 {}
rlm@0 684 (fn [world]
rlm@0 685 (.enableDebug
rlm@0 686 (.getPhysicsSpace
rlm@0 687 (.getState
rlm@0 688 (.getStateManager world)
rlm@0 689 BulletAppState))
rlm@0 690 (asset-manager))
rlm@0 691 (set-gravity* world Vector3f/ZERO)
rlm@0 692 ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0))
rlm@0 693 (let [sun (doto (DirectionalLight.)
rlm@0 694 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 695 (.setColor ColorRGBA/White))]
rlm@0 696 (.addLight (.getRootNode world) sun)))
rlm@0 697 no-op))))
rlm@0 698
rlm@0 699 (defn position-camera [game]
rlm@0 700 (doto (.getCamera game)
rlm@0 701 (.setLocation (Vector3f. 0 6 6))
rlm@0 702 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
rlm@0 703
rlm@0 704 #+end_src
rlm@0 705
rlm@0 706 Here I make the =Viewable= protocol and extend it to JME's types. Now
rlm@0 707 hello-world can be written as easily as:
rlm@0 708
rlm@0 709 #+begin_src clojure :results silent
rlm@0 710 (cortex.world/view (cortex.world/box))
rlm@0 711 #+end_src
rlm@0 712
rlm@0 713 ** Hello
rlm@0 714 Here are the jmonkeyengine "Hello" programs translated to clojure.
rlm@0 715 *** Hello Simple App
rlm@0 716 Here is the hello world example for jme3 in clojure.
rlm@0 717 It's a more or less direct translation from the java source
rlm@0 718 from
rlm@0 719 http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication.
rlm@0 720
rlm@0 721 Of note is the fact that since we don't have access to the
rlm@0 722 =AssetManager= via extendig =SimpleApplication=, we have to build one
rlm@0 723 ourselves.
rlm@0 724
rlm@0 725 #+srcname: hello-simple-app
rlm@0 726 #+begin_src clojure :results silent
rlm@0 727 (ns hello.hello-simple-app)
rlm@0 728 (require 'cortex.import)
rlm@0 729 (use 'clojure.contrib.def)
rlm@0 730 (rlm.rlm-commands/help)
rlm@0 731 (cortex.import/import-jme3)
rlm@0 732 (use 'cortex.world)
rlm@0 733
rlm@0 734
rlm@0 735 (def cube (Box. Vector3f/ZERO 1 1 1))
rlm@0 736
rlm@0 737 (def geom (Geometry. "Box" cube))
rlm@0 738
rlm@0 739 (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
rlm@0 740
rlm@0 741 (.setColor mat "Color" ColorRGBA/Blue)
rlm@0 742
rlm@0 743 (.setMaterial geom mat)
rlm@0 744
rlm@0 745 (defn simple-app []
rlm@0 746 (doto
rlm@0 747 (proxy [SimpleApplication] []
rlm@0 748 (simpleInitApp
rlm@0 749 []
rlm@0 750 ;; Don't take control of the mouse
rlm@0 751 (org.lwjgl.input.Mouse/setGrabbed false)
rlm@0 752 (.attachChild (.getRootNode this) geom)))
rlm@0 753 ;; don't show a menu to change options.
rlm@0 754 (.setShowSettings false)
rlm@0 755 (.setPauseOnLostFocus false)
rlm@0 756 (.setSettings *app-settings*)))
rlm@0 757 #+end_src
rlm@0 758
rlm@0 759 Running this program will begin a new jMonkeyEngine game which
rlm@0 760 displays a single blue cube.
rlm@0 761
rlm@0 762 #+begin_src clojure :exports code :results silent
rlm@0 763 (.start (hello.hello-simple-app/simple-app))
rlm@0 764 #+end_src
rlm@0 765
rlm@0 766 #+caption: the simplest JME game.
rlm@0 767 [[./images/simple-app.jpg]]
rlm@0 768
rlm@0 769
rlm@0 770
rlm@0 771 *** Hello Physics
rlm@0 772 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
rlm@0 773
rlm@0 774 #+srcname: brick-wall-header
rlm@0 775 #+begin_src clojure :results silent
rlm@0 776 (ns hello.brick-wall)
rlm@0 777 (require 'cortex.import)
rlm@0 778 (use 'clojure.contrib.def)
rlm@0 779 (rlm.rlm-commands/help)
rlm@0 780 (cortex.import/mega-import-jme3)
rlm@0 781 (use '[pokemon [lpsolve :only [constant-map]]])
rlm@0 782 (use 'cortex.world)
rlm@0 783 #+end_src
rlm@0 784
rlm@0 785 #+srcname: brick-wall-body
rlm@0 786 #+begin_src clojure :results silent
rlm@0 787 (in-ns 'hello.brick-wall)
rlm@0 788
rlm@0 789 (defn floor
rlm@0 790 "make a sturdy, unmovable physical floor"
rlm@0 791 []
rlm@0 792 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
rlm@0 793
rlm@0 794 (def brick-length 0.48)
rlm@0 795 (def brick-width 0.24)
rlm@0 796 (def brick-height 0.12)
rlm@0 797
rlm@0 798
rlm@0 799 (defn brick* [position]
rlm@0 800 (doto (box brick-length brick-height brick-width
rlm@0 801 :position position :name "brick"
rlm@0 802 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 803 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 804 :mass 36)
rlm@0 805 (->
rlm@0 806 (.getMesh)
rlm@0 807 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@0 808 ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 809 )
rlm@0 810 )
rlm@0 811
rlm@0 812 (defn inception-brick-wall
rlm@0 813 "construct a physical brick wall"
rlm@0 814 []
rlm@0 815 (let [node (Node. "brick-wall")]
rlm@0 816 (dorun
rlm@0 817 (map (comp #(.attachChild node %) brick*)
rlm@0 818 (for
rlm@0 819 [x (range 15)
rlm@0 820 y (range 10)
rlm@0 821 z (range 1)]
rlm@0 822 (Vector3f.
rlm@0 823 (* brick-length x 1.03)
rlm@0 824 (* brick-width y y 10)
rlm@0 825 (* brick-height z)))))
rlm@0 826 node))
rlm@0 827
rlm@0 828 (defn gravity-toggle
rlm@0 829 [new-value]
rlm@0 830 (fn [game value]
rlm@0 831 (println-repl "set gravity to " new-value)
rlm@0 832 (if value
rlm@0 833 (set-gravity* game new-value)
rlm@0 834 (set-gravity* game gravity))))
rlm@0 835
rlm@0 836 (defn fire-cannon-ball []
rlm@0 837 (fn [game value]
rlm@0 838 (if (not value)
rlm@0 839 (let [camera (.getCamera game)
rlm@0 840 cannon-ball
rlm@0 841 (sphere 0.7
rlm@0 842 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 843 :texture "Textures/PokeCopper.jpg"
rlm@0 844 :position
rlm@0 845 (.add (.getLocation camera)
rlm@0 846 (.mult (.getDirection camera) (float 1)))
rlm@0 847 :mass 3)] ;200 0.05
rlm@0 848 (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive)
rlm@0 849 (.setLinearVelocity
rlm@0 850 (.getControl cannon-ball RigidBodyControl)
rlm@0 851 (.mult (.getDirection camera) (float 50))) ;50
rlm@0 852 (add-element game cannon-ball)))))
rlm@0 853
rlm@0 854 (defn floor* []
rlm@0 855 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@0 856 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 857 :texture "Textures/Terrain/Pond/Pond.png"
rlm@0 858 :position (Vector3f. 0 -0.1 0 )
rlm@0 859 :mass 0)
rlm@0 860 (->
rlm@0 861 (.getMesh)
rlm@0 862 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@0 863 (->
rlm@0 864 (.getMaterial)
rlm@0 865 (.getTextureParam "ColorMap")
rlm@0 866 (.getTextureValue)
rlm@0 867 (.setWrap Texture$WrapMode/Repeat))
rlm@0 868 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@0 869 ))
rlm@0 870
rlm@0 871 (defn brick-wall* []
rlm@0 872 (let [node (Node. "brick-wall")]
rlm@0 873 (dorun
rlm@0 874 (map
rlm@0 875 (comp #(.attachChild node %) brick*)
rlm@0 876 (for [y (range 15)
rlm@0 877 x (range 4)
rlm@0 878 z (range 1)]
rlm@0 879 (Vector3f.
rlm@0 880 (+ (* 2 x brick-length)
rlm@0 881 (if (even? (+ y z))
rlm@0 882 (/ brick-length 4) (/ brick-length -4)))
rlm@0 883 (+ (* brick-height (inc (* 2 y))))
rlm@0 884 (* 2 z brick-width) ))))
rlm@0 885 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@0 886 node))
rlm@0 887
rlm@0 888 (defn brick-wall-game-run []
rlm@0 889 (doto
rlm@0 890 (world
rlm@0 891 (doto (Node.) (.attachChild (floor*))
rlm@0 892 (.attachChild (brick-wall*))
rlm@0 893 )
rlm@0 894 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 895 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 896 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 897 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 898 "key-k" (gravity-toggle Vector3f/ZERO)
rlm@0 899 "key-u" (gravity-toggle (Vector3f. 0 9.81 0))
rlm@0 900 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 901 "key-f" (fn[game value]
rlm@0 902 (if (not value) (add-element game (brick-wall*))))
rlm@0 903 "key-return" (fire-cannon-ball)}
rlm@0 904 position-camera
rlm@0 905 (fn [& _]))
rlm@0 906 (.start)))
rlm@0 907 #+end_src
rlm@0 908
rlm@0 909 #+begin_src clojure :results silent
rlm@0 910 (hello.brick-wall/brick-wall-game-run)
rlm@0 911 #+end_src
rlm@0 912
rlm@0 913 #+caption: the brick wall standing
rlm@0 914 [[./images/brick-wall-standing.jpg]]
rlm@0 915
rlm@0 916 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
rlm@0 917 [[./images/brick-wall-knocked-down.jpg]]
rlm@0 918
rlm@0 919 *** Other Brick Games
rlm@0 920 #+srcname: other-games
rlm@0 921 #+begin_src clojure :results silent
rlm@0 922 (ns cortex.other-games
rlm@0 923 {:author "Dylan Holmes"})
rlm@0 924 (use 'cortex.world)
rlm@0 925 (use 'hello.brick-wall)
rlm@0 926 (use 'cortex.import)
rlm@0 927 (cortex.import/mega-import-jme3)
rlm@0 928
rlm@0 929 (defn scad [position]
rlm@0 930 (doto (box 0.1 0.1 0.1
rlm@0 931 :position position :name "brick"
rlm@0 932 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 933 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 934 :mass 20)
rlm@0 935 (->
rlm@0 936 (.getMesh)
rlm@0 937 (.scaleTextureCoordinates (Vector2f. 1 0.5))
rlm@0 938 )
rlm@0 939 (-> (.getControl RigidBodyControl)
rlm@0 940 (.setLinearVelocity (Vector3f. 0 100 0))
rlm@0 941 )
rlm@0 942
rlm@0 943 ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
rlm@0 944 ))
rlm@0 945
rlm@0 946
rlm@0 947 (defn shrapnel []
rlm@0 948 (let [node (Node. "explosion-day")]
rlm@0 949 (dorun
rlm@0 950 (map
rlm@0 951 (comp #(.attachChild node %) scad)
rlm@0 952 (for [y (range 15)
rlm@0 953 x (range 4)
rlm@0 954 z (range 1)]
rlm@0 955 (Vector3f.
rlm@0 956 (+ (* 2 x brick-height)
rlm@0 957 (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
rlm@0 958 (+ (* brick-height (inc (* 2 y))))
rlm@0 959 (* 2 z brick-height) ))))
rlm@0 960 node))
rlm@0 961
rlm@0 962
rlm@0 963 (def domino-height 0.48)
rlm@0 964 (def domino-thickness 0.12)
rlm@0 965 (def domino-width 0.24)
rlm@0 966
rlm@0 967 (def domino-thickness 0.05)
rlm@0 968 (def domino-width 0.5)
rlm@0 969 (def domino-height 1)
rlm@0 970
rlm@0 971 (defn domino
rlm@0 972 ([position]
rlm@0 973 (domino position (Quaternion/IDENTITY)))
rlm@0 974 ([position rotation]
rlm@0 975 (doto (box domino-width domino-height domino-thickness
rlm@0 976 :position position :name "domino"
rlm@0 977 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 978 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 979 :mass 1
rlm@0 980 :rotation rotation)
rlm@0 981 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 982 )))
rlm@0 983
rlm@0 984
rlm@0 985 (defn domino-row []
rlm@0 986 (let [node (Node. "domino-row")]
rlm@0 987 (dorun
rlm@0 988 (map
rlm@0 989 (comp #(.attachChild node %) domino)
rlm@0 990 (for [
rlm@0 991 z (range 10)
rlm@0 992 x (range 5)
rlm@0 993 ]
rlm@0 994 (Vector3f.
rlm@0 995 (+ (* z domino-width) (* x 5 domino-width))
rlm@0 996 (/ domino-height 1)
rlm@0 997 (* -5.5 domino-thickness z) ))))
rlm@0 998
rlm@0 999 node))
rlm@0 1000
rlm@0 1001 (defn domino-cycle []
rlm@0 1002 (let [node (Node. "domino-cycle")]
rlm@0 1003 (dorun
rlm@0 1004 (map
rlm@0 1005 (comp #(.attachChild node %) (partial apply domino) )
rlm@0 1006 (for [n (range 720)]
rlm@0 1007 (let [space (* domino-height 5.5)
rlm@0 1008 r (fn[n] (* (+ n 3) domino-width 0.5))
rlm@0 1009 t (fn[n] (reduce
rlm@0 1010 +
rlm@0 1011 (map
rlm@0 1012 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
rlm@0 1013 (range n))))
rlm@0 1014 t (t n)
rlm@0 1015 r (r n)
rlm@0 1016 ct (Math/cos t)
rlm@0 1017 st (Math/sin t)
rlm@0 1018 ]
rlm@0 1019 (list
rlm@0 1020 (Vector3f.
rlm@0 1021 (* -1 r st)
rlm@0 1022 (/ domino-height 1)
rlm@0 1023 (* r ct))
rlm@0 1024 (.fromAngleAxis (Quaternion.)
rlm@0 1025 (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
rlm@0 1026 )))
rlm@0 1027 ))
rlm@0 1028 node))
rlm@0 1029
rlm@0 1030
rlm@0 1031 (defn domino-game-run []
rlm@0 1032 (doto
rlm@0 1033 (world
rlm@0 1034 (doto (Node.) (.attachChild (floor*))
rlm@0 1035 )
rlm@0 1036 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 1037 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 1038 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 1039 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 1040 "key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
rlm@0 1041 "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
rlm@0 1042 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 1043
rlm@0 1044 "key-space"
rlm@0 1045 (fn[game value]
rlm@0 1046
rlm@0 1047 (if (not value)
rlm@0 1048 (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
rlm@0 1049 (.fromAngleAxis (Quaternion.)
rlm@0 1050 (/ Math/PI 2) (Vector3f. 0 1 0)))]
rlm@0 1051 (add-element game d))))
rlm@0 1052 "key-f"
rlm@0 1053 (fn[game value](if (not value) (add-element game (domino-cycle))))
rlm@0 1054 "key-return" (fire-cannon-ball)}
rlm@0 1055 position-camera
rlm@0 1056 (fn [& _]))
rlm@0 1057 (.start)))
rlm@0 1058 #+end_src
rlm@0 1059
rlm@0 1060 #+begin_src clojure :results silent
rlm@0 1061 (cortex.other-games/domino-game-run)
rlm@0 1062 #+end_src
rlm@0 1063
rlm@0 1064 #+caption: floating dominos
rlm@0 1065 [[./images/dominos.jpg]]
rlm@0 1066
rlm@0 1067 *** Hello Loop
rlm@0 1068 #+srcname: hello-loop
rlm@0 1069 #+begin_src clojure :results silent
rlm@0 1070 (ns hello.loop)
rlm@0 1071 (use 'cortex.world)
rlm@0 1072 (use 'cortex.import)
rlm@0 1073 (cortex.import/mega-import-jme3)
rlm@0 1074 (rlm.rlm-commands/help)
rlm@0 1075
rlm@0 1076 (defn blue-cube []
rlm@0 1077 (box 1 1 1
rlm@0 1078 :color ColorRGBA/Blue
rlm@0 1079 :texture false
rlm@0 1080 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1081 :name "blue-cube"
rlm@0 1082 :physical? false))
rlm@0 1083
rlm@0 1084 (defn blue-cube-game []
rlm@0 1085 (let [cube (blue-cube)
rlm@0 1086 root (doto (Node.) (.attachChild cube))]
rlm@0 1087 (world root
rlm@0 1088 {}
rlm@0 1089 no-op
rlm@0 1090 (fn [game tpf]
rlm@0 1091 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
rlm@0 1092 #+end_src
rlm@0 1093
rlm@0 1094 *** Hello Collision
rlm@0 1095
rlm@0 1096 #+srcname: hello-collision
rlm@0 1097 #+begin_src clojure :results silent
rlm@0 1098 (ns hello.collision)
rlm@0 1099 (use 'cortex.world)
rlm@0 1100 (use 'cortex.import)
rlm@0 1101 (use 'clojure.contrib.def)
rlm@0 1102
rlm@0 1103
rlm@0 1104 (cortex.import/mega-import-jme3)
rlm@0 1105 (rlm.rlm-commands/help)
rlm@0 1106 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 1107
rlm@0 1108
rlm@0 1109 (defn environment []
rlm@0 1110 (let
rlm@0 1111 [scene-model
rlm@0 1112 (doto
rlm@0 1113 (.loadModel
rlm@0 1114 (doto (asset-manager)
rlm@0 1115 (.registerLocator
rlm@0 1116 "/home/r/cortex/assets/zips/town.zip" ZipLocator))
rlm@0 1117 "main.scene")
rlm@0 1118 (.setLocalScale (float 2.0)))
rlm@0 1119 collision-shape
rlm@0 1120 (CollisionShapeFactory/createMeshShape #^Node scene-model)
rlm@0 1121 landscape (RigidBodyControl. collision-shape 0)]
rlm@0 1122 (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
rlm@0 1123 (.addControl scene-model landscape)
rlm@0 1124 scene-model))
rlm@0 1125
rlm@0 1126 (defn player-fn []
rlm@0 1127 (doto
rlm@0 1128 (CharacterControl.
rlm@0 1129 (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
rlm@0 1130 (float 0.05))
rlm@0 1131 (.setJumpSpeed 20)
rlm@0 1132 (.setFallSpeed 30)
rlm@0 1133 (.setGravity 30) ;30
rlm@0 1134 (.setPhysicsLocation (Vector3f. 0 10 0))))
rlm@0 1135
rlm@0 1136 (defn lights []
rlm@0 1137 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
rlm@0 1138 (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
rlm@0 1139 (doto (DirectionalLight.)
rlm@0 1140 (.setColor (.mult ColorRGBA/White (float 0.9) ))
rlm@0 1141 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 1142
rlm@0 1143 (defn night-lights []
rlm@0 1144 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
rlm@0 1145 (doto (DirectionalLight.)
rlm@0 1146 (.setColor (.mult ColorRGBA/White (float 0.2) ))
rlm@0 1147 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 1148
rlm@0 1149 (def player (atom (player-fn)))
rlm@0 1150
rlm@0 1151 (defn setup-fn [game]
rlm@0 1152 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 1153 ;; set the color of the sky
rlm@0 1154 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 1155 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 1156 (doto (.getFlyByCamera game)
rlm@0 1157 (.setMoveSpeed (float 100))
rlm@0 1158 (.setRotationSpeed 3))
rlm@0 1159 (.add
rlm@0 1160 (.getPhysicsSpace
rlm@0 1161 (.getState (.getStateManager game) BulletAppState))
rlm@0 1162 @player)
rlm@0 1163
rlm@0 1164 (doto (Node.) (.attachChild (.getRootNode game))
rlm@0 1165 (.attachChild (brick-wall*))
rlm@0 1166 )
rlm@0 1167
rlm@0 1168 )
rlm@0 1169
rlm@0 1170
rlm@0 1171 (def walking-up? (atom false))
rlm@0 1172 (def walking-down? (atom false))
rlm@0 1173 (def walking-left? (atom false))
rlm@0 1174 (def walking-right? (atom false))
rlm@0 1175
rlm@0 1176 (defn set-walk [walk-atom game value]
rlm@0 1177 ;;(println-repl "setting stuff to " value)
rlm@0 1178 (reset! walk-atom value))
rlm@0 1179
rlm@0 1180 (defn responses []
rlm@0 1181 {"key-w" (partial set-walk walking-up?)
rlm@0 1182 "key-d" (partial set-walk walking-right?)
rlm@0 1183 "key-s" (partial set-walk walking-down?)
rlm@0 1184 "key-a" (partial set-walk walking-left?)
rlm@0 1185 "key-return" (fire-cannon-ball)
rlm@0 1186 "key-space" (fn [game value] (.jump @player))
rlm@0 1187 })
rlm@0 1188
rlm@0 1189 (defn update-fn
rlm@0 1190 [game tpf]
rlm@0 1191 (let [camera (.getCamera game)
rlm@0 1192 cam-dir (.multLocal
rlm@0 1193 (.clone
rlm@0 1194 (.getDirection camera)) (float 0.6))
rlm@0 1195 cam-left (.multLocal
rlm@0 1196 (.clone
rlm@0 1197 (.getLeft camera)) (float 0.4))
rlm@0 1198 walk-direction (Vector3f. 0 0 0)]
rlm@0 1199
rlm@0 1200 (cond
rlm@0 1201 @walking-up? (.addLocal walk-direction cam-dir)
rlm@0 1202 @walking-right? (.addLocal walk-direction (.negate cam-left))
rlm@0 1203 @walking-down? (.addLocal walk-direction (.negate cam-dir))
rlm@0 1204 @walking-left? (.addLocal walk-direction cam-left))
rlm@0 1205 (.setWalkDirection @player walk-direction)
rlm@0 1206 (.setLocation camera (.getPhysicsLocation @player))))
rlm@0 1207
rlm@0 1208 (defn run-game []
rlm@0 1209 (.start
rlm@0 1210 (world (environment)
rlm@0 1211 (responses)
rlm@0 1212 setup-fn
rlm@0 1213 update-fn)))
rlm@0 1214 #+end_src
rlm@0 1215
rlm@0 1216 *** Hello Terrain
rlm@0 1217 #+srcname: hello-terrain
rlm@0 1218 #+begin_src clojure :results silent
rlm@0 1219 (ns hello.terrain)
rlm@0 1220 (use 'cortex.world)
rlm@0 1221 (use 'cortex.import)
rlm@0 1222 (use 'clojure.contrib.def)
rlm@0 1223 (import jme3tools.converters.ImageToAwt)
rlm@0 1224
rlm@0 1225
rlm@0 1226 (cortex.import/mega-import-jme3)
rlm@0 1227 (rlm.rlm-commands/help)
rlm@0 1228 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 1229
rlm@0 1230
rlm@0 1231 (defn setup-fn [type game]
rlm@0 1232 (.setMoveSpeed (.getFlyByCamera game) 50)
rlm@0 1233 (.setFrustumFar (.getCamera game) 10000)
rlm@0 1234 (let [env (environment type)
rlm@0 1235 cameras [(.getCamera game)]
rlm@0 1236 control (TerrainLodControl. env cameras)]
rlm@0 1237 ;;(.addControl env control)
rlm@0 1238 (.attachChild (.getRootNode game) env)))
rlm@0 1239
rlm@0 1240 (defn environment [type]
rlm@0 1241 (let
rlm@0 1242 [mat_terrain
rlm@0 1243 (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
rlm@0 1244 grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
rlm@0 1245 dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
rlm@0 1246 rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
rlm@0 1247 heightmap-image (.loadTexture (asset-manager)
rlm@0 1248 ({:mountain "Textures/Terrain/splat/mountains512.png"
rlm@0 1249 :fortress "Textures/Terrain/splat/fortress512.png"
rlm@0 1250 }type))
rlm@0 1251 heightmap (ImageBasedHeightMap.
rlm@0 1252 (ImageToAwt/convert (.getImage heightmap-image) false true 0))
rlm@0 1253 terrain (do (.load heightmap)
rlm@0 1254 (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
rlm@0 1255 ]
rlm@0 1256
rlm@0 1257 (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
rlm@0 1258 [grass dirt rock]))
rlm@0 1259
rlm@0 1260 (doto mat_terrain
rlm@0 1261 (.setTexture "Tex1" grass)
rlm@0 1262 (.setFloat "Tex1Scale" (float 64))
rlm@0 1263
rlm@0 1264 (.setTexture "Tex2" dirt)
rlm@0 1265 (.setFloat "Tex2Scale" (float 32))
rlm@0 1266
rlm@0 1267 (.setTexture "Tex3" rock)
rlm@0 1268 (.setFloat "Tex3Scale" (float 128))
rlm@0 1269
rlm@0 1270 (.setTexture "Alpha"
rlm@0 1271 (.loadTexture
rlm@0 1272 (asset-manager)
rlm@0 1273 ({:mountain "Textures/Terrain/splat/alphamap.png"
rlm@0 1274 :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
rlm@0 1275
rlm@0 1276 (doto terrain
rlm@0 1277 (.setMaterial mat_terrain)
rlm@0 1278 (.setLocalTranslation 0 -100 0)
rlm@0 1279 (.setLocalScale 2 1 2))))
rlm@0 1280
rlm@0 1281
rlm@0 1282
rlm@0 1283 (defn run-terrain-game [type]
rlm@0 1284 (.start
rlm@0 1285 (world
rlm@0 1286 (Node.)
rlm@0 1287 {}
rlm@0 1288 (partial setup-fn type)
rlm@0 1289 no-op)))
rlm@0 1290 #+end_src
rlm@0 1291
rlm@0 1292
rlm@0 1293
rlm@0 1294 #+srcname: hello-animation
rlm@0 1295 #+begin_src clojure :results silent
rlm@0 1296 (ns hello.animation)
rlm@0 1297 (use 'cortex.world)
rlm@0 1298 (use 'cortex.import)
rlm@0 1299 (use 'clojure.contrib.def)
rlm@0 1300 (cortex.import/mega-import-jme3)
rlm@0 1301 (rlm.rlm-commands/help)
rlm@0 1302 (use '[hello [collision :only [lights]]])
rlm@0 1303
rlm@0 1304 (defn stand
rlm@0 1305 [channel]
rlm@0 1306 (doto channel
rlm@0 1307 (.setAnim "stand" (float 0.5))
rlm@0 1308 (.setLoopMode LoopMode/DontLoop)
rlm@0 1309 (.setSpeed (float 1))))
rlm@0 1310
rlm@0 1311 (defn anim-listener []
rlm@0 1312 (proxy [AnimEventListener] []
rlm@0 1313 (onAnimChange
rlm@0 1314 [control channel animation-name]
rlm@0 1315 (println-repl "RLM --- onAnimChange"))
rlm@0 1316 (onAnimCycleDone
rlm@0 1317 [control channel animation-name]
rlm@0 1318 (if (= animation-name "Walk")
rlm@0 1319 (stand channel)
rlm@0 1320 ))))
rlm@0 1321
rlm@0 1322 (defn setup-fn [channel game]
rlm@0 1323 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 1324 ;; set the color of the sky
rlm@0 1325 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 1326 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 1327 (.setAnim channel "stand")
rlm@0 1328 (doto (.getFlyByCamera game)
rlm@0 1329 (.setMoveSpeed (float 10))
rlm@0 1330 (.setRotationSpeed 1)))
rlm@0 1331
rlm@0 1332 (defn walk [channel]
rlm@0 1333 (println-repl "zzz")
rlm@0 1334 (doto channel
rlm@0 1335 (.setAnim "Walk" (float 0.5))
rlm@0 1336 (.setLoopMode LoopMode/Loop)))
rlm@0 1337
rlm@0 1338
rlm@0 1339 (defn key-map [channel]
rlm@0 1340 {"key-space" (fn [game value]
rlm@0 1341 (if (not value)
rlm@0 1342 (walk channel)))})
rlm@0 1343
rlm@0 1344 (defn player []
rlm@0 1345 (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
rlm@0 1346 control (.getControl model AnimControl)]
rlm@0 1347 (.setLocalScale model (float 0.5))
rlm@0 1348 (.clearListeners control)
rlm@0 1349 (.addListener control (anim-control))
rlm@0 1350 model))
rlm@0 1351
rlm@0 1352
rlm@0 1353
rlm@0 1354 (defn run-anim-game []
rlm@0 1355 (let [ninja (player)
rlm@0 1356 control (.getControl ninja AnimControl)
rlm@0 1357 channel (.createChannel control)]
rlm@0 1358 (.start
rlm@0 1359 (world
rlm@0 1360 ninja
rlm@0 1361 (key-map channel)
rlm@0 1362 (partial setup-fn channel)
rlm@0 1363 no-op))))
rlm@0 1364 #+end_src
rlm@0 1365
rlm@0 1366 *** Hello Materials
rlm@0 1367 #+srcname: material
rlm@0 1368 #+begin_src clojure :results silent
rlm@0 1369 (ns hello.material)
rlm@0 1370 (use 'cortex.world)
rlm@0 1371 (use 'cortex.import)
rlm@0 1372 (use 'clojure.contrib.def)
rlm@0 1373 (cortex.import/mega-import-jme3)
rlm@0 1374 (rlm.rlm-commands/help)
rlm@0 1375
rlm@0 1376 (defn simple-cube []
rlm@0 1377 (box 1 1 1
rlm@0 1378 :position (Vector3f. -3 1.1 0)
rlm@0 1379 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1380 :texture "Interface/Logo/Monkey.jpg"
rlm@0 1381 :physical? false))
rlm@0 1382
rlm@0 1383 (defn leaky-box []
rlm@0 1384 (box 1 1 1
rlm@0 1385 :position (Vector3f. 3 -1 0)
rlm@0 1386 :material "Common/MatDefs/Misc/ColoredTextured.j3md"
rlm@0 1387 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 1388 :color (ColorRGBA. 1 0 1 1)
rlm@0 1389 :physical? false))
rlm@0 1390
rlm@0 1391 (defn transparent-box []
rlm@0 1392 (doto
rlm@0 1393 (box 1 1 0.1
rlm@0 1394 :position Vector3f/ZERO
rlm@0 1395 :name "window frame"
rlm@0 1396 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1397 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 1398 :physical? false)
rlm@0 1399 (-> (.getMaterial)
rlm@0 1400 (.getAdditionalRenderState)
rlm@0 1401 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 1402 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 1403
rlm@0 1404 (defn bumpy-sphere []
rlm@0 1405 (doto
rlm@0 1406 (sphere 2
rlm@0 1407 :position (Vector3f. 0 2 -2)
rlm@0 1408 :name "Shiny rock"
rlm@0 1409 :material "Common/MatDefs/Light/Lighting.j3md"
rlm@0 1410 :texture false
rlm@0 1411 :physical? false)
rlm@0 1412 (-> (.getMesh)
rlm@0 1413 (doto
rlm@0 1414 (.setTextureMode Sphere$TextureMode/Projected)
rlm@0 1415 (TangentBinormalGenerator/generate)))
rlm@0 1416 (-> (.getMaterial)
rlm@0 1417 (doto
rlm@0 1418 (.setTexture "DiffuseMap" (.loadTexture (asset-manager)
rlm@0 1419 "Textures/Terrain/Pond/Pond.png"))
rlm@0 1420 (.setTexture "NormalMap" (.loadTexture (asset-manager)
rlm@0 1421 "Textures/Terrain/Pond/Pond_normal.png"))
rlm@0 1422 (.setFloat "Shininess" (float 5))))
rlm@0 1423 (.rotate (float 1.6) 0 0)))
rlm@0 1424
rlm@0 1425
rlm@0 1426 (defn start-game []
rlm@0 1427 (.start
rlm@0 1428 (world
rlm@0 1429 (let [root (Node.)]
rlm@0 1430 (dorun (map #(.attachChild root %)
rlm@0 1431 [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
rlm@0 1432 root)
rlm@0 1433 {}
rlm@0 1434 (fn [world]
rlm@0 1435 (let [sun (doto (DirectionalLight.)
rlm@0 1436 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 1437 (.setColor ColorRGBA/White))]
rlm@0 1438 (.addLight (.getRootNode world) sun)))
rlm@0 1439 no-op
rlm@0 1440 )))
rlm@0 1441 #+end_src
rlm@0 1442
rlm@0 1443
rlm@0 1444
rlm@0 1445 * The Body
rlm@0 1446 ** Eyes
rlm@0 1447
rlm@0 1448 Ultimately I want to make creatures with eyes. Each eye can be
rlm@0 1449 independely moved and should see its own version of the world
rlm@0 1450 depending on where it is.
rlm@0 1451 #+srcname: eyes
rlm@0 1452 #+begin_src clojure
rlm@0 1453 (ns body.eye)
rlm@0 1454 (use 'cortex.world)
rlm@0 1455 (use 'cortex.import)
rlm@0 1456 (use 'clojure.contrib.def)
rlm@0 1457 (cortex.import/mega-import-jme3)
rlm@0 1458 (rlm.rlm-commands/help)
rlm@0 1459 (import java.nio.ByteBuffer)
rlm@0 1460 (import java.awt.image.BufferedImage)
rlm@0 1461 (import java.awt.Color)
rlm@0 1462 (import java.awt.Dimension)
rlm@0 1463 (import java.awt.Graphics)
rlm@0 1464 (import java.awt.Graphics2D)
rlm@0 1465 (import java.awt.event.WindowAdapter)
rlm@0 1466 (import java.awt.event.WindowEvent)
rlm@0 1467 (import java.awt.image.BufferedImage)
rlm@0 1468 (import java.nio.ByteBuffer)
rlm@0 1469 (import javax.swing.JFrame)
rlm@0 1470 (import javax.swing.JPanel)
rlm@0 1471 (import javax.swing.SwingUtilities)
rlm@0 1472 (import javax.swing.ImageIcon)
rlm@0 1473 (import javax.swing.JOptionPane)
rlm@0 1474 (import java.awt.image.ImageObserver)
rlm@0 1475
rlm@0 1476
rlm@0 1477
rlm@0 1478 (defn scene-processor
rlm@0 1479 "deals with converting FrameBuffers to BufferedImages so
rlm@0 1480 that the continuation function can be defined only in terms
rlm@0 1481 of what it does with BufferedImages"
rlm@0 1482 [continuation]
rlm@0 1483 (let [byte-buffer (atom nil)
rlm@0 1484 renderer (atom nil)
rlm@0 1485 image (atom nil)]
rlm@0 1486 (proxy [SceneProcessor] []
rlm@0 1487 (initialize
rlm@0 1488 [renderManager viewPort]
rlm@0 1489 (let [cam (.getCamera viewPort)
rlm@0 1490 width (.getWidth cam)
rlm@0 1491 height (.getHeight cam)]
rlm@0 1492 (reset! renderer (.getRenderer renderManager))
rlm@0 1493 (reset! byte-buffer
rlm@0 1494 (BufferUtils/createByteBuffer
rlm@0 1495 (* width height 4)))
rlm@0 1496 (reset! image (BufferedImage. width height
rlm@0 1497 BufferedImage/TYPE_4BYTE_ABGR))))
rlm@0 1498 (isInitialized [] (not (nil? @byte-buffer)))
rlm@0 1499 (reshape [_ _ _])
rlm@0 1500 (preFrame [_])
rlm@0 1501 (postQueue [_])
rlm@0 1502 (postFrame
rlm@0 1503 [#^FrameBuffer fb]
rlm@0 1504 (.clear @byte-buffer)
rlm@0 1505 (.readFrameBuffer @renderer fb @byte-buffer)
rlm@0 1506 (Screenshots/convertScreenShot @byte-buffer @image)
rlm@0 1507 (continuation @image))
rlm@0 1508 (cleanup []))))
rlm@0 1509
rlm@0 1510 (defn add-eye
rlm@0 1511 "Add an eye to the world, and call continuation on
rlm@0 1512 every frame produced"
rlm@0 1513 [world camera continuation]
rlm@0 1514 (let [width (.getWidth camera)
rlm@0 1515 height (.getHeight camera)
rlm@0 1516 render-manager (.getRenderManager world)
rlm@0 1517 viewport (.createMainView render-manager "eye-view" camera)]
rlm@0 1518 (doto viewport
rlm@0 1519 (.setBackgroundColor ColorRGBA/Black)
rlm@0 1520 (.setClearFlags true true true)
rlm@0 1521 (.addProcessor (scene-processor continuation))
rlm@0 1522 (.attachScene (.getRootNode world)))))
rlm@0 1523
rlm@0 1524 (defn make-display-frame [display width height]
rlm@0 1525 (SwingUtilities/invokeLater
rlm@0 1526 (fn []
rlm@0 1527 (.setPreferredSize display (Dimension. width height))
rlm@0 1528 (doto (JFrame. "Eye Camera!")
rlm@0 1529 (-> (.getContentPane) (.add display))
rlm@0 1530 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
rlm@0 1531 (.pack)
rlm@0 1532 (.setLocationRelativeTo nil)
rlm@0 1533 (.setResizable false)
rlm@0 1534 (.setVisible true)))))
rlm@0 1535
rlm@0 1536 (defn image-monitor [#^BufferedImage image]
rlm@0 1537 (proxy [JPanel] []
rlm@0 1538 (paintComponent
rlm@0 1539 [g]
rlm@0 1540 (proxy-super paintComponent g)
rlm@0 1541 (locking image
rlm@0 1542 (.drawImage g image 0 0
rlm@0 1543 (proxy [ImageObserver]
rlm@0 1544 []
rlm@0 1545 (imageUpdate
rlm@0 1546 []
rlm@0 1547 (proxy-super imageUpdate))))))))
rlm@0 1548
rlm@0 1549 (defn movie-image []
rlm@0 1550 (let [setup
rlm@0 1551 (runonce
rlm@0 1552 (fn [#^BufferedImage image]
rlm@0 1553 (let [width (.getWidth image)
rlm@0 1554 height (.getHeight image)
rlm@0 1555 display (image-monitor image)
rlm@0 1556 frame (make-display-frame display width height)]
rlm@0 1557 display)))]
rlm@0 1558 (fn [#^BufferedImage image]
rlm@0 1559 (.repaint (setup image)))))
rlm@0 1560
rlm@0 1561
rlm@0 1562 (defn observer
rlm@0 1563 "place thy eye!"
rlm@0 1564 [world camera]
rlm@0 1565 (let [eye camera
rlm@0 1566 width (.getWidth eye)
rlm@0 1567 height (.getHeight eye)]
rlm@0 1568 (no-exceptions
rlm@0 1569 (add-eye
rlm@0 1570 world
rlm@0 1571 eye
rlm@0 1572 (movie-image)))))
rlm@0 1573 #+end_src
rlm@0 1574
rlm@0 1575 #+srcname: test-vision
rlm@0 1576 #+begin_src clojure
rlm@0 1577
rlm@0 1578 (ns test.vision)
rlm@0 1579 (use 'cortex.world)
rlm@0 1580 (use 'cortex.import)
rlm@0 1581 (use 'clojure.contrib.def)
rlm@0 1582 (use 'body.eye)
rlm@0 1583 (cortex.import/mega-import-jme3)
rlm@0 1584 (rlm.rlm-commands/help)
rlm@0 1585 (import java.nio.ByteBuffer)
rlm@0 1586 (import java.awt.image.BufferedImage)
rlm@0 1587 (import java.awt.Color)
rlm@0 1588 (import java.awt.Dimension)
rlm@0 1589 (import java.awt.Graphics)
rlm@0 1590 (import java.awt.Graphics2D)
rlm@0 1591 (import java.awt.event.WindowAdapter)
rlm@0 1592 (import java.awt.event.WindowEvent)
rlm@0 1593 (import java.awt.image.BufferedImage)
rlm@0 1594 (import java.nio.ByteBuffer)
rlm@0 1595 (import javax.swing.JFrame)
rlm@0 1596 (import javax.swing.JPanel)
rlm@0 1597 (import javax.swing.SwingUtilities)
rlm@0 1598 (import javax.swing.ImageIcon)
rlm@0 1599 (import javax.swing.JOptionPane)
rlm@0 1600 (import java.awt.image.ImageObserver)
rlm@0 1601
rlm@0 1602
rlm@0 1603 (def width 200)
rlm@0 1604 (def height 200)
rlm@0 1605
rlm@0 1606 (defn camera []
rlm@0 1607 (doto (Camera. width height)
rlm@0 1608 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1609 (.setLocation (Vector3f. -3 0 -5))
rlm@0 1610 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1611
rlm@0 1612 (defn camera2 []
rlm@0 1613 (doto (Camera. width height)
rlm@0 1614 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1615 (.setLocation (Vector3f. 3 0 -5))
rlm@0 1616 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1617
rlm@0 1618 (defn setup-fn [world]
rlm@0 1619 (let [eye (camera)
rlm@0 1620 width (.getWidth eye)
rlm@0 1621 height (.getHeight eye)]
rlm@0 1622 (no-exceptions
rlm@0 1623 (add-eye
rlm@0 1624 world
rlm@0 1625 eye
rlm@0 1626 (runonce visual))
rlm@0 1627 (add-eye
rlm@0 1628 world
rlm@0 1629 (camera2)
rlm@0 1630 (runonce visual)))))
rlm@0 1631
rlm@0 1632 (defn spider-eye [position]
rlm@0 1633 (doto (Camera. 200 200 )
rlm@0 1634 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1635 (.setLocation position)
rlm@0 1636 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1637
rlm@0 1638 (defn setup-fn* [world]
rlm@0 1639 (let [eye (camera)
rlm@0 1640 width (.getWidth eye)
rlm@0 1641 height (.getHeight eye)]
rlm@0 1642 ;;(.setClearFlags (.getViewPort world) true true true)
rlm@0 1643 (observer world (.getCamera world))
rlm@0 1644 (observer world (spider-eye (Vector3f. 3 0 -5)))
rlm@0 1645 ;;(observer world (spider-eye (Vector3f. 0 0 -5)))
rlm@0 1646 ;; (observer world (spider-eye (Vector3f. -3 0 -5)))
rlm@0 1647 ;; (observer world (spider-eye (Vector3f. 0 3 -5)))
rlm@0 1648 ;; (observer world (spider-eye (Vector3f. 0 -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 ;; (observer world (spider-eye (Vector3f. 3 -3 -5)))
rlm@0 1652 ;; (observer world (spider-eye (Vector3f. -3 -3 -5)))
rlm@0 1653
rlm@0 1654 )
rlm@0 1655 world)
rlm@0 1656
rlm@0 1657 (defn test-world []
rlm@0 1658 (let [thing (box 1 1 1 :physical? false)]
rlm@0 1659 (world
rlm@0 1660 (doto (Node.)
rlm@0 1661 (.attachChild thing))
rlm@0 1662 {}
rlm@0 1663 setup-fn
rlm@0 1664 (fn [world tpf]
rlm@0 1665 (.rotate thing (* tpf 0.2) 0 0)
rlm@0 1666 ))))
rlm@0 1667
rlm@0 1668
rlm@0 1669 #+end_src
rlm@0 1670
rlm@0 1671
rlm@0 1672 #+results: eyes
rlm@0 1673 : #'body.eye/test-world
rlm@0 1674
rlm@0 1675 Note the use of continuation passing style for connecting the eye to a
rlm@0 1676 function to process the output. The example code will create two
rlm@0 1677 videos of the same rotating cube from different angles, sutiable for
rlm@0 1678 stereoscopic vision.
rlm@0 1679
rlm@0 1680
rlm@0 1681
rlm@0 1682
rlm@0 1683
rlm@0 1684
rlm@0 1685 * COMMENT code generation
rlm@0 1686 #+begin_src clojure :tangle ../src/cortex/import.clj
rlm@0 1687 <<import>>
rlm@0 1688 #+end_src
rlm@0 1689
rlm@0 1690 #+begin_src clojure :tangle ../src/hello/brick_wall.clj
rlm@0 1691 <<brick-wall-header>>
rlm@0 1692 <<brick-wall-body>>
rlm@0 1693 #+end_src
rlm@0 1694
rlm@0 1695 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
rlm@0 1696 <<hello-simple-app>>
rlm@0 1697 #+end_src
rlm@0 1698
rlm@0 1699 #+begin_src clojure :tangle ../src/cortex/world.clj
rlm@0 1700 <<world-inputs>>
rlm@0 1701 <<world>>
rlm@0 1702 <<world-shapes>>
rlm@0 1703 <<world-view>>
rlm@0 1704 #+end_src
rlm@0 1705
rlm@0 1706 #+begin_src clojure :tangle ../src/cortex/other_games.clj
rlm@0 1707 <<other-games>>
rlm@0 1708 #+end_src
rlm@0 1709
rlm@0 1710 #+begin_src clojure :tangle ../src/hello/loop.clj
rlm@0 1711 <<hello-loop>>
rlm@0 1712 #+end_src
rlm@0 1713
rlm@0 1714 #+begin_src clojure :tangle ../src/hello/collision.clj
rlm@0 1715 <<hello-collision>>
rlm@0 1716 #+end_src
rlm@0 1717
rlm@0 1718 #+begin_src clojure :tangle ../src/hello/terrain.clj
rlm@0 1719 <<hello-terrain>>
rlm@0 1720 #+end_src
rlm@0 1721
rlm@0 1722 #+begin_src clojure :tangle ../src/hello/animation.clj
rlm@0 1723 <<hello-animation>>
rlm@0 1724 #+end_src
rlm@0 1725
rlm@0 1726 #+begin_src clojure :tangle ../src/hello/material.clj
rlm@0 1727 <<material>>
rlm@0 1728 #+end_src
rlm@0 1729
rlm@0 1730 #+begin_src clojure :tangle ../src/body/eye.clj
rlm@0 1731 <<eyes>>
rlm@0 1732 #+end_src
rlm@0 1733
rlm@0 1734 #+begin_src clojure :tangle ../src/test/vision.clj
rlm@0 1735 <<test-vision>>
rlm@0 1736 #+end_src
rlm@0 1737
rlm@0 1738
rlm@0 1739