-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathex39_old_school_tunnel.clj
68 lines (56 loc) · 2 KB
/
ex39_old_school_tunnel.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns ex39-old-school-tunnel
(:require [clojure2d.core :as c2d]
[fastmath.core :as m]
[clojure2d.pixels :as p]
[fastmath.vector :as v])
(:import [fastmath.vector Vec2]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(def ^:const w 300)
(def ^:const h 300)
(def ^:const hw (/ w 2))
(def ^:const hh (/ h 2))
(def ^:const texture-type :generative) ;; change to :image
(def texture (p/load-pixels "results/test.jpg"))
(defn texture-image
"Get color from image"
[x y]
(p/get-color texture x y))
(defn texture-gen
"Generate texture"
[^long x ^long y]
(let [c1 (bit-and 0xff (bit-xor x y))
c2 (bit-and 0xff (bit-xor (inc x) (inc y)))
c3 (bit-and 0xff (bit-xor (dec x) (dec y)))]
(v/vec4 c1 c2 c3 255)))
(def get-texture-color (if (= texture-type :generative) texture-gen texture-image))
(defn map-texture
"Return texture color after transformation"
[v ^Vec2 shift]
(let [m (v/mag v)]
(if (< m 0.04)
:black
(let [distance (+ (.x shift) (* 100.0 (/ 0.7 m)))
angle (+ (.y shift) (* 100.0 (/ (v/heading v) m/PI)))]
(get-texture-color distance angle)))))
(defn draw
"Draw frames"
[canvas _ ^long fps _]
(binding [p/*pixels-edge* :wrap]
(let [t (/ fps 100.0)
sa (* 0.5 (m/qcos (* 0.765 t)))
ca (* 0.5 (m/qsin (+ t t)))
shift (v/vec2 (* 5.0 fps) (/ fps 10.0))]
(dotimes [x hw]
(let [x2 (+ x x)
xx (m/mnorm x2 0.0 w -1.0 1.0)]
(dotimes [y hh]
(let [y2 (+ y y)
yy (m/mnorm y2 0.0 h -1.0 1.0)]
(c2d/set-color canvas (map-texture (v/vec2 (+ xx sa) (+ yy ca)) shift))
(c2d/rect canvas x2 y2 2 2))))))))
(def cnvs (c2d/canvas w h :low))
(def window (c2d/show-window cnvs "Oldschool tunnel" 600 600 60 #(draw %1 %2 %3 %4)))
(defmethod c2d/key-pressed [(:window-name window) \space] [_ _]
(c2d/save cnvs (c2d/next-filename "results/ex39/" ".jpg")))