annotate org/util.org @ 458:42ddfe406c0a

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