forked from vydd/sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.lisp
1952 lines (1620 loc) · 62.8 KB
/
dump.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;----------------------------------------------------------------------
;package
;;;; package.lisp
(uiop:define-package #:sketch
(:use #:cl)
#+nil ;;FIXME
(:import-from :kit.sdl2
:mousebutton-event
:mousemotion-event
:mousewheel-event
:textinput-event
:keyboard-event
:other-event
:close-window)
(:export :sketch
:setup
:draw
:defsketch
:sketch-title
:sketch-width
:sketch-height
:sketch-fullscreen
:sketch-copy-pixels
:sketch-y-axis
:title
:width
:height
:fullscreen
:copy-pixels
:y-axis
;; Math
:clamp-1
:normalize
:+pi+
:+two-pi+
:+tau+
:+half-pi+
:+quarter-pi+
:+epsilon+
:+phi+
:+golden-ratio+
:+e+
:radians
:degrees
;; Utils
:relative-path
;; Colors
:color
:make-color
:color-red
:color-green
:color-blue
:color-hue
:color-saturation
:color-brightness
:color-alpha
:rgb-to-hsb
:hsb-to-rgb
:rgb
:hsb
:gray
:rgb-255
:hsb-360
:gray-255
:hex-to-color
:color-rgb
:color-rgba
:color-hsba
:color-vector
:color-vector-255
:lerp-color
:random-color
:hash-color
:color-filter-grayscale
:color-filter-invert
:color-filter-rotate
:color-filter-hsb
:+red+
:+green+
:+blue+
:+yellow+
:+magenta+
:+cyan+
:+orange+
:+white+
:+black+
;; Pen
:pen
:pen-stroke
:pen-fill
:pen-weight
:make-pen
:set-pen
:copy-pen
:flip-pen
:with-pen
:background
;; Shapes
:point
:line
:polyline
:rect
:ngon
:star
:ellipse
:circle
:polygon
:bezier
;; Transforms
:set-matrix
:push-matrix
:pop-matrix
:translate
:rotate
:scale
:with-matrix
:with-identity-matrix
:with-current-matrix
;; Channels
:register-input
:in
:out
:define-channel-observer
:define-named-channel-observer
:reset-all-channels
;; Figures
:deffigure
;; Resources
:load-resource
:image
;; Font
:make-font
:with-font
:set-font
:text
))
;;----------------------------------------------------------------------
;math
;;;; math.lisp
(in-package #:sketch)
;;; __ __ _ _____ _ _
;;; | \/ | / \|_ _| | | |
;;; | |\/| | / _ \ | | | |_| |
;;; | | | |/ ___ \| | | _ |
;;; |_| |_/_/ \_\_| |_| |_|
;; Calculation
(defun clamp-1 (x)
(alexandria:clamp x 0.0 1.0))
(defun normalize (x low high &key (clamp t) (out-low 0.0) (out-high 1.0))
(let ((low (min low high))
(high (max low high))
(min-out-low (min out-low out-high))
(min-out-high (max out-low out-high)))
(let ((norm (+ out-low
(* (- out-high out-low)
(/ (- x low) (- high low))))))
(if clamp (alexandria:clamp norm min-out-low min-out-high) norm))))
;; Trigonometry
(defconstant +pi+ PI)
(defconstant +two-pi+ (* PI 2))
(defconstant +tau+ +two-pi+)
(defconstant +half-pi+(/ PI 2))
(defconstant +quarter-pi+ (/ PI 4))
(defconstant +epsilon+ single-float-epsilon)
(defconstant +phi+ 1.61803398875)
(defconstant +golden-ratio+ +phi+)
(defconstant +e+ (exp 1))
(defun radians (deg)
(* PI (/ deg 180)))
(defun degrees (rad)
(* 180 (/ rad PI)))
;;----------------------------------------------------------------------
;utils
;;;; utils.lisp
(in-package #:sketch)
;;; _ _ _____ ___ _ ____
;;; | | | |_ _|_ _| | / ___|
;;; | | | | | | | || | \___ \
;;; | |_| | | | | || |___ ___) |
;;; \___/ |_| |___|_____|____/
(defparameter *build* nil)
(defun pad-list (list pad length)
(if (>= (length list) length)
list
(append (make-list (- length (length list)) :initial-element pad)
list)))
(defun group (list &optional (group-length 2))
(flet ((split-n (list n)
(when (>= (length list) n)
(loop with acc = '()
for i below n
do (setf acc (cons (car list) acc)
list (cdr list))
finally (return (cons (nreverse acc) list))))))
(loop with acc = '()
while (or (not acc) (cdr list))
do (let ((split (split-n list group-length)))
(when (car split)
(setf acc (cons (car split) acc)))
(setf list (cdr split)))
finally (return (nreverse acc)))))
(defun group-bits (x &optional (bits 8))
(let ((bit-fill (1- (expt 2 bits))))
(do* ((x x (ash x (- bits)))
(acc `(,(boole boole-and x bit-fill))
(cons (boole boole-and x bit-fill) acc)))
((zerop x) (cdr acc)))))
(declaim (inline order-list))
(defun order-list (order list)
(loop for o in order
collect (nth o list)))
(declaim (inline mix-lists))
(defun mix-lists (&rest lists)
(apply #'append (apply #'mapcar #'list lists)))
(declaim (inline div2-inexact))
(defun div2-inexact (a)
(multiple-value-bind (x y)
(floor a 2)
(values x (+ x y))))
(defun abs-or-rel (val src)
(if (numberp val)
(cond ((< 0 val 1) (* src val))
((<= 1 val) val)
(t src))
(or src 0)))
(declaim (inline lerp-list))
(defun lerp-lists (v list-a list-b)
(mapcar (lambda (a b) (alexandria:lerp v a b)) list-a list-b))
(defun flatten (tree &optional (unless-test (lambda (_) (declare (ignore _)) nil)))
(let (list)
(labels ((traverse (subtree)
(when subtree
(if (and (consp subtree) (not (funcall unless-test subtree)))
(progn
(traverse (car subtree))
(traverse (cdr subtree)))
(push subtree list)))))
(traverse tree))
(nreverse list)))
(defun object-to-keyword-hash (object)
"Expensive operation that turns CL objects into keywords whose names
are MD5 hashes of those objects, stringified. Uniqueness is not guaranteed,
but may be considered unique for all practical purposes."
(alexandria:make-keyword
(apply #'alexandria:symbolicate
(coerce (map 'array (lambda (x) (format nil "~x" x))
(md5:md5sum-string (write-to-string object)))
'list))))
(defun coerce-float (x)
(coerce x 'single-float))
(defun copy-buffer (src dst length &key (src-offset 0) (dst-offset 0))
(declare (optimize (speed 3) (debug 0)))
(loop for i from 0 below length
do (setf (cffi:mem-aref dst :uint8 (+ i src-offset))
(cffi:mem-aref src :uint8 (+ i dst-offset)))))
(defun relative-path (path &optional (system 'sketch))
(if *build*
path
(format nil "~a" (asdf:system-relative-pathname system path))))
;;----------------------------------------------------------------------
;environment
;;;; environment.lisp
(in-package #:sketch)
;;; _____ _ ___ _____ ____ ___ _ _ __ __ _____ _ _ _____
;;; | ____| \ | \ \ / /_ _| _ \ / _ \| \ | | \/ | ____| \ | |_ _|
;;; | _| | \| |\ \ / / | || |_) | | | | \| | |\/| | _| | \| | | |
;;; | |___| |\ | \ V / | || _ <| |_| | |\ | | | | |___| |\ | | |
;;; |_____|_| \_| \_/ |___|_| \_\\___/|_| \_|_| |_|_____|_| \_| |_|
(defstruct env
;; Drawing
(pen nil)
(programs nil)
(model-matrix (sb-cga:identity-matrix))
(view-matrix nil)
(matrix-stack nil)
(y-axis-sgn +1)
(vao nil)
(buffer-position 0)
;; Typography
(font nil)
;; Textures
(white-pixel-texture nil)
(white-color-vector nil)
;; Resources
(resources (make-hash-table))
;; Debugging
(debug-key-pressed nil)
(red-screen nil))
(defparameter *env* nil)
(defun make-white-pixel-texture ()
"Sent to shaders when no image is active."
(let ((texture (car (gl:gen-textures 1))))
(gl:bind-texture :texture-2d texture)
(gl:tex-parameter :texture-2d :texture-min-filter :linear)
(gl:tex-image-2d :texture-2d 0 :rgba 1 1 0 :bgra :unsigned-byte #(255 255 255 255))
texture))
(defun set-ortho-matrix (w)
(with-slots ((env %env) width height y-axis) w
(setf (env-view-matrix env) (if (eq y-axis :down)
(kit.glm:ortho-matrix 0 width height 0 -1 1)
(kit.glm:ortho-matrix 0 width 0 height -1 1)))))
(defun set-shader-values (w)
(with-slots ((env %env) width height y-axis) w
(let ((program (env-programs env)))
(kit.gl.shader:use-program program :fill-shader)
(kit.gl.shader:uniformi program "texid" 0)
(glhelp::set-active-texture 0)
(kit.gl.shader:uniform-matrix
program :view-m 4 (vector (env-view-matrix env))))))
(defun initialize-environment (w)
(set-ortho-matrix w)
(with-slots ((env %env) width height y-axis) w
(setf (env-programs env) (kit.gl.shader:compile-shader-dictionary 'sketch-programs)
(env-y-axis-sgn env) (if (eq y-axis :down) +1 -1)
(env-vao env) (make-instance 'kit.gl.vao:vao :type 'sketch-vao)
(env-white-pixel-texture env) (make-white-pixel-texture)
(env-white-color-vector env) #(255 255 255 255)
(env-pen env) (make-default-pen)
(env-font env) (make-default-font) ;;FIXME::reenable font
))
(set-shader-values w))
(defun initialize-gl (w)
(with-slots ((env %env) width height) w
;;(sdl2:gl-set-swap-interval 1) ;;FIXME::remove -> vsync?
;;(setf (kit.sdl2:idle-render w) t) ;;FIXME::remove
(gl:viewport 0 0 width height)
(gl:scissor 0 0 width height)
(gl:enable :blend :line-smooth :polygon-smooth)
(gl:blend-func :src-alpha :one-minus-src-alpha)
(gl:hint :line-smooth-hint :nicest)
(gl:hint :polygon-smooth-hint :nicest)
(gl:clear-color 0.0 1.0 0.0 1.0)
;;(gl:clear :color-buffer :depth-buffer)
;;(gl:flush) ;;FIXME::why is this here?
))
(defun debug-mode-p ()
(and (env-red-screen *env*)
(env-debug-key-pressed *env*)))
(defun exit-debug-mode ()
(setf (env-red-screen *env*) nil
(env-debug-key-pressed *env*) nil))
(defmacro with-environment (env &body body)
`(let ((*env* ,env))
,@body))
;;----------------------------------------------------------------------
;resources
;;;; resources.lisp
(in-package #:sketch)
;;; ____ _____ ____ ___ _ _ ____ ____ _____ ____
;;; | _ \| ____/ ___| / _ \| | | | _ \ / ___| ____/ ___|
;;; | |_) | _| \___ \| | | | | | | |_) | | | _| \___ \
;;; | _ <| |___ ___) | |_| | |_| | _ <| |___| |___ ___) |
;;; |_| \_\_____|____/ \___/ \___/|_| \_\\____|_____|____/
;;; Classes
(defclass resource () ())
(defgeneric free-resource (resource))
(defmethod free-resource :around (resource)
(when resource
(call-next-method)))
(defclass image (resource)
((texture :accessor image-texture :initarg :texture)
(width :accessor image-width :initarg :width)
(height :accessor image-height :initarg :height)))
(defclass typeface (resource)
((filename :accessor typeface-filename :initarg :filename)
(pointer :accessor typeface-pointer :initarg :pointer)))
;;; Loading
(defun load-resource (filename &rest all-keys &key type force-reload-p &allow-other-keys)
(let ((*env* (or *env* (make-env)))) ;; try faking env if we still don't have one
(symbol-macrolet ((resource (gethash key (env-resources *env*))))
(let* ((key (alexandria:make-keyword
(alexandria:symbolicate filename (format nil "~a" all-keys)))))
(when force-reload-p
(free-resource resource)
(remhash key (env-resources *env*)))
(when (not resource)
(setf resource
(apply #'load-typed-resource
(list* filename
(or type
(case (alexandria:make-keyword
(alexandria:symbolicate
(string-upcase (sketch-util::file-name-extension filename))))
((:png
;;:jpg
:jpeg
;;:tga
:tiff
:pnm
:gif
:bmp)
:image)
((:ttf
;;:otf
) :typeface)))
all-keys))))
resource))))
(defgeneric load-typed-resource (filename type &key &allow-other-keys))
(defmethod load-typed-resource (filename type &key &allow-other-keys)
(if (not type)
(error (format nil "~a's type cannot be deduced." filename))
(error (format nil "Unsupported resource type ~a" type))))
(defun make-image-from-surface (surface)
(let ((texture (car (gl:gen-textures 1)))
(width (sketch-util::opticl-loaded-surface-width surface))
(height (sketch-util::opticl-loaded-surface-height surface)))
(gl:bind-texture :texture-2d texture)
(gl:tex-parameter :texture-2d :texture-min-filter :linear)
(gl:tex-image-2d :texture-2d 0 :rgba
width
height
0
:rgba
:unsigned-byte (sketch-util::opticl-loaded-surface-data surface))
(gl:bind-texture :texture-2d 0)
(make-instance 'image
:width width
:height height
:texture texture)))
(defmethod load-typed-resource (filename (type (eql :image)) &key &allow-other-keys)
(make-image-from-surface (sketch-util::make-opticl-data filename)))
(defmethod load-typed-resource (filename (type (eql :typeface))
&key (size 18) &allow-other-keys)
(make-instance 'typeface
:filename filename
:pointer (sketch-util::make-font-info
:filename filename
:size (coerce (truncate size)
'(signed-byte 32)))))
(defmethod free-resource ((image image))
(gl:delete-textures (list (image-texture image))))
(defmethod free-resource ((typeface typeface)))
;;----------------------------------------------------------------------
;color
;;;; color.lisp
(in-package #:sketch)
;;; ____ ___ _ ___ ____
;;; / ___/ _ \| | / _ \| _ \
;;; | | | | | | | | | | | |_) |
;;; | |__| |_| | |__| |_| | _ <
;;; \____\___/|_____\___/|_| \_\
;;; General
(defclass color (resource)
((red :initform 0.0 :accessor color-red :initarg :red)
(green :initform 0.0 :accessor color-green :initarg :green)
(blue :initform 0.0 :accessor color-blue :initarg :blue)
(hue :initform 0.0 :accessor color-hue :initarg :hue)
(saturation :initform 0.0 :accessor color-saturation :initarg :saturation)
(brightness :initform 0.0 :accessor color-brightness :initarg :brightness)
(alpha :initform 1.0 :accessor color-alpha :initarg :alpha)))
(defun rgb-to-hsb (r g b)
(let* ((c-max (max r g b))
(c-min (min r g b))
(delta (- c-max c-min))
(hue (* 60 (cond ((= delta 0) 0)
((= c-max r) (mod (/ (- g b) delta) 6))
((= c-max g) (+ (/ (- b r) delta) 2))
((= c-max b) (+ (/ (- r g) delta) 4))
(t 0))))
(saturation (if (zerop c-max)
0
(/ delta c-max)))
(brightness c-max))
(list (/ hue 360) saturation brightness)))
(defun hsb-to-rgb (h s b)
(let* ((h (mod (* h 360) 360))
(c (* b s))
(x (* c (- 1 (abs (- (mod (/ h 60) 2) 1)))))
(m (- b c)))
(mapcar (lambda (x) (+ m x))
(aref `#((,c ,x 0) (,x ,c 0) (0 ,c ,x)
(0 ,x ,c) (,x 0 ,c) (,c 0 ,x))
(floor (/ h 60))))))
(defun rgb (red green blue &optional (alpha 1.0))
(destructuring-bind (red green blue alpha)
(mapcar #'clamp-1 (list red green blue alpha))
(let ((hsb (rgb-to-hsb red green blue)))
(make-instance 'color :red red :green green :blue blue :alpha alpha
:hue (elt hsb 0) :saturation (elt hsb 1) :brightness (elt hsb 2)))))
(defun hsb (hue saturation brightness &optional (alpha 1.0))
(destructuring-bind (hue saturation brightness alpha)
(mapcar #'clamp-1 (list hue saturation brightness alpha))
(let ((rgb (hsb-to-rgb hue saturation brightness)))
(make-instance 'color :hue hue :saturation saturation :brightness brightness :alpha alpha
:red (elt rgb 0) :green (elt rgb 1) :blue (elt rgb 2)))))
(defun gray (amount &optional (alpha 1.0))
(rgb amount amount amount alpha))
(defun rgb-255 (red green blue &optional (alpha 255))
(rgb (/ red 255) (/ green 255) (/ blue 255) (/ alpha 255)))
(defun hsb-360 (hue saturation brightness &optional (alpha 255))
(hsb (/ hue 360) (/ saturation 100) (/ brightness 100) (/ alpha 255)))
(defun gray-255 (amount &optional (alpha 255))
(gray (/ amount 255) (/ alpha 255)))
(defun hex-to-color (string)
(let ((string (string-left-trim "#" string)))
(destructuring-bind (r g b &optional (a 1.0))
(let* ((bits (case (length string)
((3 4) 4)
((6 8) 8)
(t (error "~a is not a valid hex color." string))))
(groups (group-bits (parse-integer string :radix 16 :junk-allowed t)
bits)))
(pad-list (mapcar (lambda (x) (/ x (if (= bits 4) 15 255))) groups)
0
(if (= 4 bits)
(length string)
(/ (length string) 2))))
(rgb r g b a))))
(defun color-rgb (color)
(list (color-red color)
(color-green color)
(color-blue color)))
(defun color-rgba (color)
(list (color-red color)
(color-green color)
(color-blue color)
(color-alpha color)))
(defun color-rgba-255 (color)
(mapcar (lambda (x) (coerce (truncate (* 255 x)) 'unsigned-byte))
(color-rgba color)))
(defun color-hsba (color)
(list (color-hue color)
(color-saturation color)
(color-brightness color)
(color-alpha color)))
(defun color-vector (color)
(apply #'vector (mapcar #'coerce-float (color-rgba color))))
(defun color-vector-255 (color)
(apply #'vector (color-rgba-255 color)))
;;; Generators
(defun lerp-color (start-color end-color amount &key (mode :hsb))
(let ((a (clamp-1 amount)))
(flet ((norm (field)
(normalize a 0.0 1.0
:out-low (slot-value start-color field)
:out-high (slot-value end-color field))))
(if (eq mode :hsb)
(apply #'hsb (mapcar #'norm '(hue saturation brightness alpha)))
(apply #'rgb (mapcar #'norm '(red green blue alpha)))))))
(defun random-color (&optional (alpha 1.0))
(rgb (random 1.0) (random 1.0) (random 1.0) alpha))
(defun hash-color (n &optional (alpha 1.0))
(let* ((grp (group-bits n))
(arr (make-array (length grp)
:element-type '(unsigned-byte 8)
:initial-contents grp))
(seq (md5:md5sum-sequence arr))
(hash (loop for i across seq sum i)))
(hsb-360 (mod (+ (* 144 (mod n 20)) (mod hash 60)) 360)
(alexandria:clamp (+ 25 (* 25 (mod hash 4)) (mod hash 25)) 0 100)
(alexandria:clamp (+ 25 (* 25 (mod n 4)) (mod hash 20)) 0 100)
(* 255 alpha))))
;;; Filters
(defun color-filter-grayscale (color &optional (mode :luminosity))
(case mode
((:lightness 1) (gray (/ (+ (apply #'max (color-rgb color))
(apply #'min (color-rgb color))))
(color-alpha color)))
((:average 2) (gray (/ (apply #'+ (color-rgb color)) 3)
(color-alpha color)))
(t (gray (+ (* 0.21 (color-red color))
(* 0.72 (color-green color))
(* 0.07 (color-blue color)))
(color-alpha color)))))
(defun color-filter-invert (color)
(hsb (let ((h (- (color-hue color) 0.5)))
(if (plusp h)
h
(+ 1 h)))
(color-saturation color)
(color-brightness color)
(color-alpha color)))
(defun color-filter-rotate (color)
(rgb (color-green color)
(color-blue color)
(color-red color)))
(defun color-filter-hsb (color &key (hue 0.0) (saturation 0.0) (brightness 0.0))
(let ((hue (clamp-1 (+ hue (color-hue color))))
(saturation (clamp-1 (+ saturation (color-brightness color))))
(brightness (clamp-1 (+ brightness (color-brightness color))))
(alpha (color-alpha color)))
(destructuring-bind (red green blue) (hsb-to-rgb hue saturation brightness)
(make-instance 'color
:red red :green green :blue blue :alpha alpha
:hue hue :saturation saturation :brightness brightness))))
;;; Predefined colors
(defparameter +red+ (rgb 1 0 0))
(defparameter +green+ (rgb 0 1 0))
(defparameter +blue+ (rgb 0 0 1))
(defparameter +yellow+ (rgb 1 1 0))
(defparameter +magenta+ (rgb 1 0 1))
(defparameter +cyan+ (rgb 0 1 1))
(defparameter +orange+ (rgb 1.0 0.5 0.0))
(defparameter +white+ (gray 1))
(defparameter +black+ (gray 0))
;;----------------------------------------------------------------------
;channels
;;;; channels.lisp
(in-package #:sketch)
;;; ____ _ _ _ _ _ _ _ _____ _ ____
;;; / ___| | | | / \ | \ | | \ | | ____| | / ___|
;;; | | | |_| | / _ \ | \| | \| | _| | | \___ \
;;; | |___| _ |/ ___ \| |\ | |\ | |___| |___ ___) |
;;; \____|_| |_/_/ \_\_| \_|_| \_|_____|_____|____/
;;; Channel interface
#+nil;;FIXME::what are channels?
(progn
(defparameter *channels* (make-hash-table))
(defun register-input (channel &optional initial (adapter #'identity))
(unless (assoc adapter (gethash channel *channels*))
(push (cons adapter initial) (gethash channel *channels*)))
t)
(defun in (channel &optional initial (adapter #'identity))
(register-input channel initial adapter)
(let ((a (cdr (assoc adapter (gethash channel *channels*)))))
(or a initial)))
(defun out-1 (channel message)
(register-input channel message #'identity)
(mapcar (lambda (adapter-value-cons)
(setf (cdr adapter-value-cons)
(funcall (car adapter-value-cons) message)))
(gethash channel *channels*))
(propagate channel))
(defun out (&rest channel-message)
(mapcar (lambda (x) (out-1 (first x) (second x)))
(group channel-message))
(values))
;;; Channel propagation
(defstruct propagation
name
inputs
outputs
function)
(defparameter *propagations* (make-hash-table))
(defparameter *channel-propagations* (make-hash-table))
(defun propagate (channel)
(mapcar (lambda (p) (funcall (propagation-function p)))
(gethash channel *channel-propagations*)))
(defun find-inputs-and-outputs (body)
(let ((flat-body (alexandria:flatten body))
(inputs-and-outputs (list (list 'in) (list 'out)))
(push-into nil))
(dolist (token flat-body)
(alexandria:if-let ((io-cons (assoc push-into inputs-and-outputs)))
(progn
(when (not (member token (cdr io-cons)))
(setf (cdr io-cons) (cons token (cdr io-cons))))
(setf push-into nil))
(setf push-into token)))
inputs-and-outputs))
(defun extract-input-registration (body)
(mapcar (lambda (in-form) (cadr in-form))
(remove-if #'atom (flatten body (lambda (x) (eql (car x) 'in))))))
(defun delete-channel-propagation (channel propagation)
(setf (gethash channel *channel-propagations*)
(remove-if (lambda (x) (eql x propagation))
(gethash channel *channel-propagations*))))
(defun update-propagation-data (name inputs outputs)
(let ((propagation (gethash name *propagations*)))
(if propagation
(mapcar (lambda (channel)
(delete-channel-propagation channel propagation))
(propagation-inputs propagation))
(setf propagation (make-propagation :name name)
(gethash name *propagations*) propagation))
(setf (propagation-inputs propagation) inputs
(propagation-outputs propagation) outputs)
(mapcar (lambda (channel)
(push propagation (gethash channel *channel-propagations*)))
inputs)))
(defun %define-channel-observer (name body)
(let ((name (or name (gensym))))
(let* ((inputs-and-outputs (find-inputs-and-outputs body))
(inputs (cdr (assoc 'in inputs-and-outputs)))
(outputs (cdr (assoc 'out inputs-and-outputs)))
(input-registrations (extract-input-registration body)))
(update-propagation-data name inputs outputs)
(mapcar #'register-input input-registrations)
(setf (propagation-function (gethash name *propagations*))
(eval `(lambda () ,@body)))
(when outputs
(mapcar #'propagate inputs)))))
(defmacro define-named-channel-observer (name &body body)
(%define-channel-observer name body)
nil)
(defmacro define-channel-observer (&body body)
(%define-channel-observer nil body)
nil)
;;; Utility functions
(defun reset-channel (channel)
(remhash channel *channels*)
(remhash channel *channel-propagations*)
(maphash (lambda (name propagation)
(declare (ignore name))
(setf (propagation-inputs propagation)
(remove-if (lambda (x) (eql x channel))
(propagation-inputs propagation))
(propagation-outputs propagation)
(remove-if (lambda (x) (eql x channel))
(propagation-outputs propagation))))
*propagations*)
(values))
(defun reset-all-channels ()
(setf *channels* (make-hash-table)
*propagations* (make-hash-table)
*channel-propagations* (make-hash-table))
(values)))
;;----------------------------------------------------------------------
;shaders
;;;; shaders.lisp
(in-package #:sketch)
;;; ____ _ _ _ ____ _____ ____ ____
;;; / ___|| | | | / \ | _ \| ____| _ \/ ___|
;;; \___ \| |_| | / _ \ | | | | _| | |_) \___ \
;;; ___) | _ |/ ___ \| |_| | |___| _ < ___) |
;;; |____/|_| |_/_/ \_\____/|_____|_| \_\____/
(kit.gl.shader:defdict sketch-programs ()
(kit.gl.shader:program :fill-shader (:view-m :model-m :texid)
(:vertex-shader "
#version 330 core
uniform mat4 model_m;
uniform mat4 view_m;
layout (location = 0) in vec2 vertex;
layout (location = 1) in vec2 texcoord;
layout (location = 2) in vec4 color;
smooth out vec4 f_color;
smooth out vec2 f_texcoord;
void main() {
gl_Position = view_m * model_m * vec4(vertex, 0.0, 1.0);
f_texcoord = texcoord;
f_color = color;
}
")
(:fragment-shader "
#version 330 core
uniform sampler2D texid;
smooth in vec4 f_color;
smooth in vec2 f_texcoord;
out vec4 f_out;
void main() {
f_out = texture(texid, f_texcoord) * f_color;
}
")))
;;----------------------------------------------------------------------
;pen
;;;; pen.lisp
(in-package #:sketch)
;;; ____ _____ _ _
;;; | _ \| ____| \ | |
;;; | |_) | _| | \| |
;;; | __/| |___| |\ |
;;; |_| |_____|_| \_|
(defstruct pen
(fill nil)
(stroke nil)
(weight 1)
(curve-steps 100))
(defmacro with-pen (pen &body body)
(alexandria:once-only (pen)
`(alexandria:with-gensyms (previous-pen)
(progn
(setf previous-pen (env-pen *env*)
(env-pen *env*) ,pen)
,@body
(setf (env-pen *env*) previous-pen)))))
(defun set-pen (pen)
"Sets environment pen to PEN."
(setf (env-pen *env*) pen))
(defun flip-pen (pen)
"Makes a new pen by swapping PEN's fill and stroke colors."
(make-pen :weight (pen-weight pen)
:stroke (pen-fill pen)
:fill (pen-stroke pen)
:weight (pen-weight pen)
:curve-steps (pen-curve-steps pen)))
(defun background (color)
"Fills the sketch window with COLOR."
(apply #'gl:clear-color (color-rgba color))
(gl:clear :color-buffer))
(let ((pen))
(defun make-default-pen ()
(setf pen (or pen
(make-pen :weight 1
:fill +white+
:stroke +black+)))))
;;----------------------------------------------------------------------
;image
;;;; resources.lisp
(in-package #:sketch)
;; ___ __ __ _ ____ _____ ____
;; |_ _| \/ | / \ / ___| ____/ ___|
;; | || |\/| | / _ \| | _| _| \___ \
;; | || | | |/ ___ \ |_| | |___ ___) |
;; |___|_| |_/_/ \_\____|_____|____/
(defun image (image-resource x y &optional width height)
(with-pen (make-pen :fill image-resource
:stroke (pen-stroke (env-pen *env*))
:weight (pen-weight (env-pen *env*)))
(rect x
y
(or (abs-or-rel width (image-width image-resource)))
(or (abs-or-rel height (image-height image-resource))))))
;;----------------------------------------------------------------------
;font
;;;; font.lisp
(in-package #:sketch)
;;; _____ ___ _ _ _____
;;; | ___/ _ \| \ | |_ _|
;;; | |_ | | | | \| | | |
;;; | _|| |_| | |\ | | |
;;; |_| \___/|_| \_| |_|
(defclass font (resource)
((face :accessor font-face :initarg :face)
(color :accessor font-color :initarg :color)
(size :accessor font-size :initarg :size :initform 16)
(line-height :accessor font-line-height :initarg :line-height :initform 1.41)
(align :accessor font-align :initarg :align :initform :left)))
(defun make-font (&key face color size line-height align)
(let* ((*env* (or *env* (make-env))))
(make-instance 'font
:face (or face
(font-face (or (env-font *env*)
(make-default-font))))