rlm@34: #+title: jMonkeyEngine3 from Clojure rlm@0: #+author: Robert McIntyre rlm@0: #+email: rlm@mit.edu rlm@34: #+description: Using jMonkeyEngine3 from clojure rlm@34: #+keywords: clojure, jMonkeyEngine3, tutorial, examples rlm@4: #+SETUPFILE: ../../aurellem/org/setup.org rlm@4: #+INCLUDE: ../../aurellem/org/level-0.org rlm@21: #+babel: :mkdirp yes :noweb yes :exports both rlm@0: rlm@34: [TABLE-OF-CONTENTS] rlm@0: rlm@0: rlm@31: Here are the jMonkeyEngine "Hello" programs translated to clojure. rlm@34: rlm@34: * Hello Simple App rlm@23: Here is the hello world example for jme3 in clojure. It's a more or rlm@23: less direct translation from the java source [[http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication][here]]. rlm@0: rlm@0: Of note is the fact that since we don't have access to the rlm@306: =AssetManager= via extending =SimpleApplication=, we have to build one rlm@0: ourselves. rlm@0: rlm@66: #+name: hello-simple-app rlm@0: #+begin_src clojure :results silent rlm@34: (ns hello.hello-simple-app rlm@34: (:use cortex.world) rlm@34: (:import com.jme3.math.Vector3f) rlm@34: (:import com.jme3.material.Material) rlm@34: (:import com.jme3.scene.Geometry) rlm@34: (:import com.jme3.math.ColorRGBA) rlm@34: (:import com.jme3.app.SimpleApplication) rlm@34: (:import com.jme3.scene.shape.Box)) rlm@0: rlm@0: (def cube (Box. Vector3f/ZERO 1 1 1)) rlm@0: rlm@0: (def geom (Geometry. "Box" cube)) rlm@0: rlm@0: (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md")) rlm@0: rlm@0: (.setColor mat "Color" ColorRGBA/Blue) rlm@0: rlm@0: (.setMaterial geom mat) rlm@0: rlm@0: (defn simple-app [] rlm@0: (doto rlm@0: (proxy [SimpleApplication] [] rlm@0: (simpleInitApp rlm@0: [] rlm@0: ;; Don't take control of the mouse rlm@0: (org.lwjgl.input.Mouse/setGrabbed false) rlm@0: (.attachChild (.getRootNode this) geom))) rlm@0: ;; don't show a menu to change options. rlm@0: (.setShowSettings false) rlm@0: (.setPauseOnLostFocus false) rlm@0: (.setSettings *app-settings*))) rlm@0: #+end_src rlm@0: rlm@0: Running this program will begin a new jMonkeyEngine game which rlm@0: displays a single blue cube. rlm@0: rlm@0: #+begin_src clojure :exports code :results silent rlm@0: (.start (hello.hello-simple-app/simple-app)) rlm@0: #+end_src rlm@0: rlm@0: #+caption: the simplest JME game. rlm@31: [[../images/simple-app.jpg]] rlm@0: rlm@34: * Simpler HelloSimpleApp rlm@0: rlm@66: #+name: hello-simpler-app rlm@34: #+begin_src clojure rlm@34: (ns hello.hello-simpler-app rlm@34: (:use cortex.world) rlm@34: (:use cortex.util) rlm@34: (:import com.jme3.math.ColorRGBA) rlm@34: (:import com.jme3.scene.Node)) rlm@34: rlm@34: (defn simpler-world rlm@34: "The jMonkeyEngine3 Hello World program. Displays a blue 3D cube in rlm@34: a basic 3D world." rlm@34: [] rlm@34: (world (doto (Node.) rlm@34: (.attachChild rlm@34: (box 1 1 1 rlm@34: :color ColorRGBA/Blue :physical? false))) rlm@34: {} no-op no-op)) rlm@34: #+end_src rlm@34: rlm@34: More information about the jMonkeyEngine3 hello world program can be rlm@34: found [[http://jmonkeyengine.org/wiki/doku.php/starter:hello_world][here]]. rlm@34: rlm@34: * COMMENT Hello Physics rlm@0: From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics rlm@0: rlm@66: #+name: brick-wall-header rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.brick-wall) rlm@0: (require 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (rlm.rlm-commands/help) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (use '[pokemon [lpsolve :only [constant-map]]]) rlm@0: (use 'cortex.world) rlm@25: (use 'cortex.util) rlm@0: #+end_src rlm@0: rlm@66: #+name: brick-wall-body rlm@0: #+begin_src clojure :results silent rlm@0: (in-ns 'hello.brick-wall) rlm@0: rlm@0: (defn floor rlm@0: "make a sturdy, unmovable physical floor" rlm@0: [] rlm@0: (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0))) rlm@0: rlm@0: (def brick-length 0.48) rlm@0: (def brick-width 0.24) rlm@0: (def brick-height 0.12) rlm@34: (def gravity (Vector3f. 0 -9.81 0)) rlm@0: rlm@0: (defn brick* [position] rlm@0: (doto (box brick-length brick-height brick-width rlm@0: :position position :name "brick" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: :mass 36) rlm@0: (-> rlm@0: (.getMesh) rlm@0: (.scaleTextureCoordinates (Vector2f. 1 0.5))) rlm@0: ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive) rlm@0: ) rlm@0: ) rlm@0: rlm@0: (defn inception-brick-wall rlm@0: "construct a physical brick wall" rlm@0: [] rlm@0: (let [node (Node. "brick-wall")] rlm@0: (dorun rlm@0: (map (comp #(.attachChild node %) brick*) rlm@0: (for rlm@0: [x (range 15) rlm@0: y (range 10) rlm@0: z (range 1)] rlm@0: (Vector3f. rlm@0: (* brick-length x 1.03) rlm@0: (* brick-width y y 10) rlm@0: (* brick-height z))))) rlm@0: node)) rlm@0: rlm@0: (defn gravity-toggle rlm@0: [new-value] rlm@0: (fn [game value] rlm@0: (println-repl "set gravity to " new-value) rlm@0: (if value rlm@34: (set-gravity game new-value) rlm@34: (set-gravity game gravity)))) rlm@0: rlm@0: (defn floor* [] rlm@0: (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240 rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/Pond/Pond.png" rlm@0: :position (Vector3f. 0 -0.1 0 ) rlm@0: :mass 0) rlm@0: (-> rlm@0: (.getMesh) rlm@0: (.scaleTextureCoordinates (Vector2f. 3 6)));64 64 rlm@0: (-> rlm@0: (.getMaterial) rlm@0: (.getTextureParam "ColorMap") rlm@0: (.getTextureValue) rlm@0: (.setWrap Texture$WrapMode/Repeat)) rlm@0: (.setShadowMode RenderQueue$ShadowMode/Receive) rlm@0: )) rlm@0: rlm@0: (defn brick-wall* [] rlm@0: (let [node (Node. "brick-wall")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) brick*) rlm@0: (for [y (range 15) rlm@0: x (range 4) rlm@0: z (range 1)] rlm@0: (Vector3f. rlm@0: (+ (* 2 x brick-length) rlm@0: (if (even? (+ y z)) rlm@0: (/ brick-length 4) (/ brick-length -4))) rlm@0: (+ (* brick-height (inc (* 2 y)))) rlm@0: (* 2 z brick-width) )))) rlm@0: (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive) rlm@0: node)) rlm@0: rlm@0: (defn brick-wall-game-run [] rlm@0: (doto rlm@0: (world rlm@0: (doto (Node.) (.attachChild (floor*)) rlm@0: (.attachChild (brick-wall*)) rlm@0: ) rlm@0: {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81)) rlm@0: "key-m" (gravity-toggle (Vector3f. 0 0 9.81)) rlm@0: "key-l" (gravity-toggle (Vector3f. 9.81 0 0)) rlm@0: "key-j" (gravity-toggle (Vector3f. -9.81 0 0)) rlm@0: "key-k" (gravity-toggle Vector3f/ZERO) rlm@0: "key-u" (gravity-toggle (Vector3f. 0 9.81 0)) rlm@0: "key-o" (gravity-toggle (Vector3f. 0 -9.81 0)) rlm@0: "key-f" (fn[game value] rlm@0: (if (not value) (add-element game (brick-wall*)))) rlm@0: "key-return" (fire-cannon-ball)} rlm@0: position-camera rlm@0: (fn [& _])) rlm@0: (.start))) rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :results silent rlm@0: (hello.brick-wall/brick-wall-game-run) rlm@0: #+end_src rlm@0: rlm@0: #+caption: the brick wall standing rlm@31: [[../images/brick-wall-standing.jpg]] rlm@0: rlm@0: #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball" rlm@31: [[../images/brick-wall-knocked-down.jpg]] rlm@0: rlm@34: * COMMENT Other Brick Games rlm@66: #+name: other-games rlm@0: #+begin_src clojure :results silent rlm@34: (ns hello.other-games rlm@0: {:author "Dylan Holmes"}) rlm@0: (use 'cortex.world) rlm@0: (use 'hello.brick-wall) rlm@0: (use 'cortex.import) rlm@0: (cortex.import/mega-import-jme3) rlm@0: rlm@0: (defn scad [position] rlm@0: (doto (box 0.1 0.1 0.1 rlm@0: :position position :name "brick" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: :mass 20) rlm@0: (-> rlm@0: (.getMesh) rlm@0: (.scaleTextureCoordinates (Vector2f. 1 0.5)) rlm@0: ) rlm@0: (-> (.getControl RigidBodyControl) rlm@0: (.setLinearVelocity (Vector3f. 0 100 0)) rlm@0: ) rlm@0: rlm@0: ;;(.setShadowMode RenderQueue$ShadowMode/Cast) rlm@0: )) rlm@0: rlm@0: rlm@0: (defn shrapnel [] rlm@0: (let [node (Node. "explosion-day")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) scad) rlm@0: (for [y (range 15) rlm@0: x (range 4) rlm@0: z (range 1)] rlm@0: (Vector3f. rlm@0: (+ (* 2 x brick-height) rlm@0: (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4))) rlm@0: (+ (* brick-height (inc (* 2 y)))) rlm@0: (* 2 z brick-height) )))) rlm@0: node)) rlm@0: rlm@0: rlm@0: (def domino-height 0.48) rlm@0: (def domino-thickness 0.12) rlm@0: (def domino-width 0.24) rlm@0: rlm@0: (def domino-thickness 0.05) rlm@0: (def domino-width 0.5) rlm@0: (def domino-height 1) rlm@0: rlm@0: (defn domino rlm@0: ([position] rlm@0: (domino position (Quaternion/IDENTITY))) rlm@0: ([position rotation] rlm@0: (doto (box domino-width domino-height domino-thickness rlm@0: :position position :name "domino" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/Terrain/BrickWall/BrickWall.jpg" rlm@0: :mass 1 rlm@0: :rotation rotation) rlm@0: (.setShadowMode RenderQueue$ShadowMode/CastAndReceive) rlm@0: ))) rlm@0: rlm@0: rlm@0: (defn domino-row [] rlm@0: (let [node (Node. "domino-row")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) domino) rlm@0: (for [ rlm@0: z (range 10) rlm@0: x (range 5) rlm@0: ] rlm@0: (Vector3f. rlm@0: (+ (* z domino-width) (* x 5 domino-width)) rlm@0: (/ domino-height 1) rlm@0: (* -5.5 domino-thickness z) )))) rlm@0: rlm@0: node)) rlm@0: rlm@0: (defn domino-cycle [] rlm@0: (let [node (Node. "domino-cycle")] rlm@0: (dorun rlm@0: (map rlm@0: (comp #(.attachChild node %) (partial apply domino) ) rlm@0: (for [n (range 720)] rlm@0: (let [space (* domino-height 5.5) rlm@0: r (fn[n] (* (+ n 3) domino-width 0.5)) rlm@0: t (fn[n] (reduce rlm@0: + rlm@0: (map rlm@0: (fn dt[n] (/ space (* 2 (Math/PI) (r n)))) rlm@0: (range n)))) rlm@0: t (t n) rlm@0: r (r n) rlm@0: ct (Math/cos t) rlm@0: st (Math/sin t) rlm@0: ] rlm@0: (list rlm@0: (Vector3f. rlm@0: (* -1 r st) rlm@0: (/ domino-height 1) rlm@0: (* r ct)) rlm@0: (.fromAngleAxis (Quaternion.) rlm@0: (- (/ 3.1415926 2) t) (Vector3f. 0 1 0)) rlm@0: ))) rlm@0: )) rlm@0: node)) rlm@0: rlm@0: rlm@0: (defn domino-game-run [] rlm@0: (doto rlm@0: (world rlm@0: (doto (Node.) (.attachChild (floor*)) rlm@0: ) rlm@0: {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81)) rlm@0: "key-m" (gravity-toggle (Vector3f. 0 0 9.81)) rlm@0: "key-l" (gravity-toggle (Vector3f. 9.81 0 0)) rlm@0: "key-j" (gravity-toggle (Vector3f. -9.81 0 0)) rlm@0: "key-k" (gravity-toggle (Vector3f. 0 9.81 0) ) rlm@0: "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true)) rlm@0: "key-o" (gravity-toggle (Vector3f. 0 -9.81 0)) rlm@0: rlm@0: "key-space" rlm@0: (fn[game value] rlm@0: rlm@0: (if (not value) rlm@0: (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0) rlm@0: (.fromAngleAxis (Quaternion.) rlm@0: (/ Math/PI 2) (Vector3f. 0 1 0)))] rlm@0: (add-element game d)))) rlm@0: "key-f" rlm@0: (fn[game value](if (not value) (add-element game (domino-cycle)))) rlm@0: "key-return" (fire-cannon-ball)} rlm@0: position-camera rlm@0: (fn [& _])) rlm@0: (.start))) rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :results silent rlm@0: (cortex.other-games/domino-game-run) rlm@0: #+end_src rlm@0: rlm@0: #+caption: floating dominos rlm@31: [[../images/dominos.jpg]] rlm@0: rlm@34: * Hello Loop rlm@66: #+name: hello-loop rlm@0: #+begin_src clojure :results silent rlm@34: (ns hello.loop rlm@34: (:use cortex.world) rlm@34: (:use cortex.util) rlm@34: (:import com.jme3.math.ColorRGBA) rlm@34: (:import com.jme3.scene.Node)) rlm@34: rlm@0: (defn blue-cube [] rlm@0: (box 1 1 1 rlm@0: :color ColorRGBA/Blue rlm@0: :texture false rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :name "blue-cube" rlm@0: :physical? false)) rlm@0: rlm@0: (defn blue-cube-game [] rlm@0: (let [cube (blue-cube) rlm@0: root (doto (Node.) (.attachChild cube))] rlm@0: (world root rlm@0: {} rlm@0: no-op rlm@0: (fn [game tpf] rlm@0: (.rotate cube 0.0 (* 2 tpf) 0.0))))) rlm@0: #+end_src rlm@31: rlm@34: * COMMENT Hello Collision rlm@0: rlm@66: #+name: hello-collision rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.collision) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: rlm@0: rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]]) rlm@0: rlm@0: rlm@0: (defn environment [] rlm@0: (let rlm@0: [scene-model rlm@0: (doto rlm@0: (.loadModel rlm@0: (doto (asset-manager) rlm@0: (.registerLocator rlm@0: "/home/r/cortex/assets/zips/town.zip" ZipLocator)) rlm@0: "main.scene") rlm@0: (.setLocalScale (float 2.0))) rlm@0: collision-shape rlm@0: (CollisionShapeFactory/createMeshShape #^Node scene-model) rlm@0: landscape (RigidBodyControl. collision-shape 0)] rlm@0: (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive) rlm@0: (.addControl scene-model landscape) rlm@0: scene-model)) rlm@0: rlm@0: (defn player-fn [] rlm@0: (doto rlm@0: (CharacterControl. rlm@0: (CapsuleCollisionShape. (float 1.5) (float 6)(float 1)) rlm@0: (float 0.05)) rlm@0: (.setJumpSpeed 20) rlm@0: (.setFallSpeed 30) rlm@0: (.setGravity 30) ;30 rlm@0: (.setPhysicsLocation (Vector3f. 0 10 0)))) rlm@0: rlm@0: (defn lights [] rlm@0: [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1)))) rlm@0: (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1)))) rlm@0: (doto (DirectionalLight.) rlm@0: (.setColor (.mult ColorRGBA/White (float 0.9) )) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))]) rlm@0: rlm@0: (defn night-lights [] rlm@0: [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3)))) rlm@0: (doto (DirectionalLight.) rlm@0: (.setColor (.mult ColorRGBA/White (float 0.2) )) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))]) rlm@0: rlm@0: (def player (atom (player-fn))) rlm@0: rlm@0: (defn setup-fn [game] rlm@0: (dorun (map #(.addLight (.getRootNode game) %) (lights))) rlm@0: ;; set the color of the sky rlm@0: (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1)) rlm@0: ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1) rlm@0: (doto (.getFlyByCamera game) rlm@0: (.setMoveSpeed (float 100)) rlm@0: (.setRotationSpeed 3)) rlm@0: (.add rlm@0: (.getPhysicsSpace rlm@0: (.getState (.getStateManager game) BulletAppState)) rlm@0: @player) rlm@0: rlm@0: (doto (Node.) (.attachChild (.getRootNode game)) rlm@0: (.attachChild (brick-wall*)) rlm@0: ) rlm@0: rlm@0: ) rlm@0: rlm@0: rlm@0: (def walking-up? (atom false)) rlm@0: (def walking-down? (atom false)) rlm@0: (def walking-left? (atom false)) rlm@0: (def walking-right? (atom false)) rlm@0: rlm@0: (defn set-walk [walk-atom game value] rlm@0: ;;(println-repl "setting stuff to " value) rlm@0: (reset! walk-atom value)) rlm@0: rlm@0: (defn responses [] rlm@0: {"key-w" (partial set-walk walking-up?) rlm@0: "key-d" (partial set-walk walking-right?) rlm@0: "key-s" (partial set-walk walking-down?) rlm@0: "key-a" (partial set-walk walking-left?) rlm@0: "key-return" (fire-cannon-ball) rlm@0: "key-space" (fn [game value] (.jump @player)) rlm@0: }) rlm@0: rlm@0: (defn update-fn rlm@0: [game tpf] rlm@0: (let [camera (.getCamera game) rlm@0: cam-dir (.multLocal rlm@0: (.clone rlm@0: (.getDirection camera)) (float 0.6)) rlm@0: cam-left (.multLocal rlm@0: (.clone rlm@0: (.getLeft camera)) (float 0.4)) rlm@0: walk-direction (Vector3f. 0 0 0)] rlm@0: rlm@0: (cond rlm@0: @walking-up? (.addLocal walk-direction cam-dir) rlm@0: @walking-right? (.addLocal walk-direction (.negate cam-left)) rlm@0: @walking-down? (.addLocal walk-direction (.negate cam-dir)) rlm@0: @walking-left? (.addLocal walk-direction cam-left)) rlm@0: (.setWalkDirection @player walk-direction) rlm@0: (.setLocation camera (.getPhysicsLocation @player)))) rlm@0: rlm@0: (defn run-game [] rlm@0: (.start rlm@0: (world (environment) rlm@0: (responses) rlm@0: setup-fn rlm@0: update-fn))) rlm@0: #+end_src rlm@0: rlm@34: * COMMENT Hello Terrain rlm@66: #+name: hello-terrain rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.terrain) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (import jme3tools.converters.ImageToAwt) rlm@0: rlm@0: rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]]) rlm@0: rlm@0: rlm@0: (defn setup-fn [type game] rlm@0: (.setMoveSpeed (.getFlyByCamera game) 50) rlm@0: (.setFrustumFar (.getCamera game) 10000) rlm@0: (let [env (environment type) rlm@0: cameras [(.getCamera game)] rlm@0: control (TerrainLodControl. env cameras)] rlm@0: ;;(.addControl env control) rlm@0: (.attachChild (.getRootNode game) env))) rlm@0: rlm@0: (defn environment [type] rlm@0: (let rlm@0: [mat_terrain rlm@0: (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md") rlm@0: grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg") rlm@0: dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg") rlm@0: rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg") rlm@0: heightmap-image (.loadTexture (asset-manager) rlm@0: ({:mountain "Textures/Terrain/splat/mountains512.png" rlm@0: :fortress "Textures/Terrain/splat/fortress512.png" rlm@0: }type)) rlm@0: heightmap (ImageBasedHeightMap. rlm@0: (ImageToAwt/convert (.getImage heightmap-image) false true 0)) rlm@0: terrain (do (.load heightmap) rlm@0: (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap))) rlm@0: ] rlm@0: rlm@0: (dorun (map #(.setWrap % Texture$WrapMode/Repeat) rlm@0: [grass dirt rock])) rlm@0: rlm@0: (doto mat_terrain rlm@0: (.setTexture "Tex1" grass) rlm@0: (.setFloat "Tex1Scale" (float 64)) rlm@0: rlm@0: (.setTexture "Tex2" dirt) rlm@0: (.setFloat "Tex2Scale" (float 32)) rlm@0: rlm@0: (.setTexture "Tex3" rock) rlm@0: (.setFloat "Tex3Scale" (float 128)) rlm@0: rlm@0: (.setTexture "Alpha" rlm@0: (.loadTexture rlm@0: (asset-manager) rlm@0: ({:mountain "Textures/Terrain/splat/alphamap.png" rlm@0: :fortress "Textures/Terrain/splat/alphamap2.png"} type)))) rlm@0: rlm@0: (doto terrain rlm@0: (.setMaterial mat_terrain) rlm@0: (.setLocalTranslation 0 -100 0) rlm@0: (.setLocalScale 2 1 2)))) rlm@0: rlm@0: rlm@0: rlm@0: (defn run-terrain-game [type] rlm@0: (.start rlm@0: (world rlm@0: (Node.) rlm@0: {} rlm@0: (partial setup-fn type) rlm@0: no-op))) rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@66: #+name: hello-animation rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.animation) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: (use '[hello [collision :only [lights]]]) rlm@0: rlm@0: (defn stand rlm@0: [channel] rlm@0: (doto channel rlm@0: (.setAnim "stand" (float 0.5)) rlm@0: (.setLoopMode LoopMode/DontLoop) rlm@0: (.setSpeed (float 1)))) rlm@0: rlm@0: (defn anim-listener [] rlm@0: (proxy [AnimEventListener] [] rlm@0: (onAnimChange rlm@0: [control channel animation-name] rlm@0: (println-repl "RLM --- onAnimChange")) rlm@0: (onAnimCycleDone rlm@0: [control channel animation-name] rlm@0: (if (= animation-name "Walk") rlm@0: (stand channel) rlm@0: )))) rlm@0: rlm@0: (defn setup-fn [channel game] rlm@0: (dorun (map #(.addLight (.getRootNode game) %) (lights))) rlm@0: ;; set the color of the sky rlm@0: (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1)) rlm@0: ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1) rlm@0: (.setAnim channel "stand") rlm@0: (doto (.getFlyByCamera game) rlm@0: (.setMoveSpeed (float 10)) rlm@0: (.setRotationSpeed 1))) rlm@0: rlm@0: (defn walk [channel] rlm@0: (println-repl "zzz") rlm@0: (doto channel rlm@0: (.setAnim "Walk" (float 0.5)) rlm@0: (.setLoopMode LoopMode/Loop))) rlm@0: rlm@0: rlm@0: (defn key-map [channel] rlm@0: {"key-space" (fn [game value] rlm@0: (if (not value) rlm@0: (walk channel)))}) rlm@0: rlm@0: (defn player [] rlm@0: (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml") rlm@0: control (.getControl model AnimControl)] rlm@0: (.setLocalScale model (float 0.5)) rlm@0: (.clearListeners control) rlm@0: (.addListener control (anim-control)) rlm@0: model)) rlm@0: rlm@0: rlm@0: rlm@0: (defn run-anim-game [] rlm@0: (let [ninja (player) rlm@0: control (.getControl ninja AnimControl) rlm@0: channel (.createChannel control)] rlm@0: (.start rlm@0: (world rlm@0: ninja rlm@0: (key-map channel) rlm@0: (partial setup-fn channel) rlm@0: no-op)))) rlm@0: #+end_src rlm@0: rlm@34: * COMMENT Hello Materials rlm@66: #+name: material rlm@0: #+begin_src clojure :results silent rlm@0: (ns hello.material) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@34: (use 'cortex.util) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: rlm@0: (defn simple-cube [] rlm@0: (box 1 1 1 rlm@0: :position (Vector3f. -3 1.1 0) rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Interface/Logo/Monkey.jpg" rlm@0: :physical? false)) rlm@0: rlm@0: (defn leaky-box [] rlm@0: (box 1 1 1 rlm@0: :position (Vector3f. 3 -1 0) rlm@0: :material "Common/MatDefs/Misc/ColoredTextured.j3md" rlm@0: :texture "Textures/ColoredTex/Monkey.png" rlm@0: :color (ColorRGBA. 1 0 1 1) rlm@0: :physical? false)) rlm@0: rlm@0: (defn transparent-box [] rlm@0: (doto rlm@0: (box 1 1 0.1 rlm@0: :position Vector3f/ZERO rlm@0: :name "window frame" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/ColoredTex/Monkey.png" rlm@0: :physical? false) rlm@0: (-> (.getMaterial) rlm@0: (.getAdditionalRenderState) rlm@0: (.setBlendMode RenderState$BlendMode/Alpha)) rlm@0: (.setQueueBucket RenderQueue$Bucket/Transparent))) rlm@0: rlm@0: (defn bumpy-sphere [] rlm@0: (doto rlm@0: (sphere 2 rlm@0: :position (Vector3f. 0 2 -2) rlm@0: :name "Shiny rock" rlm@0: :material "Common/MatDefs/Light/Lighting.j3md" rlm@0: :texture false rlm@0: :physical? false) rlm@0: (-> (.getMesh) rlm@0: (doto rlm@0: (.setTextureMode Sphere$TextureMode/Projected) rlm@0: (TangentBinormalGenerator/generate))) rlm@0: (-> (.getMaterial) rlm@0: (doto rlm@31: (.setTexture "DiffuseMap" rlm@31: (.loadTexture (asset-manager) rlm@34: "Textures/Terrain/Pond/Pond.jpg")) rlm@31: (.setTexture "NormalMap" rlm@31: (.loadTexture (asset-manager) rlm@31: "Textures/Terrain/Pond/Pond_normal.png")) rlm@0: (.setFloat "Shininess" (float 5)))) rlm@0: (.rotate (float 1.6) 0 0))) rlm@0: rlm@0: rlm@0: (defn start-game [] rlm@0: (.start rlm@0: (world rlm@0: (let [root (Node.)] rlm@0: (dorun (map #(.attachChild root %) rlm@0: [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)])) rlm@0: root) rlm@0: {} rlm@0: (fn [world] rlm@0: (let [sun (doto (DirectionalLight.) rlm@0: (.setDirection (.normalizeLocal (Vector3f. 1 0 -2))) rlm@0: (.setColor ColorRGBA/White))] rlm@0: (.addLight (.getRootNode world) sun))) rlm@0: no-op rlm@0: ))) rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: * COMMENT code generation rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/brick_wall.clj rlm@0: <> rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@34: #+begin_src clojure :tangle ../src/hello/hello_simpler_app.clj rlm@34: <> rlm@34: #+end_src rlm@34: rlm@34: rlm@34: #+begin_src clojure :tangle ../src/hello/other_games.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/loop.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/collision.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/terrain.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/animation.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: #+begin_src clojure :tangle ../src/hello/material.clj rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: rlm@29: rlm@29: rlm@29: rlm@29: rlm@31: