Permalink
Browse files

First globe implementation

  • Loading branch information...
1 parent ba04fb2 commit d8bc2dad20d6cc6adc402ab2223e6c059e711b03 mikera committed May 21, 2012
Showing with 54 additions and 16 deletions.
  1. +3 −3 src/main/clojure/clisk/core.clj
  2. +30 −5 src/main/clojure/clisk/functions.clj
  3. +21 −8 src/main/clojure/clisk/patterns.clj
@@ -37,9 +37,9 @@
([vector-function w h dx dy]
(let [vector-function (vectorize vector-function)
image (Util/newImage (int w) (int h))
- fr (compile-fn (vector-function 0))
- fg (compile-fn (vector-function 1))
- fb (compile-fn (vector-function 2))
+ fr (compile-fn (component 0 vector-function))
+ fg (compile-fn (component 1 vector-function))
+ fb (compile-fn (component 2 vector-function))
w (int w)
h (int h)
dx (double dx)
@@ -10,24 +10,49 @@
(defn error [& vals]
(throw (Error. (str (reduce str vals)))))
+
+(defn ensure-scalar [x]
+ "Ensure x is a scalar value. If x is a vector, resturns the first component (index 0)."
+ (cond
+ (vector? x)
+ (ensure-scalar (first x))
+ (fn? x)
+ (x pos)
+ (number? x)
+ (double x)
+ :else x))
(defn vectorize [x]
"Converts a value into a vector function form. If x is already a factor, does nothing. If x is a function, apply it to the current position."
(cond
(vector? x)
- x
+ (vec (map ensure-scalar x))
(number? x)
(vec (repeat 4 (double x)))
(fn? x)
- (x pos)
+ (vectorize (x pos))
:else
(vec (repeat 4 x))))
(defn component [i a]
+ "Gets the scalar component of the vector a at index i. "
(let [a (vectorize a)]
(if (< i (count a))
- (a i)
+ (let [ret (a i)]
+ (ensure-scalar ret))
0.0)))
+
+(defn x [v]
+ (component 0 v))
+
+(defn y [v]
+ (component 1 v))
+
+(defn z [v]
+ (component 2 v))
+
+(defn t [v]
+ (component 3 v))
(defn check-dims [& vectors]
"Ensure that parameters are equal sized vectors. Returns the size of the vector(s) if successful."
@@ -137,9 +162,9 @@
dims (min adims bdims)]
(cons 'clojure.core/+
(for [i (range dims)]
- `(clojure.core/* ~(component a i) ~(component b i)))))))
+ `(clojure.core/* ~(component i a) ~(component i b)))))))
-(defn vlength [a]
+(defn length [a]
`(Math/sqrt ~(dot a a)))
(defn vwarp
@@ -2,13 +2,6 @@
(:use clisk.functions)
(:import clisk.Perlin))
-(defn checker
- ([a b]
- (vif '(clojure.core/*
- (clojure.core/- (clisk.functions/frac x) 0.5)
- (clojure.core/- (clisk.functions/frac y) 0.5))
- a
- b)))
(def perlin-noise
'(clisk.Perlin/noise x y z t))
@@ -37,4 +30,24 @@
(def vsnoise (vector-offsets snoise))
-(def vplasma (vector-offsets plasma))
+(def vplasma (vector-offsets plasma))
+
+(defn checker
+ ([a b]
+ (vif '(clojure.core/*
+ (clojure.core/- (clisk.functions/frac x) 0.5)
+ (clojure.core/- (clisk.functions/frac y) 0.5))
+ a
+ b)))
+
+(defn globe
+ "Creates a globe, returning the value of the function called on the surface of a unit sphere"
+ ([]
+ (globe z 0.0))
+ ([function]
+ (globe function 0.0))
+ ([function background]
+ (vif
+ `(- ~(length [x y]) 1.0)
+ (vwarp [x y `(- 1.0 ~(dot [x y] [x y]))] function )
+ background)))

0 comments on commit d8bc2da

Please sign in to comment.