annotate org/cortex.org @ 2:46b2d4c9522d

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