-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathgeometry-shapes.lisp
334 lines (301 loc) · 14.6 KB
/
geometry-shapes.lisp
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
(in-package #:org.shirakumo.fraf.trial)
;; FIXME: Allow specifying which attributes to include
;; FIXME: Generate normals
;; FIXME: Generate UVs
;; FIXME: Add shapes:
;; - Box
;; - Torus
;; - Arrow
;; FIXME: Normalise interface (vectors instead of x/y/z args?)
;; NOTE: UV convention is U is pointing RIGHT, V is pointing UP
;; NOTE: Should generate triangle vertices in CCW order
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun make-rectangle-mesh (w h &key (align :center) (normal +vz+) (x 0) (y 0) (z 0) (u- 0) (v- 0) (u+ 1) (v+ 1))
(let (l r u b)
(ecase align
(:center (setf l (- (/ w 2)) r (+ (/ w 2))
b (- (/ h 2)) u (+ (/ h 2))))
(:topleft (setf l 0 r w
b (- h) u 0))
(:topcenter (setf l (- (/ w 2)) r (+ (/ w 2))
b (- h) u 0))
(:bottomleft (setf l 0 r w
b 0 u h)))
(check-type normal vec3)
(let ((quat (qtowards +vz+ normal))
(x (float x 0f0))
(y (float y 0f0))
(z (float z 0f0))
(vec (vec3)))
(with-mesh-construction (v :attributes (location normal uv))
(macrolet ((vv (x y u v)
`(progn
(vsetf vec ,x ,y 0f0)
(!q* vec quat vec)
(v (+ x (vx vec)) (+ y (vy vec)) (+ z (vz vec)) (vx normal) (vy normal) (vz normal) ,u ,v))))
(vv l b u- v-)
(vv r b u+ v-)
(vv r u u+ v+)
(vv r u u+ v+)
(vv l u u- v+)
(vv l b u- v-))
(finalize-data)))))
(defun make-triangle-mesh (w h &key (orientation :right) (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location normal uv))
(let ((l (- x (/ w 2)))
(r (+ x (/ w 2)))
(u (+ y (/ h 2)))
(b (- y (/ h 2))))
(ecase orientation
(:up
(v l b z 0 0 1 0 0)
(v r b z 0 0 1 1 0)
(v x u z 0 0 1 0.5 1))
(:right
(v l u z 0 0 1 0 1)
(v l b z 0 0 1 0 0)
(v r y z 0 0 1 1 0.5))))
(finalize-data)))
(defun make-cube-mesh (size &key (x 0) (y 0) (z 0))
(destructuring-bind (w h d) (enlist size size size)
(let ((w (/ w 2)) (d (/ d 2)) (h (/ h 2)))
(with-mesh-construction (v :attributes (location normal uv))
(v (+ x w) (+ y h) (- z d) 0 1 0 1.0 0.0)
(v (- x w) (+ y h) (- z d) 0 1 0 0.0 0.0)
(v (- x w) (+ y h) (+ z d) 0 1 0 0.0 1.0)
(v (- x w) (+ y h) (+ z d) 0 1 0 0.0 1.0)
(v (+ x w) (+ y h) (+ z d) 0 1 0 1.0 1.0)
(v (+ x w) (+ y h) (- z d) 0 1 0 1.0 0.0)
(v (+ x w) (- y h) (+ z d) 0 -1 0 1.0 0.0)
(v (- x w) (- y h) (+ z d) 0 -1 0 0.0 0.0)
(v (- x w) (- y h) (- z d) 0 -1 0 0.0 1.0)
(v (- x w) (- y h) (- z d) 0 -1 0 0.0 1.0)
(v (+ x w) (- y h) (- z d) 0 -1 0 1.0 1.0)
(v (+ x w) (- y h) (+ z d) 0 -1 0 1.0 0.0)
(v (+ x w) (+ y h) (+ z d) 0 0 1 1.0 0.0)
(v (- x w) (+ y h) (+ z d) 0 0 1 0.0 0.0)
(v (- x w) (- y h) (+ z d) 0 0 1 0.0 1.0)
(v (- x w) (- y h) (+ z d) 0 0 1 0.0 1.0)
(v (+ x w) (- y h) (+ z d) 0 0 1 1.0 1.0)
(v (+ x w) (+ y h) (+ z d) 0 0 1 1.0 0.0)
(v (+ x w) (- y h) (- z d) 0 0 -1 1.0 0.0)
(v (- x w) (- y h) (- z d) 0 0 -1 0.0 0.0)
(v (- x w) (+ y h) (- z d) 0 0 -1 0.0 1.0)
(v (- x w) (+ y h) (- z d) 0 0 -1 0.0 1.0)
(v (+ x w) (+ y h) (- z d) 0 0 -1 1.0 1.0)
(v (+ x w) (- y h) (- z d) 0 0 -1 1.0 0.0)
(v (- x w) (+ y h) (+ z d) -1 0 0 1.0 0.0)
(v (- x w) (+ y h) (- z d) -1 0 0 0.0 0.0)
(v (- x w) (- y h) (- z d) -1 0 0 0.0 1.0)
(v (- x w) (- y h) (- z d) -1 0 0 0.0 1.0)
(v (- x w) (- y h) (+ z d) -1 0 0 1.0 1.0)
(v (- x w) (+ y h) (+ z d) -1 0 0 1.0 0.0)
(v (+ x w) (+ y h) (- z d) 1 0 0 1.0 0.0)
(v (+ x w) (+ y h) (+ z d) 1 0 0 0.0 0.0)
(v (+ x w) (- y h) (+ z d) 1 0 0 0.0 1.0)
(v (+ x w) (- y h) (+ z d) 1 0 0 0.0 1.0)
(v (+ x w) (- y h) (- z d) 1 0 0 1.0 1.0)
(v (+ x w) (+ y h) (- z d) 1 0 0 1.0 0.0)
(finalize-data)))))
(defun make-quad-grid-mesh (size x-count z-count &key (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location normal uv) :deduplicate NIL)
(loop for xi from 0 below x-count
for xc from (* x-count size -0.5) by size
do (loop for zi from 0 below z-count
for zc from (* z-count size -0.5) by size
do (let ((l (+ x xc)) (r (+ x xc size))
(u (+ z zc)) (b (+ z zc size)))
(v l y b 0 1 0 (/ (+ 0 xi) x-count) (/ (+ 0 zi) z-count))
(v r y b 0 1 0 (/ (+ 1 xi) x-count) (/ (+ 0 zi) z-count))
(v r y u 0 1 0 (/ (+ 1 xi) x-count) (/ (+ 1 zi) z-count))
(v r y u 0 1 0 (/ (+ 1 xi) x-count) (/ (+ 1 zi) z-count))
(v l y u 0 1 0 (/ (+ 0 xi) x-count) (/ (+ 1 zi) z-count))
(v l y b 0 1 0 (/ (+ 0 xi) x-count) (/ (+ 0 zi) z-count)))))
(finalize-data)))
(defun make-line-grid-mesh (size w h &key (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location))
(let ((w (/ w 2)) (h (/ h 2))
(ws (/ w size)) (hs (/ h size)))
(loop for _x from (- w) to w by ws
do (v (+ x _x) y (- z h))
(v (+ x _x) y (+ z h)))
(loop for _z from (- h) to h by hs
do (v (- x w) y (+ z _z))
(v (+ x w) y (+ z _z))))
(finalize-data :vertex-form :lines)))
(defun make-sphere-mesh (size &key (segments 24) (x 0) (y 0) (z 0))
(let ((lat segments) (lng segments)
(off (vec x y z)))
(with-mesh-construction (v :attributes (location normal uv))
(loop for i from lat downto 1
for lat0 = (* PI (- (/ (1- i) lat) 0.5))
for lat1 = (* PI (- (/ i lat) 0.5))
for z0 = (sin lat0)
for zr0 = (cos lat0)
for z1 = (sin lat1)
for zr1 = (cos lat1)
do (loop for j from lng downto 0
for l1 = (* 2 PI (/ (- j 1) lng)) for l2 = (* 2 PI (/ (- j 2) lng))
for x1 = (cos l1) for x2 = (cos l2)
for y1 = (sin l1) for y2 = (sin l2)
do (flet ((vertex (position uv)
(let ((p (v+ position off))
(n (vunit position))
(u (nv* (nv+ uv 1.0) 0.5)))
(v (vx p) (vy p) (vz p) (vx n) (vy n) (vz n) (vx u) (vy u)))))
(vertex (vec (* x1 zr0 size) (* y1 zr0 size) (* z0 size)) (vec x1 y1))
(vertex (vec (* x1 zr1 size) (* y1 zr1 size) (* z1 size)) (vec x1 y1))
(vertex (vec (* x2 zr0 size) (* y2 zr0 size) (* z0 size)) (vec x2 y2))
(vertex (vec (* x2 zr0 size) (* y2 zr0 size) (* z0 size)) (vec x2 y2))
(vertex (vec (* x1 zr1 size) (* y1 zr1 size) (* z1 size)) (vec x1 y1))
(vertex (vec (* x2 zr1 size) (* y2 zr1 size) (* z1 size)) (vec x2 y2)))))
(finalize-data))))
(defun make-disc-mesh (size &key (segments 32) (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location normal uv))
(loop with step = (/ (* 2 PI) segments)
for i1 = (- step) then i2
for i2 from 0 to (* 2 PI) by step
do (v x y z 0 0 1 0.5 0.5)
(v (+ x (* size (cos i1))) (+ y (* size (sin i1))) z 0 0 1 (+ 0.5 (* 0.5 (cos i1))) (+ 0.5 (* 0.5 (sin i1))))
(v (+ x (* size (cos i2))) (+ y (* size (sin i2))) z 0 0 1 (+ 0.5 (* 0.5 (cos i2))) (+ 0.5 (* 0.5 (sin i2)))))
(finalize-data)))
(defun make-cylinder-mesh (size height &key (segments 32) (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location))
(loop with step = (/ (* 2 PI) segments)
for i1 = (- step) then i2
for i2 from 0 to (* 2 PI) by step
for e1b = (vec (+ x (* size (cos i1))) y (+ z (* size (sin i1))))
for e2b = (vec (+ x (* size (cos i2))) y (+ z (* size (sin i2))))
for e1t = (nv+ (vec 0 height 0) e1b)
for e2t = (nv+ (vec 0 height 0) e2b)
do ;; Bottom disc
(v x y z)
(v (vx e2b) (vy e2b) (vz e2b))
(v (vx e1b) (vy e1b) (vz e1b))
;; Top Disc
(v x (+ height y) z)
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx e2t) (vy e2t) (vz e2t))
;; Wall
(v (vx e2b) (vy e2b) (vz e2b))
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx e1b) (vy e1b) (vz e1b))
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx e2b) (vy e2b) (vz e2b))
(v (vx e2t) (vy e2t) (vz e2t)))
(finalize-data)))
(defun make-cone-mesh (size height &key (segments 32) (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location))
(loop with step = (/ (* 2 PI) segments)
for i1 = (- step) then i2
for i2 from 0 to (* 2 PI) by step
do ;; Cone top
(v x (+ y height) z)
(v (+ x (* size (cos i1))) y (+ z (* size (sin i1))))
(v (+ x (* size (cos i2))) y (+ z (* size (sin i2))))
;; Bottom disc
(v x y z)
(v (+ x (* size (cos i2))) y (+ z (* size (sin i2))))
(v (+ x (* size (cos i1))) y (+ z (* size (sin i1)))))
(finalize-data)))
(defun make-tube-mesh (size height inner-size &key (segments 32) (x 0) (y 0) (z 0))
(with-mesh-construction (v :attributes (location))
(loop with step = (/ (* 2 PI) segments)
for i1 = (- step) then i2
for i2 from 0 to (* 2 PI) by step
for e1b = (vec (+ x (* size (cos i1))) y (+ z (* size (sin i1))))
for e2b = (vec (+ x (* size (cos i2))) y (+ z (* size (sin i2))))
for e1t = (nv+ (vec 0 height 0) e1b)
for e2t = (nv+ (vec 0 height 0) e2b)
for f1b = (vec (+ x (* inner-size (cos i1))) y (+ z (* inner-size (sin i1))))
for f2b = (vec (+ x (* inner-size (cos i2))) y (+ z (* inner-size (sin i2))))
for f1t = (nv+ (vec 0 height 0) f1b)
for f2t = (nv+ (vec 0 height 0) f2b)
do ;; Bottom ring
(v (vx f1b) (vy f1b) (vz f1b))
(v (vx e1b) (vy e1b) (vz e1b))
(v (vx e2b) (vy e2b) (vz e2b))
(v (vx e2b) (vy e2b) (vz e2b))
(v (vx f2b) (vy f2b) (vz f2b))
(v (vx f1b) (vy f1b) (vz f1b))
;; Top ring
(v (vx f2t) (vy f2t) (vz f2t))
(v (vx e2t) (vy e2t) (vz e2t))
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx f1t) (vy f1t) (vz f1t))
(v (vx f2t) (vy f2t) (vz f2t))
;; Outer wall
(v (vx e2b) (vy e2b) (vz e2b))
(v (vx e1b) (vy e1b) (vz e1b))
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx e1t) (vy e1t) (vz e1t))
(v (vx e2t) (vy e2t) (vz e2t))
(v (vx e2b) (vy e2b) (vz e2b))
;; Inner wall
(v (vx f2b) (vy f2b) (vz f2b))
(v (vx f1t) (vy f1t) (vz f1t))
(v (vx f1b) (vy f1b) (vz f1b))
(v (vx f1t) (vy f1t) (vz f1t))
(v (vx f2b) (vy f2b) (vz f2b))
(v (vx f2t) (vy f2t) (vz f2t)))
(finalize-data)))
(defun make-lines (points &key (default-color (vec 0 0 0 1)))
(with-mesh-construction (v :attributes (location normal color) :deduplicate NIL)
(loop for (a b) on points by #'cddr
while b
do (destructuring-bind (a ac) (enlist a default-color)
(destructuring-bind (b bc) (enlist b default-color)
(let ((a-b (v- a b))
(b-a (v- b a)))
(v (vx a) (vy a) (vz a) (vx a-b) (vy a-b) (vz a-b) (vx ac) (vy ac) (vz ac))
(v (vx b) (vy b) (vz b) (vx a-b) (vy a-b) (vz a-b) (vx bc) (vy bc) (vz bc))
(v (vx a) (vy a) (vz a) (vx b-a) (vy b-a) (vz b-a) (vx ac) (vy ac) (vz ac))
(v (vx b) (vy b) (vz b) (vx a-b) (vy a-b) (vz a-b) (vx bc) (vy bc) (vz bc))
(v (vx b) (vy b) (vz b) (vx b-a) (vy b-a) (vz b-a) (vx bc) (vy bc) (vz bc))
(v (vx a) (vy a) (vz a) (vx b-a) (vy b-a) (vz b-a) (vx ac) (vy ac) (vz ac))))))
(finalize-data :vertex-form :lines))))
(define-asset (trial fullscreen-square) mesh
(make-rectangle-mesh 2 2))
(define-asset (trial empty-vertex-array) mesh
(make-instance 'mesh-data :faces (make-array 0 :element-type '(unsigned-byte 16))))
(define-asset (trial point) mesh
(with-mesh-construction (v :attributes (location normal))
(v 0 0 0 0 1 0)
(finalize-data :vertex-form :points)))
(define-asset (trial unit-cube) mesh
(make-cube-mesh 1.0))
(define-asset (trial unit-sphere) mesh
(make-sphere-mesh 1.0))
(define-asset (trial unit-square) mesh
(make-rectangle-mesh 1 1))
(define-asset (trial unit-disc) mesh
(make-disc-mesh 1.0))
(define-asset (trial unit-cylinder) mesh
(make-cylinder-mesh 1.0 1.0))
(define-asset (trial unit-cone) mesh
(make-cone-mesh 1.0 1.0))
(define-asset (trial unit-tube) mesh
(make-tube-mesh 1.0 1.0 0.5))
(define-asset (trial unit-point) mesh
(make-triangle-mesh 0.0 0.0))
(define-asset (trial triangle) mesh
(with-mesh-construction (v :attributes (location normal uv color))
(v -0.5 -0.5 0.0 0 0 -1 0.0 0.0 1 0 0 1)
(v +0.5 -0.5 0.0 0 0 -1 0.1 0.0 0 1 0 1)
(v +0.0 +0.5 0.0 0 0 -1 0.5 0.1 0 0 1 1)
(finalize-data)))
(define-asset (trial grid) mesh
(make-line-grid-mesh 10 10 10))
(define-asset (trial axes) mesh
(make-lines (list (list (vec 0 0 0) (vec 1 0 0 1)) (list (vec 10 0 0) (vec 1 0 0 1))
(list (vec 0 0 0) (vec 0 1 0 1)) (list (vec 0 10 0) (vec 0 1 0 1))
(list (vec 0 0 0) (vec 0 0 1 1)) (list (vec 0 0 10) (vec 0 0 1 1)))))
(define-asset (trial 2d-axes) mesh
(with-mesh-construction (v :attributes (location))
;; KLUDGE: for whatever reason using most-positive/negative-single-float does not work.
(v 0 -1000000 0)
(v 0 +1000000 0)
(v -1000000 0 0)
(v +1000000 0 0)
(finalize-data :vertex-form :lines)))