Skip to content

Commit

Permalink
Updates to add teapot
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangamble committed Feb 3, 2012
1 parent 18a911b commit bb601f4
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 2 deletions.
2 changes: 1 addition & 1 deletion convolution.jnlp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<jnlp codebase="http://sites.google.com/site/juliangamble/Home/"
<jnlp codebase="http://sites.google.com/site/juliangamble/Home/"
href="convolution.jnlp">
<information>
<title>Demonstration of Penumbras 3D Clojure Functionality</title>
Expand Down
14 changes: 14 additions & 0 deletions nbody.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<jnlp codebase="http://sites.google.com/site/juliangamble/Home/"
href="nbody.jnlp">
<information>
<title>Demonstration of Penumbras 3D Clojure Functionality</title>
<vendor>Machiavellian</vendor>
</information>
<resources>
<j2se version="1.4+" />
<jar href="clj-penumbra-examples.jar" />
</resources>
<application-desc main-class="example.gpgpu.n_body" />
<security><all-permissions/></security>
</jnlp>
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
[org.clojure/clojure-contrib "1.2.0"]
[penumbra "0.6.0"]]
:source-path "src"
:main example.gpgpu.fluid
:main example.gpgpu.importer
)

9 changes: 9 additions & 0 deletions src/example/gpgpu/importer.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns example.gpgpu.importer
(:require [example.gpgpu.n-body :as nbody]
[example.gpgpu.convolution :as c]
[example.gpgpu.brians-brain :as brian]
[example.opengl.marble :as tea]
[example.gpgpu.fluid :as fluid])
(:gen-class
:main main))

96 changes: 96 additions & 0 deletions src/example/gpgpu/n_body.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
;; Copyright (c) Zachary Tellman. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns example.gpgpu.n-body
(:use [penumbra compute]
[clojure.contrib.seq :only (partition-all flatten)])
(:require [penumbra.app :as app]
[penumbra.data :as data])
(:gen-class
:main -main))

(defn gen [min max]
(+ min (* (rand) (- max min))))

(defn gen-mass [num min max]
(wrap (map float (concat [5e12] (take (dec num) (repeatedly #(gen min max))))) 1))

(defn gen-velocity [num min max]
(wrap (map float (concat [0.0 0.0 0.0] (take (* 3 (dec num)) (repeatedly #(gen min max))))) 3))

(defn gen-position [num min max]
(wrap (map float (concat [0.0 0.0 0.0] (take (* 3 (dec num)) (repeatedly #(gen min max))))) 3))

(defn init []

(defmap add
(+ %1 (* k %2)))

(defmap kinetic-energy
(let [m %1, v %2]
(float3 (* 0.5 m (dot v v)))))

(defmap potential-energy
(let [m1 %1, m2 (%1 idx)
p1 %2, p2 (%2 idx)]
(float3 (* g m1 m2 (length (- p1 p2))))))

(defmap gravity
(let [m2 (%1 idx)
p1 %2, p2 (%2 idx)
diff (- p2 p1)]
(if (= :index (float idx))
(float3 0.0)
(/ (* diff g m2) (pow (dot diff diff) 1.5)))))

(defreduce sum (+ %1 %2)))

(defn prn-tex [t]
(println (partition 3 (data/unwrap t))))

(defn piecewise-add [f size]
(let [add #(add {:k 1.0} %1 %2)
s (partition-all 10 (range size))]
(reduce add (map #(reduce add (doall (map f %))) s))))

(defn energy [m v p num]
(let [ke (first (sum [(kinetic-energy [m] [v])]))
pe (first (sum [(piecewise-add #(potential-energy {:idx % :g 6.673e-11} [m] [p]) num)]))]
(println "kinetic:" ke "potential:" pe "total:" (+ ke pe))))

(defn run-sim [num iterations]
(let [m (gen-mass num 1e3 1e4)
v (gen-velocity num -1 1)
p (gen-position num -100 100)
dt 0.01]
(time
(do
(energy m v p num)
(loop [v v, p p, i 1]
(if (> i iterations)
(do
(energy m v p num)
(data/release! m) (data/release! v) (data/release! p))
(let [a (piecewise-add #(gravity {:g 6.673e-11 :idx %} [m] [p]) num)
v* (add {:k dt} v a)
p* (add {:k dt} p [v*])]
(energy m v* p* num)
(recur v* p* (inc i)))))))))

(defn start []
(app/with-gl
(init)
(dotimes [i 1]
(let [num (* 100 (inc i))]
(println num)
(dotimes [_ 1]
(run-sim num 1))))))


(defn -main []
(start))
105 changes: 105 additions & 0 deletions src/example/opengl/marble.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
;; Copyright (c) Zachary Tellman. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns example.opengl.marble
(:use [penumbra opengl compute]
[penumbra.opengl core]
[cantor])
(:require [penumbra.app :as app]
[penumbra.text :as text]
[penumbra.data :as data])
(:gen-class
:main -main))

;;;;;;;;;;;;;;;;;

(defn reshape [[x y w h] state]
(frustum-view 60. (/ w (double h)) 0.1 10.)
(load-identity)
(translate 0 0 -3)
(light 0
:position [1 1 1 0])
(material :front-and-back
:ambient-and-diffuse [1 1 1 1]
:specular [0.5 0.4 0.4 1]
:shininess 64)
state)

(defn reset-random-texture
([state]
(reset-random-texture 32 1 state))
([dim scale state]
(when-let [tex (:tex state)]
(data/destroy! tex))
(assoc state
:tex (wrap
(take (* dim dim dim) (repeatedly #(float (- (rand (* 2 scale)) scale))))
[dim dim dim]
:target :texture-3d
:texture-wrap-s :repeat
:texture-wrap-t :repeat
:texture-wrap-r :repeat
:texture-min-filter :linear
:texture-mag-filter :linear))))

(defn init [state]

(defpipeline marble

:vertex {position (float3 :vertex)
normal (normalize (float3 (* :normal-matrix :normal)))
:position (* :model-view-projection-matrix :vertex)}

:fragment (let [noise 0
scale 0.5
pos (* position (float3 (/ 1 (.x (dim %)))))]
(dotimes [i octaves]
(+= noise (* (% pos) scale))
(*= scale 0.5)
(*= pos (float3 2)))
(let [marble (-> position .x (+ noise) (* 2) sin abs)
mixed (mix [0.2 0.15 0.1 1] [0.8 0.7 0.7 1] (pow marble 0.75))]
(* mixed (lighting 0 normal)))))

(app/title! "Marble")
(enable :depth-test)

(reset-random-texture
(assoc state
:teapot (create-display-list (teapot))
:octaves 6.0)))

(defn mouse-drag [[dx dy] _ button state]
(assoc state
:rot-x (+ (:rot-x state) dy)
:rot-y (+ (:rot-y state) dx)))

(defn key-press [key state]
(condp = key
;;"a" (update-in state [:octaves] inc)
;;"s" (update-in state [:octaves] #(max 0 (dec %)))
" " (reset-random-texture state)
state))

(defn display [[delta time] state]
(rotate (:rot-x state) 1 0 0)
(rotate (:rot-y state) 0 1 0)
(color 1 0 0)
(blit!
(with-pipeline marble [{:octaves (:octaves state)} (app/size) [(:tex state)]]
(clear)
;;(text/write-to-screen (str (int (/ 1 delta)) "fps") 0 0)
((:teapot state)))))

(defn start []
(app/start
{:reshape reshape, :display display, :init init, :mouse-drag mouse-drag, :key-press key-press}
{:rot-x 0, :rot-y 0}))

(defn -main []
(start))
14 changes: 14 additions & 0 deletions tea.jnlp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<jnlp codebase="http://sites.google.com/site/juliangamble/Home/"
href="tea.jnlp">
<information>
<title>Demonstration of Penumbras 3D Clojure Functionality</title>
<vendor>Machiavellian</vendor>
</information>
<resources>
<j2se version="1.4+" />
<jar href="clj-penumbra-examples.jar" />
</resources>
<application-desc main-class="example.opengl.marble" />
<security><all-permissions/></security>
</jnlp>

0 comments on commit bb601f4

Please sign in to comment.