Skip to content

Commit

Permalink
initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
levand committed Mar 8, 2013
1 parent fbd8530 commit 7478696
Show file tree
Hide file tree
Showing 19 changed files with 921 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/out
/target
/lib
/classes
Expand Down
13 changes: 13 additions & 0 deletions README.md
@@ -0,0 +1,13 @@
# boids

A Clojure library designed to ... well, that part is up to you.

## Usage

FIXME

## License

Copyright © 2013 FIXME

Distributed under the Eclipse Public License, the same as Clojure.
11 changes: 11 additions & 0 deletions project.clj
@@ -0,0 +1,11 @@
(defproject boids "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.5.0"]
[ring/ring-jetty-adapter "1.1.1"]
[compojure "1.1.5"]]
:source-paths ["src/clj"]
:main boids.server
:plugins [[lein-cljsbuild "0.3.0"]]
:cljsbuild {:builds [{:source-paths ["src/cljs"]
:compiler {:optimizations :none
:output-to "resources/public/deps.js"
:output-dir "resources/public/build"}}]})
1 change: 1 addition & 0 deletions resources/public/.#brepl.html
10 changes: 10 additions & 0 deletions resources/public/boids-dev.html
@@ -0,0 +1,10 @@
<html>
<head><title>Hello, World!</title></head>
<body>
<script src="lib/three.min.js"></script>
<script src="build/goog/base.js"></script>
<script src="deps.js"></script>
<script>goog.require('boids.main');</script>
<script>boids.main.main()</script>
</body>
</html>
8 changes: 8 additions & 0 deletions resources/public/brepl.html
@@ -0,0 +1,8 @@
<html>
<head><title>A Browser REPL</title></head>
<body>
<script src="build/goog/base.js"></script>
<script src="deps.js"></script>
<script>goog.require('boids.brepl');</script>
</body>
</html>
9 changes: 9 additions & 0 deletions resources/public/hello.html
@@ -0,0 +1,9 @@
<html>
<head><title>Hello, World!</title></head>
<body>
<script src="build/goog/base.js"></script>
<script src="deps.js"></script>
<script>goog.require('boids.hello');</script>
<script>boids.hello.hello_world()</script>
</body>
</html>
706 changes: 706 additions & 0 deletions resources/public/lib/three.min.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions resources/public/main.html
@@ -0,0 +1,9 @@
<html>
<head><title>Hello, World!</title></head>
<body>
<script src="build/goog/base.js"></script>
<script src="deps.js"></script>
<script>goog.require('boids.main');</script>
<script>boids.main.main()</script>
</body>
</html>
7 changes: 7 additions & 0 deletions resources/public/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions resources/public/main3d.html
@@ -0,0 +1,10 @@
<html>
<head><title>Hello, World!</title></head>
<body>
<script src="lib/three.min.js"></script>
<script src="build/goog/base.js"></script>
<script src="deps.js"></script>
<script>goog.require('boids.main');</script>
<script>boids.main.main()</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/clj/boids/.#server.clj
9 changes: 9 additions & 0 deletions src/clj/boids/server.clj
@@ -0,0 +1,9 @@
(ns boids.server
(:gen-class)
(:require [ring.adapter.jetty :as jetty]
[compojure.route :as route]))

(defn -main [& args]
(jetty/run-jetty (route/resources "/")
{:port 3000}))

4 changes: 4 additions & 0 deletions src/cljs/boids/brepl.cljs
@@ -0,0 +1,4 @@
(ns boids.brepl
(:require [clojure.browser.repl :as repl]))

(repl/connect "http://localhost:9000/repl")
5 changes: 5 additions & 0 deletions src/cljs/boids/hello.cljs
@@ -0,0 +1,5 @@
(ns boids.hello)

(defn hello-world
[]
(js/alert "Hello, world!"))
8 changes: 8 additions & 0 deletions src/cljs/boids/main.cljs
@@ -0,0 +1,8 @@
(ns boids.main
(:require [boids.protocols :as p]
[boids.view :as v]))

(defn main
"Starts everything running"
[]
(v/create (.-body js/document)))
29 changes: 29 additions & 0 deletions src/cljs/boids/protocols.cljs
@@ -0,0 +1,29 @@
(ns boids.protocols)

(defprotocol Math
"Model for operations involving position and heading."
(update-position [this position heading speed]
"Giving a position, a heading and a speed, return a new
position.")
(combine-headings [this heading-1 heading-2]
"Combine two headings into a single heading."))

(defprotocol Boid
"A representation of a boid"
(position [this]
"Returns a position vector (in Cartesian coordinates)")
(heading [this]
"Returns a heading vector (in radians)"))

(defprotocol Behavior
"A model of a boid's behavior"
(delta-heading [this boid flock]
"Given a boid and a flock (a sequence of other boids), return a
delta heading of the boid."))

(defprotocol View
"A dynamically updating view of the simulation."
(add [view boid-atom]
"Adds a boid atom to the view."))


10 changes: 10 additions & 0 deletions src/cljs/boids/view.cljs
@@ -0,0 +1,10 @@
(ns boids.view
(:require [boids.protocols :as p]))

(defn create
"Initializes a view and adds it to the provided HTML element."
[element]
(let [canvas (.createElement js/document "canvas")]
(.setAttribute canvas "width" (.-innerWidth js/window))
(.setAttribute canvas "height" (.-innerHeight js/window))
(.appendChild element canvas)))
70 changes: 70 additions & 0 deletions src/cljs/boids/view3d.cljs
@@ -0,0 +1,70 @@
(defn obj
"Convert a cljs map to a Clojure map"
[m]
(let [out (js-obj)]
(doseq [[k v] m]
(aset out (name k) v))
out))

(defn make-camera
"Creates and returns a three.js camera"
[window-width window-height]
(let [camera
(THREE/PerspectiveCamera. 75 (/ window-width window-height) 1 10000)]
(set! (.-z (.-position camera)) 1000)
camera))

(defn make-3d-object
"Creates and returns a three.js mesh object"
[]
(let [geometry (THREE/CylinderGeometry. 0 50 400 8 2 false)
material (THREE/MeshBasicMaterial. (obj {:color 0 :wireframe true}))]
(THREE/Mesh. geometry material)))

(defn make-renderer
"Creates and returns a three.js renderer"
[width height]
(let [renderer (THREE/WebGLRenderer. )]
(.setSize renderer width height)
renderer))

(defn display
"Initializes a full display and adds it to the given DOM
element. Returns a display map."
[element width height]
(let [scene (THREE/Scene.)
objects [(make-3d-object)]
renderer (make-renderer width height)
camera (make-camera width height)]
(doseq [mesh objects]
(.add scene mesh))
(.appendChild element (.-domElement renderer))
{:scene scene
:objects objects
:renderer renderer
:camera camera}))

(def the-display (atom nil))

(defn rotate
"Rotates a mesh"
[mesh x y z]
(let [rotation (.-rotation mesh)]
(set! (.-x rotation) (+ x (.-x rotation)))
(set! (.-y rotation) (+ y (.-y rotation)))
(set! (.-z rotation) (+ z (.-z rotation)))))

(defn animate
"Animates the display contained in the given display atom"
[display]
(let [{:keys [objects camera scene renderer]} @display]
(doseq [mesh objects]
(rotate mesh 0.00 0.01 0.01))
(.render renderer scene camera))
(js/requestAnimationFrame #(animate display)))

(defn main []
(reset! the-display (display (.-body js/document)
(.-innerWidth js/window)
(.-innerHeight js/window)))
(animate the-display))

0 comments on commit 7478696

Please sign in to comment.