annotate org/util.org @ 459:a86555b02916

changes from laptop.
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Mar 2014 17:56:26 -0400
parents 42ddfe406c0a
children 763d13f77e03
rev   line source
rlm@29 1 #+title: Clojure Utilities for jMonkeyEngine3
rlm@23 2 #+author: Robert McIntyre
rlm@23 3 #+email: rlm@mit.edu
rlm@29 4 #+description:
rlm@29 5 #+keywords: JME3, clojure, import, utilities
rlm@23 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@23 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@29 8
rlm@34 9 [TABLE-OF-CONTENTS]
rlm@29 10
rlm@29 11 These are a collection of functions to make programming jMonkeyEngine
rlm@29 12 in clojure easier.
rlm@23 13
rlm@34 14 * Imports
rlm@28 15
rlm@66 16 #+name: import
rlm@23 17 #+begin_src clojure :results silent
rlm@23 18 (ns cortex.import
rlm@388 19 (:import java.io.File java.util.jar.JarFile))
rlm@23 20
rlm@23 21 (defn permissive-import
rlm@23 22 [classname]
rlm@23 23 (eval `(try (import '~classname)
rlm@23 24 (catch java.lang.Exception e#
rlm@23 25 (println "couldn't import " '~classname))))
rlm@23 26 classname)
rlm@23 27
rlm@23 28 (defn jme-class? [classname]
rlm@23 29 (and
rlm@23 30 (.startsWith classname "com.jme3.")
rlm@306 31 ;; Don't import the LWJGL stuff since it can throw exceptions
rlm@23 32 ;; upon being loaded.
rlm@23 33 (not (re-matches #".*Lwjgl.*" classname))))
rlm@23 34
rlm@388 35 (defn jme-jars []
rlm@388 36 (map
rlm@388 37 #(JarFile. (File. %))
rlm@388 38 (filter (partial re-matches #".*jME3.*")
rlm@388 39 (clojure.string/split
rlm@388 40 (System/getProperty "java.class.path") #":"))))
rlm@388 41
rlm@388 42 (defn jme-class-names []
rlm@23 43 (filter
rlm@23 44 jme-class?
rlm@388 45 (map
rlm@388 46 (comp
rlm@388 47 #(.replace % File/separator ".")
rlm@388 48 #(clojure.string/replace % ".class" ""))
rlm@388 49 (filter
rlm@388 50 (partial re-matches #".*\.class$")
rlm@388 51 (mapcat
rlm@388 52 #(map
rlm@388 53 str
rlm@388 54 (enumeration-seq
rlm@388 55 (.entries %)))
rlm@388 56 (jme-jars))))))
rlm@388 57
rlm@23 58 (defn mega-import-jme3
rlm@23 59 "Import ALL the jme classes. For REPL use."
rlm@23 60 []
rlm@388 61 (dorun
rlm@459 62 (import com.aurellem.capture.IsoTimer)
rlm@388 63 (map (comp permissive-import symbol) (jme-class-names))))
rlm@23 64 #+end_src
rlm@23 65
rlm@29 66 jMonkeyEngine3 has a plethora of classes which can be overwhelming to
rlm@29 67 manage. This code uses reflection to import all of them. Once I'm
rlm@29 68 happy with the general structure of a namespace I can deal with
rlm@29 69 importing only the classes it actually needs.
rlm@29 70
rlm@306 71 The =mega-import-jme3= is quite useful for debugging purposes since
rlm@34 72 it allows completion for almost all of JME's classes from the REPL.
rlm@23 73
rlm@306 74 Out of curiosity, let's see just how many classes =mega-import-jme3=
rlm@23 75 imports:
rlm@23 76
rlm@29 77 #+begin_src clojure :exports both :results output
rlm@455 78 (println (clojure.core/count (cortex.import/jme-class-names)) "classes")
rlm@23 79 #+end_src
rlm@23 80
rlm@23 81 #+results:
rlm@455 82 : 938 classes
rlm@23 83
rlm@25 84
rlm@34 85 * Utilities
rlm@23 86
rlm@29 87 The utilities here come in three main groups:
rlm@29 88 - Changing settings in a running =Application=
rlm@29 89 - Creating objects
rlm@50 90 - Debug Actions
rlm@29 91 - Visualizing objects
rlm@23 92
rlm@29 93 *** Changing Settings
rlm@24 94
rlm@66 95 #+name: util
rlm@25 96 #+begin_src clojure
rlm@29 97 (ns cortex.util
rlm@34 98 "Utility functions for making jMonkeyEngine3 easier to program from
rlm@34 99 clojure."
rlm@29 100 {:author "Robert McIntyre"}
rlm@29 101 (:use cortex.world)
rlm@29 102 (:import com.jme3.math.Vector3f)
rlm@29 103 (:import com.jme3.math.Quaternion)
rlm@29 104 (:import com.jme3.asset.TextureKey)
rlm@29 105 (:import com.jme3.bullet.control.RigidBodyControl)
rlm@29 106 (:import com.jme3.bullet.collision.shapes.GImpactCollisionShape)
rlm@29 107 (:import com.jme3.scene.shape.Box)
rlm@29 108 (:import com.jme3.scene.Node)
rlm@29 109 (:import com.jme3.scene.shape.Sphere)
rlm@47 110 (:import com.jme3.light.AmbientLight)
rlm@29 111 (:import com.jme3.light.DirectionalLight)
rlm@114 112 (:import (com.jme3.math Triangle ColorRGBA))
rlm@29 113 (:import com.jme3.bullet.BulletAppState)
rlm@29 114 (:import com.jme3.material.Material)
rlm@40 115 (:import com.jme3.scene.Geometry)
rlm@114 116 (:import java.awt.image.BufferedImage)
rlm@114 117 (:import javax.swing.JPanel)
rlm@114 118 (:import javax.swing.JFrame)
rlm@335 119 (:import ij.ImagePlus)
rlm@114 120 (:import javax.swing.SwingUtilities)
rlm@192 121 (:import com.jme3.scene.plugins.blender.BlenderModelLoader)
rlm@40 122 (:import (java.util.logging Level Logger)))
rlm@40 123
rlm@320 124 (def println-repl
rlm@29 125 "println called from the LWJGL thread will not go to the REPL, but
rlm@114 126 instead to whatever terminal started the JVM process. This function
rlm@320 127 will always output to the REPL"
rlm@320 128 (bound-fn [& args] (apply println args)))
rlm@25 129
rlm@29 130 (defn position-camera
rlm@34 131 "Change the position of the in-world camera."
rlm@297 132 [world #^Vector3f position #^Quaternion rotation]
rlm@34 133 (doto (.getCamera world)
rlm@297 134 (.setLocation position)
rlm@297 135 (.setRotation rotation)))
rlm@25 136
rlm@29 137 (defn enable-debug
rlm@34 138 "Turn on debug wireframes for every object in this simulation."
rlm@29 139 [world]
rlm@29 140 (.enableDebug
rlm@29 141 (.getPhysicsSpace
rlm@29 142 (.getState
rlm@29 143 (.getStateManager world)
rlm@29 144 BulletAppState))
rlm@29 145 (asset-manager)))
rlm@29 146
rlm@78 147 (defn speed-up
rlm@78 148 "Increase the dismally slow speed of the world's camera."
rlm@78 149 [world]
rlm@78 150 (.setMoveSpeed (.getFlyByCamera world)
rlm@122 151 (float 60))
rlm@78 152 (.setRotationSpeed (.getFlyByCamera world)
rlm@122 153 (float 3))
rlm@78 154 world)
rlm@78 155
rlm@78 156
rlm@40 157 (defn no-logging
rlm@40 158 "Disable all of jMonkeyEngine's logging."
rlm@40 159 []
rlm@40 160 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@40 161
rlm@40 162 (defn set-accuracy
rlm@40 163 "Change the accuracy at which the World's Physics is calculated."
rlm@40 164 [world new-accuracy]
rlm@40 165 (let [physics-manager
rlm@40 166 (.getState
rlm@40 167 (.getStateManager world) BulletAppState)]
rlm@40 168 (.setAccuracy
rlm@40 169 (.getPhysicsSpace physics-manager)
rlm@40 170 (float new-accuracy))))
rlm@40 171
rlm@40 172
rlm@34 173 (defn set-gravity
rlm@29 174 "In order to change the gravity of a scene, it is not only necessary
rlm@29 175 to set the gravity variable, but to \"tap\" every physics object in
rlm@29 176 the scene to reactivate physics calculations."
rlm@34 177 [world gravity]
rlm@25 178 (traverse
rlm@25 179 (fn [geom]
rlm@25 180 (if-let
rlm@29 181 ;; only set gravity for physical objects.
rlm@25 182 [control (.getControl geom RigidBodyControl)]
rlm@25 183 (do
rlm@25 184 (.setGravity control gravity)
rlm@29 185 ;; tappsies!
rlm@29 186 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO))))
rlm@34 187 (.getRootNode world)))
rlm@29 188
rlm@29 189 (defn add-element
rlm@34 190 "Add the Spatial to the world's environment"
rlm@34 191 ([world element node]
rlm@29 192 (.addAll
rlm@29 193 (.getPhysicsSpace
rlm@29 194 (.getState
rlm@34 195 (.getStateManager world)
rlm@29 196 BulletAppState))
rlm@29 197 element)
rlm@29 198 (.attachChild node element))
rlm@34 199 ([world element]
rlm@34 200 (add-element world element (.getRootNode world))))
rlm@29 201
rlm@29 202 (defn apply-map
rlm@29 203 "Like apply, but works for maps and functions that expect an
rlm@29 204 implicit map and nothing else as in (fn [& {}]).
rlm@29 205 ------- Example -------
rlm@29 206 (defn demo [& {:keys [www] :or {www \"oh yeah\"} :as env}]
rlm@29 207 (println www))
rlm@29 208 (apply-map demo {:www \"hello!\"})
rlm@29 209 -->\"hello\""
rlm@29 210 [fn m]
rlm@29 211 (apply fn (reduce #(into %1 %2) [] m)))
rlm@29 212
rlm@151 213 (defn map-vals
rlm@151 214 "Transform a map by applying a function to its values,
rlm@151 215 keeping the keys the same."
rlm@151 216 [f m] (zipmap (keys m) (map f (vals m))))
rlm@151 217
rlm@164 218 (defn runonce
rlm@164 219 "Decorator. returns a function which will run only once.
rlm@164 220 Inspired by Halloway's version from Lancet."
rlm@164 221 {:author "Robert McIntyre"}
rlm@164 222 [function]
rlm@164 223 (let [sentinel (Object.)
rlm@164 224 result (atom sentinel)]
rlm@164 225 (fn [& args]
rlm@164 226 (locking sentinel
rlm@164 227 (if (= @result sentinel)
rlm@164 228 (reset! result (apply function args))
rlm@164 229 @result)))))
rlm@164 230
rlm@151 231
rlm@25 232 #+end_src
rlm@25 233
rlm@47 234 #+results: util
rlm@297 235 : #'cortex.util/runonce
rlm@73 236
rlm@25 237
rlm@29 238 *** Creating Basic Shapes
rlm@29 239
rlm@66 240 #+name: shapes
rlm@25 241 #+begin_src clojure :results silent
rlm@25 242 (in-ns 'cortex.util)
rlm@29 243
rlm@153 244 (defn load-bullet
rlm@306 245 "Running this function unpacks the native bullet libraries and makes
rlm@153 246 them available."
rlm@153 247 []
rlm@153 248 (let [sim (world (Node.) {} no-op no-op)]
rlm@153 249 (doto sim
rlm@153 250 (.enqueue
rlm@153 251 (fn []
rlm@153 252 (.stop sim)))
rlm@153 253 (.start))))
rlm@153 254
rlm@153 255
rlm@25 256 (defrecord shape-description
rlm@25 257 [name
rlm@25 258 color
rlm@25 259 mass
rlm@25 260 friction
rlm@25 261 texture
rlm@25 262 material
rlm@25 263 position
rlm@25 264 rotation
rlm@25 265 shape
rlm@29 266 physical?
rlm@29 267 GImpact?
rlm@29 268 ])
rlm@25 269
rlm@320 270 (def base-shape
rlm@320 271 "Basic settings for shapes."
rlm@320 272 (shape-description.
rlm@320 273 "default-shape"
rlm@320 274 false
rlm@320 275 ;;ColorRGBA/Blue
rlm@320 276 1.0 ;; mass
rlm@320 277 1.0 ;; friction
rlm@320 278 ;; texture
rlm@320 279 "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@320 280 ;; material
rlm@320 281 "Common/MatDefs/Misc/Unshaded.j3md"
rlm@320 282 Vector3f/ZERO
rlm@320 283 Quaternion/IDENTITY
rlm@320 284 (Box. Vector3f/ZERO 0.5 0.5 0.5)
rlm@320 285 true
rlm@320 286 false))
rlm@25 287
rlm@25 288 (defn make-shape
rlm@25 289 [#^shape-description d]
rlm@29 290 (let [asset-manager (asset-manager)
rlm@25 291 mat (Material. asset-manager (:material d))
rlm@25 292 geom (Geometry. (:name d) (:shape d))]
rlm@25 293 (if (:texture d)
rlm@25 294 (let [key (TextureKey. (:texture d))]
rlm@363 295 (.setGenerateMips key true)
rlm@363 296 (.setTexture mat "ColorMap" (.loadTexture asset-manager key))
rlm@34 297 ))
rlm@25 298 (if (:color d) (.setColor mat "Color" (:color d)))
rlm@25 299 (.setMaterial geom mat)
rlm@25 300 (if-let [rotation (:rotation d)] (.rotate geom rotation))
rlm@25 301 (.setLocalTranslation geom (:position d))
rlm@25 302 (if (:physical? d)
rlm@29 303 (let [physics-control
rlm@29 304 (if (:GImpact d)
rlm@29 305 ;; Create an accurate mesh collision shape if desired.
rlm@29 306 (RigidBodyControl.
rlm@29 307 (doto (GImpactCollisionShape.
rlm@29 308 (.getMesh geom))
rlm@29 309 (.createJmeMesh)
rlm@73 310 ;;(.setMargin 0)
rlm@73 311 )
rlm@29 312 (float (:mass d)))
rlm@29 313 ;; otherwise use jme3's default
rlm@29 314 (RigidBodyControl. (float (:mass d))))]
rlm@25 315 (.addControl geom physics-control)
rlm@25 316 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
rlm@25 317 (.setFriction physics-control (:friction d))))
rlm@25 318 geom))
rlm@25 319
rlm@25 320 (defn box
rlm@25 321 ([l w h & {:as options}]
rlm@25 322 (let [options (merge base-shape options)]
rlm@25 323 (make-shape (assoc options
rlm@25 324 :shape (Box. l w h)))))
rlm@25 325 ([] (box 0.5 0.5 0.5)))
rlm@25 326
rlm@25 327 (defn sphere
rlm@25 328 ([r & {:as options}]
rlm@25 329 (let [options (merge base-shape options)]
rlm@25 330 (make-shape (assoc options
rlm@25 331 :shape (Sphere. 32 32 (float r))))))
rlm@25 332 ([] (sphere 0.5)))
rlm@62 333
rlm@192 334 (defn x-ray
rlm@306 335 "A useful material for debugging -- it can be seen no matter what
rlm@306 336 object occludes it."
rlm@192 337 [#^ColorRGBA color]
rlm@192 338 (doto (Material. (asset-manager)
rlm@192 339 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@192 340 (.setColor "Color" color)
rlm@192 341 (-> (.getAdditionalRenderState)
rlm@192 342 (.setDepthTest false))))
rlm@74 343
rlm@62 344 (defn node-seq
rlm@62 345 "Take a node and return a seq of all its children
rlm@62 346 recursively. There will be no nodes left in the resulting
rlm@62 347 structure"
rlm@62 348 [#^Node node]
rlm@62 349 (tree-seq #(isa? (class %) Node) #(.getChildren %) node))
rlm@62 350
rlm@62 351 (defn nodify
rlm@62 352 "Take a sequence of things that can be attached to a node and return
rlm@62 353 a node with all of them attached"
rlm@62 354 ([name children]
rlm@62 355 (let [node (Node. name)]
rlm@62 356 (dorun (map #(.attachChild node %) children))
rlm@62 357 node))
rlm@62 358 ([children] (nodify "" children)))
rlm@62 359
rlm@192 360 (defn load-blender-model
rlm@192 361 "Load a .blend file using an asset folder relative path."
rlm@192 362 [^String model]
rlm@192 363 (.loadModel
rlm@192 364 (doto (asset-manager)
rlm@192 365 (.registerLoader BlenderModelLoader
rlm@192 366 (into-array String ["blend"]))) model))
rlm@192 367
rlm@62 368
rlm@458 369
rlm@458 370 (def brick-length 0.48)
rlm@458 371 (def brick-width 0.24)
rlm@458 372 (def brick-height 0.12)
rlm@458 373 (def gravity (Vector3f. 0 -9.81 0))
rlm@458 374
rlm@458 375 (import com.jme3.math.Vector2f)
rlm@458 376 (import com.jme3.renderer.queue.RenderQueue$ShadowMode)
rlm@458 377 (import com.jme3.texture.Texture$WrapMode)
rlm@458 378
rlm@458 379 (defn brick* [position]
rlm@458 380 (println "get brick.")
rlm@458 381 (doto (box brick-length brick-height brick-width
rlm@458 382 :position position :name "brick"
rlm@458 383 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@458 384 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@458 385 :mass 34)
rlm@458 386 (->
rlm@458 387 (.getMesh)
rlm@458 388 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@458 389 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@458 390 )
rlm@458 391 )
rlm@458 392
rlm@458 393
rlm@458 394 (defn floor*
rlm@458 395 "make a sturdy, unmovable physical floor"
rlm@458 396 []
rlm@458 397 (box 10 0.1 5 :name "floor" :mass 0
rlm@458 398 :color ColorRGBA/Gray :position (Vector3f. 0 0 0)))
rlm@458 399
rlm@458 400 (defn floor* []
rlm@458 401 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@458 402 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@458 403 :texture "Textures/BronzeCopper030.jpg"
rlm@458 404 :position (Vector3f. 0 0 0 )
rlm@458 405 :mass 0)
rlm@458 406 (->
rlm@458 407 (.getMesh)
rlm@458 408 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@458 409 (->
rlm@458 410 (.getMaterial)
rlm@458 411 (.getTextureParam "ColorMap")
rlm@458 412 (.getTextureValue)
rlm@458 413 (.setWrap Texture$WrapMode/Repeat))
rlm@458 414 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@458 415 ))
rlm@458 416
rlm@458 417
rlm@458 418 (defn brick-wall* []
rlm@458 419 (let [node (Node. "brick-wall")]
rlm@458 420 (dorun
rlm@458 421 (map
rlm@458 422 (comp #(.attachChild node %) brick*)
rlm@458 423 (for [y (range 10)
rlm@458 424 x (range 4)
rlm@458 425 z (range 1)]
rlm@458 426 (Vector3f.
rlm@458 427 (+ (* 2 x brick-length)
rlm@458 428 (if (even? (+ y z))
rlm@458 429 (/ brick-length 4) (/ brick-length -4)))
rlm@458 430 (+ (* brick-height (inc (* 2 y))))
rlm@458 431 (* 2 z brick-width) ))))
rlm@458 432 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@458 433 node))
rlm@458 434
rlm@458 435
rlm@29 436 #+end_src
rlm@25 437
rlm@25 438
rlm@50 439 *** Debug Actions
rlm@66 440 #+name: debug-actions
rlm@29 441 #+begin_src clojure :results silent
rlm@60 442 (in-ns 'cortex.util)
rlm@60 443
rlm@47 444 (defn basic-light-setup
rlm@306 445 "returns a sequence of lights appropriate for fully lighting a scene"
rlm@47 446 []
rlm@47 447 (conj
rlm@47 448 (doall
rlm@47 449 (map
rlm@47 450 (fn [direction]
rlm@47 451 (doto (DirectionalLight.)
rlm@47 452 (.setDirection direction)
rlm@47 453 (.setColor ColorRGBA/White)))
rlm@47 454 [;; six faces of a cube
rlm@47 455 Vector3f/UNIT_X
rlm@47 456 Vector3f/UNIT_Y
rlm@47 457 Vector3f/UNIT_Z
rlm@47 458 (.mult Vector3f/UNIT_X (float -1))
rlm@47 459 (.mult Vector3f/UNIT_Y (float -1))
rlm@47 460 (.mult Vector3f/UNIT_Z (float -1))]))
rlm@47 461 (doto (AmbientLight.)
rlm@47 462 (.setColor ColorRGBA/White))))
rlm@47 463
rlm@47 464 (defn light-up-everything
rlm@306 465 "Add lights to a world appropriate for quickly seeing everything
rlm@47 466 in the scene. Adds six DirectionalLights facing in orthogonal
rlm@47 467 directions, and one AmbientLight to provide overall lighting
rlm@47 468 coverage."
rlm@47 469 [world]
rlm@47 470 (dorun
rlm@47 471 (map
rlm@47 472 #(.addLight (.getRootNode world) %)
rlm@47 473 (basic-light-setup))))
rlm@50 474
rlm@50 475 (defn fire-cannon-ball
rlm@50 476 "Creates a function that fires a cannon-ball from the current game's
rlm@50 477 camera. The cannon-ball will be attached to the node if provided, or
rlm@50 478 to the game's RootNode if no node is provided."
rlm@50 479 ([node]
rlm@50 480 (fn [game value]
rlm@50 481 (if (not value)
rlm@50 482 (let [camera (.getCamera game)
rlm@50 483 cannon-ball
rlm@363 484 (sphere 0.4
rlm@363 485 ;;:texture nil
rlm@50 486 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@363 487 :color ColorRGBA/Blue
rlm@216 488 :name "cannonball!"
rlm@50 489 :position
rlm@50 490 (.add (.getLocation camera)
rlm@50 491 (.mult (.getDirection camera) (float 1)))
rlm@363 492 :mass 25)] ;200 0.05
rlm@50 493 (.setLinearVelocity
rlm@50 494 (.getControl cannon-ball RigidBodyControl)
rlm@50 495 (.mult (.getDirection camera) (float 50))) ;50
rlm@216 496 (add-element game cannon-ball (if node node (.getRootNode
rlm@216 497 game)))
rlm@216 498 cannon-ball))))
rlm@50 499 ([]
rlm@50 500 (fire-cannon-ball false)))
rlm@60 501
rlm@60 502 (def standard-debug-controls
rlm@60 503 {"key-space" (fire-cannon-ball)})
rlm@60 504
rlm@60 505
rlm@160 506 (defn tap [obj direction force]
rlm@160 507 (let [control (.getControl obj RigidBodyControl)]
rlm@160 508 (.applyTorque
rlm@160 509 control
rlm@160 510 (.mult (.getPhysicsRotation control)
rlm@160 511 (.mult (.normalize direction) (float force))))))
rlm@160 512
rlm@160 513
rlm@160 514 (defn with-movement
rlm@160 515 [object
rlm@160 516 [up down left right roll-up roll-down :as keyboard]
rlm@160 517 forces
rlm@160 518 [root-node
rlm@160 519 keymap
rlm@306 520 initialization
rlm@160 521 world-loop]]
rlm@160 522 (let [add-keypress
rlm@160 523 (fn [state keymap key]
rlm@160 524 (merge keymap
rlm@160 525 {key
rlm@160 526 (fn [_ pressed?]
rlm@160 527 (reset! state pressed?))}))
rlm@160 528 move-up? (atom false)
rlm@160 529 move-down? (atom false)
rlm@160 530 move-left? (atom false)
rlm@160 531 move-right? (atom false)
rlm@160 532 roll-left? (atom false)
rlm@160 533 roll-right? (atom false)
rlm@160 534
rlm@160 535 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)
rlm@160 536 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)
rlm@160 537 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]
rlm@160 538 atoms [move-left? move-right? move-up? move-down?
rlm@160 539 roll-left? roll-right?]
rlm@160 540
rlm@160 541 keymap* (reduce merge
rlm@160 542 (map #(add-keypress %1 keymap %2)
rlm@160 543 atoms
rlm@160 544 keyboard))
rlm@160 545
rlm@160 546 splice-loop (fn []
rlm@160 547 (dorun
rlm@160 548 (map
rlm@160 549 (fn [sym direction force]
rlm@160 550 (if @sym
rlm@160 551 (tap object direction force)))
rlm@160 552 atoms directions forces)))
rlm@160 553
rlm@160 554 world-loop* (fn [world tpf]
rlm@160 555 (world-loop world tpf)
rlm@160 556 (splice-loop))]
rlm@160 557 [root-node
rlm@160 558 keymap*
rlm@306 559 initialization
rlm@160 560 world-loop*]))
rlm@160 561
rlm@205 562 (import com.jme3.font.BitmapText)
rlm@205 563 (import com.jme3.scene.control.AbstractControl)
rlm@205 564 (import com.aurellem.capture.IsoTimer)
rlm@60 565
rlm@306 566 (defn display-dilated-time
rlm@205 567 "Shows the time as it is flowing in the simulation on a HUD display.
rlm@205 568 Useful for making videos."
rlm@205 569 [world timer]
rlm@205 570 (let [font (.loadFont (asset-manager) "Interface/Fonts/Default.fnt")
rlm@205 571 text (BitmapText. font false)]
rlm@205 572 (.setLocalTranslation text 300 (.getLineHeight text) 0)
rlm@205 573 (.addControl
rlm@205 574 text
rlm@205 575 (proxy [AbstractControl] []
rlm@205 576 (controlUpdate [tpf]
rlm@205 577 (.setText text (format
rlm@205 578 "%.2f"
rlm@341 579 (float (.getTimeInSeconds timer)))))
rlm@205 580 (controlRender [_ _])))
rlm@205 581 (.attachChild (.getGuiNode world) text)))
rlm@50 582 #+end_src
rlm@50 583
rlm@50 584
rlm@50 585 *** Viewing Objects
rlm@50 586
rlm@66 587 #+name: world-view
rlm@50 588 #+begin_src clojure :results silent
rlm@50 589 (in-ns 'cortex.util)
rlm@50 590
rlm@50 591 (defprotocol Viewable
rlm@50 592 (view [something]))
rlm@50 593
rlm@50 594 (extend-type com.jme3.scene.Geometry
rlm@50 595 Viewable
rlm@50 596 (view [geo]
rlm@50 597 (view (doto (Node.)(.attachChild geo)))))
rlm@47 598
rlm@29 599 (extend-type com.jme3.scene.Node
rlm@29 600 Viewable
rlm@29 601 (view
rlm@29 602 [node]
rlm@29 603 (.start
rlm@29 604 (world
rlm@29 605 node
rlm@29 606 {}
rlm@29 607 (fn [world]
rlm@29 608 (enable-debug world)
rlm@29 609 (set-gravity world Vector3f/ZERO)
rlm@47 610 (light-up-everything world))
rlm@29 611 no-op))))
rlm@62 612
rlm@62 613 (extend-type com.jme3.math.ColorRGBA
rlm@62 614 Viewable
rlm@62 615 (view
rlm@62 616 [color]
rlm@62 617 (view (doto (Node.)
rlm@62 618 (.attachChild (box 1 1 1 :color color))))))
rlm@62 619
rlm@336 620 (extend-type ij.ImagePlus
rlm@336 621 Viewable
rlm@336 622 (view [image]
rlm@336 623 (.show image)))
rlm@336 624
rlm@335 625 (extend-type java.awt.image.BufferedImage
rlm@335 626 Viewable
rlm@335 627 (view
rlm@335 628 [image]
rlm@336 629 (view (ImagePlus. "view-buffered-image" image))))
rlm@336 630
rlm@336 631
rlm@62 632 (defprotocol Textual
rlm@62 633 (text [something]
rlm@62 634 "Display a detailed textual analysis of the given object."))
rlm@62 635
rlm@62 636 (extend-type com.jme3.scene.Node
rlm@62 637 Textual
rlm@62 638 (text [node]
rlm@62 639 (println "Total Vertexes: " (.getVertexCount node))
rlm@62 640 (println "Total Triangles: " (.getTriangleCount node))
rlm@62 641 (println "Controls :")
rlm@62 642 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
rlm@62 643 (println "Has " (.getQuantity node) " Children:")
rlm@62 644 (doall (map text (.getChildren node)))))
rlm@62 645
rlm@62 646 (extend-type com.jme3.animation.AnimControl
rlm@62 647 Textual
rlm@62 648 (text [control]
rlm@62 649 (let [animations (.getAnimationNames control)]
rlm@62 650 (println "Animation Control with " (count animations) " animation(s):")
rlm@62 651 (dorun (map println animations)))))
rlm@62 652
rlm@62 653 (extend-type com.jme3.animation.SkeletonControl
rlm@62 654 Textual
rlm@62 655 (text [control]
rlm@62 656 (println "Skeleton Control with the following skeleton:")
rlm@62 657 (println (.getSkeleton control))))
rlm@62 658
rlm@62 659 (extend-type com.jme3.bullet.control.KinematicRagdollControl
rlm@62 660 Textual
rlm@62 661 (text [control]
rlm@62 662 (println "Ragdoll Control")))
rlm@62 663
rlm@62 664 (extend-type com.jme3.scene.Geometry
rlm@62 665 Textual
rlm@62 666 (text [control]
rlm@62 667 (println "...geo...")))
rlm@108 668
rlm@108 669 (extend-type Triangle
rlm@108 670 Textual
rlm@108 671 (text [t]
rlm@108 672 (println "Triangle: " \newline (.get1 t) \newline
rlm@108 673 (.get2 t) \newline (.get3 t))))
rlm@108 674
rlm@25 675 #+end_src
rlm@25 676
rlm@29 677 Here I make the =Viewable= protocol and extend it to JME's types. Now
rlm@34 678 JME3's =hello-world= can be written as easily as:
rlm@29 679
rlm@29 680 #+begin_src clojure :results silent
rlm@29 681 (cortex.util/view (cortex.util/box))
rlm@29 682 #+end_src
rlm@29 683
rlm@29 684
rlm@24 685 * COMMENT code generation
rlm@24 686 #+begin_src clojure :tangle ../src/cortex/import.clj
rlm@24 687 <<import>>
rlm@24 688 #+end_src
rlm@25 689
rlm@25 690
rlm@29 691 #+begin_src clojure :tangle ../src/cortex/util.clj :noweb yes
rlm@25 692 <<util>>
rlm@25 693 <<shapes>>
rlm@50 694 <<debug-actions>>
rlm@29 695 <<world-view>>
rlm@25 696 #+end_src
rlm@25 697
rlm@29 698
rlm@29 699
rlm@29 700
rlm@29 701
rlm@29 702
rlm@29 703
rlm@29 704
rlm@29 705
rlm@29 706
rlm@29 707
rlm@29 708
rlm@29 709
rlm@29 710
rlm@29 711
rlm@29 712
rlm@29 713