Last active
August 29, 2015 14:10
-
-
Save svanellewee/46e24a78ea63d2d13228 to your computer and use it in GitHub Desktop.
Kind of boids... but not really
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns boids-gl.core | |
| (:import (org.lwjgl LWJGLException ) | |
| (org.lwjgl.opengl Display DisplayMode GL11)) | |
| (:gen-class)) | |
| (defn start [] | |
| (let [ display (DisplayMode. 800 600) ] | |
| (try | |
| (do | |
| (Display/setDisplayMode display) | |
| (Display/create)) | |
| (catch LWJGLException e | |
| (do (str "caught exception: " (.getMessage e))) | |
| (System/exit 1))))) | |
| (defn setupOpenGL [] | |
| (GL11/glMatrixMode GL11/GL_PROJECTION) | |
| (GL11/glLoadIdentity) | |
| (GL11/glOrtho 0 800 0 600 1 -1) | |
| (GL11/glMatrixMode GL11/GL_MODELVIEW) | |
| ) | |
| (defn magnitute | |
| [x y] | |
| (Math/sqrt (+ (* x x) (* y y)))) | |
| (defrecord Boid [ x y dx dy ]) | |
| (defn update-position | |
| [b] | |
| (let [ newx (+ (:x b) (:dx b)) | |
| newy (+ (:y b) (:dy b)) | |
| delta-x (if (or (> newx 800) (< newx 0) ) (* -1 (:dx b)) (:dx b) ) | |
| delta-y (if (or (> newy 400) (< newy 0) ) (* -1 (:dy b)) (:dy b) ) ] | |
| (Boid. newx newy delta-x delta-y))) | |
| (defn draw-boid | |
| [ a-boid ] | |
| (GL11/glColor3f 0.5 0.5 1.0) | |
| (let [ x (:x a-boid) | |
| y (:y a-boid) | |
| unitize (fn [ xx yy ] | |
| (let [m (magnitute xx yy) | |
| nx (if (< 0 xx) (* xx (/ 1 m)) 0) | |
| ny (if (< 0 yy) (* yy (/ 1 m)) 1) ] | |
| [ nx ny ])) | |
| perpindicular (fn [ x y ] | |
| [(* y -1) x ]) | |
| [ dx dy ] (unitize (:dx a-boid) (:dy a-boid)) | |
| [ px py ] (perpindicular dx dy ) ] | |
| (GL11/glBegin GL11/GL_TRIANGLES) | |
| (GL11/glVertex2f (+ x (* dx 10)) (+ y (* dy 10))) | |
| (GL11/glVertex2f (+ x (* px 5)) (+ y (* py 5))) | |
| (GL11/glVertex2f (- x (* px 5)) (- y (* py 5))) | |
| (GL11/glEnd) | |
| )) | |
| (defn draw-boids [boid-agents] | |
| (doseq [boid boid-agents] | |
| (draw-boid @boid))) | |
| (defn mainloop [ some-boids ] | |
| (when-not (Display/isCloseRequested) | |
| (GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT)) | |
| (draw-boids some-boids) | |
| (doseq [ agent-boid some-boids] | |
| (send-off agent-boid update-position )) | |
| (Display/update) | |
| (recur some-boids))) | |
| (defn -main | |
| [& args] | |
| (println "Test" ) | |
| (start) | |
| (setupOpenGL) | |
| (let [ the-boids (take 50 | |
| (repeatedly | |
| (fn[] (agent (Boid. (rand-int 800) (rand-int 600) 1 1 )))))] | |
| (mainloop the-boids)) | |
| (Display/destroy) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defproject boids-gl "0.1.0-SNAPSHOT" | |
| :description "FIXME: write description" | |
| :url "http://example.com/FIXME" | |
| :license {:name "Eclipse Public License" | |
| :url "http://www.eclipse.org/legal/epl-v10.html"} | |
| :dependencies [[org.clojure/clojure "1.6.0"] | |
| [org.lwjgl.lwjgl/lwjgl "2.9.1"] | |
| [org.lwjgl.lwjgl/lwjgl-platform "2.9.1" | |
| :classifier "natives-osx" | |
| ;; LWJGL stores natives in the root of the jar; this | |
| ;; :native-prefix will extract them. | |
| :native-prefix ""]] | |
| :main ^:skip-aot boids-gl.core | |
| :target-path "target/%s" | |
| :profiles {:uberjar {:aot :all}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment