-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathshapes.lisp
677 lines (573 loc) · 18.2 KB
/
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
;;;; shapes.lisp
;;;; Please see the licence.txt for the CLinch
;;
(in-package #:clinch)
(defun make-quad-indices ()
(make-instance 'clinch:index-buffer :data '(0 1 2 0 2 3)))
(defun make-quad-vertexes (width height &key (center :center))
(let* ((x/2 (float (/ width 2)))
(y/2 (float (/ height 2)))
(x)
(y)
(-x)
(-y))
(if (typep center 'SIMPLE-ARRAY)
(let ((x-offset (aref center 0))
(y-offset (aref center 1)))
(setf x (+ x/2 x-offset)
-x (+ (- x/2) x-offset)
y (+ y/2 y-offset)
-y (+ (- y/2) y-offset)))
(case center
(:center (setf x x/2
-x (- x/2)
y y/2
-y (- y/2)))
(:top-center (setf x x/2
-x (- x/2)
y 0.0
-y (- height)))
(:bottom-center (setf x x/2
-x (- x/2)
y height
-y 0.0))
(:center-left (setf x width
-x 0.0
y y/2
-y (- y/2)))
(:center-right (setf x 0.0
-x (- width)
y y/2
-y (- y/2)))
(:top-left (setf x width
-x 0.0
y 0.0
-y (- height)))
(:bottom-left (setf x width
-x 0.0
y height
-y 0.0))
(:top-right (setf x 0.0
-x (- width)
y 0.0
-y (- height)))
(:bottom-right (setf x 0.0
-x (- width)
y height
-y 0.0))))
(make-instance 'clinch:buffer
:Stride 3
:data (map 'list (lambda (x)
(coerce x 'single-float))
(list -x y 0
-x -y 0
x -y 0
x y 0)))))
(defun make-quad-texture-coordinates ()
(make-instance 'clinch:buffer
:Stride 2
:data (map 'list (lambda (x)
(coerce x 'single-float))
'(0.0 1.0
0.0 0.0
1.0 0.0
1.0 1.0))))
(defun make-quad (width height &key (center :center) shader-program texture (parent *node*))
"Creates a quad entity of width and height."
(make-instance 'clinch:entity
:parent parent
:shader-program (or shader-program (get-generic-single-texture-shader))
:indexes (make-quad-indices)
:attributes (copy-list `(("v" . ,(make-quad-vertexes width height :center center))
("tc1" . ,(make-quad-texture-coordinates))))
:uniforms (copy-list `(("M" . :model)
("P" . :projection)
("t1" . ,(or texture (get-identity-texture)))))))
(defmethod make-quad-for-texture ((this texture) &key width height (center :center) shader-program parent no-parent)
"Creates a quad for a texture which defaults to texture's width and height."
(make-quad (or width (width this))
(or height (height this))
:center center
:shader-program shader-program
:texture this
:parent parent))
(defmethod make-quad-and-texture (width height &key (center :center) shader-program (parent *node*) no-parent)
(make-quad-for-texture
(make-instance 'texture :width width :height height)
:center center :shader-program shader-program :parent parent :no-parent no-parent))
(defun get-default-texture ()
(unless *texture*
(setf *texture*
(make-instance 'texture
:width (width *viewport*)
:height (height *viewport*))))
(unless *entity*
(setf *entity*
(make-quad-for-texture *texture* :parent nil)))
*texture*)
(defun make-sphere ()
(multiple-value-bind (verts indices normals) (get-sphere-vertex-normal-and-index-buffers)
(make-instance 'clinch:entity
:parent nil
:shader-program (get-generic-single-diffuse-light-shader)
:indexes indices
:attributes (copy-list `(("v" . ,verts)
("n" . ,normals)
("c" . (1.0 1.0 1.0 1.0))
("tc1" . (0 0))))
:uniforms (copy-list `(("M" . :model)
("P" . :projection)
("N" . :normal)
("t1" . ,(get-identity-texture))
("ambientLight" . (.2 .2 .2))
("lightDirection" . (0.5772705 -0.5772705 -0.5772705))
("lightIntensity" . (.8 .8 .8)))))))
;; Interesting note: the normals are the same as the vertices.
(defun get-sphere-vertex-normal-and-index-buffers ()
(let* ((ICX 0.525731112119133606)
(ICZ 0.850650808352039932)
(v (make-instance 'buffer :data
(map 'list (lambda (x)
(coerce x 'single-float))
`( ,(- ICX) 0 ,ICZ
,ICX 0 ,ICZ
,(- ICX) 0 ,(- ICZ)
,ICX 0 ,(- ICZ)
0 ,ICZ ,ICX
0 ,ICZ ,(- ICX)
0 ,(- ICZ) ,ICX
0 ,(- ICZ) ,(- ICX)
,ICZ ,ICX 0
,(- ICZ) ,ICX 0
,ICZ ,(- ICX) 0
,(- ICZ) ,(- ICX) 0)))))
(values v
(make-instance 'index-buffer
:data '(0 1 4
0 4 9
9 4 5
4 8 5
4 1 8
8 1 10
8 10 3
5 8 3
5 3 2
2 3 7
7 3 10
7 10 6
7 6 11
11 6 0
0 6 1
6 10 1
9 11 0
9 2 11
9 5 2
7 11 2))
v)))
(defun make-box ()
(multiple-value-bind (verts indices) (get-box-vertex-normal-texture-and-index-buffers)
(make-instance 'clinch:entity
:parent nil
:shader-program (get-generic-single-color-shader)
:indexes indices
:attributes (copy-list `(("v" . ,verts)))
:uniforms (copy-list `(("M" . :model)
("P" . :projection)
("color" . (1 1 1 1)))))))
(defun get-box-vertex-normal-texture-and-index-buffers ()
(! (values
(make-instance 'clinch:buffer
:Stride 3
:data '(-0.5 -0.5 0.5
0.5 -0.5 0.5
-0.5 0.5 0.5
0.5 0.5 0.5
-0.5 -0.5 -0.5
0.5 -0.5 -0.5
-0.5 -0.5 0.5
0.5 -0.5 0.5
-0.5 0.5 -0.5
0.5 0.5 -0.5
-0.5 -0.5 -0.5
0.5 -0.5 -0.5
-0.5 0.5 0.5
0.5 0.5 0.5
-0.5 0.5 -0.5
0.5 0.5 -0.5
0.5 -0.5 0.5
0.5 -0.5 -0.5
0.5 0.5 0.5
0.5 0.5 -0.5
-0.5 -0.5 -0.5
-0.5 -0.5 0.5
-0.5 0.5 -0.5
-0.5 0.5 0.5))
(make-instance 'clinch:index-buffer
:data '(0 1 2
2 1 3
4 5 6
6 5 7
8 9 10
10 9 11
12 13 14
14 13 15
16 17 18
18 17 19
20 21 22
22 21 23))
(make-instance 'clinch:buffer
:data '(0.0 0.0 1.0
0.0 0.0 1.0
0.0 0.0 1.0
0.0 0.0 1.0
0.0 -1.0 0.0
0.0 -1.0 0.0
0.0 -1.0 0.0
0.0 -1.0 0.0
0.0 0.0 -1.0
0.0 0.0 -1.0
0.0 0.0 -1.0
0.0 0.0 -1.0
0.0 1.0 0.0
0.0 1.0 0.0
0.0 1.0 0.0
0.0 1.0 0.0
1.0 0.0 0.0
1.0 0.0 0.0
1.0 0.0 0.0
1.0 0.0 0.0
-1.0 0.0 0.0
-1.0 0.0 0.0
-1.0 0.0 0.0
-1.0 0.0 0.0))
(make-instance 'clinch:buffer
:Stride 2
:data '(0.0 1.0
1.0 1.0
0.0 0.0
1.0 0.0
0.0 1.0
1.0 1.0
0.0 0.0
1.0 0.0
0.0 1.0
1.0 1.0
0.0 0.0
1.0 0.0
0.0 1.0
1.0 1.0
0.0 0.0
1.0 0.0
0.0 1.0
1.0 1.0
0.0 0.0
1.0 0.0
0.0 1.0
1.0 1.0
0.0 0.0
1.0 0.0)))))
(defun make-cylinder (&optional (sides 10))
(multiple-value-bind (verts indices) (get-cylinder-vertex-normal-and-index-buffers sides)
(make-instance 'clinch:entity
:parent nil
:shader-program (get-generic-single-color-shader)
:indexes indices
:attributes (copy-list `(("v" . ,verts)))
:uniforms (copy-list `(("M" . :model)
("P" . :projection)
("color" . (1 1 1 1)))))))
(defun get-cylinder-vertex-normal-and-index-buffers (sides)
(multiple-value-bind (v n)
(loop
for i from 0 below (* 2 pi) by (/ (* 2 pi) sides)
for x = (cos i)
for y = (sin i)
append (list x y 0.5
x y -0.5
x y 0.5
x y -0.5) into verts
append (list x y 0.0
0.0 0.0 1.0
x y 0.0
x y -1.0) into norms
finally (return (values verts norms)))
(setf v (append v '(0.0 0.0 0.5 0.0 0.0 -0.5)))
(setf n (append n '(0.0 0.0 0.5 0.0 0.0 -0.5)))
(let* ((len (/ (length v) 3)))
(values (make-instance 'buffer :data (map 'list (lambda (x) (coerce x 'float)) v))
(make-instance 'index-buffer
:data (loop for x from 0 below sides
append (let ((offset (* x 4)))
(map 'list (lambda (y)
(if (< y 0)
(+ len y)
(mod (+ offset y) (- len 2))))
`(0 1 4 1 5 4 ; side
5 1 -1
0 4 -2)))))
(make-instance 'buffer :data (map 'list (lambda (x) (coerce x 'float)) n))))))
(defun random-heightfield (w h)
(make-array (* w h 3)
:element-type 'single-float
:initial-contents
(map 'list (lambda (x)
(coerce x 'single-float))
(loop for i from 0 below w
append (loop for j from 0 below h
append (list j
(/ (random 1000) 1000)
i))))))
(defun heightmap->buffers (arr w h)
(let ((tx (/ (1- w)))
(ty (/ (1- h)))
(points)
(normals)
(tex-coords))
(labels ((get-point (x)
(let ((start (* 3 x)))
(subseq arr start (+ 3 start))))
(make-triangle (a b c)
(let ((n (v3:normalize
(v3:cross (v3:- b a) (v3:- c a)))))
(values (concatenate 'vector a b c)
(concatenate 'vector n n n))))
(add-triangle (a)
(multiple-value-bind (p n)
(apply #'make-triangle
(map 'list #'get-point a))
(push p points)
(push n normals)))
(add-tc (x y)
(push (v! (* tx x) (* ty y)) tex-coords)
(push (v! (* tx (1+ x)) (* ty y)) tex-coords)
(push (v! (* tx x) (* ty (1+ y))) tex-coords)
(push (v! (* tx (1+ x)) (* ty y)) tex-coords)
(push (v! (* tx (1+ x)) (* ty (1+ y))) tex-coords)
(push (v! (* tx x) (* ty (1+ y))) tex-coords)))
(loop for i from 0 below (1- w)
do (loop for j from 0 below (1- h)
for a1 = (+ j (* w i))
for a2 = (1+ a1)
for b1 = (+ w a1)
for b2 = (1+ b1)
do (progn (add-triangle (list a1 a2 b1))
(add-triangle(list a2 b2 b1))
(add-tc i j))))
(values (make-instance 'buffer :data (apply #'concatenate 'vector (reverse points)))
(make-instance 'index-buffer
:data (loop for i from 0 below (* 3 2 w h)
collect i))
(make-instance 'buffer :data (apply #'concatenate 'vector (reverse normals)))
(make-instance 'buffer :stride 2 :data (apply #'concatenate 'vector (reverse tex-coords)))))))
(defun make-heightmap-entity (height-map w h &optional texture)
(multiple-value-bind (verts indices normals tex) (heightmap->buffers height-map w h)
(make-instance 'clinch:entity
:parent nil
:shader-program (get-generic-single-diffuse-light-shader)
:indexes indices
:attributes (copy-list `(("v" . ,verts)
("n" . ,normals)
("c" . (1.0 1.0 1.0 1.0))
("tc1" . ,tex))) ;;,(or texture-coordinate-buffer '(0 0))))
:uniforms (copy-list `(("M" . :model)
("P" . :projection)
("N" . :normal)
("t1" . ,(or texture (get-identity-texture)))
("ambientLight" . (.2 .2 .2))
("lightDirection" . (0.5772705 -0.5772705 -0.5772705))
("lightIntensity" . (.8 .8 .8)))))))
(defun make-dummy-entity (num-points triangles &key texture)
(! (let ((indexes (make-instance 'index-buffer :count (* triangles 3)))
(points (make-instance 'buffer :count (* num-points 3)))
(normals (make-instance 'buffer :count (* num-points 3)))
(tex-coords (make-instance 'buffer :count (* num-points 2))))
(make-instance 'clinch:entity
:parent nil
:shader-program (get-generic-single-diffuse-light-shader)
:indexes indexes
:attributes (copy-list `(("v" . ,points)
("n" . ,normals)
("c" . (1.0 1.0 1.0 1.0))
("tc1" . ,tex-coords)))
:uniforms (copy-list `(("M" . :model)
("P" . :projection)
("N" . :normal)
("t1" . ,(or texture (get-identity-texture)))
("ambientLight" . (.2 .2 .2))
("lightDirection" . (0.5772705 -0.5772705 -0.5772705))
("lightIntensity" . (.8 .8 .8))))))))
(defmethod delete-entity ((this entity) &key)
(map nil #'unload (attributes this))
;;(map nil #'unload (uniforms this))
(unload (indexes this))
(unload this)
nil)
(defun set-point (arr num x y z)
(setf (elt arr (+ num 0)) x)
(setf (elt arr (+ num 1)) y)
(setf (elt arr (+ num 2)) z))
(defmacro average-vectors (&rest v)
`(map 'list (lambda (x)
(/ x 4))
(map 'list #'+
,@v)))
(defun get-point-or-vert (arr x y w h)
(cond ((< x 0) nil)
((>= x w) nil)
((< y 0) nil)
((>= y h) nil)
(t (let ((pos (* 3 (+ x (* y w)))))
(v! (elt arr (+ pos 0))
(elt arr (+ pos 1))
(elt arr (+ pos 2)))))))
(defun get-middle-square-and-normal (arr x y w h)
(let* ((bl (get-point-or-vert arr x y w h))
(br (get-point-or-vert arr (1+ x) y w h))
(tl (get-point-or-vert arr x (1+ y) w h))
(tr (get-point-or-vert arr (1+ x) (1+ y) w h))
(center (v3:/s (v3:+ (v3:+ (v3:+ bl br) tl) tr)
4.0))
(bl- (v3:- bl center))
(br- (v3:- br center))
(tl- (v3:- tl center))
(tr- (v3:- tr center)))
(values
(v3:normalize
(reduce #'v3:+
(list (v3:cross br- bl-)
(v3:cross bl- tl-)
(v3:cross tl- tr-)
(v3:cross tr- br-))))
center)))
(defun get-middle-squares-and-normals (arr w h)
(let ((tmp (loop for y from 0 below (1- h)
append (loop for x from 0 below (1- w)
collect (multiple-value-bind (a b)
(get-middle-square-and-normal arr x y w h)
(list a b (list (/ (+ .5 x) (1- w))
(/ (+ .5 y) (1- h)))))))))
(values (map 'list #'second tmp) ;; vertexes
(map 'list #'first tmp) ;; normals
(map 'list #'third tmp))));; tex-coords
(defun get-height-node-normal (arr x y w h)
(let ((c (get-point-or-vert arr x y w h)))
(labels ((get-vec-or-none (x y)
(let ((p (get-point-or-vert arr x y w h)))
(when p
(v3:- p c))))
(cross-x (a b)
(if (and a b)
(v3:cross a b)
(v! 0 0 0))))
(let ((l (get-vec-or-none (1- x) y))
(r (get-vec-or-none (1+ x) y))
(b (get-vec-or-none x (1- y)))
(top (get-vec-or-none x (1+ y))))
(v3:normalize
(reduce #'v3:+ (list
(cross-x l top)
(cross-x b l)
(cross-x r b)
(cross-x top r))))))))
(defun get-height-node-normals (arr w h)
(loop for y from 0 below h
append (loop for x from 0 below w
collect (get-height-node-normal arr x y w h))))
(defun tst (e arr w h &key texture)
(let* ((squares (* (1- w) (1- h)))
(triangles (* squares 4))
(num-points (* triangles 3))
(indexes)
(points)
(normals)
(tex-coords))
;; So processing can be done in another thread...
(!
;;(setf e (make-dummy-entity num-points triangles))
(setf indexes (pullg (indexes e))
points (pullg (attribute e "v"))
normals (pullg (attribute e "n"))
tex-coords (pullg (attribute e "tc1"))))
;; Now create the data...
(loop for x from 0 below (length arr)
do (setf (elt points (* x 3))
(elt arr x)))))
;; :attributes `(("v" . ,verts)
;; ("n" . ,normals))
;; :uniforms `(("M" . :model)
;; ("P" . :projection)
;; ("color" . (1 1 1 1))))))
;; (make-instance 'clinch:entity
;; :parent parent
;; :shader-program (cond (shader-program shader-program)
;; (bones (get-generic-single-diffuse-light-animation-shader))
;; (t (get-generic-single-diffuse-light-shader)))
;; :indexes index-buffer
;; :attributes `(("v" . ,vertex-buffer)
;; ("n" . ,normal-buffer)
;; ("c" . ,(or vertex-color-buffer '(1.0 1.0 1.0 1.0)))
;; ("tc1" . ,(or texture-coordinate-buffer '(0 0))))
;; :uniforms `(("M" . :model)
;; ("P" . :projection)
;; ("N" . :normal)
;; ("t1" . ,(or texture (get-identity-texture)))
;; ("ambientLight" . (.2 .2 .2))
;; ("lightDirection" . (0.5772705 0.5772705 -0.5772705))
;; ("lightIntensity" . (.8 .8 .8))))))
;; (setf diamond '(#(0.0 1.0 0.0) #(-0.70710677 0.0 0.70710677) #(0.70710677 0.0 0.70710677)
;; #(0.0 1.0 0.0) #(0.9659259 0.0 0.25881892) #(0.25881892 0.0 -0.9659259)
;; #(0.0 1.0 0.0) #(-0.25881928 0.0 -0.9659258) #(-0.9659258 0.0 0.25881928)
;; #(0.0 -1.0 0.0) #(-0.70710677 0.0 0.70710677) #(0.70710677 0.0 0.70710677)
;; #(0.0 -1.0 0.0) #(0.9659259 0.0 0.25881892) #(0.25881892 0.0 -0.9659259)
;; #(0.0 -1.0 0.0) #(-0.25881928 0.0 -0.9659258) #(-0.9659258 0.0 0.25881928)))
;; (let ((i '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)))
;; (make-array (length i) :initial-contents i))
;; (defun subdivide (vectors indexes)
;; (let ((midpoint (midpoint vectors)))
;; (values (list (nth 0 vectors)
;; (nth 1 vectors)
;; (nth 2 vectors)
;; midpoint)
;; '((0 1 3) (1 2 3) (2 0 3)))))
;; ;; returns indexes, vertexes, normals, and texcoords in values
;; (defun make-sphere (radius rings sectors)
;; (let ((vertices (make-array (* rings sectors 3)))
;; (normals (make-array (* rings sectors 3)))
;; (texcoords (make-array (* rings sectors 2)))
;; (indices (make-array (* rings sectors 6)))
;; (slice-r (/ (1- rings)))
;; (slice-s (/ (1- sectors))))
;; (loop for r from 0 to (1- rings)
;; do (loop for s from 0 to (1- sectors)
;; for y = (sin (+ (/ pi -2)
;; (* pi r slice-r)))
;; for x = (* (cos (* pi 2 s slice-s))
;; (sin (* pi r slice-r)))
;; for z = (* (sin (* 2 pi s slice-s))
;; (sin (* pi r slice-r)))
;; do (let* ((triangle (+ (* r sectors) s))
;; (3t (* 3 triangle))
;; (2t (* 2 triangle))
;; (6t (* 6 triangle)))
;; (when (and (< (abs x) .01)
;; (< (abs y) .01)
;; (< (abs z) .01))
;; (format t "~A ~A was all zeros (~A ~A ~A)~%" r s x y z))
;; (setf (aref vertices (+ 3t 0)) (* radius x)
;; (aref vertices (+ 3t 1)) (* radius y)
;; (aref vertices (+ 3t 2)) (* radius z)
;; (aref normals (+ 3t 0)) x
;; (aref normals (+ 3t 1)) y
;; (aref normals (+ 3t 2)) z
;; (aref texcoords (+ 2t 0)) (* s slice-s)
;; (aref texcoords (+ 2t 1)) (* r slice-r)
;; (aref indices (+ 6t 0)) (+ (* r sectors) s)
;; (aref indices (+ 6t 1)) (+ (* r sectors) s 1)
;; (aref indices (+ 6t 2)) (+ (* (1+ r) sectors) s 1)
;; (aref indices (+ 6t 3)) (+ (* r sectors) s)
;; (aref indices (+ 6t 4)) (+ (* (1+ r) sectors) s 1)
;; (aref indices (+ 6t 5)) (+ (* (1+ r) sectors) s)))))
;; (values (coerce indices 'list)
;; (coerce vertices 'list)
;; (coerce normals 'list)
;; (coerce texcoords 'list))))