rlm@0
|
1 #+title: Simulated Senses
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@0
|
4 #+description: Simulating senses for AI research using JMonkeyEngine3
|
rlm@4
|
5 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
6 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@6
|
7 #+babel: :mkdirp yes :noweb yes
|
rlm@0
|
8
|
rlm@0
|
9 * Background
|
rlm@0
|
10 Artificial Intelligence has tried and failed for more than half a
|
rlm@0
|
11 century to produce programs as flexible, creative, and “intelligent”
|
rlm@0
|
12 as the human mind itself. Clearly, we are still missing some important
|
rlm@0
|
13 ideas concerning intelligent programs or we would have strong AI
|
rlm@0
|
14 already. What idea could be missing?
|
rlm@0
|
15
|
rlm@0
|
16 When Turing first proposed his famous “Turing Test” in the
|
rlm@0
|
17 groundbreaking paper [[./sources/turing.pdf][/Computing Machines and Intelligence/]], he gave
|
rlm@0
|
18 little importance to how a computer program might interact with the
|
rlm@0
|
19 world:
|
rlm@0
|
20
|
rlm@0
|
21 #+BEGIN_QUOTE
|
rlm@0
|
22 \ldquo{}We need not be too concerned about the legs, eyes, etc. The example of
|
rlm@0
|
23 Miss Helen Keller shows that education can take place provided that
|
rlm@0
|
24 communication in both directions between teacher and pupil can take
|
rlm@0
|
25 place by some means or other.\rdquo{}
|
rlm@0
|
26 #+END_QUOTE
|
rlm@0
|
27
|
rlm@0
|
28 And from the example of Hellen Keller he went on to assume that the
|
rlm@0
|
29 only thing a fledgling AI program could need by way of communication
|
rlm@0
|
30 is a teletypewriter. But Hellen Keller did possess vision and hearing
|
rlm@0
|
31 for the first few months of her life, and her tactile sense was far
|
rlm@0
|
32 more rich than any text-stream could hope to achieve. She possessed a
|
rlm@0
|
33 body she could move freely, and had continual access to the real world
|
rlm@0
|
34 to learn from her actions.
|
rlm@0
|
35
|
rlm@0
|
36 I believe that our programs are suffering from too little sensory
|
rlm@0
|
37 input to become really intelligent. Imagine for a moment that you
|
rlm@0
|
38 lived in a world completely cut off form all sensory stimulation. You
|
rlm@0
|
39 have no eyes to see, no ears to hear, no mouth to speak. No body, no
|
rlm@0
|
40 taste, no feeling whatsoever. The only sense you get at all is a
|
rlm@0
|
41 single point of light, flickering on and off in the void. If this was
|
rlm@0
|
42 your life from birth, you would never learn anything, and could never
|
rlm@0
|
43 become intelligent. Actual humans placed in sensory deprivation
|
rlm@0
|
44 chambers experience hallucinations and can begin to loose their sense
|
rlm@0
|
45 of reality in as little as 15 minutes[sensory-deprivation]. Most of
|
rlm@0
|
46 the time, the programs we write are in exactly this situation. They do
|
rlm@0
|
47 not interface with cameras and microphones, and they do not control a
|
rlm@0
|
48 real or simulated body or interact with any sort of world.
|
rlm@0
|
49
|
rlm@0
|
50
|
rlm@0
|
51 * Simulation vs. Reality
|
rlm@0
|
52 I want demonstrate that multiple senses are what enable
|
rlm@0
|
53 intelligence. There are two ways of playing around with senses and
|
rlm@0
|
54 computer programs:
|
rlm@0
|
55
|
rlm@0
|
56 The first is to go entirely with simulation: virtual world, virtual
|
rlm@0
|
57 character, virtual senses. The advantages are that when everything is
|
rlm@0
|
58 a simulation, experiments in that simulation are absolutely
|
rlm@0
|
59 reproducible. It's also easier to change the character and world to
|
rlm@0
|
60 explore new situations and different sensory combinations.
|
rlm@0
|
61
|
rlm@0
|
62
|
rlm@0
|
63 ** Issues with Simulation
|
rlm@0
|
64
|
rlm@0
|
65 If the world is to be simulated on a computer, then not only do you
|
rlm@0
|
66 have to worry about whether the character's senses are rich enough to
|
rlm@0
|
67 learn from the world, but whether the world itself is rendered with
|
rlm@0
|
68 enough detail and realism to give enough working material to the
|
rlm@0
|
69 character's senses. To name just a few difficulties facing modern
|
rlm@0
|
70 physics simulators: destructibility of the environment, simulation of
|
rlm@0
|
71 water/other fluids, large areas, nonrigid bodies, lots of objects,
|
rlm@0
|
72 smoke. I don't know of any computer simulation that would allow a
|
rlm@0
|
73 character to take a rock and grind it into fine dust, then use that
|
rlm@0
|
74 dust to make a clay sculpture, at least not without spending years
|
rlm@0
|
75 calculating the interactions of every single small grain of
|
rlm@0
|
76 dust. Maybe a simulated world with today's limitations doesn't provide
|
rlm@0
|
77 enough richness for real intelligence to evolve.
|
rlm@0
|
78
|
rlm@0
|
79 ** Issues with Reality
|
rlm@0
|
80
|
rlm@0
|
81 The other approach for playing with senses is to hook your software up
|
rlm@0
|
82 to real cameras, microphones, robots, etc., and let it loose in the
|
rlm@0
|
83 real world. This has the advantage of eliminating concerns about
|
rlm@0
|
84 simulating the world at the expense of increasing the complexity of
|
rlm@0
|
85 implementing the senses. Instead of just grabbing the current rendered
|
rlm@0
|
86 frame for processing, you have to use an actual camera with real
|
rlm@0
|
87 lenses and interact with photons to get an image. It is much harder to
|
rlm@0
|
88 change the character, which is now partly a physical robot of some
|
rlm@0
|
89 sort, since doing so involves changing things around in the real world
|
rlm@0
|
90 instead of modifying lines of code. While the real world is very rich
|
rlm@0
|
91 and definitely provides enough stimulation for intelligence to develop
|
rlm@0
|
92 as evidenced by our own existence, it is also uncontrollable in the
|
rlm@0
|
93 sense that a particular situation cannot be recreated perfectly or
|
rlm@0
|
94 saved for later use. It is harder to conduct science because it is
|
rlm@0
|
95 harder to repeat an experiment. The worst thing about using the real
|
rlm@0
|
96 world instead of a simulation is the matter of time. Instead of
|
rlm@0
|
97 simulated time you get the constant and unstoppable flow of real
|
rlm@0
|
98 time. This severely limits the sorts of software you can use to
|
rlm@0
|
99 program the AI because all sense inputs must be handled in real
|
rlm@0
|
100 time. Complicated ideas may have to be implemented in hardware or may
|
rlm@0
|
101 simply be impossible given the current speed of our
|
rlm@0
|
102 processors. Contrast this with a simulation, in which the flow of time
|
rlm@0
|
103 in the simulated world can be slowed down to accommodate the
|
rlm@0
|
104 limitations of the character's programming. In terms of cost, doing
|
rlm@0
|
105 everything in software is far cheaper than building custom real-time
|
rlm@0
|
106 hardware. All you need is a laptop and some patience.
|
rlm@0
|
107
|
rlm@0
|
108 * Choose a Simulation Engine
|
rlm@0
|
109
|
rlm@0
|
110 Mainly because of issues with controlling the flow of time, I chose to
|
rlm@0
|
111 simulate both the world and the character. I set out to make a minimal
|
rlm@0
|
112 world in which I could embed a character with multiple senses. My main
|
rlm@0
|
113 goal is to make an environment where I can perform further experiments
|
rlm@0
|
114 in simulated senses.
|
rlm@0
|
115
|
rlm@0
|
116 As Carl Sagan once said, "If you wish to make an apple pie from
|
rlm@0
|
117 scratch, you must first invent the universe.” I examined many
|
rlm@0
|
118 different 3D environments to try and find something I would use as the
|
rlm@0
|
119 base for my simulation; eventually the choice came down to three
|
rlm@0
|
120 engines: the Quake II engine, the Source Engine, and jMonkeyEngine.
|
rlm@0
|
121
|
rlm@0
|
122 ** Quake II/Jake2
|
rlm@0
|
123
|
rlm@0
|
124 I spent a bit more than a month working with the Quake II Engine from
|
rlm@0
|
125 ID software to see if I could use it for my purposes. All the source
|
rlm@0
|
126 code was released by ID software into the Public Domain several years
|
rlm@0
|
127 ago, and as a result it has been ported and modified for many
|
rlm@0
|
128 different reasons. This engine was famous for its advanced use of
|
rlm@0
|
129 realistic shading and had decent and fast physics
|
rlm@0
|
130 simulation. Researchers at Princeton [[http://www.nature.com/nature/journal/v461/n7266/pdf/nature08499.pdf][used this code]] to study spatial
|
rlm@0
|
131 information encoding in the hippocampal cells of rats. Those
|
rlm@0
|
132 researchers created a special Quake II level that simulated a maze,
|
rlm@0
|
133 and added an interface where a mouse could run around inside a ball in
|
rlm@0
|
134 various directions to move the character in the simulated maze. They
|
rlm@0
|
135 measured hippocampal activity during this exercise to try and tease
|
rlm@0
|
136 out the method in which spatial data was stored in that area of the
|
rlm@0
|
137 brain. I find this promising because if a real living rat can interact
|
rlm@0
|
138 with a computer simulation of a maze in the same way as it interacts
|
rlm@0
|
139 with a real-world maze, then maybe that simulation is close enough to
|
rlm@0
|
140 reality that a simulated sense of vision and motor control interacting
|
rlm@0
|
141 with that simulation could reveal useful information about the real
|
rlm@0
|
142 thing. It happens that there is a Java port of the original C source
|
rlm@0
|
143 code called Jake2. The port demonstrates Java's OpenGL bindings and
|
rlm@0
|
144 runs anywhere from 90% to 105% as fast as the C version. After
|
rlm@0
|
145 reviewing much of the source of Jake2, I eventually rejected it
|
rlm@0
|
146 because the engine is too tied to the concept of a first-person
|
rlm@0
|
147 shooter game. One of the problems I had was that there does not seem
|
rlm@0
|
148 to be any easy way to attach multiple cameras to a single
|
rlm@0
|
149 character. There are also several physics clipping issues that are
|
rlm@0
|
150 corrected in a way that only applies to the main character and does
|
rlm@0
|
151 not apply to arbitrary objects. While there is a large community of
|
rlm@0
|
152 level modders, I couldn't find a community to support using the engine
|
rlm@0
|
153 to make new things.
|
rlm@0
|
154
|
rlm@0
|
155 ** Source Engine
|
rlm@0
|
156
|
rlm@0
|
157 The Source Engine evolved from the Quake II and Quake I engines and is
|
rlm@0
|
158 used by Valve in the Half-Life series of games. The physics simulation
|
rlm@0
|
159 in the Source Engine is quite accurate and probably the best out of
|
rlm@0
|
160 all the engines I investigated. There is also an extensive community
|
rlm@0
|
161 actively working with the engine. However, applications that use the
|
rlm@0
|
162 Source Engine must be written in C++, the code is not open, it only
|
rlm@0
|
163 runs on Windows, and the tools that come with the SDK to handle models
|
rlm@0
|
164 and textures are complicated and awkward to use.
|
rlm@0
|
165
|
rlm@0
|
166 ** jMonkeyEngine
|
rlm@0
|
167
|
rlm@0
|
168 jMonkeyEngine is a new library for creating games in Java. It uses
|
rlm@0
|
169 OpenGL to render to the screen and uses screengraphs to avoid drawing
|
rlm@0
|
170 things that do not appear on the screen. It has an active community
|
rlm@0
|
171 and several games in the pipeline. The engine was not built to serve
|
rlm@0
|
172 any particular game but is instead meant to be used for any 3D
|
rlm@0
|
173 game. After experimenting with each of these three engines and a few
|
rlm@0
|
174 others for about 2 months I settled on jMonkeyEngine. I chose it
|
rlm@0
|
175 because it had the most features out of all the open projects I looked
|
rlm@0
|
176 at, and because I could then write my code in Clojure, an
|
rlm@0
|
177 implementation of LISP that runs on the JVM.
|
rlm@0
|
178
|
rlm@0
|
179 * Setup
|
rlm@0
|
180
|
rlm@0
|
181 First, I checked out the source to jMonkeyEngine:
|
rlm@0
|
182
|
rlm@0
|
183 #+srcname: checkout
|
rlm@0
|
184 #+begin_src sh :results verbatim
|
rlm@0
|
185 svn checkout http://jmonkeyengine.googlecode.com/svn/trunk/engine jme3
|
rlm@0
|
186 #+end_src
|
rlm@0
|
187
|
rlm@0
|
188 #+results: checkout
|
rlm@0
|
189 : Checked out revision 7975.
|
rlm@0
|
190
|
rlm@0
|
191
|
rlm@0
|
192 Building jMonkeyEngine is easy enough:
|
rlm@0
|
193
|
rlm@0
|
194 #+srcname: build
|
rlm@0
|
195 #+begin_src sh :results verbatim
|
rlm@0
|
196 cd jme3
|
rlm@0
|
197 ant jar | tail -n 2
|
rlm@0
|
198 #+end_src
|
rlm@0
|
199
|
rlm@0
|
200 #+results: build
|
rlm@0
|
201 : BUILD SUCCESSFUL
|
rlm@0
|
202 : Total time: 15 seconds
|
rlm@0
|
203
|
rlm@0
|
204
|
rlm@0
|
205 Also build the javadoc:
|
rlm@0
|
206
|
rlm@0
|
207 #+srcname: javadoc
|
rlm@0
|
208 #+begin_src sh :results verbatim
|
rlm@0
|
209 cd jme3
|
rlm@0
|
210 ant javadoc | tail -n 2
|
rlm@0
|
211 #+end_src
|
rlm@0
|
212
|
rlm@0
|
213 #+results: javadoc
|
rlm@0
|
214 : BUILD SUCCESSFUL
|
rlm@0
|
215 : Total time: 12 seconds
|
rlm@0
|
216
|
rlm@0
|
217 Now, move the jars from the compilation into the project's lib folder.
|
rlm@0
|
218
|
rlm@0
|
219 #+srcname: move-jars
|
rlm@0
|
220 #+begin_src sh :results verbatim
|
rlm@0
|
221 mkdir -p lib
|
rlm@0
|
222 mkdir -p src
|
rlm@0
|
223 cp jme3/dist/jMonkeyEngine3.jar lib/
|
rlm@0
|
224 cp jme3/dist/lib/* lib/
|
rlm@0
|
225 ls lib
|
rlm@0
|
226 #+end_src
|
rlm@0
|
227
|
rlm@0
|
228 #+results: move-jars
|
rlm@0
|
229 #+begin_example
|
rlm@0
|
230 eventbus-1.4.jar
|
rlm@0
|
231 jbullet.jar
|
rlm@0
|
232 jheora-jst-debug-0.6.0.jar
|
rlm@0
|
233 jinput.jar
|
rlm@0
|
234 jME3-jbullet.jar
|
rlm@0
|
235 jME3-lwjgl-natives.jar
|
rlm@0
|
236 jME3-testdata.jar
|
rlm@0
|
237 jME3-test.jar
|
rlm@0
|
238 jMonkeyEngine3.jar
|
rlm@0
|
239 j-ogg-oggd.jar
|
rlm@0
|
240 j-ogg-vorbisd.jar
|
rlm@0
|
241 lwjgl.jar
|
rlm@0
|
242 nifty-1.3.jar
|
rlm@0
|
243 nifty-default-controls-1.3.jar
|
rlm@0
|
244 nifty-examples-1.3.jar
|
rlm@0
|
245 nifty-lwjgl-renderer-1.3.jar
|
rlm@0
|
246 nifty-openal-soundsystem-1.0.jar
|
rlm@0
|
247 nifty-style-black-1.3.jar
|
rlm@0
|
248 nifty-style-grey-1.0.jar
|
rlm@0
|
249 noise-0.0.1-SNAPSHOT.jar
|
rlm@0
|
250 stack-alloc.jar
|
rlm@0
|
251 vecmath.jar
|
rlm@0
|
252 xmlpull-xpp3-1.1.4c.jar
|
rlm@0
|
253 #+end_example
|
rlm@0
|
254
|
rlm@0
|
255 It's good to create a =assets= directory in the style that the
|
rlm@0
|
256 =AssetManager= will like.
|
rlm@0
|
257
|
rlm@0
|
258 #+srcname: create-assets
|
rlm@0
|
259 #+begin_src sh :results verbatim
|
rlm@0
|
260 mkdir -p assets
|
rlm@0
|
261 mkdir -p assets/Interface
|
rlm@0
|
262 mkdir -p assets/Materials
|
rlm@0
|
263 mkdir -p assets/MatDefs
|
rlm@0
|
264 mkdir -p assets/Models
|
rlm@0
|
265 mkdir -p assets/Scenes
|
rlm@0
|
266 mkdir -p assets/Shaders
|
rlm@0
|
267 mkdir -p assets/Sounds
|
rlm@0
|
268 mkdir -p assets/Textures
|
rlm@0
|
269 tree -L 1 assets
|
rlm@0
|
270 #+end_src
|
rlm@0
|
271
|
rlm@0
|
272 #+results: create-assets
|
rlm@0
|
273 #+begin_example
|
rlm@0
|
274 assets
|
rlm@0
|
275 |-- Interface
|
rlm@0
|
276 |-- MatDefs
|
rlm@0
|
277 |-- Materials
|
rlm@0
|
278 |-- Models
|
rlm@0
|
279 |-- Scenes
|
rlm@0
|
280 |-- Shaders
|
rlm@0
|
281 |-- Sounds
|
rlm@0
|
282 `-- Textures
|
rlm@0
|
283
|
rlm@0
|
284 8 directories, 0 files
|
rlm@0
|
285 #+end_example
|
rlm@0
|
286
|
rlm@0
|
287
|
rlm@0
|
288 The java classpath should have all the jars contained in the =lib=
|
rlm@0
|
289 directory as well as the src directory.
|
rlm@0
|
290
|
rlm@0
|
291 For example, here is the file I use to run my REPL for clojure.
|
rlm@0
|
292
|
rlm@0
|
293 #+include: "~/swank-all" src sh :exports code
|
rlm@0
|
294
|
rlm@0
|
295 The important thing here is that =cortex/lib/*=, =cortex/src=, and
|
rlm@0
|
296 =cortex/assets= appear on the classpath. (=cortex= is the base
|
rlm@0
|
297 directory of this project.)
|
rlm@0
|
298
|
rlm@0
|
299 #+srcname: pwd
|
rlm@0
|
300 #+begin_src sh
|
rlm@0
|
301 pwd
|
rlm@0
|
302 #+end_src
|
rlm@0
|
303
|
rlm@0
|
304 #+results: pwd
|
rlm@0
|
305 : /home/r/cortex
|
rlm@0
|
306
|
rlm@0
|
307
|
rlm@0
|
308 * Simulation Base
|
rlm@0
|
309
|
rlm@0
|
310 ** Imports
|
rlm@0
|
311 First, I'll import jme core classes.
|
rlm@0
|
312 #+srcname: import
|
rlm@0
|
313 #+begin_src clojure :results silent
|
rlm@0
|
314 (ns cortex.import
|
rlm@0
|
315 (:require swank.util.class-browse))
|
rlm@0
|
316
|
rlm@0
|
317 (defn import-jme3 []
|
rlm@0
|
318 (import '[com.jme3.system AppSettings JmeSystem])
|
rlm@0
|
319 (import '[com.jme3.app Application SimpleApplication])
|
rlm@0
|
320 (import 'com.jme3.material.Material)
|
rlm@0
|
321 (import '[com.jme3.math Vector3f ColorRGBA Quaternion Transform])
|
rlm@0
|
322 (import '[com.jme3.scene Node Geometry])
|
rlm@0
|
323 (import '[com.jme3.scene.shape Box Sphere Sphere$TextureMode])
|
rlm@0
|
324 (import 'com.jme3.font.BitmapText)
|
rlm@0
|
325 (import '[com.jme3.input KeyInput InputManager])
|
rlm@0
|
326 (import '[com.jme3.input.controls
|
rlm@0
|
327 ActionListener AnalogListener KeyTrigger MouseButtonTrigger])
|
rlm@0
|
328 (import '[com.jme3.asset AssetManager DesktopAssetManager] )
|
rlm@0
|
329 (import '[com.jme3.asset.plugins HttpZipLocator ZipLocator])
|
rlm@0
|
330 (import '[com.jme3.light PointLight DirectionalLight])
|
rlm@0
|
331 (import '[com.jme3.animation AnimControl Skeleton Bone])
|
rlm@0
|
332 (import '[com.jme3.bullet.collision.shapes
|
rlm@0
|
333 MeshCollisionShape SphereCollisionShape BoxCollisionShape])
|
rlm@0
|
334 (import 'com.jme3.renderer.queue.RenderQueue$ShadowMode)
|
rlm@0
|
335 (import 'jme3test.TestChooser)
|
rlm@0
|
336 (import '[com.jme3.bullet PhysicsTickListener PhysicsSpace])
|
rlm@0
|
337 (import '[com.jme3.bullet.joints SixDofJoint HingeJoint
|
rlm@0
|
338 SliderJoint Point2PointJoint ConeJoint]))
|
rlm@0
|
339
|
rlm@0
|
340
|
rlm@0
|
341 (defmacro permissive-import* [class-symbol]
|
rlm@0
|
342 `(try
|
rlm@0
|
343 (import ~class-symbol)
|
rlm@0
|
344 (catch Exception e#
|
rlm@0
|
345 (println "can't import " ~class-symbol))))
|
rlm@0
|
346
|
rlm@0
|
347 (defn permissive-import [class-symbol]
|
rlm@0
|
348 (eval (list 'cortex.import/permissive-import* class-symbol)))
|
rlm@0
|
349
|
rlm@0
|
350 (defn selection-import [selection-fn]
|
rlm@0
|
351 (dorun
|
rlm@0
|
352 (map (comp permissive-import symbol)
|
rlm@0
|
353 (filter selection-fn
|
rlm@0
|
354 (map :name
|
rlm@0
|
355 swank.util.class-browse/available-classes)))))
|
rlm@0
|
356
|
rlm@0
|
357 (defn mega-import-jme3
|
rlm@0
|
358 "ALL the jme classes. For REPL use."
|
rlm@0
|
359 []
|
rlm@0
|
360 (selection-import
|
rlm@0
|
361 #(and
|
rlm@0
|
362 (.startsWith % "com.jme3.")
|
rlm@0
|
363 ;; Don't import the Lwjgl stuff since it can throw exceptions
|
rlm@0
|
364 ;; upon being loaded.
|
rlm@0
|
365 (not (re-matches #".*Lwjgl.*" %)))))
|
rlm@0
|
366 #+end_src
|
rlm@0
|
367
|
rlm@0
|
368 The =mega-import-jme3= is quite usefull for debugging purposes since
|
rlm@0
|
369 it allows completion for almost all of JME's classes
|
rlm@0
|
370
|
rlm@0
|
371 ** Simplification
|
rlm@0
|
372 *** World
|
rlm@0
|
373
|
rlm@0
|
374 It is comvienent to wrap the JME elements that deal with creating a
|
rlm@0
|
375 world, creation of basic objects, and Keyboard input with a nicer
|
rlm@0
|
376 interface (at least for my purposes).
|
rlm@0
|
377
|
rlm@0
|
378 #+srcname: world-inputs
|
rlm@0
|
379 #+begin_src clojure :results silent
|
rlm@0
|
380 (ns cortex.world)
|
rlm@0
|
381 (require 'cortex.import)
|
rlm@0
|
382 (use 'clojure.contrib.def)
|
rlm@0
|
383 (rlm.rlm-commands/help)
|
rlm@0
|
384 (cortex.import/mega-import-jme3)
|
rlm@0
|
385
|
rlm@0
|
386 (defvar *app-settings*
|
rlm@0
|
387 (doto (AppSettings. true)
|
rlm@0
|
388 (.setFullscreen false)
|
rlm@0
|
389 (.setTitle "Aurellem.")
|
rlm@0
|
390 ;; disable 32 bit stuff for now
|
rlm@6
|
391 ;;(.setAudioRenderer "Send")
|
rlm@0
|
392 )
|
rlm@0
|
393 "These settings control how the game is displayed on the screen for
|
rlm@0
|
394 debugging purposes. Use binding forms to change this if desired.
|
rlm@0
|
395 Full-screen mode does not work on some computers.")
|
rlm@0
|
396
|
rlm@0
|
397 (defn asset-manager
|
rlm@0
|
398 "returns a new, configured assetManager" []
|
rlm@0
|
399 (JmeSystem/newAssetManager
|
rlm@0
|
400 (.getResource
|
rlm@0
|
401 (.getContextClassLoader (Thread/currentThread))
|
rlm@0
|
402 "com/jme3/asset/Desktop.cfg")))
|
rlm@0
|
403
|
rlm@0
|
404 (defmacro no-exceptions
|
rlm@0
|
405 "Sweet relief like I never knew."
|
rlm@0
|
406 [& forms]
|
rlm@0
|
407 `(try ~@forms (catch Exception e# (.printStackTrace e#))))
|
rlm@0
|
408
|
rlm@0
|
409 (defn thread-exception-removal []
|
rlm@0
|
410 (println "removing exceptions from " (Thread/currentThread))
|
rlm@0
|
411 (.setUncaughtExceptionHandler
|
rlm@0
|
412 (Thread/currentThread)
|
rlm@0
|
413 (proxy [Thread$UncaughtExceptionHandler] []
|
rlm@0
|
414 (uncaughtException
|
rlm@0
|
415 [thread thrown]
|
rlm@0
|
416 (println "uncaught-exception thrown in " thread)
|
rlm@0
|
417 (println (.getMessage thrown))))))
|
rlm@0
|
418
|
rlm@0
|
419 (def println-repl (bound-fn [& args] (apply println args)))
|
rlm@0
|
420
|
rlm@0
|
421 (use '[pokemon [lpsolve :only [constant-map]]])
|
rlm@0
|
422
|
rlm@0
|
423 (defn no-op [& _])
|
rlm@0
|
424
|
rlm@0
|
425 (defn all-keys
|
rlm@0
|
426 "Construct a map of strings representing all the manual inputs from
|
rlm@0
|
427 either the keyboard or mouse."
|
rlm@0
|
428 []
|
rlm@0
|
429 (let [inputs (constant-map KeyInput)]
|
rlm@0
|
430 (assoc
|
rlm@0
|
431 (zipmap (map (fn [field]
|
rlm@0
|
432 (.toLowerCase (re-gsub #"_" "-" field))) (vals inputs))
|
rlm@0
|
433 (map (fn [val] (KeyTrigger. val)) (keys inputs)))
|
rlm@0
|
434 ;;explicitly add mouse controls
|
rlm@0
|
435 "mouse-left" (MouseButtonTrigger. 0)
|
rlm@0
|
436 "mouse-middle" (MouseButtonTrigger. 2)
|
rlm@0
|
437 "mouse-right" (MouseButtonTrigger. 1))))
|
rlm@0
|
438
|
rlm@0
|
439 (defn initialize-inputs
|
rlm@0
|
440 "more java-interop cruft to establish keybindings for a particular virtual world"
|
rlm@0
|
441 [game input-manager key-map]
|
rlm@0
|
442 (doall (map (fn [[name trigger]]
|
rlm@0
|
443 (.addMapping ^InputManager input-manager
|
rlm@0
|
444 name (into-array (class trigger) [trigger]))) key-map))
|
rlm@0
|
445 (doall (map (fn [name]
|
rlm@0
|
446 (.addListener ^InputManager input-manager game
|
rlm@0
|
447 (into-array String [name]))) (keys key-map))))
|
rlm@0
|
448
|
rlm@0
|
449 #+end_src
|
rlm@0
|
450
|
rlm@0
|
451 These functions are all for debug controlling of the world through
|
rlm@0
|
452 keyboard and mouse.
|
rlm@0
|
453
|
rlm@0
|
454 We reuse =constant-map= from =pokemon.lpsolve= to get the numerical
|
rlm@0
|
455 values for all the keys defined in the =KeyInput= class. The
|
rlm@0
|
456 documentation for =constant-map= is:
|
rlm@0
|
457
|
rlm@0
|
458 #+begin_src clojure :results output
|
rlm@0
|
459 (doc pokemon.lpsolve/constant-map)
|
rlm@0
|
460 #+end_src
|
rlm@0
|
461
|
rlm@0
|
462 #+results:
|
rlm@0
|
463 : -------------------------
|
rlm@0
|
464 : pokemon.lpsolve/constant-map
|
rlm@0
|
465 : ([class])
|
rlm@0
|
466 : Takes a class and creates a map of the static constant integer
|
rlm@0
|
467 : fields with their names. This helps with C wrappers where they have
|
rlm@0
|
468 : just defined a bunch of integer constants instead of enums
|
rlm@0
|
469
|
rlm@0
|
470
|
rlm@0
|
471 Then, =all-keys= converts the constant names like =KEY_J= to the more
|
rlm@0
|
472 clojure-like =key-j=, and returns a map from these keys to
|
rlm@0
|
473 jMonkeyEngine KeyTrigger objects, the use of which will soon become
|
rlm@0
|
474 apparent. =all-keys= also adds the three mouse button controls to the
|
rlm@0
|
475 map.
|
rlm@0
|
476
|
rlm@0
|
477 #+srcname: world
|
rlm@0
|
478 #+begin_src clojure :results silent
|
rlm@0
|
479 (in-ns 'cortex.world)
|
rlm@0
|
480
|
rlm@0
|
481 (defn traverse
|
rlm@0
|
482 "apply f to every non-node, deeply"
|
rlm@0
|
483 [f node]
|
rlm@0
|
484 (if (isa? (class node) Node)
|
rlm@0
|
485 (dorun (map (partial traverse f) (.getChildren node)))
|
rlm@0
|
486 (f node)))
|
rlm@0
|
487
|
rlm@0
|
488 (def gravity (Vector3f. 0 -9.81 0))
|
rlm@0
|
489
|
rlm@0
|
490 (defn world
|
rlm@0
|
491 [root-node key-map setup-fn update-fn]
|
rlm@0
|
492 (let [physics-manager (BulletAppState.)
|
rlm@0
|
493 shadow-renderer (BasicShadowRenderer. (asset-manager) (int 256))
|
rlm@0
|
494 ;;maybe use a better shadow renderer someday!
|
rlm@0
|
495 ;;shadow-renderer (PssmShadowRenderer. (asset-manager) 256 1)
|
rlm@0
|
496 ]
|
rlm@0
|
497 (doto
|
rlm@0
|
498 (proxy [SimpleApplication ActionListener] []
|
rlm@0
|
499 (simpleInitApp
|
rlm@0
|
500 []
|
rlm@0
|
501 (no-exceptions
|
rlm@0
|
502 (.setTimer this (IsoTimer. 60))
|
rlm@0
|
503 ;; Create key-map.
|
rlm@0
|
504 (.setFrustumFar (.getCamera this) 300)
|
rlm@0
|
505 (initialize-inputs this (.getInputManager this) (all-keys))
|
rlm@0
|
506 ;; Don't take control of the mouse
|
rlm@0
|
507 (org.lwjgl.input.Mouse/setGrabbed false)
|
rlm@0
|
508 ;; add all objects to the world
|
rlm@0
|
509 (.attachChild (.getRootNode this) root-node)
|
rlm@0
|
510 ;; enable physics
|
rlm@0
|
511 ;; add a physics manager
|
rlm@0
|
512 (.attach (.getStateManager this) physics-manager)
|
rlm@0
|
513 (.setGravity (.getPhysicsSpace physics-manager) gravity)
|
rlm@0
|
514
|
rlm@0
|
515
|
rlm@0
|
516 ;; go through every object and add it to the physics manager
|
rlm@0
|
517 ;; if relavant.
|
rlm@0
|
518 (traverse (fn [geom]
|
rlm@0
|
519 (dorun
|
rlm@0
|
520 (for [n (range (.getNumControls geom))]
|
rlm@0
|
521 (do
|
rlm@0
|
522 (println-repl "adding control " (.getControl geom n))
|
rlm@0
|
523 (.add (.getPhysicsSpace physics-manager)
|
rlm@0
|
524 (.getControl geom n))))))
|
rlm@0
|
525 (.getRootNode this))
|
rlm@0
|
526 ;;(.addAll (.getPhysicsSpace physics-manager) (.getRootNode this))
|
rlm@0
|
527
|
rlm@0
|
528 (setup-fn this)
|
rlm@0
|
529 (.setDirection shadow-renderer
|
rlm@0
|
530 (.normalizeLocal (Vector3f. -1 -1 -1)))
|
rlm@0
|
531 (.addProcessor (.getViewPort this) shadow-renderer)
|
rlm@0
|
532 (.setShadowMode (.getRootNode this) RenderQueue$ShadowMode/Off)
|
rlm@0
|
533 ))
|
rlm@0
|
534 (simpleUpdate
|
rlm@0
|
535 [tpf]
|
rlm@0
|
536 (no-exceptions
|
rlm@0
|
537 (update-fn this tpf)))
|
rlm@0
|
538 (onAction
|
rlm@0
|
539 [binding value tpf]
|
rlm@0
|
540 ;; whenever a key is pressed, call the function returned from
|
rlm@0
|
541 ;; key-map.
|
rlm@0
|
542 (no-exceptions
|
rlm@0
|
543 (if-let [react (key-map binding)]
|
rlm@0
|
544 (react this value)))))
|
rlm@0
|
545 ;; don't show a menu to change options.
|
rlm@0
|
546
|
rlm@0
|
547 (.setShowSettings false)
|
rlm@0
|
548 (.setPauseOnLostFocus false)
|
rlm@0
|
549 (.setSettings *app-settings*))))
|
rlm@0
|
550
|
rlm@0
|
551 (defn apply-map
|
rlm@0
|
552 "Like apply, but works for maps and functions that expect an implicit map
|
rlm@0
|
553 and nothing else as in (fn [& {}]).
|
rlm@0
|
554 ------- Example -------
|
rlm@4
|
555 (defn jjj [& {:keys [www] :or {www \"oh yeah\"} :as env}] (println www))
|
rlm@0
|
556 (apply-map jjj {:www \"whatever\"})
|
rlm@0
|
557 -->\"whatever\""
|
rlm@0
|
558 [fn m]
|
rlm@0
|
559 (apply fn (reduce #(into %1 %2) [] m)))
|
rlm@0
|
560
|
rlm@0
|
561 #+end_src
|
rlm@0
|
562
|
rlm@0
|
563
|
rlm@0
|
564 =world= is the most important function here.
|
rlm@0
|
565 *** TODO more documentation
|
rlm@0
|
566
|
rlm@0
|
567 #+srcname: world-shapes
|
rlm@0
|
568 #+begin_src clojure :results silent
|
rlm@0
|
569 (in-ns 'cortex.world)
|
rlm@0
|
570 (defrecord shape-description
|
rlm@0
|
571 [name
|
rlm@0
|
572 color
|
rlm@0
|
573 mass
|
rlm@0
|
574 friction
|
rlm@0
|
575 texture
|
rlm@0
|
576 material
|
rlm@0
|
577 position
|
rlm@0
|
578 rotation
|
rlm@0
|
579 shape
|
rlm@0
|
580 physical?])
|
rlm@0
|
581
|
rlm@0
|
582 (def base-shape
|
rlm@0
|
583 (shape-description.
|
rlm@0
|
584 "default-shape"
|
rlm@0
|
585 false
|
rlm@0
|
586 ;;ColorRGBA/Blue
|
rlm@0
|
587 1.0 ;; mass
|
rlm@0
|
588 1.0 ;; friction
|
rlm@0
|
589 ;; texture
|
rlm@0
|
590 "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
591 ;; material
|
rlm@0
|
592 "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
593 Vector3f/ZERO
|
rlm@0
|
594 Quaternion/IDENTITY
|
rlm@0
|
595 (Box. Vector3f/ZERO 0.5 0.5 0.5)
|
rlm@0
|
596 true))
|
rlm@0
|
597
|
rlm@0
|
598 (defn make-shape
|
rlm@0
|
599 [#^shape-description d]
|
rlm@6
|
600 (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))
|
rlm@6
|
601 mat (Material. asset-manager (:material d))
|
rlm@0
|
602 geom (Geometry. (:name d) (:shape d))]
|
rlm@0
|
603 (if (:texture d)
|
rlm@0
|
604 (let [key (TextureKey. (:texture d))]
|
rlm@0
|
605 (.setGenerateMips key true)
|
rlm@6
|
606 (.setTexture mat "ColorMap" (.loadTexture asset-manager key))))
|
rlm@0
|
607 (if (:color d) (.setColor mat "Color" (:color d)))
|
rlm@0
|
608 (.setMaterial geom mat)
|
rlm@0
|
609 (if-let [rotation (:rotation d)] (.rotate geom rotation))
|
rlm@0
|
610 (.setLocalTranslation geom (:position d))
|
rlm@0
|
611 (if (:physical? d)
|
rlm@6
|
612 (let [impact-shape (doto (GImpactCollisionShape.
|
rlm@6
|
613 (.getMesh geom)) (.setMargin 0))
|
rlm@0
|
614 physics-control (RigidBodyControl.
|
rlm@6
|
615 ;;impact-shape ;; comment to disable
|
rlm@0
|
616 (float (:mass d)))]
|
rlm@0
|
617 (.createJmeMesh impact-shape)
|
rlm@0
|
618 (.addControl geom physics-control)
|
rlm@0
|
619 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
|
rlm@0
|
620 (.setFriction physics-control (:friction d))))
|
rlm@0
|
621 ;;the default is to keep this node in the physics engine forever.
|
rlm@0
|
622 ;;these commands must come after the control is added to the geometry.
|
rlm@0
|
623 ;;
|
rlm@0
|
624 geom))
|
rlm@0
|
625
|
rlm@0
|
626 (defn box
|
rlm@0
|
627 ([l w h & {:as options}]
|
rlm@0
|
628 (let [options (merge base-shape options)]
|
rlm@0
|
629 (make-shape (assoc options
|
rlm@0
|
630 :shape (Box. l w h)))))
|
rlm@0
|
631 ([] (box 0.5 0.5 0.5)))
|
rlm@0
|
632
|
rlm@0
|
633 (defn sphere
|
rlm@0
|
634 ([r & {:as options}]
|
rlm@0
|
635 (let [options (merge base-shape options)]
|
rlm@0
|
636 (make-shape (assoc options
|
rlm@0
|
637 :shape (Sphere. 32 32 (float r))))))
|
rlm@0
|
638 ([] (sphere 0.5)))
|
rlm@0
|
639
|
rlm@15
|
640 (defn add-element
|
rlm@15
|
641 ([game element node]
|
rlm@0
|
642 (.addAll
|
rlm@0
|
643 (.getPhysicsSpace
|
rlm@0
|
644 (.getState
|
rlm@0
|
645 (.getStateManager game)
|
rlm@0
|
646 BulletAppState))
|
rlm@15
|
647 element)
|
rlm@15
|
648 (.attachChild node element))
|
rlm@15
|
649 ([game element]
|
rlm@15
|
650 (add-element game element (.getRootNode game))))
|
rlm@15
|
651
|
rlm@0
|
652
|
rlm@0
|
653 (defn set-gravity*
|
rlm@0
|
654 [game gravity]
|
rlm@0
|
655 (traverse
|
rlm@0
|
656 (fn [geom]
|
rlm@0
|
657 (if-let
|
rlm@0
|
658 [control (.getControl geom RigidBodyControl)]
|
rlm@0
|
659 (do
|
rlm@0
|
660 (.setGravity control gravity)
|
rlm@0
|
661 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
|
rlm@0
|
662 )))
|
rlm@0
|
663 (.getRootNode game)))
|
rlm@0
|
664
|
rlm@0
|
665 #+end_src
|
rlm@0
|
666
|
rlm@0
|
667 These are convienence functions for creating JME objects and
|
rlm@0
|
668 manipulating a world.
|
rlm@0
|
669
|
rlm@0
|
670 #+srcname: world-view
|
rlm@0
|
671 #+begin_src clojure :results silent
|
rlm@0
|
672 (in-ns 'cortex.world)
|
rlm@0
|
673
|
rlm@0
|
674 (defprotocol Viewable
|
rlm@0
|
675 (view [something]))
|
rlm@0
|
676
|
rlm@0
|
677 (extend-type com.jme3.scene.Geometry
|
rlm@0
|
678 Viewable
|
rlm@0
|
679 (view [geo]
|
rlm@0
|
680 (view (doto (Node.)(.attachChild geo)))))
|
rlm@0
|
681
|
rlm@0
|
682 (extend-type com.jme3.scene.Node
|
rlm@0
|
683 Viewable
|
rlm@0
|
684 (view [node]
|
rlm@0
|
685 (.start
|
rlm@0
|
686 (world node
|
rlm@0
|
687 {}
|
rlm@0
|
688 (fn [world]
|
rlm@0
|
689 (.enableDebug
|
rlm@0
|
690 (.getPhysicsSpace
|
rlm@0
|
691 (.getState
|
rlm@0
|
692 (.getStateManager world)
|
rlm@0
|
693 BulletAppState))
|
rlm@0
|
694 (asset-manager))
|
rlm@0
|
695 (set-gravity* world Vector3f/ZERO)
|
rlm@0
|
696 ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0))
|
rlm@0
|
697 (let [sun (doto (DirectionalLight.)
|
rlm@0
|
698 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
|
rlm@0
|
699 (.setColor ColorRGBA/White))]
|
rlm@0
|
700 (.addLight (.getRootNode world) sun)))
|
rlm@0
|
701 no-op))))
|
rlm@0
|
702
|
rlm@0
|
703 (defn position-camera [game]
|
rlm@0
|
704 (doto (.getCamera game)
|
rlm@0
|
705 (.setLocation (Vector3f. 0 6 6))
|
rlm@0
|
706 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
|
rlm@0
|
707
|
rlm@0
|
708 #+end_src
|
rlm@0
|
709
|
rlm@0
|
710 Here I make the =Viewable= protocol and extend it to JME's types. Now
|
rlm@0
|
711 hello-world can be written as easily as:
|
rlm@0
|
712
|
rlm@0
|
713 #+begin_src clojure :results silent
|
rlm@0
|
714 (cortex.world/view (cortex.world/box))
|
rlm@0
|
715 #+end_src
|
rlm@0
|
716
|
rlm@0
|
717 ** Hello
|
rlm@0
|
718 Here are the jmonkeyengine "Hello" programs translated to clojure.
|
rlm@0
|
719 *** Hello Simple App
|
rlm@0
|
720 Here is the hello world example for jme3 in clojure.
|
rlm@0
|
721 It's a more or less direct translation from the java source
|
rlm@0
|
722 from
|
rlm@0
|
723 http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication.
|
rlm@0
|
724
|
rlm@0
|
725 Of note is the fact that since we don't have access to the
|
rlm@0
|
726 =AssetManager= via extendig =SimpleApplication=, we have to build one
|
rlm@0
|
727 ourselves.
|
rlm@0
|
728
|
rlm@0
|
729 #+srcname: hello-simple-app
|
rlm@0
|
730 #+begin_src clojure :results silent
|
rlm@0
|
731 (ns hello.hello-simple-app)
|
rlm@0
|
732 (require 'cortex.import)
|
rlm@0
|
733 (use 'clojure.contrib.def)
|
rlm@0
|
734 (rlm.rlm-commands/help)
|
rlm@0
|
735 (cortex.import/import-jme3)
|
rlm@0
|
736 (use 'cortex.world)
|
rlm@0
|
737
|
rlm@0
|
738
|
rlm@0
|
739 (def cube (Box. Vector3f/ZERO 1 1 1))
|
rlm@0
|
740
|
rlm@0
|
741 (def geom (Geometry. "Box" cube))
|
rlm@0
|
742
|
rlm@0
|
743 (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
|
rlm@0
|
744
|
rlm@0
|
745 (.setColor mat "Color" ColorRGBA/Blue)
|
rlm@0
|
746
|
rlm@0
|
747 (.setMaterial geom mat)
|
rlm@0
|
748
|
rlm@0
|
749 (defn simple-app []
|
rlm@0
|
750 (doto
|
rlm@0
|
751 (proxy [SimpleApplication] []
|
rlm@0
|
752 (simpleInitApp
|
rlm@0
|
753 []
|
rlm@0
|
754 ;; Don't take control of the mouse
|
rlm@0
|
755 (org.lwjgl.input.Mouse/setGrabbed false)
|
rlm@0
|
756 (.attachChild (.getRootNode this) geom)))
|
rlm@0
|
757 ;; don't show a menu to change options.
|
rlm@0
|
758 (.setShowSettings false)
|
rlm@0
|
759 (.setPauseOnLostFocus false)
|
rlm@0
|
760 (.setSettings *app-settings*)))
|
rlm@0
|
761 #+end_src
|
rlm@0
|
762
|
rlm@0
|
763 Running this program will begin a new jMonkeyEngine game which
|
rlm@0
|
764 displays a single blue cube.
|
rlm@0
|
765
|
rlm@0
|
766 #+begin_src clojure :exports code :results silent
|
rlm@0
|
767 (.start (hello.hello-simple-app/simple-app))
|
rlm@0
|
768 #+end_src
|
rlm@0
|
769
|
rlm@0
|
770 #+caption: the simplest JME game.
|
rlm@0
|
771 [[./images/simple-app.jpg]]
|
rlm@0
|
772
|
rlm@0
|
773
|
rlm@0
|
774
|
rlm@0
|
775 *** Hello Physics
|
rlm@0
|
776 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
|
rlm@0
|
777
|
rlm@0
|
778 #+srcname: brick-wall-header
|
rlm@0
|
779 #+begin_src clojure :results silent
|
rlm@0
|
780 (ns hello.brick-wall)
|
rlm@0
|
781 (require 'cortex.import)
|
rlm@0
|
782 (use 'clojure.contrib.def)
|
rlm@0
|
783 (rlm.rlm-commands/help)
|
rlm@0
|
784 (cortex.import/mega-import-jme3)
|
rlm@0
|
785 (use '[pokemon [lpsolve :only [constant-map]]])
|
rlm@0
|
786 (use 'cortex.world)
|
rlm@0
|
787 #+end_src
|
rlm@0
|
788
|
rlm@0
|
789 #+srcname: brick-wall-body
|
rlm@0
|
790 #+begin_src clojure :results silent
|
rlm@0
|
791 (in-ns 'hello.brick-wall)
|
rlm@0
|
792
|
rlm@0
|
793 (defn floor
|
rlm@0
|
794 "make a sturdy, unmovable physical floor"
|
rlm@0
|
795 []
|
rlm@0
|
796 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
|
rlm@0
|
797
|
rlm@0
|
798 (def brick-length 0.48)
|
rlm@0
|
799 (def brick-width 0.24)
|
rlm@0
|
800 (def brick-height 0.12)
|
rlm@0
|
801
|
rlm@0
|
802
|
rlm@0
|
803 (defn brick* [position]
|
rlm@0
|
804 (doto (box brick-length brick-height brick-width
|
rlm@0
|
805 :position position :name "brick"
|
rlm@0
|
806 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
807 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
808 :mass 36)
|
rlm@0
|
809 (->
|
rlm@0
|
810 (.getMesh)
|
rlm@0
|
811 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
|
rlm@0
|
812 ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
813 )
|
rlm@0
|
814 )
|
rlm@0
|
815
|
rlm@0
|
816 (defn inception-brick-wall
|
rlm@0
|
817 "construct a physical brick wall"
|
rlm@0
|
818 []
|
rlm@0
|
819 (let [node (Node. "brick-wall")]
|
rlm@0
|
820 (dorun
|
rlm@0
|
821 (map (comp #(.attachChild node %) brick*)
|
rlm@0
|
822 (for
|
rlm@0
|
823 [x (range 15)
|
rlm@0
|
824 y (range 10)
|
rlm@0
|
825 z (range 1)]
|
rlm@0
|
826 (Vector3f.
|
rlm@0
|
827 (* brick-length x 1.03)
|
rlm@0
|
828 (* brick-width y y 10)
|
rlm@0
|
829 (* brick-height z)))))
|
rlm@0
|
830 node))
|
rlm@0
|
831
|
rlm@0
|
832 (defn gravity-toggle
|
rlm@0
|
833 [new-value]
|
rlm@0
|
834 (fn [game value]
|
rlm@0
|
835 (println-repl "set gravity to " new-value)
|
rlm@0
|
836 (if value
|
rlm@0
|
837 (set-gravity* game new-value)
|
rlm@0
|
838 (set-gravity* game gravity))))
|
rlm@0
|
839
|
rlm@15
|
840 (defn fire-cannon-ball
|
rlm@15
|
841 ([node]
|
rlm@15
|
842 (fn [game value]
|
rlm@15
|
843 (if (not value)
|
rlm@15
|
844 (let [camera (.getCamera game)
|
rlm@15
|
845 cannon-ball
|
rlm@15
|
846 (sphere 0.7
|
rlm@15
|
847 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@15
|
848 :texture "Textures/PokeCopper.jpg"
|
rlm@15
|
849 :position
|
rlm@15
|
850 (.add (.getLocation camera)
|
rlm@15
|
851 (.mult (.getDirection camera) (float 1)))
|
rlm@15
|
852 :mass 3)] ;200 0.05
|
rlm@15
|
853 (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive)
|
rlm@15
|
854 (.setLinearVelocity
|
rlm@15
|
855 (.getControl cannon-ball RigidBodyControl)
|
rlm@15
|
856 (.mult (.getDirection camera) (float 50))) ;50
|
rlm@15
|
857 (add-element game cannon-ball node)))))
|
rlm@15
|
858 ([]
|
rlm@15
|
859 (fire-cannon-ball false)))
|
rlm@15
|
860
|
rlm@0
|
861
|
rlm@0
|
862 (defn floor* []
|
rlm@0
|
863 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
|
rlm@0
|
864 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
865 :texture "Textures/Terrain/Pond/Pond.png"
|
rlm@0
|
866 :position (Vector3f. 0 -0.1 0 )
|
rlm@0
|
867 :mass 0)
|
rlm@0
|
868 (->
|
rlm@0
|
869 (.getMesh)
|
rlm@0
|
870 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
|
rlm@0
|
871 (->
|
rlm@0
|
872 (.getMaterial)
|
rlm@0
|
873 (.getTextureParam "ColorMap")
|
rlm@0
|
874 (.getTextureValue)
|
rlm@0
|
875 (.setWrap Texture$WrapMode/Repeat))
|
rlm@0
|
876 (.setShadowMode RenderQueue$ShadowMode/Receive)
|
rlm@0
|
877 ))
|
rlm@0
|
878
|
rlm@0
|
879 (defn brick-wall* []
|
rlm@0
|
880 (let [node (Node. "brick-wall")]
|
rlm@0
|
881 (dorun
|
rlm@0
|
882 (map
|
rlm@0
|
883 (comp #(.attachChild node %) brick*)
|
rlm@0
|
884 (for [y (range 15)
|
rlm@0
|
885 x (range 4)
|
rlm@0
|
886 z (range 1)]
|
rlm@0
|
887 (Vector3f.
|
rlm@0
|
888 (+ (* 2 x brick-length)
|
rlm@0
|
889 (if (even? (+ y z))
|
rlm@0
|
890 (/ brick-length 4) (/ brick-length -4)))
|
rlm@0
|
891 (+ (* brick-height (inc (* 2 y))))
|
rlm@0
|
892 (* 2 z brick-width) ))))
|
rlm@0
|
893 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
894 node))
|
rlm@0
|
895
|
rlm@0
|
896 (defn brick-wall-game-run []
|
rlm@0
|
897 (doto
|
rlm@0
|
898 (world
|
rlm@0
|
899 (doto (Node.) (.attachChild (floor*))
|
rlm@0
|
900 (.attachChild (brick-wall*))
|
rlm@0
|
901 )
|
rlm@0
|
902 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
|
rlm@0
|
903 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
|
rlm@0
|
904 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
|
rlm@0
|
905 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
|
rlm@0
|
906 "key-k" (gravity-toggle Vector3f/ZERO)
|
rlm@0
|
907 "key-u" (gravity-toggle (Vector3f. 0 9.81 0))
|
rlm@0
|
908 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
|
rlm@0
|
909 "key-f" (fn[game value]
|
rlm@0
|
910 (if (not value) (add-element game (brick-wall*))))
|
rlm@0
|
911 "key-return" (fire-cannon-ball)}
|
rlm@0
|
912 position-camera
|
rlm@0
|
913 (fn [& _]))
|
rlm@0
|
914 (.start)))
|
rlm@0
|
915 #+end_src
|
rlm@0
|
916
|
rlm@0
|
917 #+begin_src clojure :results silent
|
rlm@0
|
918 (hello.brick-wall/brick-wall-game-run)
|
rlm@0
|
919 #+end_src
|
rlm@0
|
920
|
rlm@0
|
921 #+caption: the brick wall standing
|
rlm@0
|
922 [[./images/brick-wall-standing.jpg]]
|
rlm@0
|
923
|
rlm@0
|
924 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
|
rlm@0
|
925 [[./images/brick-wall-knocked-down.jpg]]
|
rlm@0
|
926
|
rlm@0
|
927 *** Other Brick Games
|
rlm@0
|
928 #+srcname: other-games
|
rlm@0
|
929 #+begin_src clojure :results silent
|
rlm@0
|
930 (ns cortex.other-games
|
rlm@0
|
931 {:author "Dylan Holmes"})
|
rlm@0
|
932 (use 'cortex.world)
|
rlm@0
|
933 (use 'hello.brick-wall)
|
rlm@0
|
934 (use 'cortex.import)
|
rlm@0
|
935 (cortex.import/mega-import-jme3)
|
rlm@0
|
936
|
rlm@0
|
937 (defn scad [position]
|
rlm@0
|
938 (doto (box 0.1 0.1 0.1
|
rlm@0
|
939 :position position :name "brick"
|
rlm@0
|
940 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
941 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
942 :mass 20)
|
rlm@0
|
943 (->
|
rlm@0
|
944 (.getMesh)
|
rlm@0
|
945 (.scaleTextureCoordinates (Vector2f. 1 0.5))
|
rlm@0
|
946 )
|
rlm@0
|
947 (-> (.getControl RigidBodyControl)
|
rlm@0
|
948 (.setLinearVelocity (Vector3f. 0 100 0))
|
rlm@0
|
949 )
|
rlm@0
|
950
|
rlm@0
|
951 ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
|
rlm@0
|
952 ))
|
rlm@0
|
953
|
rlm@0
|
954
|
rlm@0
|
955 (defn shrapnel []
|
rlm@0
|
956 (let [node (Node. "explosion-day")]
|
rlm@0
|
957 (dorun
|
rlm@0
|
958 (map
|
rlm@0
|
959 (comp #(.attachChild node %) scad)
|
rlm@0
|
960 (for [y (range 15)
|
rlm@0
|
961 x (range 4)
|
rlm@0
|
962 z (range 1)]
|
rlm@0
|
963 (Vector3f.
|
rlm@0
|
964 (+ (* 2 x brick-height)
|
rlm@0
|
965 (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
|
rlm@0
|
966 (+ (* brick-height (inc (* 2 y))))
|
rlm@0
|
967 (* 2 z brick-height) ))))
|
rlm@0
|
968 node))
|
rlm@0
|
969
|
rlm@0
|
970
|
rlm@0
|
971 (def domino-height 0.48)
|
rlm@0
|
972 (def domino-thickness 0.12)
|
rlm@0
|
973 (def domino-width 0.24)
|
rlm@0
|
974
|
rlm@0
|
975 (def domino-thickness 0.05)
|
rlm@0
|
976 (def domino-width 0.5)
|
rlm@0
|
977 (def domino-height 1)
|
rlm@0
|
978
|
rlm@0
|
979 (defn domino
|
rlm@0
|
980 ([position]
|
rlm@0
|
981 (domino position (Quaternion/IDENTITY)))
|
rlm@0
|
982 ([position rotation]
|
rlm@0
|
983 (doto (box domino-width domino-height domino-thickness
|
rlm@0
|
984 :position position :name "domino"
|
rlm@0
|
985 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
986 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
987 :mass 1
|
rlm@0
|
988 :rotation rotation)
|
rlm@0
|
989 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
990 )))
|
rlm@0
|
991
|
rlm@0
|
992
|
rlm@0
|
993 (defn domino-row []
|
rlm@0
|
994 (let [node (Node. "domino-row")]
|
rlm@0
|
995 (dorun
|
rlm@0
|
996 (map
|
rlm@0
|
997 (comp #(.attachChild node %) domino)
|
rlm@0
|
998 (for [
|
rlm@0
|
999 z (range 10)
|
rlm@0
|
1000 x (range 5)
|
rlm@0
|
1001 ]
|
rlm@0
|
1002 (Vector3f.
|
rlm@0
|
1003 (+ (* z domino-width) (* x 5 domino-width))
|
rlm@0
|
1004 (/ domino-height 1)
|
rlm@0
|
1005 (* -5.5 domino-thickness z) ))))
|
rlm@0
|
1006
|
rlm@0
|
1007 node))
|
rlm@0
|
1008
|
rlm@0
|
1009 (defn domino-cycle []
|
rlm@0
|
1010 (let [node (Node. "domino-cycle")]
|
rlm@0
|
1011 (dorun
|
rlm@0
|
1012 (map
|
rlm@0
|
1013 (comp #(.attachChild node %) (partial apply domino) )
|
rlm@0
|
1014 (for [n (range 720)]
|
rlm@0
|
1015 (let [space (* domino-height 5.5)
|
rlm@0
|
1016 r (fn[n] (* (+ n 3) domino-width 0.5))
|
rlm@0
|
1017 t (fn[n] (reduce
|
rlm@0
|
1018 +
|
rlm@0
|
1019 (map
|
rlm@0
|
1020 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
|
rlm@0
|
1021 (range n))))
|
rlm@0
|
1022 t (t n)
|
rlm@0
|
1023 r (r n)
|
rlm@0
|
1024 ct (Math/cos t)
|
rlm@0
|
1025 st (Math/sin t)
|
rlm@0
|
1026 ]
|
rlm@0
|
1027 (list
|
rlm@0
|
1028 (Vector3f.
|
rlm@0
|
1029 (* -1 r st)
|
rlm@0
|
1030 (/ domino-height 1)
|
rlm@0
|
1031 (* r ct))
|
rlm@0
|
1032 (.fromAngleAxis (Quaternion.)
|
rlm@0
|
1033 (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
|
rlm@0
|
1034 )))
|
rlm@0
|
1035 ))
|
rlm@0
|
1036 node))
|
rlm@0
|
1037
|
rlm@0
|
1038
|
rlm@0
|
1039 (defn domino-game-run []
|
rlm@0
|
1040 (doto
|
rlm@0
|
1041 (world
|
rlm@0
|
1042 (doto (Node.) (.attachChild (floor*))
|
rlm@0
|
1043 )
|
rlm@0
|
1044 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
|
rlm@0
|
1045 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
|
rlm@0
|
1046 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
|
rlm@0
|
1047 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
|
rlm@0
|
1048 "key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
|
rlm@0
|
1049 "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
|
rlm@0
|
1050 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
|
rlm@0
|
1051
|
rlm@0
|
1052 "key-space"
|
rlm@0
|
1053 (fn[game value]
|
rlm@0
|
1054
|
rlm@0
|
1055 (if (not value)
|
rlm@0
|
1056 (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
|
rlm@0
|
1057 (.fromAngleAxis (Quaternion.)
|
rlm@0
|
1058 (/ Math/PI 2) (Vector3f. 0 1 0)))]
|
rlm@0
|
1059 (add-element game d))))
|
rlm@0
|
1060 "key-f"
|
rlm@0
|
1061 (fn[game value](if (not value) (add-element game (domino-cycle))))
|
rlm@0
|
1062 "key-return" (fire-cannon-ball)}
|
rlm@0
|
1063 position-camera
|
rlm@0
|
1064 (fn [& _]))
|
rlm@0
|
1065 (.start)))
|
rlm@0
|
1066 #+end_src
|
rlm@0
|
1067
|
rlm@0
|
1068 #+begin_src clojure :results silent
|
rlm@0
|
1069 (cortex.other-games/domino-game-run)
|
rlm@0
|
1070 #+end_src
|
rlm@0
|
1071
|
rlm@0
|
1072 #+caption: floating dominos
|
rlm@0
|
1073 [[./images/dominos.jpg]]
|
rlm@0
|
1074
|
rlm@0
|
1075 *** Hello Loop
|
rlm@0
|
1076 #+srcname: hello-loop
|
rlm@0
|
1077 #+begin_src clojure :results silent
|
rlm@0
|
1078 (ns hello.loop)
|
rlm@0
|
1079 (use 'cortex.world)
|
rlm@0
|
1080 (use 'cortex.import)
|
rlm@0
|
1081 (cortex.import/mega-import-jme3)
|
rlm@0
|
1082 (rlm.rlm-commands/help)
|
rlm@0
|
1083
|
rlm@0
|
1084 (defn blue-cube []
|
rlm@0
|
1085 (box 1 1 1
|
rlm@0
|
1086 :color ColorRGBA/Blue
|
rlm@0
|
1087 :texture false
|
rlm@0
|
1088 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
1089 :name "blue-cube"
|
rlm@0
|
1090 :physical? false))
|
rlm@0
|
1091
|
rlm@0
|
1092 (defn blue-cube-game []
|
rlm@0
|
1093 (let [cube (blue-cube)
|
rlm@0
|
1094 root (doto (Node.) (.attachChild cube))]
|
rlm@0
|
1095 (world root
|
rlm@0
|
1096 {}
|
rlm@0
|
1097 no-op
|
rlm@0
|
1098 (fn [game tpf]
|
rlm@0
|
1099 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
|
rlm@0
|
1100 #+end_src
|
rlm@0
|
1101
|
rlm@0
|
1102 *** Hello Collision
|
rlm@0
|
1103
|
rlm@0
|
1104 #+srcname: hello-collision
|
rlm@0
|
1105 #+begin_src clojure :results silent
|
rlm@0
|
1106 (ns hello.collision)
|
rlm@0
|
1107 (use 'cortex.world)
|
rlm@0
|
1108 (use 'cortex.import)
|
rlm@0
|
1109 (use 'clojure.contrib.def)
|
rlm@0
|
1110
|
rlm@0
|
1111
|
rlm@0
|
1112 (cortex.import/mega-import-jme3)
|
rlm@0
|
1113 (rlm.rlm-commands/help)
|
rlm@0
|
1114 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
|
rlm@0
|
1115
|
rlm@0
|
1116
|
rlm@0
|
1117 (defn environment []
|
rlm@0
|
1118 (let
|
rlm@0
|
1119 [scene-model
|
rlm@0
|
1120 (doto
|
rlm@0
|
1121 (.loadModel
|
rlm@0
|
1122 (doto (asset-manager)
|
rlm@0
|
1123 (.registerLocator
|
rlm@0
|
1124 "/home/r/cortex/assets/zips/town.zip" ZipLocator))
|
rlm@0
|
1125 "main.scene")
|
rlm@0
|
1126 (.setLocalScale (float 2.0)))
|
rlm@0
|
1127 collision-shape
|
rlm@0
|
1128 (CollisionShapeFactory/createMeshShape #^Node scene-model)
|
rlm@0
|
1129 landscape (RigidBodyControl. collision-shape 0)]
|
rlm@0
|
1130 (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
1131 (.addControl scene-model landscape)
|
rlm@0
|
1132 scene-model))
|
rlm@0
|
1133
|
rlm@0
|
1134 (defn player-fn []
|
rlm@0
|
1135 (doto
|
rlm@0
|
1136 (CharacterControl.
|
rlm@0
|
1137 (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
|
rlm@0
|
1138 (float 0.05))
|
rlm@0
|
1139 (.setJumpSpeed 20)
|
rlm@0
|
1140 (.setFallSpeed 30)
|
rlm@0
|
1141 (.setGravity 30) ;30
|
rlm@0
|
1142 (.setPhysicsLocation (Vector3f. 0 10 0))))
|
rlm@0
|
1143
|
rlm@0
|
1144 (defn lights []
|
rlm@0
|
1145 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
|
rlm@0
|
1146 (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
|
rlm@0
|
1147 (doto (DirectionalLight.)
|
rlm@0
|
1148 (.setColor (.mult ColorRGBA/White (float 0.9) ))
|
rlm@0
|
1149 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
|
rlm@0
|
1150
|
rlm@0
|
1151 (defn night-lights []
|
rlm@0
|
1152 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
|
rlm@0
|
1153 (doto (DirectionalLight.)
|
rlm@0
|
1154 (.setColor (.mult ColorRGBA/White (float 0.2) ))
|
rlm@0
|
1155 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
|
rlm@0
|
1156
|
rlm@0
|
1157 (def player (atom (player-fn)))
|
rlm@0
|
1158
|
rlm@0
|
1159 (defn setup-fn [game]
|
rlm@0
|
1160 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
|
rlm@0
|
1161 ;; set the color of the sky
|
rlm@0
|
1162 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
|
rlm@0
|
1163 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
|
rlm@0
|
1164 (doto (.getFlyByCamera game)
|
rlm@0
|
1165 (.setMoveSpeed (float 100))
|
rlm@0
|
1166 (.setRotationSpeed 3))
|
rlm@0
|
1167 (.add
|
rlm@0
|
1168 (.getPhysicsSpace
|
rlm@0
|
1169 (.getState (.getStateManager game) BulletAppState))
|
rlm@0
|
1170 @player)
|
rlm@0
|
1171
|
rlm@0
|
1172 (doto (Node.) (.attachChild (.getRootNode game))
|
rlm@0
|
1173 (.attachChild (brick-wall*))
|
rlm@0
|
1174 )
|
rlm@0
|
1175
|
rlm@0
|
1176 )
|
rlm@0
|
1177
|
rlm@0
|
1178
|
rlm@0
|
1179 (def walking-up? (atom false))
|
rlm@0
|
1180 (def walking-down? (atom false))
|
rlm@0
|
1181 (def walking-left? (atom false))
|
rlm@0
|
1182 (def walking-right? (atom false))
|
rlm@0
|
1183
|
rlm@0
|
1184 (defn set-walk [walk-atom game value]
|
rlm@0
|
1185 ;;(println-repl "setting stuff to " value)
|
rlm@0
|
1186 (reset! walk-atom value))
|
rlm@0
|
1187
|
rlm@0
|
1188 (defn responses []
|
rlm@0
|
1189 {"key-w" (partial set-walk walking-up?)
|
rlm@0
|
1190 "key-d" (partial set-walk walking-right?)
|
rlm@0
|
1191 "key-s" (partial set-walk walking-down?)
|
rlm@0
|
1192 "key-a" (partial set-walk walking-left?)
|
rlm@0
|
1193 "key-return" (fire-cannon-ball)
|
rlm@0
|
1194 "key-space" (fn [game value] (.jump @player))
|
rlm@0
|
1195 })
|
rlm@0
|
1196
|
rlm@0
|
1197 (defn update-fn
|
rlm@0
|
1198 [game tpf]
|
rlm@0
|
1199 (let [camera (.getCamera game)
|
rlm@0
|
1200 cam-dir (.multLocal
|
rlm@0
|
1201 (.clone
|
rlm@0
|
1202 (.getDirection camera)) (float 0.6))
|
rlm@0
|
1203 cam-left (.multLocal
|
rlm@0
|
1204 (.clone
|
rlm@0
|
1205 (.getLeft camera)) (float 0.4))
|
rlm@0
|
1206 walk-direction (Vector3f. 0 0 0)]
|
rlm@0
|
1207
|
rlm@0
|
1208 (cond
|
rlm@0
|
1209 @walking-up? (.addLocal walk-direction cam-dir)
|
rlm@0
|
1210 @walking-right? (.addLocal walk-direction (.negate cam-left))
|
rlm@0
|
1211 @walking-down? (.addLocal walk-direction (.negate cam-dir))
|
rlm@0
|
1212 @walking-left? (.addLocal walk-direction cam-left))
|
rlm@0
|
1213 (.setWalkDirection @player walk-direction)
|
rlm@0
|
1214 (.setLocation camera (.getPhysicsLocation @player))))
|
rlm@0
|
1215
|
rlm@0
|
1216 (defn run-game []
|
rlm@0
|
1217 (.start
|
rlm@0
|
1218 (world (environment)
|
rlm@0
|
1219 (responses)
|
rlm@0
|
1220 setup-fn
|
rlm@0
|
1221 update-fn)))
|
rlm@0
|
1222 #+end_src
|
rlm@0
|
1223
|
rlm@0
|
1224 *** Hello Terrain
|
rlm@0
|
1225 #+srcname: hello-terrain
|
rlm@0
|
1226 #+begin_src clojure :results silent
|
rlm@0
|
1227 (ns hello.terrain)
|
rlm@0
|
1228 (use 'cortex.world)
|
rlm@0
|
1229 (use 'cortex.import)
|
rlm@0
|
1230 (use 'clojure.contrib.def)
|
rlm@0
|
1231 (import jme3tools.converters.ImageToAwt)
|
rlm@0
|
1232
|
rlm@0
|
1233
|
rlm@0
|
1234 (cortex.import/mega-import-jme3)
|
rlm@0
|
1235 (rlm.rlm-commands/help)
|
rlm@0
|
1236 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
|
rlm@0
|
1237
|
rlm@0
|
1238
|
rlm@0
|
1239 (defn setup-fn [type game]
|
rlm@0
|
1240 (.setMoveSpeed (.getFlyByCamera game) 50)
|
rlm@0
|
1241 (.setFrustumFar (.getCamera game) 10000)
|
rlm@0
|
1242 (let [env (environment type)
|
rlm@0
|
1243 cameras [(.getCamera game)]
|
rlm@0
|
1244 control (TerrainLodControl. env cameras)]
|
rlm@0
|
1245 ;;(.addControl env control)
|
rlm@0
|
1246 (.attachChild (.getRootNode game) env)))
|
rlm@0
|
1247
|
rlm@0
|
1248 (defn environment [type]
|
rlm@0
|
1249 (let
|
rlm@0
|
1250 [mat_terrain
|
rlm@0
|
1251 (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
|
rlm@0
|
1252 grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
|
rlm@0
|
1253 dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
|
rlm@0
|
1254 rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
|
rlm@0
|
1255 heightmap-image (.loadTexture (asset-manager)
|
rlm@0
|
1256 ({:mountain "Textures/Terrain/splat/mountains512.png"
|
rlm@0
|
1257 :fortress "Textures/Terrain/splat/fortress512.png"
|
rlm@0
|
1258 }type))
|
rlm@0
|
1259 heightmap (ImageBasedHeightMap.
|
rlm@0
|
1260 (ImageToAwt/convert (.getImage heightmap-image) false true 0))
|
rlm@0
|
1261 terrain (do (.load heightmap)
|
rlm@0
|
1262 (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
|
rlm@0
|
1263 ]
|
rlm@0
|
1264
|
rlm@0
|
1265 (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
|
rlm@0
|
1266 [grass dirt rock]))
|
rlm@0
|
1267
|
rlm@0
|
1268 (doto mat_terrain
|
rlm@0
|
1269 (.setTexture "Tex1" grass)
|
rlm@0
|
1270 (.setFloat "Tex1Scale" (float 64))
|
rlm@0
|
1271
|
rlm@0
|
1272 (.setTexture "Tex2" dirt)
|
rlm@0
|
1273 (.setFloat "Tex2Scale" (float 32))
|
rlm@0
|
1274
|
rlm@0
|
1275 (.setTexture "Tex3" rock)
|
rlm@0
|
1276 (.setFloat "Tex3Scale" (float 128))
|
rlm@0
|
1277
|
rlm@0
|
1278 (.setTexture "Alpha"
|
rlm@0
|
1279 (.loadTexture
|
rlm@0
|
1280 (asset-manager)
|
rlm@0
|
1281 ({:mountain "Textures/Terrain/splat/alphamap.png"
|
rlm@0
|
1282 :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
|
rlm@0
|
1283
|
rlm@0
|
1284 (doto terrain
|
rlm@0
|
1285 (.setMaterial mat_terrain)
|
rlm@0
|
1286 (.setLocalTranslation 0 -100 0)
|
rlm@0
|
1287 (.setLocalScale 2 1 2))))
|
rlm@0
|
1288
|
rlm@0
|
1289
|
rlm@0
|
1290
|
rlm@0
|
1291 (defn run-terrain-game [type]
|
rlm@0
|
1292 (.start
|
rlm@0
|
1293 (world
|
rlm@0
|
1294 (Node.)
|
rlm@0
|
1295 {}
|
rlm@0
|
1296 (partial setup-fn type)
|
rlm@0
|
1297 no-op)))
|
rlm@0
|
1298 #+end_src
|
rlm@0
|
1299
|
rlm@0
|
1300
|
rlm@0
|
1301
|
rlm@0
|
1302 #+srcname: hello-animation
|
rlm@0
|
1303 #+begin_src clojure :results silent
|
rlm@0
|
1304 (ns hello.animation)
|
rlm@0
|
1305 (use 'cortex.world)
|
rlm@0
|
1306 (use 'cortex.import)
|
rlm@0
|
1307 (use 'clojure.contrib.def)
|
rlm@0
|
1308 (cortex.import/mega-import-jme3)
|
rlm@0
|
1309 (rlm.rlm-commands/help)
|
rlm@0
|
1310 (use '[hello [collision :only [lights]]])
|
rlm@0
|
1311
|
rlm@0
|
1312 (defn stand
|
rlm@0
|
1313 [channel]
|
rlm@0
|
1314 (doto channel
|
rlm@0
|
1315 (.setAnim "stand" (float 0.5))
|
rlm@0
|
1316 (.setLoopMode LoopMode/DontLoop)
|
rlm@0
|
1317 (.setSpeed (float 1))))
|
rlm@0
|
1318
|
rlm@0
|
1319 (defn anim-listener []
|
rlm@0
|
1320 (proxy [AnimEventListener] []
|
rlm@0
|
1321 (onAnimChange
|
rlm@0
|
1322 [control channel animation-name]
|
rlm@0
|
1323 (println-repl "RLM --- onAnimChange"))
|
rlm@0
|
1324 (onAnimCycleDone
|
rlm@0
|
1325 [control channel animation-name]
|
rlm@0
|
1326 (if (= animation-name "Walk")
|
rlm@0
|
1327 (stand channel)
|
rlm@0
|
1328 ))))
|
rlm@0
|
1329
|
rlm@0
|
1330 (defn setup-fn [channel game]
|
rlm@0
|
1331 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
|
rlm@0
|
1332 ;; set the color of the sky
|
rlm@0
|
1333 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
|
rlm@0
|
1334 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
|
rlm@0
|
1335 (.setAnim channel "stand")
|
rlm@0
|
1336 (doto (.getFlyByCamera game)
|
rlm@0
|
1337 (.setMoveSpeed (float 10))
|
rlm@0
|
1338 (.setRotationSpeed 1)))
|
rlm@0
|
1339
|
rlm@0
|
1340 (defn walk [channel]
|
rlm@0
|
1341 (println-repl "zzz")
|
rlm@0
|
1342 (doto channel
|
rlm@0
|
1343 (.setAnim "Walk" (float 0.5))
|
rlm@0
|
1344 (.setLoopMode LoopMode/Loop)))
|
rlm@0
|
1345
|
rlm@0
|
1346
|
rlm@0
|
1347 (defn key-map [channel]
|
rlm@0
|
1348 {"key-space" (fn [game value]
|
rlm@0
|
1349 (if (not value)
|
rlm@0
|
1350 (walk channel)))})
|
rlm@0
|
1351
|
rlm@0
|
1352 (defn player []
|
rlm@0
|
1353 (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
|
rlm@0
|
1354 control (.getControl model AnimControl)]
|
rlm@0
|
1355 (.setLocalScale model (float 0.5))
|
rlm@0
|
1356 (.clearListeners control)
|
rlm@0
|
1357 (.addListener control (anim-control))
|
rlm@0
|
1358 model))
|
rlm@0
|
1359
|
rlm@0
|
1360
|
rlm@0
|
1361
|
rlm@0
|
1362 (defn run-anim-game []
|
rlm@0
|
1363 (let [ninja (player)
|
rlm@0
|
1364 control (.getControl ninja AnimControl)
|
rlm@0
|
1365 channel (.createChannel control)]
|
rlm@0
|
1366 (.start
|
rlm@0
|
1367 (world
|
rlm@0
|
1368 ninja
|
rlm@0
|
1369 (key-map channel)
|
rlm@0
|
1370 (partial setup-fn channel)
|
rlm@0
|
1371 no-op))))
|
rlm@0
|
1372 #+end_src
|
rlm@0
|
1373
|
rlm@0
|
1374 *** Hello Materials
|
rlm@0
|
1375 #+srcname: material
|
rlm@0
|
1376 #+begin_src clojure :results silent
|
rlm@0
|
1377 (ns hello.material)
|
rlm@0
|
1378 (use 'cortex.world)
|
rlm@0
|
1379 (use 'cortex.import)
|
rlm@0
|
1380 (use 'clojure.contrib.def)
|
rlm@0
|
1381 (cortex.import/mega-import-jme3)
|
rlm@0
|
1382 (rlm.rlm-commands/help)
|
rlm@0
|
1383
|
rlm@0
|
1384 (defn simple-cube []
|
rlm@0
|
1385 (box 1 1 1
|
rlm@0
|
1386 :position (Vector3f. -3 1.1 0)
|
rlm@0
|
1387 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
1388 :texture "Interface/Logo/Monkey.jpg"
|
rlm@0
|
1389 :physical? false))
|
rlm@0
|
1390
|
rlm@0
|
1391 (defn leaky-box []
|
rlm@0
|
1392 (box 1 1 1
|
rlm@0
|
1393 :position (Vector3f. 3 -1 0)
|
rlm@0
|
1394 :material "Common/MatDefs/Misc/ColoredTextured.j3md"
|
rlm@0
|
1395 :texture "Textures/ColoredTex/Monkey.png"
|
rlm@0
|
1396 :color (ColorRGBA. 1 0 1 1)
|
rlm@0
|
1397 :physical? false))
|
rlm@0
|
1398
|
rlm@0
|
1399 (defn transparent-box []
|
rlm@0
|
1400 (doto
|
rlm@0
|
1401 (box 1 1 0.1
|
rlm@0
|
1402 :position Vector3f/ZERO
|
rlm@0
|
1403 :name "window frame"
|
rlm@0
|
1404 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
1405 :texture "Textures/ColoredTex/Monkey.png"
|
rlm@0
|
1406 :physical? false)
|
rlm@0
|
1407 (-> (.getMaterial)
|
rlm@0
|
1408 (.getAdditionalRenderState)
|
rlm@0
|
1409 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
1410 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
1411
|
rlm@0
|
1412 (defn bumpy-sphere []
|
rlm@0
|
1413 (doto
|
rlm@0
|
1414 (sphere 2
|
rlm@0
|
1415 :position (Vector3f. 0 2 -2)
|
rlm@0
|
1416 :name "Shiny rock"
|
rlm@0
|
1417 :material "Common/MatDefs/Light/Lighting.j3md"
|
rlm@0
|
1418 :texture false
|
rlm@0
|
1419 :physical? false)
|
rlm@0
|
1420 (-> (.getMesh)
|
rlm@0
|
1421 (doto
|
rlm@0
|
1422 (.setTextureMode Sphere$TextureMode/Projected)
|
rlm@0
|
1423 (TangentBinormalGenerator/generate)))
|
rlm@0
|
1424 (-> (.getMaterial)
|
rlm@0
|
1425 (doto
|
rlm@0
|
1426 (.setTexture "DiffuseMap" (.loadTexture (asset-manager)
|
rlm@0
|
1427 "Textures/Terrain/Pond/Pond.png"))
|
rlm@0
|
1428 (.setTexture "NormalMap" (.loadTexture (asset-manager)
|
rlm@0
|
1429 "Textures/Terrain/Pond/Pond_normal.png"))
|
rlm@0
|
1430 (.setFloat "Shininess" (float 5))))
|
rlm@0
|
1431 (.rotate (float 1.6) 0 0)))
|
rlm@0
|
1432
|
rlm@0
|
1433
|
rlm@0
|
1434 (defn start-game []
|
rlm@0
|
1435 (.start
|
rlm@0
|
1436 (world
|
rlm@0
|
1437 (let [root (Node.)]
|
rlm@0
|
1438 (dorun (map #(.attachChild root %)
|
rlm@0
|
1439 [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
|
rlm@0
|
1440 root)
|
rlm@0
|
1441 {}
|
rlm@0
|
1442 (fn [world]
|
rlm@0
|
1443 (let [sun (doto (DirectionalLight.)
|
rlm@0
|
1444 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
|
rlm@0
|
1445 (.setColor ColorRGBA/White))]
|
rlm@0
|
1446 (.addLight (.getRootNode world) sun)))
|
rlm@0
|
1447 no-op
|
rlm@0
|
1448 )))
|
rlm@0
|
1449 #+end_src
|
rlm@0
|
1450
|
rlm@0
|
1451
|
rlm@0
|
1452
|
rlm@0
|
1453 * The Body
|
rlm@0
|
1454 ** Eyes
|
rlm@0
|
1455
|
rlm@0
|
1456 Ultimately I want to make creatures with eyes. Each eye can be
|
rlm@0
|
1457 independely moved and should see its own version of the world
|
rlm@0
|
1458 depending on where it is.
|
rlm@0
|
1459 #+srcname: eyes
|
rlm@0
|
1460 #+begin_src clojure
|
rlm@0
|
1461 (ns body.eye)
|
rlm@0
|
1462 (use 'cortex.world)
|
rlm@0
|
1463 (use 'cortex.import)
|
rlm@0
|
1464 (use 'clojure.contrib.def)
|
rlm@0
|
1465 (cortex.import/mega-import-jme3)
|
rlm@0
|
1466 (rlm.rlm-commands/help)
|
rlm@0
|
1467 (import java.nio.ByteBuffer)
|
rlm@0
|
1468 (import java.awt.image.BufferedImage)
|
rlm@0
|
1469 (import java.awt.Color)
|
rlm@0
|
1470 (import java.awt.Dimension)
|
rlm@0
|
1471 (import java.awt.Graphics)
|
rlm@0
|
1472 (import java.awt.Graphics2D)
|
rlm@0
|
1473 (import java.awt.event.WindowAdapter)
|
rlm@0
|
1474 (import java.awt.event.WindowEvent)
|
rlm@0
|
1475 (import java.awt.image.BufferedImage)
|
rlm@0
|
1476 (import java.nio.ByteBuffer)
|
rlm@0
|
1477 (import javax.swing.JFrame)
|
rlm@0
|
1478 (import javax.swing.JPanel)
|
rlm@0
|
1479 (import javax.swing.SwingUtilities)
|
rlm@0
|
1480 (import javax.swing.ImageIcon)
|
rlm@0
|
1481 (import javax.swing.JOptionPane)
|
rlm@0
|
1482 (import java.awt.image.ImageObserver)
|
rlm@0
|
1483
|
rlm@0
|
1484
|
rlm@0
|
1485
|
rlm@0
|
1486 (defn scene-processor
|
rlm@0
|
1487 "deals with converting FrameBuffers to BufferedImages so
|
rlm@0
|
1488 that the continuation function can be defined only in terms
|
rlm@0
|
1489 of what it does with BufferedImages"
|
rlm@0
|
1490 [continuation]
|
rlm@0
|
1491 (let [byte-buffer (atom nil)
|
rlm@0
|
1492 renderer (atom nil)
|
rlm@0
|
1493 image (atom nil)]
|
rlm@0
|
1494 (proxy [SceneProcessor] []
|
rlm@0
|
1495 (initialize
|
rlm@0
|
1496 [renderManager viewPort]
|
rlm@0
|
1497 (let [cam (.getCamera viewPort)
|
rlm@0
|
1498 width (.getWidth cam)
|
rlm@0
|
1499 height (.getHeight cam)]
|
rlm@0
|
1500 (reset! renderer (.getRenderer renderManager))
|
rlm@0
|
1501 (reset! byte-buffer
|
rlm@0
|
1502 (BufferUtils/createByteBuffer
|
rlm@0
|
1503 (* width height 4)))
|
rlm@0
|
1504 (reset! image (BufferedImage. width height
|
rlm@0
|
1505 BufferedImage/TYPE_4BYTE_ABGR))))
|
rlm@0
|
1506 (isInitialized [] (not (nil? @byte-buffer)))
|
rlm@0
|
1507 (reshape [_ _ _])
|
rlm@0
|
1508 (preFrame [_])
|
rlm@0
|
1509 (postQueue [_])
|
rlm@0
|
1510 (postFrame
|
rlm@0
|
1511 [#^FrameBuffer fb]
|
rlm@0
|
1512 (.clear @byte-buffer)
|
rlm@0
|
1513 (.readFrameBuffer @renderer fb @byte-buffer)
|
rlm@0
|
1514 (Screenshots/convertScreenShot @byte-buffer @image)
|
rlm@0
|
1515 (continuation @image))
|
rlm@0
|
1516 (cleanup []))))
|
rlm@0
|
1517
|
rlm@0
|
1518 (defn add-eye
|
rlm@0
|
1519 "Add an eye to the world, and call continuation on
|
rlm@0
|
1520 every frame produced"
|
rlm@0
|
1521 [world camera continuation]
|
rlm@0
|
1522 (let [width (.getWidth camera)
|
rlm@0
|
1523 height (.getHeight camera)
|
rlm@0
|
1524 render-manager (.getRenderManager world)
|
rlm@0
|
1525 viewport (.createMainView render-manager "eye-view" camera)]
|
rlm@0
|
1526 (doto viewport
|
rlm@0
|
1527 (.setBackgroundColor ColorRGBA/Black)
|
rlm@0
|
1528 (.setClearFlags true true true)
|
rlm@0
|
1529 (.addProcessor (scene-processor continuation))
|
rlm@0
|
1530 (.attachScene (.getRootNode world)))))
|
rlm@0
|
1531
|
rlm@0
|
1532 (defn make-display-frame [display width height]
|
rlm@0
|
1533 (SwingUtilities/invokeLater
|
rlm@0
|
1534 (fn []
|
rlm@0
|
1535 (.setPreferredSize display (Dimension. width height))
|
rlm@0
|
1536 (doto (JFrame. "Eye Camera!")
|
rlm@0
|
1537 (-> (.getContentPane) (.add display))
|
rlm@0
|
1538 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
|
rlm@0
|
1539 (.pack)
|
rlm@0
|
1540 (.setLocationRelativeTo nil)
|
rlm@0
|
1541 (.setResizable false)
|
rlm@0
|
1542 (.setVisible true)))))
|
rlm@0
|
1543
|
rlm@0
|
1544 (defn image-monitor [#^BufferedImage image]
|
rlm@0
|
1545 (proxy [JPanel] []
|
rlm@0
|
1546 (paintComponent
|
rlm@0
|
1547 [g]
|
rlm@0
|
1548 (proxy-super paintComponent g)
|
rlm@0
|
1549 (locking image
|
rlm@0
|
1550 (.drawImage g image 0 0
|
rlm@0
|
1551 (proxy [ImageObserver]
|
rlm@0
|
1552 []
|
rlm@0
|
1553 (imageUpdate
|
rlm@0
|
1554 []
|
rlm@0
|
1555 (proxy-super imageUpdate))))))))
|
rlm@0
|
1556
|
rlm@0
|
1557 (defn movie-image []
|
rlm@0
|
1558 (let [setup
|
rlm@0
|
1559 (runonce
|
rlm@0
|
1560 (fn [#^BufferedImage image]
|
rlm@0
|
1561 (let [width (.getWidth image)
|
rlm@0
|
1562 height (.getHeight image)
|
rlm@0
|
1563 display (image-monitor image)
|
rlm@0
|
1564 frame (make-display-frame display width height)]
|
rlm@0
|
1565 display)))]
|
rlm@0
|
1566 (fn [#^BufferedImage image]
|
rlm@0
|
1567 (.repaint (setup image)))))
|
rlm@0
|
1568
|
rlm@0
|
1569
|
rlm@0
|
1570 (defn observer
|
rlm@0
|
1571 "place thy eye!"
|
rlm@0
|
1572 [world camera]
|
rlm@0
|
1573 (let [eye camera
|
rlm@0
|
1574 width (.getWidth eye)
|
rlm@0
|
1575 height (.getHeight eye)]
|
rlm@0
|
1576 (no-exceptions
|
rlm@0
|
1577 (add-eye
|
rlm@0
|
1578 world
|
rlm@0
|
1579 eye
|
rlm@0
|
1580 (movie-image)))))
|
rlm@0
|
1581 #+end_src
|
rlm@0
|
1582
|
rlm@0
|
1583 #+srcname: test-vision
|
rlm@0
|
1584 #+begin_src clojure
|
rlm@0
|
1585
|
rlm@0
|
1586 (ns test.vision)
|
rlm@0
|
1587 (use 'cortex.world)
|
rlm@0
|
1588 (use 'cortex.import)
|
rlm@0
|
1589 (use 'clojure.contrib.def)
|
rlm@0
|
1590 (use 'body.eye)
|
rlm@0
|
1591 (cortex.import/mega-import-jme3)
|
rlm@0
|
1592 (rlm.rlm-commands/help)
|
rlm@0
|
1593 (import java.nio.ByteBuffer)
|
rlm@0
|
1594 (import java.awt.image.BufferedImage)
|
rlm@0
|
1595 (import java.awt.Color)
|
rlm@0
|
1596 (import java.awt.Dimension)
|
rlm@0
|
1597 (import java.awt.Graphics)
|
rlm@0
|
1598 (import java.awt.Graphics2D)
|
rlm@0
|
1599 (import java.awt.event.WindowAdapter)
|
rlm@0
|
1600 (import java.awt.event.WindowEvent)
|
rlm@0
|
1601 (import java.awt.image.BufferedImage)
|
rlm@0
|
1602 (import java.nio.ByteBuffer)
|
rlm@0
|
1603 (import javax.swing.JFrame)
|
rlm@0
|
1604 (import javax.swing.JPanel)
|
rlm@0
|
1605 (import javax.swing.SwingUtilities)
|
rlm@0
|
1606 (import javax.swing.ImageIcon)
|
rlm@0
|
1607 (import javax.swing.JOptionPane)
|
rlm@0
|
1608 (import java.awt.image.ImageObserver)
|
rlm@0
|
1609
|
rlm@0
|
1610
|
rlm@0
|
1611 (def width 200)
|
rlm@0
|
1612 (def height 200)
|
rlm@0
|
1613
|
rlm@0
|
1614 (defn camera []
|
rlm@0
|
1615 (doto (Camera. width height)
|
rlm@0
|
1616 (.setFrustumPerspective 45 1 1 1000)
|
rlm@0
|
1617 (.setLocation (Vector3f. -3 0 -5))
|
rlm@0
|
1618 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
|
rlm@0
|
1619
|
rlm@0
|
1620 (defn camera2 []
|
rlm@0
|
1621 (doto (Camera. width height)
|
rlm@0
|
1622 (.setFrustumPerspective 45 1 1 1000)
|
rlm@0
|
1623 (.setLocation (Vector3f. 3 0 -5))
|
rlm@0
|
1624 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
|
rlm@0
|
1625
|
rlm@0
|
1626 (defn setup-fn [world]
|
rlm@0
|
1627 (let [eye (camera)
|
rlm@0
|
1628 width (.getWidth eye)
|
rlm@0
|
1629 height (.getHeight eye)]
|
rlm@0
|
1630 (no-exceptions
|
rlm@0
|
1631 (add-eye
|
rlm@0
|
1632 world
|
rlm@0
|
1633 eye
|
rlm@0
|
1634 (runonce visual))
|
rlm@0
|
1635 (add-eye
|
rlm@0
|
1636 world
|
rlm@0
|
1637 (camera2)
|
rlm@0
|
1638 (runonce visual)))))
|
rlm@0
|
1639
|
rlm@0
|
1640 (defn spider-eye [position]
|
rlm@0
|
1641 (doto (Camera. 200 200 )
|
rlm@0
|
1642 (.setFrustumPerspective 45 1 1 1000)
|
rlm@0
|
1643 (.setLocation position)
|
rlm@0
|
1644 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)))
|
rlm@0
|
1645
|
rlm@0
|
1646 (defn setup-fn* [world]
|
rlm@0
|
1647 (let [eye (camera)
|
rlm@0
|
1648 width (.getWidth eye)
|
rlm@0
|
1649 height (.getHeight eye)]
|
rlm@0
|
1650 ;;(.setClearFlags (.getViewPort world) true true true)
|
rlm@0
|
1651 (observer world (.getCamera world))
|
rlm@0
|
1652 (observer world (spider-eye (Vector3f. 3 0 -5)))
|
rlm@0
|
1653 ;;(observer world (spider-eye (Vector3f. 0 0 -5)))
|
rlm@0
|
1654 ;; (observer world (spider-eye (Vector3f. -3 0 -5)))
|
rlm@0
|
1655 ;; (observer world (spider-eye (Vector3f. 0 3 -5)))
|
rlm@0
|
1656 ;; (observer world (spider-eye (Vector3f. 0 -3 -5)))
|
rlm@0
|
1657 ;; (observer world (spider-eye (Vector3f. 3 3 -5)))
|
rlm@0
|
1658 ;; (observer world (spider-eye (Vector3f. -3 3 -5)))
|
rlm@0
|
1659 ;; (observer world (spider-eye (Vector3f. 3 -3 -5)))
|
rlm@0
|
1660 ;; (observer world (spider-eye (Vector3f. -3 -3 -5)))
|
rlm@0
|
1661
|
rlm@0
|
1662 )
|
rlm@0
|
1663 world)
|
rlm@0
|
1664
|
rlm@0
|
1665 (defn test-world []
|
rlm@0
|
1666 (let [thing (box 1 1 1 :physical? false)]
|
rlm@0
|
1667 (world
|
rlm@0
|
1668 (doto (Node.)
|
rlm@0
|
1669 (.attachChild thing))
|
rlm@0
|
1670 {}
|
rlm@0
|
1671 setup-fn
|
rlm@0
|
1672 (fn [world tpf]
|
rlm@0
|
1673 (.rotate thing (* tpf 0.2) 0 0)
|
rlm@0
|
1674 ))))
|
rlm@0
|
1675
|
rlm@0
|
1676
|
rlm@0
|
1677 #+end_src
|
rlm@0
|
1678
|
rlm@0
|
1679
|
rlm@0
|
1680 #+results: eyes
|
rlm@0
|
1681 : #'body.eye/test-world
|
rlm@0
|
1682
|
rlm@0
|
1683 Note the use of continuation passing style for connecting the eye to a
|
rlm@0
|
1684 function to process the output. The example code will create two
|
rlm@0
|
1685 videos of the same rotating cube from different angles, sutiable for
|
rlm@0
|
1686 stereoscopic vision.
|
rlm@0
|
1687
|
rlm@0
|
1688
|
rlm@0
|
1689
|
rlm@0
|
1690
|
rlm@0
|
1691
|
rlm@0
|
1692
|
rlm@0
|
1693 * COMMENT code generation
|
rlm@0
|
1694 #+begin_src clojure :tangle ../src/cortex/import.clj
|
rlm@0
|
1695 <<import>>
|
rlm@0
|
1696 #+end_src
|
rlm@0
|
1697
|
rlm@0
|
1698 #+begin_src clojure :tangle ../src/hello/brick_wall.clj
|
rlm@0
|
1699 <<brick-wall-header>>
|
rlm@0
|
1700 <<brick-wall-body>>
|
rlm@0
|
1701 #+end_src
|
rlm@0
|
1702
|
rlm@0
|
1703 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
|
rlm@0
|
1704 <<hello-simple-app>>
|
rlm@0
|
1705 #+end_src
|
rlm@0
|
1706
|
rlm@0
|
1707 #+begin_src clojure :tangle ../src/cortex/world.clj
|
rlm@0
|
1708 <<world-inputs>>
|
rlm@0
|
1709 <<world>>
|
rlm@0
|
1710 <<world-shapes>>
|
rlm@0
|
1711 <<world-view>>
|
rlm@0
|
1712 #+end_src
|
rlm@0
|
1713
|
rlm@0
|
1714 #+begin_src clojure :tangle ../src/cortex/other_games.clj
|
rlm@0
|
1715 <<other-games>>
|
rlm@0
|
1716 #+end_src
|
rlm@0
|
1717
|
rlm@0
|
1718 #+begin_src clojure :tangle ../src/hello/loop.clj
|
rlm@0
|
1719 <<hello-loop>>
|
rlm@0
|
1720 #+end_src
|
rlm@0
|
1721
|
rlm@0
|
1722 #+begin_src clojure :tangle ../src/hello/collision.clj
|
rlm@0
|
1723 <<hello-collision>>
|
rlm@0
|
1724 #+end_src
|
rlm@0
|
1725
|
rlm@0
|
1726 #+begin_src clojure :tangle ../src/hello/terrain.clj
|
rlm@0
|
1727 <<hello-terrain>>
|
rlm@0
|
1728 #+end_src
|
rlm@0
|
1729
|
rlm@0
|
1730 #+begin_src clojure :tangle ../src/hello/animation.clj
|
rlm@0
|
1731 <<hello-animation>>
|
rlm@0
|
1732 #+end_src
|
rlm@0
|
1733
|
rlm@0
|
1734 #+begin_src clojure :tangle ../src/hello/material.clj
|
rlm@0
|
1735 <<material>>
|
rlm@0
|
1736 #+end_src
|
rlm@0
|
1737
|
rlm@0
|
1738 #+begin_src clojure :tangle ../src/body/eye.clj
|
rlm@0
|
1739 <<eyes>>
|
rlm@0
|
1740 #+end_src
|
rlm@0
|
1741
|
rlm@0
|
1742 #+begin_src clojure :tangle ../src/test/vision.clj
|
rlm@0
|
1743 <<test-vision>>
|
rlm@0
|
1744 #+end_src
|
rlm@0
|
1745
|
rlm@0
|
1746
|
rlm@0
|
1747
|