annotate org/util.org @ 78:77b506ac64f3

trying to make offset cone joints
author Robert McIntyre <rlm@mit.edu>
date Wed, 04 Jan 2012 20:34:00 -0700
parents fb810a2c50c2
children 92b857b6145d
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@29 97 (:import com.jme3.math.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@40 101 (:import (java.util.logging Level Logger)))
rlm@40 102
rlm@40 103
rlm@25 104
rlm@29 105 (defvar println-repl
rlm@29 106 (bound-fn [& args] (apply println args))
rlm@29 107 "println called from the LWJGL thread will not go to the REPL, but
rlm@29 108 instead to whatever terminal started the JVM process. This function
rlm@29 109 will always output to the REPL")
rlm@25 110
rlm@29 111 (defn position-camera
rlm@34 112 "Change the position of the in-world camera."
rlm@34 113 ([world position direction up]
rlm@34 114 (doto (.getCamera world)
rlm@29 115 (.setLocation )
rlm@29 116 (.lookAt direction up)))
rlm@34 117 ([world position direction]
rlm@29 118 (position-camera
rlm@34 119 world position direction Vector3f/UNIT_Y)))
rlm@25 120
rlm@29 121 (defn enable-debug
rlm@34 122 "Turn on debug wireframes for every object in this simulation."
rlm@29 123 [world]
rlm@29 124 (.enableDebug
rlm@29 125 (.getPhysicsSpace
rlm@29 126 (.getState
rlm@29 127 (.getStateManager world)
rlm@29 128 BulletAppState))
rlm@29 129 (asset-manager)))
rlm@29 130
rlm@78 131 (defn speed-up
rlm@78 132 "Increase the dismally slow speed of the world's camera."
rlm@78 133 [world]
rlm@78 134 (.setMoveSpeed (.getFlyByCamera world)
rlm@78 135 (float 100))
rlm@78 136 (.setRotationSpeed (.getFlyByCamera world)
rlm@78 137 (float 20))
rlm@78 138 world)
rlm@78 139
rlm@78 140
rlm@40 141 (defn no-logging
rlm@40 142 "Disable all of jMonkeyEngine's logging."
rlm@40 143 []
rlm@40 144 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@40 145
rlm@40 146 (defn set-accuracy
rlm@40 147 "Change the accuracy at which the World's Physics is calculated."
rlm@40 148 [world new-accuracy]
rlm@40 149 (let [physics-manager
rlm@40 150 (.getState
rlm@40 151 (.getStateManager world) BulletAppState)]
rlm@40 152 (.setAccuracy
rlm@40 153 (.getPhysicsSpace physics-manager)
rlm@40 154 (float new-accuracy))))
rlm@40 155
rlm@40 156
rlm@34 157 (defn set-gravity
rlm@29 158 "In order to change the gravity of a scene, it is not only necessary
rlm@29 159 to set the gravity variable, but to \"tap\" every physics object in
rlm@29 160 the scene to reactivate physics calculations."
rlm@34 161 [world gravity]
rlm@25 162 (traverse
rlm@25 163 (fn [geom]
rlm@25 164 (if-let
rlm@29 165 ;; only set gravity for physical objects.
rlm@25 166 [control (.getControl geom RigidBodyControl)]
rlm@25 167 (do
rlm@25 168 (.setGravity control gravity)
rlm@29 169 ;; tappsies!
rlm@29 170 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO))))
rlm@34 171 (.getRootNode world)))
rlm@29 172
rlm@29 173 (defn add-element
rlm@34 174 "Add the Spatial to the world's environment"
rlm@34 175 ([world element node]
rlm@29 176 (.addAll
rlm@29 177 (.getPhysicsSpace
rlm@29 178 (.getState
rlm@34 179 (.getStateManager world)
rlm@29 180 BulletAppState))
rlm@29 181 element)
rlm@29 182 (.attachChild node element))
rlm@34 183 ([world element]
rlm@34 184 (add-element world element (.getRootNode world))))
rlm@29 185
rlm@29 186 (defn apply-map
rlm@29 187 "Like apply, but works for maps and functions that expect an
rlm@29 188 implicit map and nothing else as in (fn [& {}]).
rlm@29 189 ------- Example -------
rlm@29 190 (defn demo [& {:keys [www] :or {www \"oh yeah\"} :as env}]
rlm@29 191 (println www))
rlm@29 192 (apply-map demo {:www \"hello!\"})
rlm@29 193 -->\"hello\""
rlm@29 194 [fn m]
rlm@29 195 (apply fn (reduce #(into %1 %2) [] m)))
rlm@29 196
rlm@25 197 #+end_src
rlm@25 198
rlm@47 199 #+results: util
rlm@47 200 : #'cortex.util/apply-map
rlm@73 201
rlm@25 202
rlm@29 203 *** Creating Basic Shapes
rlm@29 204
rlm@66 205 #+name: shapes
rlm@25 206 #+begin_src clojure :results silent
rlm@25 207 (in-ns 'cortex.util)
rlm@29 208
rlm@25 209 (defrecord shape-description
rlm@25 210 [name
rlm@25 211 color
rlm@25 212 mass
rlm@25 213 friction
rlm@25 214 texture
rlm@25 215 material
rlm@25 216 position
rlm@25 217 rotation
rlm@25 218 shape
rlm@29 219 physical?
rlm@29 220 GImpact?
rlm@29 221 ])
rlm@25 222
rlm@34 223 (defvar base-shape
rlm@25 224 (shape-description.
rlm@25 225 "default-shape"
rlm@25 226 false
rlm@25 227 ;;ColorRGBA/Blue
rlm@25 228 1.0 ;; mass
rlm@25 229 1.0 ;; friction
rlm@25 230 ;; texture
rlm@25 231 "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@25 232 ;; material
rlm@25 233 "Common/MatDefs/Misc/Unshaded.j3md"
rlm@25 234 Vector3f/ZERO
rlm@25 235 Quaternion/IDENTITY
rlm@25 236 (Box. Vector3f/ZERO 0.5 0.5 0.5)
rlm@29 237 true
rlm@34 238 false)
rlm@34 239 "Basic settings for shapes.")
rlm@25 240
rlm@25 241 (defn make-shape
rlm@25 242 [#^shape-description d]
rlm@29 243 (let [asset-manager (asset-manager)
rlm@25 244 mat (Material. asset-manager (:material d))
rlm@25 245 geom (Geometry. (:name d) (:shape d))]
rlm@25 246 (if (:texture d)
rlm@25 247 (let [key (TextureKey. (:texture d))]
rlm@34 248 ;;(.setGenerateMips key true)
rlm@34 249 ;;(.setTexture mat "ColorMap" (.loadTexture asset-manager key))
rlm@34 250 ))
rlm@25 251 (if (:color d) (.setColor mat "Color" (:color d)))
rlm@25 252 (.setMaterial geom mat)
rlm@25 253 (if-let [rotation (:rotation d)] (.rotate geom rotation))
rlm@25 254 (.setLocalTranslation geom (:position d))
rlm@25 255 (if (:physical? d)
rlm@29 256 (let [physics-control
rlm@29 257 (if (:GImpact d)
rlm@29 258 ;; Create an accurate mesh collision shape if desired.
rlm@29 259 (RigidBodyControl.
rlm@29 260 (doto (GImpactCollisionShape.
rlm@29 261 (.getMesh geom))
rlm@29 262 (.createJmeMesh)
rlm@73 263 ;;(.setMargin 0)
rlm@73 264 )
rlm@29 265 (float (:mass d)))
rlm@29 266 ;; otherwise use jme3's default
rlm@29 267 (RigidBodyControl. (float (:mass d))))]
rlm@25 268 (.addControl geom physics-control)
rlm@25 269 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
rlm@25 270 (.setFriction physics-control (:friction d))))
rlm@25 271 geom))
rlm@25 272
rlm@25 273 (defn box
rlm@25 274 ([l w h & {:as options}]
rlm@25 275 (let [options (merge base-shape options)]
rlm@25 276 (make-shape (assoc options
rlm@25 277 :shape (Box. l w h)))))
rlm@25 278 ([] (box 0.5 0.5 0.5)))
rlm@25 279
rlm@25 280 (defn sphere
rlm@25 281 ([r & {:as options}]
rlm@25 282 (let [options (merge base-shape options)]
rlm@25 283 (make-shape (assoc options
rlm@25 284 :shape (Sphere. 32 32 (float r))))))
rlm@25 285 ([] (sphere 0.5)))
rlm@62 286
rlm@74 287 (defn green-x-ray
rlm@74 288 "A usefull material for debuging -- it can be seen no matter what
rlm@74 289 object occuldes it."
rlm@74 290 []
rlm@74 291 (doto (Material. (asset-manager)
rlm@74 292 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@74 293 (.setColor "Color" ColorRGBA/Green)
rlm@74 294 (-> (.getAdditionalRenderState)
rlm@74 295 (.setDepthTest false))))
rlm@74 296
rlm@62 297 (defn node-seq
rlm@62 298 "Take a node and return a seq of all its children
rlm@62 299 recursively. There will be no nodes left in the resulting
rlm@62 300 structure"
rlm@62 301 [#^Node node]
rlm@62 302 (tree-seq #(isa? (class %) Node) #(.getChildren %) node))
rlm@62 303
rlm@62 304 (defn nodify
rlm@62 305 "Take a sequence of things that can be attached to a node and return
rlm@62 306 a node with all of them attached"
rlm@62 307 ([name children]
rlm@62 308 (let [node (Node. name)]
rlm@62 309 (dorun (map #(.attachChild node %) children))
rlm@62 310 node))
rlm@62 311 ([children] (nodify "" children)))
rlm@62 312
rlm@62 313
rlm@29 314 #+end_src
rlm@25 315
rlm@25 316
rlm@50 317 *** Debug Actions
rlm@66 318 #+name: debug-actions
rlm@29 319 #+begin_src clojure :results silent
rlm@60 320 (in-ns 'cortex.util)
rlm@60 321
rlm@47 322 (defn basic-light-setup
rlm@47 323 "returns a sequence of lights appropiate for fully lighting a scene"
rlm@47 324 []
rlm@47 325 (conj
rlm@47 326 (doall
rlm@47 327 (map
rlm@47 328 (fn [direction]
rlm@47 329 (doto (DirectionalLight.)
rlm@47 330 (.setDirection direction)
rlm@47 331 (.setColor ColorRGBA/White)))
rlm@47 332 [;; six faces of a cube
rlm@47 333 Vector3f/UNIT_X
rlm@47 334 Vector3f/UNIT_Y
rlm@47 335 Vector3f/UNIT_Z
rlm@47 336 (.mult Vector3f/UNIT_X (float -1))
rlm@47 337 (.mult Vector3f/UNIT_Y (float -1))
rlm@47 338 (.mult Vector3f/UNIT_Z (float -1))]))
rlm@47 339 (doto (AmbientLight.)
rlm@47 340 (.setColor ColorRGBA/White))))
rlm@47 341
rlm@47 342 (defn light-up-everything
rlm@47 343 "Add lights to a world appropiate for quickly seeing everything
rlm@47 344 in the scene. Adds six DirectionalLights facing in orthogonal
rlm@47 345 directions, and one AmbientLight to provide overall lighting
rlm@47 346 coverage."
rlm@47 347 [world]
rlm@47 348 (dorun
rlm@47 349 (map
rlm@47 350 #(.addLight (.getRootNode world) %)
rlm@47 351 (basic-light-setup))))
rlm@50 352
rlm@50 353 (defn fire-cannon-ball
rlm@50 354 "Creates a function that fires a cannon-ball from the current game's
rlm@50 355 camera. The cannon-ball will be attached to the node if provided, or
rlm@50 356 to the game's RootNode if no node is provided."
rlm@50 357 ([node]
rlm@50 358 (fn [game value]
rlm@50 359 (if (not value)
rlm@50 360 (let [camera (.getCamera game)
rlm@50 361 cannon-ball
rlm@50 362 (sphere 0.7
rlm@50 363 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@50 364 :texture "Textures/PokeCopper.jpg"
rlm@50 365 :position
rlm@50 366 (.add (.getLocation camera)
rlm@50 367 (.mult (.getDirection camera) (float 1)))
rlm@50 368 :mass 3)] ;200 0.05
rlm@50 369 (.setLinearVelocity
rlm@50 370 (.getControl cannon-ball RigidBodyControl)
rlm@50 371 (.mult (.getDirection camera) (float 50))) ;50
rlm@50 372 (add-element game cannon-ball (if node node (.getRootNode game)))))))
rlm@50 373 ([]
rlm@50 374 (fire-cannon-ball false)))
rlm@60 375
rlm@60 376 (def standard-debug-controls
rlm@60 377 {"key-space" (fire-cannon-ball)})
rlm@60 378
rlm@60 379
rlm@60 380
rlm@60 381
rlm@50 382 #+end_src
rlm@50 383
rlm@50 384
rlm@50 385 *** Viewing Objects
rlm@50 386
rlm@66 387 #+name: world-view
rlm@50 388 #+begin_src clojure :results silent
rlm@50 389 (in-ns 'cortex.util)
rlm@50 390
rlm@50 391 (defprotocol Viewable
rlm@50 392 (view [something]))
rlm@50 393
rlm@50 394 (extend-type com.jme3.scene.Geometry
rlm@50 395 Viewable
rlm@50 396 (view [geo]
rlm@50 397 (view (doto (Node.)(.attachChild geo)))))
rlm@47 398
rlm@29 399 (extend-type com.jme3.scene.Node
rlm@29 400 Viewable
rlm@29 401 (view
rlm@29 402 [node]
rlm@29 403 (.start
rlm@29 404 (world
rlm@29 405 node
rlm@29 406 {}
rlm@29 407 (fn [world]
rlm@29 408 (enable-debug world)
rlm@29 409 (set-gravity world Vector3f/ZERO)
rlm@47 410 (light-up-everything world))
rlm@29 411 no-op))))
rlm@62 412
rlm@62 413 (extend-type com.jme3.math.ColorRGBA
rlm@62 414 Viewable
rlm@62 415 (view
rlm@62 416 [color]
rlm@62 417 (view (doto (Node.)
rlm@62 418 (.attachChild (box 1 1 1 :color color))))))
rlm@62 419
rlm@62 420 (defprotocol Textual
rlm@62 421 (text [something]
rlm@62 422 "Display a detailed textual analysis of the given object."))
rlm@62 423
rlm@62 424 (extend-type com.jme3.scene.Node
rlm@62 425 Textual
rlm@62 426 (text [node]
rlm@62 427 (println "Total Vertexes: " (.getVertexCount node))
rlm@62 428 (println "Total Triangles: " (.getTriangleCount node))
rlm@62 429 (println "Controls :")
rlm@62 430 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
rlm@62 431 (println "Has " (.getQuantity node) " Children:")
rlm@62 432 (doall (map text (.getChildren node)))))
rlm@62 433
rlm@62 434 (extend-type com.jme3.animation.AnimControl
rlm@62 435 Textual
rlm@62 436 (text [control]
rlm@62 437 (let [animations (.getAnimationNames control)]
rlm@62 438 (println "Animation Control with " (count animations) " animation(s):")
rlm@62 439 (dorun (map println animations)))))
rlm@62 440
rlm@62 441 (extend-type com.jme3.animation.SkeletonControl
rlm@62 442 Textual
rlm@62 443 (text [control]
rlm@62 444 (println "Skeleton Control with the following skeleton:")
rlm@62 445 (println (.getSkeleton control))))
rlm@62 446
rlm@62 447 (extend-type com.jme3.bullet.control.KinematicRagdollControl
rlm@62 448 Textual
rlm@62 449 (text [control]
rlm@62 450 (println "Ragdoll Control")))
rlm@62 451
rlm@62 452 (extend-type com.jme3.scene.Geometry
rlm@62 453 Textual
rlm@62 454 (text [control]
rlm@62 455 (println "...geo...")))
rlm@25 456 #+end_src
rlm@25 457
rlm@29 458 Here I make the =Viewable= protocol and extend it to JME's types. Now
rlm@34 459 JME3's =hello-world= can be written as easily as:
rlm@29 460
rlm@29 461 #+begin_src clojure :results silent
rlm@29 462 (cortex.util/view (cortex.util/box))
rlm@29 463 #+end_src
rlm@29 464
rlm@29 465
rlm@24 466 * COMMENT code generation
rlm@24 467 #+begin_src clojure :tangle ../src/cortex/import.clj
rlm@24 468 <<import>>
rlm@24 469 #+end_src
rlm@25 470
rlm@25 471
rlm@29 472 #+begin_src clojure :tangle ../src/cortex/util.clj :noweb yes
rlm@25 473 <<util>>
rlm@25 474 <<shapes>>
rlm@50 475 <<debug-actions>>
rlm@29 476 <<world-view>>
rlm@25 477 #+end_src
rlm@25 478
rlm@29 479
rlm@29 480
rlm@29 481
rlm@29 482
rlm@29 483
rlm@29 484
rlm@29 485
rlm@29 486
rlm@29 487
rlm@29 488
rlm@29 489
rlm@29 490
rlm@29 491
rlm@29 492
rlm@29 493
rlm@29 494