annotate org/util.org @ 404:939bcc5950b2

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