-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ss
More file actions
346 lines (313 loc) · 12.1 KB
/
main.ss
File metadata and controls
346 lines (313 loc) · 12.1 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(module (define-scheme-record)
(import scheme)
(define-syntax define-scheme-record (identifier-syntax define-record)))
(include "records.ss")
(include "defaults.ss")
(include "math.ss")
(include "color.ss")
(define EPSILON 0.0005)
(define MAXDEPTH 5)
(define-record <scene> background-color objects lights)
(define-record <view> left right bottom top)
(define-record <camera>
output-width output-height type position target view)
(define-record <display> x-samples y-samples filter x-width y-width gain gamma)
(define-record <intersect> time object extra)
(define-record <ray> origin direction)
(define-syntax fold-list
(syntax-rules ()
[(_ [item ls] [acc init] b1 b2 ...)
(and (identifier? #'item) (identifier? #'acc))
(fold-left (lambda (acc item) b1 b2 ...)
init
ls)]))
(define-syntax list-of-helper
(syntax-rules ()
[(_ $acc $exp () $guard)
(if $guard
(cons $exp $acc)
$acc)]
[(_ $acc $exp ([$var $gen] . $rest) $guard)
(fold-left
(lambda (acc $var)
(list-of-helper acc $exp $rest $guard))
$acc
$gen)]))
(define-syntax list-of
(syntax-rules ()
[(_ $exp ([$var $gen] . $rest))
(list-of $exp ([$var $gen] . $rest) #t)]
[(_ $exp ([$var $gen] . $rest) $guard)
(reverse
(list-of-helper '() $exp ([$var $gen] . $rest) $guard))]))
(load "noise.ss")
(load "env.ss")
(load "shaders.ss")
(load "lights.ss")
(load "objects.ss")
(load "image.ss")
(define (filterwidth x) 1)
(define (filterwidthp x) 1)
(define (traverse-ray ray t)
(vec-add (<ray> origin ray) (vec-num-mul (<ray> direction ray) t)))
(define (find-intersections ray scene)
(sort-intersections
(fold-list [obj (<scene> objects scene)] [acc '()]
(fold-list [inter (object-intersections obj ray)] [acc acc]
;; Make sure we don't accidently hit the object at the intersect-point
(if (< (<intersect> time inter) EPSILON)
acc
(cons inter acc))))))
(define (light-shade l)
(let ([shader (light-shader l)])
(if shader
(parameterize ([$light l])
(shader))
(errorf 'light-shade "no light shader defined for ~a" l))))
(define (opaque? opacity)
(and (>= (<color> r opacity) 1)
(>= (<color> g opacity) 1)
(>= (<color> b opacity) 1)))
(define (pixel-color-from-ray scene ray Kr depth)
(if (or (= depth 0) (<= Kr 0))
black
(color-num-mul
(let lp ([ls (find-intersections ray scene)])
(match ls
[() (<scene> background-color scene)]
[(,(intersect <= `(<intersect> ,time ,object ,extra)) . ,rest)
(let ([incoming (<ray> direction ray)]
[intersect-point (traverse-ray ray time)])
(let*-values
([(geometric-normal) (object-normal object extra intersect-point)]
[(intersect-point normal)
(object-displace object intersect intersect-point
geometric-normal)]
[(color opacity)
(object-shade object intersect extra intersect-point geometric-normal normal
incoming depth)])
(if (opaque? opacity)
color
(cond
[(object-volume object) =>
(lambda (shader)
(let-values ([(vol-color vol-opacity)
(volume-shade object
intersect-point incoming
color opacity)])
(let ([color (color-add color vol-color)])
(if (opaque? vol-opacity)
color
(color-add color (lp rest))))))]
[else
(color-add color (lp rest))]))))]))
Kr)))
(define (ray-gun camera)
(define view (<camera> view camera))
(define xt
(make-linear-transform 0 (- (<camera> output-width camera) 1)
(<view> left view) (<view> right view)))
(define yt
(make-linear-transform 0 (- (<camera> output-height camera) 1)
(<view> top view) (<view> bottom view)))
(define (vlincomb3 k1 v1 k2 v2 k3 v3)
(make-vec
(+ (* k1 (vec-i v1)) (* k2 (vec-i v2)) (* k3 (vec-i v3)))
(+ (* k1 (vec-j v1)) (* k2 (vec-j v2)) (* k3 (vec-j v3)))
(+ (* k1 (vec-k v1)) (* k2 (vec-k v2)) (* k3 (vec-k v3)))))
(let* ([pos (<camera> position camera)]
[up (make-vec 0 1 0)]
[dir (vec-sub (<camera> target camera) pos)]
[u (vec-normalize (vec-cross dir up))]
[v (vec-normalize (vec-cross u dir))])
(match (<camera> type camera)
[perspective
(lambda (x y)
(<ray> make
[origin pos]
[direction (vlincomb3 1 dir (xt x) u (yt y) v)]))]
[orthographic
(lambda (x y)
(<ray> make
[origin (vlincomb3 1 pos (xt x) u (yt y) v)]
[direction dir]))])))
(define (make-exposure display)
(define gain (<display> gain display))
(define gamma (<display> gamma display))
(if (and (= gain 1) (= gamma 1))
(lambda (c) c)
(let ([g (/ 1 gamma)])
(lambda (c)
(make-color
(expt (* (color-r c) gain) g)
(expt (* (color-g c) gain) g)
(expt (* (color-b c) gain) g))))))
(define (image-simple camera display scene)
(define shoot-ray (ray-gun camera))
(define (f x y)
(let ([ray (shoot-ray x y)])
(parameterize ([$E (<ray> origin ray)])
(pixel-color-from-ray scene ray 1 MAXDEPTH))))
(define antialias (make-antialias
(<display> x-samples display)
(<display> y-samples display)
(<display> filter display)
(<display> x-width display)
(<display> y-width display)
f))
(define exposure (make-exposure display))
(let ([width (<camera> output-width camera)]
[height (<camera> output-height camera)])
(parameterize ([$camera camera]
[$display display]
[$scene scene])
(make-image width height 0 0
(lambda (set-pixel)
(do ([y 0 (+ y 1)]) ((= y height))
(do ([x 0 (+ x 1)]) ((= x width))
(set-pixel x y (exposure (antialias x y))))))))))
(define (render f filename camera display scene)
(let ([image (time (f camera display scene))])
(write-tga image filename)))
;; Scene Syntax
(define-defaults sphere ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[radius 1]
[M (matrix-identity 3)])
(let ([M (matrix-mul (scale radius radius radius) M)])
(make-sphere color opacity surface volume displacement
center M (matrix-inverse M))))
(define-defaults plane ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)])
(make-plane color opacity surface volume displacement
center M (matrix-inverse M)))
(define-defaults tetrahedron ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)])
(make-polyhedron color opacity surface volume displacement
center M (matrix-inverse M)
(list
(make-vec -1 -1 -1)
(make-vec -1 1 1)
(make-vec 1 1 -1)
(make-vec 1 -1 1))))
(define cube-planes
(list
(make-vec 0 0 1) (make-vec 0 0 -1)
(make-vec 0 1 0) (make-vec 0 -1 0)
(make-vec 1 0 0) (make-vec -1 0 0)))
(define (cube? object)
(and (polyhedron? object)
(eq? (polyhedron-planes object) cube-planes)))
(define-defaults cube ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)])
(make-polyhedron color opacity surface volume displacement
center M (matrix-inverse M)
cube-planes))
(define-defaults octahedron ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)])
(make-polyhedron color opacity surface volume displacement
center M (matrix-inverse M)
(list
(make-vec 1 1 1)
(make-vec -1 1 1)
(make-vec -1 -1 1)
(make-vec 1 -1 1)
(make-vec 1 1 -1)
(make-vec -1 1 -1)
(make-vec -1 -1 -1)
(make-vec 1 -1 -1))))
(define-defaults icosahedron ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)])
(make-polyhedron color opacity surface volume displacement
center M (matrix-inverse M)
(let* ([t (/ (- (sqrt 5) 1) 2)]
[nt (- t)]
[st (* t t)]
[nst (- st)])
(list
(make-vec t t t)
(make-vec 1 0 st)
(make-vec t nt t)
(make-vec 0 nst 1)
(make-vec 0 st 1)
(make-vec 1 0 nst)
(make-vec t t nt)
(make-vec st 1 0)
(make-vec nst 1 0)
(make-vec nt t nt)
(make-vec 0 st -1)
(make-vec t nt nt)
(make-vec st -1 0)
(make-vec nst -1 0)
(make-vec -1 0 st)
(make-vec nt t t)
(make-vec nt nt nt)
(make-vec 0 nst -1)
(make-vec nt nt t)
(make-vec -1 0 nst)))))
(define-defaults quadric ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)]
[coefficients #f])
(unless (vector? coefficients)
(errorf 'quadric "coefficients are not a vector: ~s" coefficients))
(unless (= (vector-length coefficients) 10)
(errorf 'quadric "incorrect number of coefficients: ~s" coefficients))
(make-quadric color opacity surface volume displacement
center M (matrix-inverse M) coefficients))
(define-defaults torus ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[radius 1]
[radius2 1/3]
[M (matrix-identity 3)])
(let ([M (matrix-mul (scale radius radius radius) M)])
(make-torus color opacity surface volume displacement
center M (matrix-inverse M) radius2)))
(define-defaults union ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)]
[A #f]
[B #f])
(make-csg-union color opacity surface volume displacement
center M (matrix-inverse M) A B))
(define-defaults intersect ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)]
[A #f]
[B #f])
(make-csg-intersect color opacity surface volume displacement
center M (matrix-inverse M) A B))
(define-defaults difference ([color white]
[opacity opaque]
[surface #f] [volume #f] [displacement #f]
[center (make-vec 0 0 0)]
[M (matrix-identity 3)]
[A #f]
[B #f])
(make-csg-difference color opacity surface volume displacement
center M (matrix-inverse M) A B))