annotate org/util.org @ 198:fc0bf33bded2

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