comparison org/util.org @ 34:183744c179e6

MASSIVE cleanup, especially in the vision code
author Robert McIntyre <rlm@mit.edu>
date Thu, 03 Nov 2011 08:28:26 -0700
parents 0206878c28b4
children bc93abad23ee
comparison
equal deleted inserted replaced
33:c377222528e6 34:183744c179e6
4 #+description: 4 #+description:
5 #+keywords: JME3, clojure, import, utilities 5 #+keywords: JME3, clojure, import, utilities
6 #+SETUPFILE: ../../aurellem/org/setup.org 6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org 7 #+INCLUDE: ../../aurellem/org/level-0.org
8 8
9 * Utilities 9 [TABLE-OF-CONTENTS]
10 10
11 These are a collection of functions to make programming jMonkeyEngine 11 These are a collection of functions to make programming jMonkeyEngine
12 in clojure easier. 12 in clojure easier.
13 13
14 ** Imports 14 * Imports
15 15
16 #+srcname: import 16 #+srcname: import
17 #+begin_src clojure :results silent 17 #+begin_src clojure :results silent
18 (ns cortex.import 18 (ns cortex.import
19 (:require swank.util.class-browse)) 19 (:require swank.util.class-browse))
51 manage. This code uses reflection to import all of them. Once I'm 51 manage. This code uses reflection to import all of them. Once I'm
52 happy with the general structure of a namespace I can deal with 52 happy with the general structure of a namespace I can deal with
53 importing only the classes it actually needs. 53 importing only the classes it actually needs.
54 54
55 The =mega-import-jme3= is quite usefull for debugging purposes since 55 The =mega-import-jme3= is quite usefull for debugging purposes since
56 it allows completion for almost all of JME's classes. 56 it allows completion for almost all of JME's classes from the REPL.
57 57
58 Out of curiousity, let's see just how many classes =mega-import-jme3= 58 Out of curiousity, let's see just how many classes =mega-import-jme3=
59 imports: 59 imports:
60 60
61 #+begin_src clojure :exports both :results output 61 #+begin_src clojure :exports both :results output
64 64
65 #+results: 65 #+results:
66 : 955 classes 66 : 955 classes
67 67
68 68
69 ** Utilities 69 * Utilities
70 70
71 The utilities here come in three main groups: 71 The utilities here come in three main groups:
72 - Changing settings in a running =Application= 72 - Changing settings in a running =Application=
73 - Creating objects 73 - Creating objects
74 - Visualizing objects 74 - Visualizing objects
77 *** Changing Settings 77 *** Changing Settings
78 78
79 #+srcname: util 79 #+srcname: util
80 #+begin_src clojure 80 #+begin_src clojure
81 (ns cortex.util 81 (ns cortex.util
82 "Utility functions for making jMonkeyEngine easier to program from 82 "Utility functions for making jMonkeyEngine3 easier to program from
83 clojure" 83 clojure."
84 {:author "Robert McIntyre"} 84 {:author "Robert McIntyre"}
85 (:use cortex.world) 85 (:use cortex.world)
86 (:use clojure.contrib.def) 86 (:use clojure.contrib.def)
87 (:import com.jme3.math.Vector3f) 87 (:import com.jme3.math.Vector3f)
88 (:import com.jme3.math.Quaternion) 88 (:import com.jme3.math.Quaternion)
103 "println called from the LWJGL thread will not go to the REPL, but 103 "println called from the LWJGL thread will not go to the REPL, but
104 instead to whatever terminal started the JVM process. This function 104 instead to whatever terminal started the JVM process. This function
105 will always output to the REPL") 105 will always output to the REPL")
106 106
107 (defn position-camera 107 (defn position-camera
108 ([game position direction up] 108 "Change the position of the in-world camera."
109 (doto (.getCamera game) 109 ([world position direction up]
110 (doto (.getCamera world)
110 (.setLocation ) 111 (.setLocation )
111 (.lookAt direction up))) 112 (.lookAt direction up)))
112 ([game position direction] 113 ([world position direction]
113 (position-camera 114 (position-camera
114 game position direction Vector3f/UNIT_Y))) 115 world position direction Vector3f/UNIT_Y)))
115 116
116 (defn enable-debug 117 (defn enable-debug
117 "Turn on the debug wireframes for every object in this simulation" 118 "Turn on debug wireframes for every object in this simulation."
118 [world] 119 [world]
119 (.enableDebug 120 (.enableDebug
120 (.getPhysicsSpace 121 (.getPhysicsSpace
121 (.getState 122 (.getState
122 (.getStateManager world) 123 (.getStateManager world)
123 BulletAppState)) 124 BulletAppState))
124 (asset-manager))) 125 (asset-manager)))
125 126
126 (defn set-gravity 127 (defn set-gravity
127 "In order to change the gravity of a scene, it is not only necessary 128 "In order to change the gravity of a scene, it is not only necessary
128 to set the gravity variable, but to \"tap\" every physics object in 129 to set the gravity variable, but to \"tap\" every physics object in
129 the scene to reactivate physics calculations." 130 the scene to reactivate physics calculations."
130 [game gravity] 131 [world gravity]
131 (traverse 132 (traverse
132 (fn [geom] 133 (fn [geom]
133 (if-let 134 (if-let
134 ;; only set gravity for physical objects. 135 ;; only set gravity for physical objects.
135 [control (.getControl geom RigidBodyControl)] 136 [control (.getControl geom RigidBodyControl)]
136 (do 137 (do
137 (.setGravity control gravity) 138 (.setGravity control gravity)
138 ;; tappsies! 139 ;; tappsies!
139 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)))) 140 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO))))
140 (.getRootNode game))) 141 (.getRootNode world)))
141 142
142 (defn add-element 143 (defn add-element
143 "Add the Spatial to the game's environment" 144 "Add the Spatial to the world's environment"
144 ([game element node] 145 ([world element node]
145 (.addAll 146 (.addAll
146 (.getPhysicsSpace 147 (.getPhysicsSpace
147 (.getState 148 (.getState
148 (.getStateManager game) 149 (.getStateManager world)
149 BulletAppState)) 150 BulletAppState))
150 element) 151 element)
151 (.attachChild node element)) 152 (.attachChild node element))
152 ([game element] 153 ([world element]
153 (add-element game element (.getRootNode game)))) 154 (add-element world element (.getRootNode world))))
154 155
155 (defn apply-map 156 (defn apply-map
156 "Like apply, but works for maps and functions that expect an 157 "Like apply, but works for maps and functions that expect an
157 implicit map and nothing else as in (fn [& {}]). 158 implicit map and nothing else as in (fn [& {}]).
158 ------- Example ------- 159 ------- Example -------
184 shape 185 shape
185 physical? 186 physical?
186 GImpact? 187 GImpact?
187 ]) 188 ])
188 189
189 (def base-shape 190 (defvar base-shape
190 (shape-description. 191 (shape-description.
191 "default-shape" 192 "default-shape"
192 false 193 false
193 ;;ColorRGBA/Blue 194 ;;ColorRGBA/Blue
194 1.0 ;; mass 195 1.0 ;; mass
199 "Common/MatDefs/Misc/Unshaded.j3md" 200 "Common/MatDefs/Misc/Unshaded.j3md"
200 Vector3f/ZERO 201 Vector3f/ZERO
201 Quaternion/IDENTITY 202 Quaternion/IDENTITY
202 (Box. Vector3f/ZERO 0.5 0.5 0.5) 203 (Box. Vector3f/ZERO 0.5 0.5 0.5)
203 true 204 true
204 false)) 205 false)
206 "Basic settings for shapes.")
205 207
206 (defn make-shape 208 (defn make-shape
207 [#^shape-description d] 209 [#^shape-description d]
208 (let [asset-manager (asset-manager) 210 (let [asset-manager (asset-manager)
209 mat (Material. asset-manager (:material d)) 211 mat (Material. asset-manager (:material d))
210 geom (Geometry. (:name d) (:shape d))] 212 geom (Geometry. (:name d) (:shape d))]
211 (if (:texture d) 213 (if (:texture d)
212 (let [key (TextureKey. (:texture d))] 214 (let [key (TextureKey. (:texture d))]
213 (.setGenerateMips key true) 215 ;;(.setGenerateMips key true)
214 (.setTexture mat "ColorMap" (.loadTexture asset-manager key)))) 216 ;;(.setTexture mat "ColorMap" (.loadTexture asset-manager key))
217 ))
215 (if (:color d) (.setColor mat "Color" (:color d))) 218 (if (:color d) (.setColor mat "Color" (:color d)))
216 (.setMaterial geom mat) 219 (.setMaterial geom mat)
217 (if-let [rotation (:rotation d)] (.rotate geom rotation)) 220 (if-let [rotation (:rotation d)] (.rotate geom rotation))
218 (.setLocalTranslation geom (:position d)) 221 (.setLocalTranslation geom (:position d))
219 (if (:physical? d) 222 (if (:physical? d)
246 (make-shape (assoc options 249 (make-shape (assoc options
247 :shape (Sphere. 32 32 (float r)))))) 250 :shape (Sphere. 32 32 (float r))))))
248 ([] (sphere 0.5))) 251 ([] (sphere 0.5)))
249 #+end_src 252 #+end_src
250 253
251
252 *** Viewing Objects 254 *** Viewing Objects
253 255
254 #+srcname: world-view 256 #+srcname: world-view
255 #+begin_src clojure :results silent 257 #+begin_src clojure :results silent
256 (in-ns 'cortex.util) 258 (in-ns 'cortex.util)
277 (let [sun 279 (let [sun
278 (doto (DirectionalLight.) 280 (doto (DirectionalLight.)
279 (.setDirection 281 (.setDirection
280 (.normalizeLocal (Vector3f. 1 0 -2))) 282 (.normalizeLocal (Vector3f. 1 0 -2)))
281 (.setColor ColorRGBA/White))] 283 (.setColor ColorRGBA/White))]
284 ;; lights are required to view some objects.
282 (.addLight (.getRootNode world) sun))) 285 (.addLight (.getRootNode world) sun)))
283 no-op)))) 286 no-op))))
284 #+end_src 287 #+end_src
285 288
286 Here I make the =Viewable= protocol and extend it to JME's types. Now 289 Here I make the =Viewable= protocol and extend it to JME's types. Now
287 hello-world can be written as easily as: 290 JME3's =hello-world= can be written as easily as:
288 291
289 #+begin_src clojure :results silent 292 #+begin_src clojure :results silent
290 (cortex.util/view (cortex.util/box)) 293 (cortex.util/view (cortex.util/box))
291 #+end_src 294 #+end_src
292 295