-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.clj
More file actions
253 lines (201 loc) · 6.05 KB
/
Copy pathrender.clj
File metadata and controls
253 lines (201 loc) · 6.05 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(ns moarquil.render
"
This namespace provides functionality for rendering and user
interaction, based on the purely mathematical objects provided by
`geom.clj`.
Most functions here are callbacks for the various Quil UI events.
See `https://github.com/quil/quil` for the basics.
"
(:require [moarquil.geom :refer [content reset-content!]]
[moarquil.util :refer [with-style with-shape with-matrix]]
[quil.core :refer :all]))
;; General Quil drawing parameters.
(defn setup []
(fill 0)
(stroke 00))
;; Keep track of when we're dragging the mouse, so we can stop
;; animation at that time.
(def ^:private dragging (atom false))
;; Save and toggle the paused state so we can decide whether to move
;; the camera or not.
(def ^:private paused (atom false))
(defn toggle-paused [] (swap! paused not))
;; <em>Camera dynamics</em>
;; Store camera info, including current position and orientation.
(def ^:private camera-positions (atom {:points-to [0 0 0]
:theta 0
:phi 0
:r 1000}))
;; Camera is also moving with some initial theta, phi velocity.
(def ^:private velocity (atom [0.00002
0.0001]))
(defn change-velocities-fractionally
"
Speed up or slow down the camera movement.
"
[f]
(let [[vth vph] @velocity]
(reset! velocity [(* vth f) (* vph f)])))
(defn update-camera-positions []
(when-not (or @paused @dragging)
(swap! camera-positions (fn [m]
(let [[vth vph] @velocity]
(-> m
(update :theta + vth)
(update :phi + vph)))))))
(defn update-camera-positions-continuously
"
Basically, we always want to update where the camera is, unless
we're dragging the mouse or the user has paused the movement.
"
[]
(while true
(update-camera-positions)
(Thread/sleep 1)))
;; <em>Methods for drawing individual objects.</em>
(defn ^:private draw-planet
"
Draw individual planet as a sphere in space. Draw craters, but only
if display is not updating, since drawing them is slow.
"
[{:keys [r pos craters]}]
(with-matrix
(with-style
(fill 255)
(no-stroke)
(apply translate pos)
(sphere-detail 30)
(sphere r)))
(when (and @paused (not @dragging))
(with-style
(stroke 80)
(doseq [c craters]
(doseq [p c]
(apply point p))))))
(defn ^:private draw-sphere [{:keys [value origin radius]}]
(with-matrix
(with-style
(fill value)
(no-stroke)
(sphere-detail 15)
(translate origin)
(sphere radius))))
(defn ^:private draw-ring [{:keys [pos r1 r2 dr rotx color points]}]
(with-matrix
(apply translate pos)
(rotate-x rotx)
(with-style
(doseq [p points]
(apply point p)))))
(defn ^:private draw-spiral [l]
(with-style
(stroke 150)
(doseq [p (:points l)]
(stroke-weight (min 0.1 (- (/ (last p) 300))))
(apply line p))))
(defn ^:private draw-text [l]
(apply (partial text (:txt l))
(:pos l)))
;; Initially, all objects are visible.
(def ^:private to-render (atom {:spirals true
:text true
:spheres true
:planets true
:rings true}))
(defmacro deftoggle
"
Handle toggle-able objects. This got fairly repetetive so I
replaced the repeated boilerplate with this macro.
"
[name]
(let [fn-name (->> name (str "toggle-") symbol)
kw (keyword name)]
`(defn ~fn-name [] (swap! to-render update ~kw not))))
(deftoggle spirals)
(deftoggle text)
(deftoggle spheres)
(deftoggle planets)
(deftoggle rings)
(defn render
"
Render all available objects, dispatching on object type.
"
[objects]
(doseq [{type_ :type :as l} objects]
(cond
(and (= type_ :spiral)
(:spirals @to-render))
(draw-spiral l)
(and (= type_ :text)
(:text @to-render))
(draw-text l)
(and (= type_ :sphere)
(:spheres @to-render))
(draw-sphere l)
(and (= type_ :ring)
(:rings @to-render))
(draw-ring l)
(and (= type_ :planet)
(:planets @to-render))
(draw-planet l))))
(defn key-press
"
Handle any keys pressed; mostly for toggling things on and off.
"
[]
(try
(condp = (raw-key)
\r (toggle-rings)
\s (toggle-spheres)
\p (toggle-planets)
\t (toggle-text)
\q (toggle-spirals)
\R (reset-content!)
\+ (change-velocities-fractionally 1.1)
\- (change-velocities-fractionally 0.9)
\space (toggle-paused)
(println "Unknown key"))
(catch Throwable t
(prn t))))
(defn draw
"
Based on camera position, show the current view. Current snapshot
of existing objects in the world is provided from the `geom`
namespace via the `content` function.
"
[]
(background 220)
(let [theta (:theta @camera-positions)
phi (:phi @camera-positions)
r (:r @camera-positions)]
(camera (* r (Math/cos phi) (Math/sin theta))
(* r (Math/sin phi) (Math/sin theta))
(* r (Math/cos theta))
0 0 0
0 1 1))
(render (content)))
(defn mouse-dragged
"
Move the camera around when the mouse is dragged and mouse button pressed.
"
[]
(reset! dragging true)
(let [delx (- (mouse-x)
(pmouse-x))
mdx (/ delx 3)
dely (- (pmouse-y)
(mouse-y))
mdy (/ dely 3)]
(swap! camera-positions update :phi - (radians mdx))
(swap! camera-positions update :theta + (radians mdy))))
(defn mouse-pressed [] (reset! dragging true))
(defn mouse-released [] (reset! dragging false))
(defn mouse-wheel
"
Zoom in and out when mouse wheel moves.
"
[amount]
(reset! dragging true)
(future (Thread/sleep 1000)
(reset! dragging false))
(swap! camera-positions update :r #(max 1 (+ % (* 3 amount)))))