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@29
|
368 #+end_src
|
rlm@25
|
369
|
rlm@25
|
370
|
rlm@50
|
371 *** Debug Actions
|
rlm@66
|
372 #+name: debug-actions
|
rlm@29
|
373 #+begin_src clojure :results silent
|
rlm@60
|
374 (in-ns 'cortex.util)
|
rlm@60
|
375
|
rlm@47
|
376 (defn basic-light-setup
|
rlm@306
|
377 "returns a sequence of lights appropriate for fully lighting a scene"
|
rlm@47
|
378 []
|
rlm@47
|
379 (conj
|
rlm@47
|
380 (doall
|
rlm@47
|
381 (map
|
rlm@47
|
382 (fn [direction]
|
rlm@47
|
383 (doto (DirectionalLight.)
|
rlm@47
|
384 (.setDirection direction)
|
rlm@47
|
385 (.setColor ColorRGBA/White)))
|
rlm@47
|
386 [;; six faces of a cube
|
rlm@47
|
387 Vector3f/UNIT_X
|
rlm@47
|
388 Vector3f/UNIT_Y
|
rlm@47
|
389 Vector3f/UNIT_Z
|
rlm@47
|
390 (.mult Vector3f/UNIT_X (float -1))
|
rlm@47
|
391 (.mult Vector3f/UNIT_Y (float -1))
|
rlm@47
|
392 (.mult Vector3f/UNIT_Z (float -1))]))
|
rlm@47
|
393 (doto (AmbientLight.)
|
rlm@47
|
394 (.setColor ColorRGBA/White))))
|
rlm@47
|
395
|
rlm@47
|
396 (defn light-up-everything
|
rlm@306
|
397 "Add lights to a world appropriate for quickly seeing everything
|
rlm@47
|
398 in the scene. Adds six DirectionalLights facing in orthogonal
|
rlm@47
|
399 directions, and one AmbientLight to provide overall lighting
|
rlm@47
|
400 coverage."
|
rlm@47
|
401 [world]
|
rlm@47
|
402 (dorun
|
rlm@47
|
403 (map
|
rlm@47
|
404 #(.addLight (.getRootNode world) %)
|
rlm@47
|
405 (basic-light-setup))))
|
rlm@50
|
406
|
rlm@50
|
407 (defn fire-cannon-ball
|
rlm@50
|
408 "Creates a function that fires a cannon-ball from the current game's
|
rlm@50
|
409 camera. The cannon-ball will be attached to the node if provided, or
|
rlm@50
|
410 to the game's RootNode if no node is provided."
|
rlm@50
|
411 ([node]
|
rlm@50
|
412 (fn [game value]
|
rlm@50
|
413 (if (not value)
|
rlm@50
|
414 (let [camera (.getCamera game)
|
rlm@50
|
415 cannon-ball
|
rlm@363
|
416 (sphere 0.4
|
rlm@363
|
417 ;;:texture nil
|
rlm@50
|
418 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@363
|
419 :color ColorRGBA/Blue
|
rlm@216
|
420 :name "cannonball!"
|
rlm@50
|
421 :position
|
rlm@50
|
422 (.add (.getLocation camera)
|
rlm@50
|
423 (.mult (.getDirection camera) (float 1)))
|
rlm@363
|
424 :mass 25)] ;200 0.05
|
rlm@50
|
425 (.setLinearVelocity
|
rlm@50
|
426 (.getControl cannon-ball RigidBodyControl)
|
rlm@50
|
427 (.mult (.getDirection camera) (float 50))) ;50
|
rlm@216
|
428 (add-element game cannon-ball (if node node (.getRootNode
|
rlm@216
|
429 game)))
|
rlm@216
|
430 cannon-ball))))
|
rlm@50
|
431 ([]
|
rlm@50
|
432 (fire-cannon-ball false)))
|
rlm@60
|
433
|
rlm@60
|
434 (def standard-debug-controls
|
rlm@60
|
435 {"key-space" (fire-cannon-ball)})
|
rlm@60
|
436
|
rlm@60
|
437
|
rlm@160
|
438 (defn tap [obj direction force]
|
rlm@160
|
439 (let [control (.getControl obj RigidBodyControl)]
|
rlm@160
|
440 (.applyTorque
|
rlm@160
|
441 control
|
rlm@160
|
442 (.mult (.getPhysicsRotation control)
|
rlm@160
|
443 (.mult (.normalize direction) (float force))))))
|
rlm@160
|
444
|
rlm@160
|
445
|
rlm@160
|
446 (defn with-movement
|
rlm@160
|
447 [object
|
rlm@160
|
448 [up down left right roll-up roll-down :as keyboard]
|
rlm@160
|
449 forces
|
rlm@160
|
450 [root-node
|
rlm@160
|
451 keymap
|
rlm@306
|
452 initialization
|
rlm@160
|
453 world-loop]]
|
rlm@160
|
454 (let [add-keypress
|
rlm@160
|
455 (fn [state keymap key]
|
rlm@160
|
456 (merge keymap
|
rlm@160
|
457 {key
|
rlm@160
|
458 (fn [_ pressed?]
|
rlm@160
|
459 (reset! state pressed?))}))
|
rlm@160
|
460 move-up? (atom false)
|
rlm@160
|
461 move-down? (atom false)
|
rlm@160
|
462 move-left? (atom false)
|
rlm@160
|
463 move-right? (atom false)
|
rlm@160
|
464 roll-left? (atom false)
|
rlm@160
|
465 roll-right? (atom false)
|
rlm@160
|
466
|
rlm@160
|
467 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)
|
rlm@160
|
468 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)
|
rlm@160
|
469 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]
|
rlm@160
|
470 atoms [move-left? move-right? move-up? move-down?
|
rlm@160
|
471 roll-left? roll-right?]
|
rlm@160
|
472
|
rlm@160
|
473 keymap* (reduce merge
|
rlm@160
|
474 (map #(add-keypress %1 keymap %2)
|
rlm@160
|
475 atoms
|
rlm@160
|
476 keyboard))
|
rlm@160
|
477
|
rlm@160
|
478 splice-loop (fn []
|
rlm@160
|
479 (dorun
|
rlm@160
|
480 (map
|
rlm@160
|
481 (fn [sym direction force]
|
rlm@160
|
482 (if @sym
|
rlm@160
|
483 (tap object direction force)))
|
rlm@160
|
484 atoms directions forces)))
|
rlm@160
|
485
|
rlm@160
|
486 world-loop* (fn [world tpf]
|
rlm@160
|
487 (world-loop world tpf)
|
rlm@160
|
488 (splice-loop))]
|
rlm@160
|
489 [root-node
|
rlm@160
|
490 keymap*
|
rlm@306
|
491 initialization
|
rlm@160
|
492 world-loop*]))
|
rlm@160
|
493
|
rlm@205
|
494 (import com.jme3.font.BitmapText)
|
rlm@205
|
495 (import com.jme3.scene.control.AbstractControl)
|
rlm@205
|
496 (import com.aurellem.capture.IsoTimer)
|
rlm@60
|
497
|
rlm@306
|
498 (defn display-dilated-time
|
rlm@205
|
499 "Shows the time as it is flowing in the simulation on a HUD display.
|
rlm@205
|
500 Useful for making videos."
|
rlm@205
|
501 [world timer]
|
rlm@205
|
502 (let [font (.loadFont (asset-manager) "Interface/Fonts/Default.fnt")
|
rlm@205
|
503 text (BitmapText. font false)]
|
rlm@205
|
504 (.setLocalTranslation text 300 (.getLineHeight text) 0)
|
rlm@205
|
505 (.addControl
|
rlm@205
|
506 text
|
rlm@205
|
507 (proxy [AbstractControl] []
|
rlm@205
|
508 (controlUpdate [tpf]
|
rlm@205
|
509 (.setText text (format
|
rlm@205
|
510 "%.2f"
|
rlm@341
|
511 (float (.getTimeInSeconds timer)))))
|
rlm@205
|
512 (controlRender [_ _])))
|
rlm@205
|
513 (.attachChild (.getGuiNode world) text)))
|
rlm@50
|
514 #+end_src
|
rlm@50
|
515
|
rlm@50
|
516
|
rlm@50
|
517 *** Viewing Objects
|
rlm@50
|
518
|
rlm@66
|
519 #+name: world-view
|
rlm@50
|
520 #+begin_src clojure :results silent
|
rlm@50
|
521 (in-ns 'cortex.util)
|
rlm@50
|
522
|
rlm@50
|
523 (defprotocol Viewable
|
rlm@50
|
524 (view [something]))
|
rlm@50
|
525
|
rlm@50
|
526 (extend-type com.jme3.scene.Geometry
|
rlm@50
|
527 Viewable
|
rlm@50
|
528 (view [geo]
|
rlm@50
|
529 (view (doto (Node.)(.attachChild geo)))))
|
rlm@47
|
530
|
rlm@29
|
531 (extend-type com.jme3.scene.Node
|
rlm@29
|
532 Viewable
|
rlm@29
|
533 (view
|
rlm@29
|
534 [node]
|
rlm@29
|
535 (.start
|
rlm@29
|
536 (world
|
rlm@29
|
537 node
|
rlm@29
|
538 {}
|
rlm@29
|
539 (fn [world]
|
rlm@29
|
540 (enable-debug world)
|
rlm@29
|
541 (set-gravity world Vector3f/ZERO)
|
rlm@47
|
542 (light-up-everything world))
|
rlm@29
|
543 no-op))))
|
rlm@62
|
544
|
rlm@62
|
545 (extend-type com.jme3.math.ColorRGBA
|
rlm@62
|
546 Viewable
|
rlm@62
|
547 (view
|
rlm@62
|
548 [color]
|
rlm@62
|
549 (view (doto (Node.)
|
rlm@62
|
550 (.attachChild (box 1 1 1 :color color))))))
|
rlm@62
|
551
|
rlm@336
|
552 (extend-type ij.ImagePlus
|
rlm@336
|
553 Viewable
|
rlm@336
|
554 (view [image]
|
rlm@336
|
555 (.show image)))
|
rlm@336
|
556
|
rlm@335
|
557 (extend-type java.awt.image.BufferedImage
|
rlm@335
|
558 Viewable
|
rlm@335
|
559 (view
|
rlm@335
|
560 [image]
|
rlm@336
|
561 (view (ImagePlus. "view-buffered-image" image))))
|
rlm@336
|
562
|
rlm@336
|
563
|
rlm@62
|
564 (defprotocol Textual
|
rlm@62
|
565 (text [something]
|
rlm@62
|
566 "Display a detailed textual analysis of the given object."))
|
rlm@62
|
567
|
rlm@62
|
568 (extend-type com.jme3.scene.Node
|
rlm@62
|
569 Textual
|
rlm@62
|
570 (text [node]
|
rlm@62
|
571 (println "Total Vertexes: " (.getVertexCount node))
|
rlm@62
|
572 (println "Total Triangles: " (.getTriangleCount node))
|
rlm@62
|
573 (println "Controls :")
|
rlm@62
|
574 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
|
rlm@62
|
575 (println "Has " (.getQuantity node) " Children:")
|
rlm@62
|
576 (doall (map text (.getChildren node)))))
|
rlm@62
|
577
|
rlm@62
|
578 (extend-type com.jme3.animation.AnimControl
|
rlm@62
|
579 Textual
|
rlm@62
|
580 (text [control]
|
rlm@62
|
581 (let [animations (.getAnimationNames control)]
|
rlm@62
|
582 (println "Animation Control with " (count animations) " animation(s):")
|
rlm@62
|
583 (dorun (map println animations)))))
|
rlm@62
|
584
|
rlm@62
|
585 (extend-type com.jme3.animation.SkeletonControl
|
rlm@62
|
586 Textual
|
rlm@62
|
587 (text [control]
|
rlm@62
|
588 (println "Skeleton Control with the following skeleton:")
|
rlm@62
|
589 (println (.getSkeleton control))))
|
rlm@62
|
590
|
rlm@62
|
591 (extend-type com.jme3.bullet.control.KinematicRagdollControl
|
rlm@62
|
592 Textual
|
rlm@62
|
593 (text [control]
|
rlm@62
|
594 (println "Ragdoll Control")))
|
rlm@62
|
595
|
rlm@62
|
596 (extend-type com.jme3.scene.Geometry
|
rlm@62
|
597 Textual
|
rlm@62
|
598 (text [control]
|
rlm@62
|
599 (println "...geo...")))
|
rlm@108
|
600
|
rlm@108
|
601 (extend-type Triangle
|
rlm@108
|
602 Textual
|
rlm@108
|
603 (text [t]
|
rlm@108
|
604 (println "Triangle: " \newline (.get1 t) \newline
|
rlm@108
|
605 (.get2 t) \newline (.get3 t))))
|
rlm@108
|
606
|
rlm@25
|
607 #+end_src
|
rlm@25
|
608
|
rlm@29
|
609 Here I make the =Viewable= protocol and extend it to JME's types. Now
|
rlm@34
|
610 JME3's =hello-world= can be written as easily as:
|
rlm@29
|
611
|
rlm@29
|
612 #+begin_src clojure :results silent
|
rlm@29
|
613 (cortex.util/view (cortex.util/box))
|
rlm@29
|
614 #+end_src
|
rlm@29
|
615
|
rlm@29
|
616
|
rlm@24
|
617 * COMMENT code generation
|
rlm@24
|
618 #+begin_src clojure :tangle ../src/cortex/import.clj
|
rlm@24
|
619 <<import>>
|
rlm@24
|
620 #+end_src
|
rlm@25
|
621
|
rlm@25
|
622
|
rlm@29
|
623 #+begin_src clojure :tangle ../src/cortex/util.clj :noweb yes
|
rlm@25
|
624 <<util>>
|
rlm@25
|
625 <<shapes>>
|
rlm@50
|
626 <<debug-actions>>
|
rlm@29
|
627 <<world-view>>
|
rlm@25
|
628 #+end_src
|
rlm@25
|
629
|
rlm@29
|
630
|
rlm@29
|
631
|
rlm@29
|
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
|