|
| 1 | +(ns ray.core |
| 2 | + (:require [clojure.math.numeric-tower :as math])) |
| 3 | + |
| 4 | +(defn sq [x] (* x x)) |
| 5 | + |
| 6 | +(defn mag [x y z] |
| 7 | + (math/sqrt (+ (sq x) (sq y) (sq z)))) |
| 8 | + |
| 9 | +(defn unit-vector [x y z] |
| 10 | + (let [d (mag x y z)] |
| 11 | + [(/ x d) (/ y d) (/ z d)])) |
| 12 | + |
| 13 | +(defrecord Point [x y z]) |
| 14 | + |
| 15 | +(defn distance [p1 p2] |
| 16 | + (mag (- (:x p1) (:x p2)) |
| 17 | + (- (:y p1) (:y p2)) |
| 18 | + (- (:z p1) (:z p2)))) |
| 19 | + |
| 20 | +(defn minroot [a b c] |
| 21 | + (if (zero? a) |
| 22 | + (/ (- c) b) |
| 23 | + (let [disc (- (sq b) (* 4 a c))] |
| 24 | + (if (pos? disc) |
| 25 | + (let [discrt (math/sqrt disc)] |
| 26 | + (min (/ (+ (- b) discrt) (* 2 a)) |
| 27 | + (/ (- (- b) discrt) (* 2 a)))) |
| 28 | + nil)))) |
| 29 | + |
| 30 | +(defrecord Surface [color]) |
| 31 | + |
| 32 | +(def ^:dynamic *world* nil) |
| 33 | + |
| 34 | +(def eye (Point. 0 0 200)) |
| 35 | + |
| 36 | +(defn lambert [s int xr yr zr] |
| 37 | + (let [[xn yn zn] (normal s int)] |
| 38 | + (max 0 (+ (* xr xn) (* yr yn) (* zr zn))))) |
| 39 | + |
| 40 | +(defn first-hit [pt xr yr zr] |
| 41 | + (let [hits (for [s *world* :let [h (hit s pt xr yr zr)] :when h] h)] |
| 42 | + (if (empty? hits) |
| 43 | + [nil nil] |
| 44 | + (second (apply min-key #(first %) hits))))) |
| 45 | + |
| 46 | +(defn hit [s pt xr yr zr] |
| 47 | + (let [h (intersect s pt xr yr zr)] |
| 48 | + (when h |
| 49 | + (let [d (distance h pt)] |
| 50 | + (when d |
| 51 | + [d [s h]]))))) |
| 52 | + |
| 53 | +(defn sendray [pt xr yr zr] |
| 54 | + (let [[s int] (first-hit pt xr yr zr)] |
| 55 | + (* (lambert s int xr yr zr) (:color (:surface s))))) |
| 56 | + |
| 57 | +(defn color-at [x y] |
| 58 | + (let [[xr yr zr] |
| 59 | + (unit-vector (- x (:x eye)) |
| 60 | + (- y (:y eye)) |
| 61 | + (- 0 (:z eye)))] |
| 62 | + (math/round (* (sendray eye xr yr zr) 255)))) |
| 63 | + |
| 64 | +(defprotocol Shape |
| 65 | + (intersect [this pt xr yr zr]) |
| 66 | + (normal [this pt])) |
| 67 | + |
| 68 | +(defn sphere-intersect [s pt xr yr zr] |
| 69 | + (let [c (:center s) |
| 70 | + n (minroot (+ (sq xr) (sq yr) (sq zr)) |
| 71 | + (* 2 (+ (* (- (:x pt) (:x c)) xr) |
| 72 | + (* (- (:y pt) (:y c)) yr) |
| 73 | + (* (- (:z pt) (:z c)) zr))) |
| 74 | + (+ (sq (- (:x pt) (:x c))) |
| 75 | + (sq (- (:y pt) (:y c))) |
| 76 | + (sq (- (:z pt) (:z c))) |
| 77 | + (- (sq (:radius s)))))] |
| 78 | + (if n |
| 79 | + (Point. (+ (:x pt) (* n xr)) |
| 80 | + (+ (:y pt) (* n yr)) |
| 81 | + (+ (:z pt) (* n zr)))))) |
| 82 | + |
| 83 | + |
| 84 | +(defn sphere-normal [s pt] |
| 85 | + (let [c (:center s)] |
| 86 | + (unit-vector (- (:x c) (:x pt)) |
| 87 | + (- (:y c) (:y pt)) |
| 88 | + (- (:z c) (:z pt))))) |
| 89 | + |
| 90 | +(defrecord Sphere [radius center surface] Shape |
| 91 | + (intersect [this pt xr yr zr] |
| 92 | + (sphere-intersect [this pt xr yr zr])) |
| 93 | + (normal [this pt] |
| 94 | + (sphere-normal [this pt]))) |
| 95 | + |
| 96 | +(defn defsphere [x y z r c] |
| 97 | + (let [s (Sphere. r (Point. x y z) (Surface. c))] |
| 98 | + (def ^:dynamic *world* (cons s *world*)))) |
| 99 | + |
| 100 | +(defn tracer [pathname] |
| 101 | + (with-open [w (java.io.FileWriter. pathname)] |
| 102 | + )) |
0 commit comments