annotate org/cortex.org @ 22:157b416152ea

continuing splitting
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 23:35:04 -0700
parents 01e1427126af
children cab2da252494
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@21 7 #+babel: :mkdirp yes :noweb yes :exports both
rlm@0 8
rlm@0 9
rlm@0 10 * Simulation Base
rlm@0 11
rlm@0 12 ** Imports
rlm@21 13 jMonkeyEngine has a plethora of classes which can be overwhelming at
rlm@21 14 first. So that I one can get right to coding, it's good to take the
rlm@21 15 time right now and make a "import all" function which brings in all of
rlm@21 16 the important jme3 classes. Once I'm happy with the general structure
rlm@21 17 of a namespace I can deal with importing only the classes it actually
rlm@21 18 needs.
rlm@21 19
rlm@0 20 #+srcname: import
rlm@0 21 #+begin_src clojure :results silent
rlm@0 22 (ns cortex.import
rlm@0 23 (:require swank.util.class-browse))
rlm@0 24
rlm@21 25 (defn permissive-import
rlm@21 26 [classname]
rlm@21 27 (eval `(try (import '~classname)
rlm@21 28 (catch java.lang.Exception e#
rlm@21 29 (println "couldn't import " '~classname))))
rlm@21 30 classname)
rlm@0 31
rlm@21 32 (defn jme-class? [classname]
rlm@21 33 (and
rlm@21 34 (.startsWith classname "com.jme3.")
rlm@21 35 ;; Don't import the Lwjgl stuff since it can throw exceptions
rlm@21 36 ;; upon being loaded.
rlm@21 37 (not (re-matches #".*Lwjgl.*" classname))))
rlm@0 38
rlm@21 39 (defn jme-classes
rlm@21 40 "returns a list of all jme3 classes"
rlm@21 41 []
rlm@21 42 (filter
rlm@21 43 jme-class?
rlm@21 44 (map :name
rlm@21 45 swank.util.class-browse/available-classes)))
rlm@0 46
rlm@0 47 (defn mega-import-jme3
rlm@21 48 "Import ALL the jme classes. For REPL use."
rlm@0 49 []
rlm@21 50 (doall
rlm@21 51 (map (comp permissive-import symbol) (jme-classes))))
rlm@0 52 #+end_src
rlm@0 53
rlm@0 54 The =mega-import-jme3= is quite usefull for debugging purposes since
rlm@21 55 it allows completion for almost all of JME's classes.
rlm@21 56
rlm@21 57 Out of curiousity, let's see just how many classes =mega-import-jme3=
rlm@21 58 imports:
rlm@21 59
rlm@21 60 #+begin_src clojure :exports both
rlm@21 61 (clojure.core/count (cortex.import/jme-classes))
rlm@21 62 #+end_src
rlm@21 63
rlm@21 64 #+results:
rlm@21 65 : 955
rlm@0 66
rlm@0 67 ** Simplification
rlm@0 68 *** World
rlm@0 69
rlm@0 70 It is comvienent to wrap the JME elements that deal with creating a
rlm@0 71 world, creation of basic objects, and Keyboard input with a nicer
rlm@0 72 interface (at least for my purposes).
rlm@0 73
rlm@0 74 #+srcname: world-inputs
rlm@0 75 #+begin_src clojure :results silent
rlm@0 76 (ns cortex.world)
rlm@0 77 (require 'cortex.import)
rlm@0 78 (use 'clojure.contrib.def)
rlm@0 79 (rlm.rlm-commands/help)
rlm@0 80 (cortex.import/mega-import-jme3)
rlm@0 81
rlm@0 82 (defvar *app-settings*
rlm@0 83 (doto (AppSettings. true)
rlm@0 84 (.setFullscreen false)
rlm@0 85 (.setTitle "Aurellem.")
rlm@0 86 ;; disable 32 bit stuff for now
rlm@6 87 ;;(.setAudioRenderer "Send")
rlm@0 88 )
rlm@0 89 "These settings control how the game is displayed on the screen for
rlm@0 90 debugging purposes. Use binding forms to change this if desired.
rlm@0 91 Full-screen mode does not work on some computers.")
rlm@0 92
rlm@0 93 (defn asset-manager
rlm@0 94 "returns a new, configured assetManager" []
rlm@0 95 (JmeSystem/newAssetManager
rlm@0 96 (.getResource
rlm@0 97 (.getContextClassLoader (Thread/currentThread))
rlm@0 98 "com/jme3/asset/Desktop.cfg")))
rlm@0 99
rlm@0 100 (defmacro no-exceptions
rlm@0 101 "Sweet relief like I never knew."
rlm@0 102 [& forms]
rlm@0 103 `(try ~@forms (catch Exception e# (.printStackTrace e#))))
rlm@0 104
rlm@0 105 (defn thread-exception-removal []
rlm@0 106 (println "removing exceptions from " (Thread/currentThread))
rlm@0 107 (.setUncaughtExceptionHandler
rlm@0 108 (Thread/currentThread)
rlm@0 109 (proxy [Thread$UncaughtExceptionHandler] []
rlm@0 110 (uncaughtException
rlm@0 111 [thread thrown]
rlm@0 112 (println "uncaught-exception thrown in " thread)
rlm@0 113 (println (.getMessage thrown))))))
rlm@0 114
rlm@0 115 (def println-repl (bound-fn [& args] (apply println args)))
rlm@0 116
rlm@0 117 (use '[pokemon [lpsolve :only [constant-map]]])
rlm@0 118
rlm@0 119 (defn no-op [& _])
rlm@0 120
rlm@0 121 (defn all-keys
rlm@0 122 "Construct a map of strings representing all the manual inputs from
rlm@0 123 either the keyboard or mouse."
rlm@0 124 []
rlm@0 125 (let [inputs (constant-map KeyInput)]
rlm@0 126 (assoc
rlm@0 127 (zipmap (map (fn [field]
rlm@0 128 (.toLowerCase (re-gsub #"_" "-" field))) (vals inputs))
rlm@0 129 (map (fn [val] (KeyTrigger. val)) (keys inputs)))
rlm@0 130 ;;explicitly add mouse controls
rlm@0 131 "mouse-left" (MouseButtonTrigger. 0)
rlm@0 132 "mouse-middle" (MouseButtonTrigger. 2)
rlm@0 133 "mouse-right" (MouseButtonTrigger. 1))))
rlm@0 134
rlm@0 135 (defn initialize-inputs
rlm@0 136 "more java-interop cruft to establish keybindings for a particular virtual world"
rlm@0 137 [game input-manager key-map]
rlm@0 138 (doall (map (fn [[name trigger]]
rlm@0 139 (.addMapping ^InputManager input-manager
rlm@0 140 name (into-array (class trigger) [trigger]))) key-map))
rlm@0 141 (doall (map (fn [name]
rlm@0 142 (.addListener ^InputManager input-manager game
rlm@0 143 (into-array String [name]))) (keys key-map))))
rlm@0 144
rlm@0 145 #+end_src
rlm@0 146
rlm@0 147 These functions are all for debug controlling of the world through
rlm@0 148 keyboard and mouse.
rlm@0 149
rlm@0 150 We reuse =constant-map= from =pokemon.lpsolve= to get the numerical
rlm@0 151 values for all the keys defined in the =KeyInput= class. The
rlm@0 152 documentation for =constant-map= is:
rlm@0 153
rlm@0 154 #+begin_src clojure :results output
rlm@0 155 (doc pokemon.lpsolve/constant-map)
rlm@0 156 #+end_src
rlm@0 157
rlm@0 158 #+results:
rlm@0 159 : -------------------------
rlm@0 160 : pokemon.lpsolve/constant-map
rlm@0 161 : ([class])
rlm@0 162 : Takes a class and creates a map of the static constant integer
rlm@0 163 : fields with their names. This helps with C wrappers where they have
rlm@0 164 : just defined a bunch of integer constants instead of enums
rlm@0 165
rlm@0 166
rlm@0 167 Then, =all-keys= converts the constant names like =KEY_J= to the more
rlm@0 168 clojure-like =key-j=, and returns a map from these keys to
rlm@0 169 jMonkeyEngine KeyTrigger objects, the use of which will soon become
rlm@0 170 apparent. =all-keys= also adds the three mouse button controls to the
rlm@0 171 map.
rlm@0 172
rlm@0 173 #+srcname: world
rlm@0 174 #+begin_src clojure :results silent
rlm@0 175 (in-ns 'cortex.world)
rlm@0 176
rlm@0 177 (defn traverse
rlm@0 178 "apply f to every non-node, deeply"
rlm@0 179 [f node]
rlm@0 180 (if (isa? (class node) Node)
rlm@0 181 (dorun (map (partial traverse f) (.getChildren node)))
rlm@0 182 (f node)))
rlm@0 183
rlm@0 184 (def gravity (Vector3f. 0 -9.81 0))
rlm@0 185
rlm@0 186 (defn world
rlm@0 187 [root-node key-map setup-fn update-fn]
rlm@0 188 (let [physics-manager (BulletAppState.)
rlm@0 189 shadow-renderer (BasicShadowRenderer. (asset-manager) (int 256))
rlm@0 190 ;;maybe use a better shadow renderer someday!
rlm@0 191 ;;shadow-renderer (PssmShadowRenderer. (asset-manager) 256 1)
rlm@0 192 ]
rlm@0 193 (doto
rlm@0 194 (proxy [SimpleApplication ActionListener] []
rlm@0 195 (simpleInitApp
rlm@0 196 []
rlm@0 197 (no-exceptions
rlm@0 198 (.setTimer this (IsoTimer. 60))
rlm@0 199 ;; Create key-map.
rlm@0 200 (.setFrustumFar (.getCamera this) 300)
rlm@0 201 (initialize-inputs this (.getInputManager this) (all-keys))
rlm@0 202 ;; Don't take control of the mouse
rlm@0 203 (org.lwjgl.input.Mouse/setGrabbed false)
rlm@0 204 ;; add all objects to the world
rlm@0 205 (.attachChild (.getRootNode this) root-node)
rlm@0 206 ;; enable physics
rlm@0 207 ;; add a physics manager
rlm@0 208 (.attach (.getStateManager this) physics-manager)
rlm@0 209 (.setGravity (.getPhysicsSpace physics-manager) gravity)
rlm@0 210
rlm@0 211
rlm@0 212 ;; go through every object and add it to the physics manager
rlm@0 213 ;; if relavant.
rlm@0 214 (traverse (fn [geom]
rlm@0 215 (dorun
rlm@0 216 (for [n (range (.getNumControls geom))]
rlm@0 217 (do
rlm@0 218 (println-repl "adding control " (.getControl geom n))
rlm@0 219 (.add (.getPhysicsSpace physics-manager)
rlm@0 220 (.getControl geom n))))))
rlm@0 221 (.getRootNode this))
rlm@0 222 ;;(.addAll (.getPhysicsSpace physics-manager) (.getRootNode this))
rlm@0 223
rlm@0 224 (setup-fn this)
rlm@0 225 (.setDirection shadow-renderer
rlm@0 226 (.normalizeLocal (Vector3f. -1 -1 -1)))
rlm@0 227 (.addProcessor (.getViewPort this) shadow-renderer)
rlm@0 228 (.setShadowMode (.getRootNode this) RenderQueue$ShadowMode/Off)
rlm@0 229 ))
rlm@0 230 (simpleUpdate
rlm@0 231 [tpf]
rlm@0 232 (no-exceptions
rlm@0 233 (update-fn this tpf)))
rlm@0 234 (onAction
rlm@0 235 [binding value tpf]
rlm@0 236 ;; whenever a key is pressed, call the function returned from
rlm@0 237 ;; key-map.
rlm@0 238 (no-exceptions
rlm@0 239 (if-let [react (key-map binding)]
rlm@0 240 (react this value)))))
rlm@0 241 ;; don't show a menu to change options.
rlm@0 242
rlm@0 243 (.setShowSettings false)
rlm@0 244 (.setPauseOnLostFocus false)
rlm@0 245 (.setSettings *app-settings*))))
rlm@0 246
rlm@0 247 (defn apply-map
rlm@0 248 "Like apply, but works for maps and functions that expect an implicit map
rlm@0 249 and nothing else as in (fn [& {}]).
rlm@0 250 ------- Example -------
rlm@4 251 (defn jjj [& {:keys [www] :or {www \"oh yeah\"} :as env}] (println www))
rlm@0 252 (apply-map jjj {:www \"whatever\"})
rlm@0 253 -->\"whatever\""
rlm@0 254 [fn m]
rlm@0 255 (apply fn (reduce #(into %1 %2) [] m)))
rlm@0 256
rlm@0 257 #+end_src
rlm@0 258
rlm@0 259
rlm@0 260 =world= is the most important function here.
rlm@0 261 *** TODO more documentation
rlm@0 262
rlm@0 263 #+srcname: world-shapes
rlm@0 264 #+begin_src clojure :results silent
rlm@0 265 (in-ns 'cortex.world)
rlm@0 266 (defrecord shape-description
rlm@0 267 [name
rlm@0 268 color
rlm@0 269 mass
rlm@0 270 friction
rlm@0 271 texture
rlm@0 272 material
rlm@0 273 position
rlm@0 274 rotation
rlm@0 275 shape
rlm@0 276 physical?])
rlm@0 277
rlm@0 278 (def base-shape
rlm@0 279 (shape-description.
rlm@0 280 "default-shape"
rlm@0 281 false
rlm@0 282 ;;ColorRGBA/Blue
rlm@0 283 1.0 ;; mass
rlm@0 284 1.0 ;; friction
rlm@0 285 ;; texture
rlm@0 286 "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 287 ;; material
rlm@0 288 "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 289 Vector3f/ZERO
rlm@0 290 Quaternion/IDENTITY
rlm@0 291 (Box. Vector3f/ZERO 0.5 0.5 0.5)
rlm@0 292 true))
rlm@0 293
rlm@0 294 (defn make-shape
rlm@0 295 [#^shape-description d]
rlm@6 296 (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))
rlm@6 297 mat (Material. asset-manager (:material d))
rlm@0 298 geom (Geometry. (:name d) (:shape d))]
rlm@0 299 (if (:texture d)
rlm@0 300 (let [key (TextureKey. (:texture d))]
rlm@0 301 (.setGenerateMips key true)
rlm@6 302 (.setTexture mat "ColorMap" (.loadTexture asset-manager key))))
rlm@0 303 (if (:color d) (.setColor mat "Color" (:color d)))
rlm@0 304 (.setMaterial geom mat)
rlm@0 305 (if-let [rotation (:rotation d)] (.rotate geom rotation))
rlm@0 306 (.setLocalTranslation geom (:position d))
rlm@0 307 (if (:physical? d)
rlm@6 308 (let [impact-shape (doto (GImpactCollisionShape.
rlm@6 309 (.getMesh geom)) (.setMargin 0))
rlm@0 310 physics-control (RigidBodyControl.
rlm@6 311 ;;impact-shape ;; comment to disable
rlm@0 312 (float (:mass d)))]
rlm@0 313 (.createJmeMesh impact-shape)
rlm@0 314 (.addControl geom physics-control)
rlm@0 315 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
rlm@0 316 (.setFriction physics-control (:friction d))))
rlm@0 317 ;;the default is to keep this node in the physics engine forever.
rlm@0 318 ;;these commands must come after the control is added to the geometry.
rlm@0 319 ;;
rlm@0 320 geom))
rlm@0 321
rlm@0 322 (defn box
rlm@0 323 ([l w h & {:as options}]
rlm@0 324 (let [options (merge base-shape options)]
rlm@0 325 (make-shape (assoc options
rlm@0 326 :shape (Box. l w h)))))
rlm@0 327 ([] (box 0.5 0.5 0.5)))
rlm@0 328
rlm@0 329 (defn sphere
rlm@0 330 ([r & {:as options}]
rlm@0 331 (let [options (merge base-shape options)]
rlm@0 332 (make-shape (assoc options
rlm@0 333 :shape (Sphere. 32 32 (float r))))))
rlm@0 334 ([] (sphere 0.5)))
rlm@0 335
rlm@15 336 (defn add-element
rlm@15 337 ([game element node]
rlm@0 338 (.addAll
rlm@0 339 (.getPhysicsSpace
rlm@0 340 (.getState
rlm@0 341 (.getStateManager game)
rlm@0 342 BulletAppState))
rlm@15 343 element)
rlm@15 344 (.attachChild node element))
rlm@15 345 ([game element]
rlm@15 346 (add-element game element (.getRootNode game))))
rlm@15 347
rlm@0 348
rlm@0 349 (defn set-gravity*
rlm@0 350 [game gravity]
rlm@0 351 (traverse
rlm@0 352 (fn [geom]
rlm@0 353 (if-let
rlm@0 354 [control (.getControl geom RigidBodyControl)]
rlm@0 355 (do
rlm@0 356 (.setGravity control gravity)
rlm@0 357 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
rlm@0 358 )))
rlm@0 359 (.getRootNode game)))
rlm@0 360
rlm@0 361 #+end_src
rlm@0 362
rlm@0 363 These are convienence functions for creating JME objects and
rlm@0 364 manipulating a world.
rlm@0 365
rlm@0 366 #+srcname: world-view
rlm@0 367 #+begin_src clojure :results silent
rlm@0 368 (in-ns 'cortex.world)
rlm@0 369
rlm@0 370 (defprotocol Viewable
rlm@0 371 (view [something]))
rlm@0 372
rlm@0 373 (extend-type com.jme3.scene.Geometry
rlm@0 374 Viewable
rlm@0 375 (view [geo]
rlm@0 376 (view (doto (Node.)(.attachChild geo)))))
rlm@0 377
rlm@0 378 (extend-type com.jme3.scene.Node
rlm@0 379 Viewable
rlm@0 380 (view [node]
rlm@0 381 (.start
rlm@0 382 (world node
rlm@0 383 {}
rlm@0 384 (fn [world]
rlm@0 385 (.enableDebug
rlm@0 386 (.getPhysicsSpace
rlm@0 387 (.getState
rlm@0 388 (.getStateManager world)
rlm@0 389 BulletAppState))
rlm@0 390 (asset-manager))
rlm@0 391 (set-gravity* world Vector3f/ZERO)
rlm@0 392 ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0))
rlm@0 393 (let [sun (doto (DirectionalLight.)
rlm@0 394 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 395 (.setColor ColorRGBA/White))]
rlm@0 396 (.addLight (.getRootNode world) sun)))
rlm@0 397 no-op))))
rlm@0 398
rlm@0 399 (defn position-camera [game]
rlm@0 400 (doto (.getCamera game)
rlm@0 401 (.setLocation (Vector3f. 0 6 6))
rlm@0 402 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
rlm@0 403
rlm@0 404 #+end_src
rlm@0 405
rlm@0 406 Here I make the =Viewable= protocol and extend it to JME's types. Now
rlm@0 407 hello-world can be written as easily as:
rlm@0 408
rlm@0 409 #+begin_src clojure :results silent
rlm@0 410 (cortex.world/view (cortex.world/box))
rlm@0 411 #+end_src
rlm@0 412
rlm@0 413 ** Hello
rlm@0 414 Here are the jmonkeyengine "Hello" programs translated to clojure.
rlm@0 415 *** Hello Simple App
rlm@0 416 Here is the hello world example for jme3 in clojure.
rlm@0 417 It's a more or less direct translation from the java source
rlm@0 418 from
rlm@0 419 http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication.
rlm@0 420
rlm@0 421 Of note is the fact that since we don't have access to the
rlm@0 422 =AssetManager= via extendig =SimpleApplication=, we have to build one
rlm@0 423 ourselves.
rlm@0 424
rlm@0 425 #+srcname: hello-simple-app
rlm@0 426 #+begin_src clojure :results silent
rlm@0 427 (ns hello.hello-simple-app)
rlm@0 428 (require 'cortex.import)
rlm@0 429 (use 'clojure.contrib.def)
rlm@0 430 (rlm.rlm-commands/help)
rlm@21 431 (cortex.import/mega-import-jme3)
rlm@0 432 (use 'cortex.world)
rlm@0 433
rlm@0 434
rlm@0 435 (def cube (Box. Vector3f/ZERO 1 1 1))
rlm@0 436
rlm@0 437 (def geom (Geometry. "Box" cube))
rlm@0 438
rlm@0 439 (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
rlm@0 440
rlm@0 441 (.setColor mat "Color" ColorRGBA/Blue)
rlm@0 442
rlm@0 443 (.setMaterial geom mat)
rlm@0 444
rlm@0 445 (defn simple-app []
rlm@0 446 (doto
rlm@0 447 (proxy [SimpleApplication] []
rlm@0 448 (simpleInitApp
rlm@0 449 []
rlm@0 450 ;; Don't take control of the mouse
rlm@0 451 (org.lwjgl.input.Mouse/setGrabbed false)
rlm@0 452 (.attachChild (.getRootNode this) geom)))
rlm@0 453 ;; don't show a menu to change options.
rlm@0 454 (.setShowSettings false)
rlm@0 455 (.setPauseOnLostFocus false)
rlm@0 456 (.setSettings *app-settings*)))
rlm@0 457 #+end_src
rlm@0 458
rlm@0 459 Running this program will begin a new jMonkeyEngine game which
rlm@0 460 displays a single blue cube.
rlm@0 461
rlm@0 462 #+begin_src clojure :exports code :results silent
rlm@0 463 (.start (hello.hello-simple-app/simple-app))
rlm@0 464 #+end_src
rlm@0 465
rlm@0 466 #+caption: the simplest JME game.
rlm@0 467 [[./images/simple-app.jpg]]
rlm@0 468
rlm@0 469
rlm@0 470
rlm@0 471 *** Hello Physics
rlm@0 472 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
rlm@0 473
rlm@0 474 #+srcname: brick-wall-header
rlm@0 475 #+begin_src clojure :results silent
rlm@0 476 (ns hello.brick-wall)
rlm@0 477 (require 'cortex.import)
rlm@0 478 (use 'clojure.contrib.def)
rlm@0 479 (rlm.rlm-commands/help)
rlm@0 480 (cortex.import/mega-import-jme3)
rlm@0 481 (use '[pokemon [lpsolve :only [constant-map]]])
rlm@0 482 (use 'cortex.world)
rlm@0 483 #+end_src
rlm@0 484
rlm@0 485 #+srcname: brick-wall-body
rlm@0 486 #+begin_src clojure :results silent
rlm@0 487 (in-ns 'hello.brick-wall)
rlm@0 488
rlm@0 489 (defn floor
rlm@0 490 "make a sturdy, unmovable physical floor"
rlm@0 491 []
rlm@0 492 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
rlm@0 493
rlm@0 494 (def brick-length 0.48)
rlm@0 495 (def brick-width 0.24)
rlm@0 496 (def brick-height 0.12)
rlm@0 497
rlm@0 498
rlm@0 499 (defn brick* [position]
rlm@0 500 (doto (box brick-length brick-height brick-width
rlm@0 501 :position position :name "brick"
rlm@0 502 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 503 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 504 :mass 36)
rlm@0 505 (->
rlm@0 506 (.getMesh)
rlm@0 507 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@0 508 ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 509 )
rlm@0 510 )
rlm@0 511
rlm@0 512 (defn inception-brick-wall
rlm@0 513 "construct a physical brick wall"
rlm@0 514 []
rlm@0 515 (let [node (Node. "brick-wall")]
rlm@0 516 (dorun
rlm@0 517 (map (comp #(.attachChild node %) brick*)
rlm@0 518 (for
rlm@0 519 [x (range 15)
rlm@0 520 y (range 10)
rlm@0 521 z (range 1)]
rlm@0 522 (Vector3f.
rlm@0 523 (* brick-length x 1.03)
rlm@0 524 (* brick-width y y 10)
rlm@0 525 (* brick-height z)))))
rlm@0 526 node))
rlm@0 527
rlm@0 528 (defn gravity-toggle
rlm@0 529 [new-value]
rlm@0 530 (fn [game value]
rlm@0 531 (println-repl "set gravity to " new-value)
rlm@0 532 (if value
rlm@0 533 (set-gravity* game new-value)
rlm@0 534 (set-gravity* game gravity))))
rlm@0 535
rlm@15 536 (defn fire-cannon-ball
rlm@15 537 ([node]
rlm@15 538 (fn [game value]
rlm@15 539 (if (not value)
rlm@15 540 (let [camera (.getCamera game)
rlm@15 541 cannon-ball
rlm@15 542 (sphere 0.7
rlm@15 543 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@15 544 :texture "Textures/PokeCopper.jpg"
rlm@15 545 :position
rlm@15 546 (.add (.getLocation camera)
rlm@15 547 (.mult (.getDirection camera) (float 1)))
rlm@15 548 :mass 3)] ;200 0.05
rlm@15 549 (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive)
rlm@15 550 (.setLinearVelocity
rlm@15 551 (.getControl cannon-ball RigidBodyControl)
rlm@15 552 (.mult (.getDirection camera) (float 50))) ;50
rlm@21 553 (add-element game cannon-ball (if node node (.getRootNode game)))))))
rlm@15 554 ([]
rlm@15 555 (fire-cannon-ball false)))
rlm@15 556
rlm@0 557
rlm@0 558 (defn floor* []
rlm@0 559 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@0 560 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 561 :texture "Textures/Terrain/Pond/Pond.png"
rlm@0 562 :position (Vector3f. 0 -0.1 0 )
rlm@0 563 :mass 0)
rlm@0 564 (->
rlm@0 565 (.getMesh)
rlm@0 566 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@0 567 (->
rlm@0 568 (.getMaterial)
rlm@0 569 (.getTextureParam "ColorMap")
rlm@0 570 (.getTextureValue)
rlm@0 571 (.setWrap Texture$WrapMode/Repeat))
rlm@0 572 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@0 573 ))
rlm@0 574
rlm@0 575 (defn brick-wall* []
rlm@0 576 (let [node (Node. "brick-wall")]
rlm@0 577 (dorun
rlm@0 578 (map
rlm@0 579 (comp #(.attachChild node %) brick*)
rlm@0 580 (for [y (range 15)
rlm@0 581 x (range 4)
rlm@0 582 z (range 1)]
rlm@0 583 (Vector3f.
rlm@0 584 (+ (* 2 x brick-length)
rlm@0 585 (if (even? (+ y z))
rlm@0 586 (/ brick-length 4) (/ brick-length -4)))
rlm@0 587 (+ (* brick-height (inc (* 2 y))))
rlm@0 588 (* 2 z brick-width) ))))
rlm@0 589 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@0 590 node))
rlm@0 591
rlm@0 592 (defn brick-wall-game-run []
rlm@0 593 (doto
rlm@0 594 (world
rlm@0 595 (doto (Node.) (.attachChild (floor*))
rlm@0 596 (.attachChild (brick-wall*))
rlm@0 597 )
rlm@0 598 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 599 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 600 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 601 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 602 "key-k" (gravity-toggle Vector3f/ZERO)
rlm@0 603 "key-u" (gravity-toggle (Vector3f. 0 9.81 0))
rlm@0 604 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 605 "key-f" (fn[game value]
rlm@0 606 (if (not value) (add-element game (brick-wall*))))
rlm@0 607 "key-return" (fire-cannon-ball)}
rlm@0 608 position-camera
rlm@0 609 (fn [& _]))
rlm@0 610 (.start)))
rlm@0 611 #+end_src
rlm@0 612
rlm@0 613 #+begin_src clojure :results silent
rlm@0 614 (hello.brick-wall/brick-wall-game-run)
rlm@0 615 #+end_src
rlm@0 616
rlm@0 617 #+caption: the brick wall standing
rlm@0 618 [[./images/brick-wall-standing.jpg]]
rlm@0 619
rlm@0 620 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
rlm@0 621 [[./images/brick-wall-knocked-down.jpg]]
rlm@0 622
rlm@0 623 *** Other Brick Games
rlm@0 624 #+srcname: other-games
rlm@0 625 #+begin_src clojure :results silent
rlm@0 626 (ns cortex.other-games
rlm@0 627 {:author "Dylan Holmes"})
rlm@0 628 (use 'cortex.world)
rlm@0 629 (use 'hello.brick-wall)
rlm@0 630 (use 'cortex.import)
rlm@0 631 (cortex.import/mega-import-jme3)
rlm@0 632
rlm@0 633 (defn scad [position]
rlm@0 634 (doto (box 0.1 0.1 0.1
rlm@0 635 :position position :name "brick"
rlm@0 636 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 637 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 638 :mass 20)
rlm@0 639 (->
rlm@0 640 (.getMesh)
rlm@0 641 (.scaleTextureCoordinates (Vector2f. 1 0.5))
rlm@0 642 )
rlm@0 643 (-> (.getControl RigidBodyControl)
rlm@0 644 (.setLinearVelocity (Vector3f. 0 100 0))
rlm@0 645 )
rlm@0 646
rlm@0 647 ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
rlm@0 648 ))
rlm@0 649
rlm@0 650
rlm@0 651 (defn shrapnel []
rlm@0 652 (let [node (Node. "explosion-day")]
rlm@0 653 (dorun
rlm@0 654 (map
rlm@0 655 (comp #(.attachChild node %) scad)
rlm@0 656 (for [y (range 15)
rlm@0 657 x (range 4)
rlm@0 658 z (range 1)]
rlm@0 659 (Vector3f.
rlm@0 660 (+ (* 2 x brick-height)
rlm@0 661 (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
rlm@0 662 (+ (* brick-height (inc (* 2 y))))
rlm@0 663 (* 2 z brick-height) ))))
rlm@0 664 node))
rlm@0 665
rlm@0 666
rlm@0 667 (def domino-height 0.48)
rlm@0 668 (def domino-thickness 0.12)
rlm@0 669 (def domino-width 0.24)
rlm@0 670
rlm@0 671 (def domino-thickness 0.05)
rlm@0 672 (def domino-width 0.5)
rlm@0 673 (def domino-height 1)
rlm@0 674
rlm@0 675 (defn domino
rlm@0 676 ([position]
rlm@0 677 (domino position (Quaternion/IDENTITY)))
rlm@0 678 ([position rotation]
rlm@0 679 (doto (box domino-width domino-height domino-thickness
rlm@0 680 :position position :name "domino"
rlm@0 681 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 682 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 683 :mass 1
rlm@0 684 :rotation rotation)
rlm@0 685 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 686 )))
rlm@0 687
rlm@0 688
rlm@0 689 (defn domino-row []
rlm@0 690 (let [node (Node. "domino-row")]
rlm@0 691 (dorun
rlm@0 692 (map
rlm@0 693 (comp #(.attachChild node %) domino)
rlm@0 694 (for [
rlm@0 695 z (range 10)
rlm@0 696 x (range 5)
rlm@0 697 ]
rlm@0 698 (Vector3f.
rlm@0 699 (+ (* z domino-width) (* x 5 domino-width))
rlm@0 700 (/ domino-height 1)
rlm@0 701 (* -5.5 domino-thickness z) ))))
rlm@0 702
rlm@0 703 node))
rlm@0 704
rlm@0 705 (defn domino-cycle []
rlm@0 706 (let [node (Node. "domino-cycle")]
rlm@0 707 (dorun
rlm@0 708 (map
rlm@0 709 (comp #(.attachChild node %) (partial apply domino) )
rlm@0 710 (for [n (range 720)]
rlm@0 711 (let [space (* domino-height 5.5)
rlm@0 712 r (fn[n] (* (+ n 3) domino-width 0.5))
rlm@0 713 t (fn[n] (reduce
rlm@0 714 +
rlm@0 715 (map
rlm@0 716 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
rlm@0 717 (range n))))
rlm@0 718 t (t n)
rlm@0 719 r (r n)
rlm@0 720 ct (Math/cos t)
rlm@0 721 st (Math/sin t)
rlm@0 722 ]
rlm@0 723 (list
rlm@0 724 (Vector3f.
rlm@0 725 (* -1 r st)
rlm@0 726 (/ domino-height 1)
rlm@0 727 (* r ct))
rlm@0 728 (.fromAngleAxis (Quaternion.)
rlm@0 729 (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
rlm@0 730 )))
rlm@0 731 ))
rlm@0 732 node))
rlm@0 733
rlm@0 734
rlm@0 735 (defn domino-game-run []
rlm@0 736 (doto
rlm@0 737 (world
rlm@0 738 (doto (Node.) (.attachChild (floor*))
rlm@0 739 )
rlm@0 740 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 741 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 742 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 743 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 744 "key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
rlm@0 745 "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
rlm@0 746 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 747
rlm@0 748 "key-space"
rlm@0 749 (fn[game value]
rlm@0 750
rlm@0 751 (if (not value)
rlm@0 752 (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
rlm@0 753 (.fromAngleAxis (Quaternion.)
rlm@0 754 (/ Math/PI 2) (Vector3f. 0 1 0)))]
rlm@0 755 (add-element game d))))
rlm@0 756 "key-f"
rlm@0 757 (fn[game value](if (not value) (add-element game (domino-cycle))))
rlm@0 758 "key-return" (fire-cannon-ball)}
rlm@0 759 position-camera
rlm@0 760 (fn [& _]))
rlm@0 761 (.start)))
rlm@0 762 #+end_src
rlm@0 763
rlm@0 764 #+begin_src clojure :results silent
rlm@0 765 (cortex.other-games/domino-game-run)
rlm@0 766 #+end_src
rlm@0 767
rlm@0 768 #+caption: floating dominos
rlm@0 769 [[./images/dominos.jpg]]
rlm@0 770
rlm@0 771 *** Hello Loop
rlm@0 772 #+srcname: hello-loop
rlm@0 773 #+begin_src clojure :results silent
rlm@0 774 (ns hello.loop)
rlm@0 775 (use 'cortex.world)
rlm@0 776 (use 'cortex.import)
rlm@0 777 (cortex.import/mega-import-jme3)
rlm@0 778 (rlm.rlm-commands/help)
rlm@0 779
rlm@0 780 (defn blue-cube []
rlm@0 781 (box 1 1 1
rlm@0 782 :color ColorRGBA/Blue
rlm@0 783 :texture false
rlm@0 784 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 785 :name "blue-cube"
rlm@0 786 :physical? false))
rlm@0 787
rlm@0 788 (defn blue-cube-game []
rlm@0 789 (let [cube (blue-cube)
rlm@0 790 root (doto (Node.) (.attachChild cube))]
rlm@0 791 (world root
rlm@0 792 {}
rlm@0 793 no-op
rlm@0 794 (fn [game tpf]
rlm@0 795 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
rlm@0 796 #+end_src
rlm@0 797
rlm@0 798 *** Hello Collision
rlm@0 799
rlm@0 800 #+srcname: hello-collision
rlm@0 801 #+begin_src clojure :results silent
rlm@0 802 (ns hello.collision)
rlm@0 803 (use 'cortex.world)
rlm@0 804 (use 'cortex.import)
rlm@0 805 (use 'clojure.contrib.def)
rlm@0 806
rlm@0 807
rlm@0 808 (cortex.import/mega-import-jme3)
rlm@0 809 (rlm.rlm-commands/help)
rlm@0 810 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 811
rlm@0 812
rlm@0 813 (defn environment []
rlm@0 814 (let
rlm@0 815 [scene-model
rlm@0 816 (doto
rlm@0 817 (.loadModel
rlm@0 818 (doto (asset-manager)
rlm@0 819 (.registerLocator
rlm@0 820 "/home/r/cortex/assets/zips/town.zip" ZipLocator))
rlm@0 821 "main.scene")
rlm@0 822 (.setLocalScale (float 2.0)))
rlm@0 823 collision-shape
rlm@0 824 (CollisionShapeFactory/createMeshShape #^Node scene-model)
rlm@0 825 landscape (RigidBodyControl. collision-shape 0)]
rlm@0 826 (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
rlm@0 827 (.addControl scene-model landscape)
rlm@0 828 scene-model))
rlm@0 829
rlm@0 830 (defn player-fn []
rlm@0 831 (doto
rlm@0 832 (CharacterControl.
rlm@0 833 (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
rlm@0 834 (float 0.05))
rlm@0 835 (.setJumpSpeed 20)
rlm@0 836 (.setFallSpeed 30)
rlm@0 837 (.setGravity 30) ;30
rlm@0 838 (.setPhysicsLocation (Vector3f. 0 10 0))))
rlm@0 839
rlm@0 840 (defn lights []
rlm@0 841 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
rlm@0 842 (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
rlm@0 843 (doto (DirectionalLight.)
rlm@0 844 (.setColor (.mult ColorRGBA/White (float 0.9) ))
rlm@0 845 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 846
rlm@0 847 (defn night-lights []
rlm@0 848 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
rlm@0 849 (doto (DirectionalLight.)
rlm@0 850 (.setColor (.mult ColorRGBA/White (float 0.2) ))
rlm@0 851 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 852
rlm@0 853 (def player (atom (player-fn)))
rlm@0 854
rlm@0 855 (defn setup-fn [game]
rlm@0 856 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 857 ;; set the color of the sky
rlm@0 858 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 859 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 860 (doto (.getFlyByCamera game)
rlm@0 861 (.setMoveSpeed (float 100))
rlm@0 862 (.setRotationSpeed 3))
rlm@0 863 (.add
rlm@0 864 (.getPhysicsSpace
rlm@0 865 (.getState (.getStateManager game) BulletAppState))
rlm@0 866 @player)
rlm@0 867
rlm@0 868 (doto (Node.) (.attachChild (.getRootNode game))
rlm@0 869 (.attachChild (brick-wall*))
rlm@0 870 )
rlm@0 871
rlm@0 872 )
rlm@0 873
rlm@0 874
rlm@0 875 (def walking-up? (atom false))
rlm@0 876 (def walking-down? (atom false))
rlm@0 877 (def walking-left? (atom false))
rlm@0 878 (def walking-right? (atom false))
rlm@0 879
rlm@0 880 (defn set-walk [walk-atom game value]
rlm@0 881 ;;(println-repl "setting stuff to " value)
rlm@0 882 (reset! walk-atom value))
rlm@0 883
rlm@0 884 (defn responses []
rlm@0 885 {"key-w" (partial set-walk walking-up?)
rlm@0 886 "key-d" (partial set-walk walking-right?)
rlm@0 887 "key-s" (partial set-walk walking-down?)
rlm@0 888 "key-a" (partial set-walk walking-left?)
rlm@0 889 "key-return" (fire-cannon-ball)
rlm@0 890 "key-space" (fn [game value] (.jump @player))
rlm@0 891 })
rlm@0 892
rlm@0 893 (defn update-fn
rlm@0 894 [game tpf]
rlm@0 895 (let [camera (.getCamera game)
rlm@0 896 cam-dir (.multLocal
rlm@0 897 (.clone
rlm@0 898 (.getDirection camera)) (float 0.6))
rlm@0 899 cam-left (.multLocal
rlm@0 900 (.clone
rlm@0 901 (.getLeft camera)) (float 0.4))
rlm@0 902 walk-direction (Vector3f. 0 0 0)]
rlm@0 903
rlm@0 904 (cond
rlm@0 905 @walking-up? (.addLocal walk-direction cam-dir)
rlm@0 906 @walking-right? (.addLocal walk-direction (.negate cam-left))
rlm@0 907 @walking-down? (.addLocal walk-direction (.negate cam-dir))
rlm@0 908 @walking-left? (.addLocal walk-direction cam-left))
rlm@0 909 (.setWalkDirection @player walk-direction)
rlm@0 910 (.setLocation camera (.getPhysicsLocation @player))))
rlm@0 911
rlm@0 912 (defn run-game []
rlm@0 913 (.start
rlm@0 914 (world (environment)
rlm@0 915 (responses)
rlm@0 916 setup-fn
rlm@0 917 update-fn)))
rlm@0 918 #+end_src
rlm@0 919
rlm@0 920 *** Hello Terrain
rlm@0 921 #+srcname: hello-terrain
rlm@0 922 #+begin_src clojure :results silent
rlm@0 923 (ns hello.terrain)
rlm@0 924 (use 'cortex.world)
rlm@0 925 (use 'cortex.import)
rlm@0 926 (use 'clojure.contrib.def)
rlm@0 927 (import jme3tools.converters.ImageToAwt)
rlm@0 928
rlm@0 929
rlm@0 930 (cortex.import/mega-import-jme3)
rlm@0 931 (rlm.rlm-commands/help)
rlm@0 932 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 933
rlm@0 934
rlm@0 935 (defn setup-fn [type game]
rlm@0 936 (.setMoveSpeed (.getFlyByCamera game) 50)
rlm@0 937 (.setFrustumFar (.getCamera game) 10000)
rlm@0 938 (let [env (environment type)
rlm@0 939 cameras [(.getCamera game)]
rlm@0 940 control (TerrainLodControl. env cameras)]
rlm@0 941 ;;(.addControl env control)
rlm@0 942 (.attachChild (.getRootNode game) env)))
rlm@0 943
rlm@0 944 (defn environment [type]
rlm@0 945 (let
rlm@0 946 [mat_terrain
rlm@0 947 (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
rlm@0 948 grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
rlm@0 949 dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
rlm@0 950 rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
rlm@0 951 heightmap-image (.loadTexture (asset-manager)
rlm@0 952 ({:mountain "Textures/Terrain/splat/mountains512.png"
rlm@0 953 :fortress "Textures/Terrain/splat/fortress512.png"
rlm@0 954 }type))
rlm@0 955 heightmap (ImageBasedHeightMap.
rlm@0 956 (ImageToAwt/convert (.getImage heightmap-image) false true 0))
rlm@0 957 terrain (do (.load heightmap)
rlm@0 958 (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
rlm@0 959 ]
rlm@0 960
rlm@0 961 (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
rlm@0 962 [grass dirt rock]))
rlm@0 963
rlm@0 964 (doto mat_terrain
rlm@0 965 (.setTexture "Tex1" grass)
rlm@0 966 (.setFloat "Tex1Scale" (float 64))
rlm@0 967
rlm@0 968 (.setTexture "Tex2" dirt)
rlm@0 969 (.setFloat "Tex2Scale" (float 32))
rlm@0 970
rlm@0 971 (.setTexture "Tex3" rock)
rlm@0 972 (.setFloat "Tex3Scale" (float 128))
rlm@0 973
rlm@0 974 (.setTexture "Alpha"
rlm@0 975 (.loadTexture
rlm@0 976 (asset-manager)
rlm@0 977 ({:mountain "Textures/Terrain/splat/alphamap.png"
rlm@0 978 :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
rlm@0 979
rlm@0 980 (doto terrain
rlm@0 981 (.setMaterial mat_terrain)
rlm@0 982 (.setLocalTranslation 0 -100 0)
rlm@0 983 (.setLocalScale 2 1 2))))
rlm@0 984
rlm@0 985
rlm@0 986
rlm@0 987 (defn run-terrain-game [type]
rlm@0 988 (.start
rlm@0 989 (world
rlm@0 990 (Node.)
rlm@0 991 {}
rlm@0 992 (partial setup-fn type)
rlm@0 993 no-op)))
rlm@0 994 #+end_src
rlm@0 995
rlm@0 996
rlm@0 997
rlm@0 998 #+srcname: hello-animation
rlm@0 999 #+begin_src clojure :results silent
rlm@0 1000 (ns hello.animation)
rlm@0 1001 (use 'cortex.world)
rlm@0 1002 (use 'cortex.import)
rlm@0 1003 (use 'clojure.contrib.def)
rlm@0 1004 (cortex.import/mega-import-jme3)
rlm@0 1005 (rlm.rlm-commands/help)
rlm@0 1006 (use '[hello [collision :only [lights]]])
rlm@0 1007
rlm@0 1008 (defn stand
rlm@0 1009 [channel]
rlm@0 1010 (doto channel
rlm@0 1011 (.setAnim "stand" (float 0.5))
rlm@0 1012 (.setLoopMode LoopMode/DontLoop)
rlm@0 1013 (.setSpeed (float 1))))
rlm@0 1014
rlm@0 1015 (defn anim-listener []
rlm@0 1016 (proxy [AnimEventListener] []
rlm@0 1017 (onAnimChange
rlm@0 1018 [control channel animation-name]
rlm@0 1019 (println-repl "RLM --- onAnimChange"))
rlm@0 1020 (onAnimCycleDone
rlm@0 1021 [control channel animation-name]
rlm@0 1022 (if (= animation-name "Walk")
rlm@0 1023 (stand channel)
rlm@0 1024 ))))
rlm@0 1025
rlm@0 1026 (defn setup-fn [channel game]
rlm@0 1027 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 1028 ;; set the color of the sky
rlm@0 1029 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 1030 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 1031 (.setAnim channel "stand")
rlm@0 1032 (doto (.getFlyByCamera game)
rlm@0 1033 (.setMoveSpeed (float 10))
rlm@0 1034 (.setRotationSpeed 1)))
rlm@0 1035
rlm@0 1036 (defn walk [channel]
rlm@0 1037 (println-repl "zzz")
rlm@0 1038 (doto channel
rlm@0 1039 (.setAnim "Walk" (float 0.5))
rlm@0 1040 (.setLoopMode LoopMode/Loop)))
rlm@0 1041
rlm@0 1042
rlm@0 1043 (defn key-map [channel]
rlm@0 1044 {"key-space" (fn [game value]
rlm@0 1045 (if (not value)
rlm@0 1046 (walk channel)))})
rlm@0 1047
rlm@0 1048 (defn player []
rlm@0 1049 (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
rlm@0 1050 control (.getControl model AnimControl)]
rlm@0 1051 (.setLocalScale model (float 0.5))
rlm@0 1052 (.clearListeners control)
rlm@0 1053 (.addListener control (anim-control))
rlm@0 1054 model))
rlm@0 1055
rlm@0 1056
rlm@0 1057
rlm@0 1058 (defn run-anim-game []
rlm@0 1059 (let [ninja (player)
rlm@0 1060 control (.getControl ninja AnimControl)
rlm@0 1061 channel (.createChannel control)]
rlm@0 1062 (.start
rlm@0 1063 (world
rlm@0 1064 ninja
rlm@0 1065 (key-map channel)
rlm@0 1066 (partial setup-fn channel)
rlm@0 1067 no-op))))
rlm@0 1068 #+end_src
rlm@0 1069
rlm@0 1070 *** Hello Materials
rlm@0 1071 #+srcname: material
rlm@0 1072 #+begin_src clojure :results silent
rlm@0 1073 (ns hello.material)
rlm@0 1074 (use 'cortex.world)
rlm@0 1075 (use 'cortex.import)
rlm@0 1076 (use 'clojure.contrib.def)
rlm@0 1077 (cortex.import/mega-import-jme3)
rlm@0 1078 (rlm.rlm-commands/help)
rlm@0 1079
rlm@0 1080 (defn simple-cube []
rlm@0 1081 (box 1 1 1
rlm@0 1082 :position (Vector3f. -3 1.1 0)
rlm@0 1083 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1084 :texture "Interface/Logo/Monkey.jpg"
rlm@0 1085 :physical? false))
rlm@0 1086
rlm@0 1087 (defn leaky-box []
rlm@0 1088 (box 1 1 1
rlm@0 1089 :position (Vector3f. 3 -1 0)
rlm@0 1090 :material "Common/MatDefs/Misc/ColoredTextured.j3md"
rlm@0 1091 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 1092 :color (ColorRGBA. 1 0 1 1)
rlm@0 1093 :physical? false))
rlm@0 1094
rlm@0 1095 (defn transparent-box []
rlm@0 1096 (doto
rlm@0 1097 (box 1 1 0.1
rlm@0 1098 :position Vector3f/ZERO
rlm@0 1099 :name "window frame"
rlm@0 1100 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 1101 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 1102 :physical? false)
rlm@0 1103 (-> (.getMaterial)
rlm@0 1104 (.getAdditionalRenderState)
rlm@0 1105 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 1106 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 1107
rlm@0 1108 (defn bumpy-sphere []
rlm@0 1109 (doto
rlm@0 1110 (sphere 2
rlm@0 1111 :position (Vector3f. 0 2 -2)
rlm@0 1112 :name "Shiny rock"
rlm@0 1113 :material "Common/MatDefs/Light/Lighting.j3md"
rlm@0 1114 :texture false
rlm@0 1115 :physical? false)
rlm@0 1116 (-> (.getMesh)
rlm@0 1117 (doto
rlm@0 1118 (.setTextureMode Sphere$TextureMode/Projected)
rlm@0 1119 (TangentBinormalGenerator/generate)))
rlm@0 1120 (-> (.getMaterial)
rlm@0 1121 (doto
rlm@0 1122 (.setTexture "DiffuseMap" (.loadTexture (asset-manager)
rlm@0 1123 "Textures/Terrain/Pond/Pond.png"))
rlm@0 1124 (.setTexture "NormalMap" (.loadTexture (asset-manager)
rlm@0 1125 "Textures/Terrain/Pond/Pond_normal.png"))
rlm@0 1126 (.setFloat "Shininess" (float 5))))
rlm@0 1127 (.rotate (float 1.6) 0 0)))
rlm@0 1128
rlm@0 1129
rlm@0 1130 (defn start-game []
rlm@0 1131 (.start
rlm@0 1132 (world
rlm@0 1133 (let [root (Node.)]
rlm@0 1134 (dorun (map #(.attachChild root %)
rlm@0 1135 [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
rlm@0 1136 root)
rlm@0 1137 {}
rlm@0 1138 (fn [world]
rlm@0 1139 (let [sun (doto (DirectionalLight.)
rlm@0 1140 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 1141 (.setColor ColorRGBA/White))]
rlm@0 1142 (.addLight (.getRootNode world) sun)))
rlm@0 1143 no-op
rlm@0 1144 )))
rlm@0 1145 #+end_src
rlm@0 1146
rlm@0 1147
rlm@0 1148
rlm@0 1149 * The Body
rlm@0 1150 ** Eyes
rlm@0 1151
rlm@0 1152 Ultimately I want to make creatures with eyes. Each eye can be
rlm@0 1153 independely moved and should see its own version of the world
rlm@0 1154 depending on where it is.
rlm@0 1155 #+srcname: eyes
rlm@0 1156 #+begin_src clojure
rlm@0 1157 (ns body.eye)
rlm@0 1158 (use 'cortex.world)
rlm@0 1159 (use 'cortex.import)
rlm@0 1160 (use 'clojure.contrib.def)
rlm@0 1161 (cortex.import/mega-import-jme3)
rlm@0 1162 (rlm.rlm-commands/help)
rlm@0 1163 (import java.nio.ByteBuffer)
rlm@0 1164 (import java.awt.image.BufferedImage)
rlm@0 1165 (import java.awt.Color)
rlm@0 1166 (import java.awt.Dimension)
rlm@0 1167 (import java.awt.Graphics)
rlm@0 1168 (import java.awt.Graphics2D)
rlm@0 1169 (import java.awt.event.WindowAdapter)
rlm@0 1170 (import java.awt.event.WindowEvent)
rlm@0 1171 (import java.awt.image.BufferedImage)
rlm@0 1172 (import java.nio.ByteBuffer)
rlm@0 1173 (import javax.swing.JFrame)
rlm@0 1174 (import javax.swing.JPanel)
rlm@0 1175 (import javax.swing.SwingUtilities)
rlm@0 1176 (import javax.swing.ImageIcon)
rlm@0 1177 (import javax.swing.JOptionPane)
rlm@0 1178 (import java.awt.image.ImageObserver)
rlm@0 1179
rlm@0 1180
rlm@0 1181
rlm@0 1182 (defn scene-processor
rlm@0 1183 "deals with converting FrameBuffers to BufferedImages so
rlm@0 1184 that the continuation function can be defined only in terms
rlm@0 1185 of what it does with BufferedImages"
rlm@0 1186 [continuation]
rlm@0 1187 (let [byte-buffer (atom nil)
rlm@0 1188 renderer (atom nil)
rlm@0 1189 image (atom nil)]
rlm@0 1190 (proxy [SceneProcessor] []
rlm@0 1191 (initialize
rlm@0 1192 [renderManager viewPort]
rlm@0 1193 (let [cam (.getCamera viewPort)
rlm@0 1194 width (.getWidth cam)
rlm@0 1195 height (.getHeight cam)]
rlm@0 1196 (reset! renderer (.getRenderer renderManager))
rlm@0 1197 (reset! byte-buffer
rlm@0 1198 (BufferUtils/createByteBuffer
rlm@0 1199 (* width height 4)))
rlm@0 1200 (reset! image (BufferedImage. width height
rlm@0 1201 BufferedImage/TYPE_4BYTE_ABGR))))
rlm@0 1202 (isInitialized [] (not (nil? @byte-buffer)))
rlm@0 1203 (reshape [_ _ _])
rlm@0 1204 (preFrame [_])
rlm@0 1205 (postQueue [_])
rlm@0 1206 (postFrame
rlm@0 1207 [#^FrameBuffer fb]
rlm@0 1208 (.clear @byte-buffer)
rlm@0 1209 (.readFrameBuffer @renderer fb @byte-buffer)
rlm@0 1210 (Screenshots/convertScreenShot @byte-buffer @image)
rlm@0 1211 (continuation @image))
rlm@0 1212 (cleanup []))))
rlm@0 1213
rlm@0 1214 (defn add-eye
rlm@0 1215 "Add an eye to the world, and call continuation on
rlm@0 1216 every frame produced"
rlm@0 1217 [world camera continuation]
rlm@0 1218 (let [width (.getWidth camera)
rlm@0 1219 height (.getHeight camera)
rlm@0 1220 render-manager (.getRenderManager world)
rlm@0 1221 viewport (.createMainView render-manager "eye-view" camera)]
rlm@0 1222 (doto viewport
rlm@0 1223 (.setBackgroundColor ColorRGBA/Black)
rlm@0 1224 (.setClearFlags true true true)
rlm@0 1225 (.addProcessor (scene-processor continuation))
rlm@0 1226 (.attachScene (.getRootNode world)))))
rlm@0 1227
rlm@0 1228 (defn make-display-frame [display width height]
rlm@0 1229 (SwingUtilities/invokeLater
rlm@0 1230 (fn []
rlm@0 1231 (.setPreferredSize display (Dimension. width height))
rlm@0 1232 (doto (JFrame. "Eye Camera!")
rlm@0 1233 (-> (.getContentPane) (.add display))
rlm@0 1234 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
rlm@0 1235 (.pack)
rlm@0 1236 (.setLocationRelativeTo nil)
rlm@0 1237 (.setResizable false)
rlm@0 1238 (.setVisible true)))))
rlm@0 1239
rlm@0 1240 (defn image-monitor [#^BufferedImage image]
rlm@0 1241 (proxy [JPanel] []
rlm@0 1242 (paintComponent
rlm@0 1243 [g]
rlm@0 1244 (proxy-super paintComponent g)
rlm@0 1245 (locking image
rlm@0 1246 (.drawImage g image 0 0
rlm@0 1247 (proxy [ImageObserver]
rlm@0 1248 []
rlm@0 1249 (imageUpdate
rlm@0 1250 []
rlm@0 1251 (proxy-super imageUpdate))))))))
rlm@0 1252
rlm@0 1253 (defn movie-image []
rlm@0 1254 (let [setup
rlm@0 1255 (runonce
rlm@0 1256 (fn [#^BufferedImage image]
rlm@0 1257 (let [width (.getWidth image)
rlm@0 1258 height (.getHeight image)
rlm@0 1259 display (image-monitor image)
rlm@0 1260 frame (make-display-frame display width height)]
rlm@0 1261 display)))]
rlm@0 1262 (fn [#^BufferedImage image]
rlm@0 1263 (.repaint (setup image)))))
rlm@0 1264
rlm@0 1265
rlm@0 1266 (defn observer
rlm@0 1267 "place thy eye!"
rlm@0 1268 [world camera]
rlm@0 1269 (let [eye camera
rlm@0 1270 width (.getWidth eye)
rlm@0 1271 height (.getHeight eye)]
rlm@0 1272 (no-exceptions
rlm@0 1273 (add-eye
rlm@0 1274 world
rlm@0 1275 eye
rlm@0 1276 (movie-image)))))
rlm@0 1277 #+end_src
rlm@0 1278
rlm@0 1279 #+srcname: test-vision
rlm@0 1280 #+begin_src clojure
rlm@0 1281
rlm@0 1282 (ns test.vision)
rlm@0 1283 (use 'cortex.world)
rlm@0 1284 (use 'cortex.import)
rlm@0 1285 (use 'clojure.contrib.def)
rlm@0 1286 (use 'body.eye)
rlm@0 1287 (cortex.import/mega-import-jme3)
rlm@0 1288 (rlm.rlm-commands/help)
rlm@0 1289 (import java.nio.ByteBuffer)
rlm@0 1290 (import java.awt.image.BufferedImage)
rlm@0 1291 (import java.awt.Color)
rlm@0 1292 (import java.awt.Dimension)
rlm@0 1293 (import java.awt.Graphics)
rlm@0 1294 (import java.awt.Graphics2D)
rlm@0 1295 (import java.awt.event.WindowAdapter)
rlm@0 1296 (import java.awt.event.WindowEvent)
rlm@0 1297 (import java.awt.image.BufferedImage)
rlm@0 1298 (import java.nio.ByteBuffer)
rlm@0 1299 (import javax.swing.JFrame)
rlm@0 1300 (import javax.swing.JPanel)
rlm@0 1301 (import javax.swing.SwingUtilities)
rlm@0 1302 (import javax.swing.ImageIcon)
rlm@0 1303 (import javax.swing.JOptionPane)
rlm@0 1304 (import java.awt.image.ImageObserver)
rlm@0 1305
rlm@0 1306
rlm@0 1307 (def width 200)
rlm@0 1308 (def height 200)
rlm@0 1309
rlm@0 1310 (defn camera []
rlm@0 1311 (doto (Camera. width height)
rlm@0 1312 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1313 (.setLocation (Vector3f. -3 0 -5))
rlm@0 1314 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1315
rlm@0 1316 (defn camera2 []
rlm@0 1317 (doto (Camera. width height)
rlm@0 1318 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1319 (.setLocation (Vector3f. 3 0 -5))
rlm@0 1320 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1321
rlm@0 1322 (defn setup-fn [world]
rlm@0 1323 (let [eye (camera)
rlm@0 1324 width (.getWidth eye)
rlm@0 1325 height (.getHeight eye)]
rlm@0 1326 (no-exceptions
rlm@0 1327 (add-eye
rlm@0 1328 world
rlm@0 1329 eye
rlm@0 1330 (runonce visual))
rlm@0 1331 (add-eye
rlm@0 1332 world
rlm@0 1333 (camera2)
rlm@0 1334 (runonce visual)))))
rlm@0 1335
rlm@0 1336 (defn spider-eye [position]
rlm@0 1337 (doto (Camera. 200 200 )
rlm@0 1338 (.setFrustumPerspective 45 1 1 1000)
rlm@0 1339 (.setLocation position)
rlm@0 1340 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
rlm@0 1341
rlm@0 1342 (defn setup-fn* [world]
rlm@0 1343 (let [eye (camera)
rlm@0 1344 width (.getWidth eye)
rlm@0 1345 height (.getHeight eye)]
rlm@0 1346 ;;(.setClearFlags (.getViewPort world) true true true)
rlm@0 1347 (observer world (.getCamera world))
rlm@0 1348 (observer world (spider-eye (Vector3f. 3 0 -5)))
rlm@0 1349 ;;(observer world (spider-eye (Vector3f. 0 0 -5)))
rlm@0 1350 ;; (observer world (spider-eye (Vector3f. -3 0 -5)))
rlm@0 1351 ;; (observer world (spider-eye (Vector3f. 0 3 -5)))
rlm@0 1352 ;; (observer world (spider-eye (Vector3f. 0 -3 -5)))
rlm@0 1353 ;; (observer world (spider-eye (Vector3f. 3 3 -5)))
rlm@0 1354 ;; (observer world (spider-eye (Vector3f. -3 3 -5)))
rlm@0 1355 ;; (observer world (spider-eye (Vector3f. 3 -3 -5)))
rlm@0 1356 ;; (observer world (spider-eye (Vector3f. -3 -3 -5)))
rlm@0 1357
rlm@0 1358 )
rlm@0 1359 world)
rlm@0 1360
rlm@0 1361 (defn test-world []
rlm@0 1362 (let [thing (box 1 1 1 :physical? false)]
rlm@0 1363 (world
rlm@0 1364 (doto (Node.)
rlm@0 1365 (.attachChild thing))
rlm@0 1366 {}
rlm@0 1367 setup-fn
rlm@0 1368 (fn [world tpf]
rlm@0 1369 (.rotate thing (* tpf 0.2) 0 0)
rlm@0 1370 ))))
rlm@0 1371
rlm@0 1372
rlm@0 1373 #+end_src
rlm@0 1374
rlm@0 1375
rlm@0 1376 #+results: eyes
rlm@0 1377 : #'body.eye/test-world
rlm@0 1378
rlm@0 1379 Note the use of continuation passing style for connecting the eye to a
rlm@0 1380 function to process the output. The example code will create two
rlm@0 1381 videos of the same rotating cube from different angles, sutiable for
rlm@0 1382 stereoscopic vision.
rlm@0 1383
rlm@0 1384
rlm@0 1385
rlm@0 1386
rlm@0 1387
rlm@0 1388
rlm@0 1389 * COMMENT code generation
rlm@0 1390 #+begin_src clojure :tangle ../src/cortex/import.clj
rlm@0 1391 <<import>>
rlm@0 1392 #+end_src
rlm@0 1393
rlm@0 1394 #+begin_src clojure :tangle ../src/hello/brick_wall.clj
rlm@0 1395 <<brick-wall-header>>
rlm@0 1396 <<brick-wall-body>>
rlm@0 1397 #+end_src
rlm@0 1398
rlm@0 1399 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
rlm@0 1400 <<hello-simple-app>>
rlm@0 1401 #+end_src
rlm@0 1402
rlm@0 1403 #+begin_src clojure :tangle ../src/cortex/world.clj
rlm@0 1404 <<world-inputs>>
rlm@0 1405 <<world>>
rlm@0 1406 <<world-shapes>>
rlm@0 1407 <<world-view>>
rlm@0 1408 #+end_src
rlm@0 1409
rlm@0 1410 #+begin_src clojure :tangle ../src/cortex/other_games.clj
rlm@0 1411 <<other-games>>
rlm@0 1412 #+end_src
rlm@0 1413
rlm@0 1414 #+begin_src clojure :tangle ../src/hello/loop.clj
rlm@0 1415 <<hello-loop>>
rlm@0 1416 #+end_src
rlm@0 1417
rlm@0 1418 #+begin_src clojure :tangle ../src/hello/collision.clj
rlm@0 1419 <<hello-collision>>
rlm@0 1420 #+end_src
rlm@0 1421
rlm@0 1422 #+begin_src clojure :tangle ../src/hello/terrain.clj
rlm@0 1423 <<hello-terrain>>
rlm@0 1424 #+end_src
rlm@0 1425
rlm@0 1426 #+begin_src clojure :tangle ../src/hello/animation.clj
rlm@0 1427 <<hello-animation>>
rlm@0 1428 #+end_src
rlm@0 1429
rlm@0 1430 #+begin_src clojure :tangle ../src/hello/material.clj
rlm@0 1431 <<material>>
rlm@0 1432 #+end_src
rlm@0 1433
rlm@0 1434 #+begin_src clojure :tangle ../src/body/eye.clj
rlm@0 1435 <<eyes>>
rlm@0 1436 #+end_src
rlm@0 1437
rlm@0 1438 #+begin_src clojure :tangle ../src/test/vision.clj
rlm@0 1439 <<test-vision>>
rlm@0 1440 #+end_src
rlm@0 1441
rlm@0 1442
rlm@0 1443