-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinpack2-vis2.lisp
318 lines (274 loc) · 9.18 KB
/
binpack2-vis2.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
(defpackage #:binpack2-vis2
(:use :cl #:3b-glim-example/s #:3b-glim-example/s-shaders)
(:local-nicknames (#:glim #:3b-glim/s)
(#:2d #:3b-glim/2d)
(#:a #:alexandria-2)
(#:b #:binpack)))
(in-package binpack2-vis2)
(defvar *format*
(glim:compile-vertex-format
'(1
(0 :vec3) ;; position
(1 :vec3) ;; normal
(2 :vec4) ;; uv
(3 :vec4) ;; color
)))
(defvar *validate* nil)
(declaim (inline vertex vertex-v color normal uv))
(defun vertex (x &optional (y 0.0) (z 0.0))
(glim:attrib-f 0 x y z))
(defun vertex-v (v)
(glim:attrib-fv 0 v))
(defun normal (x &optional (y 0.0) (z 0.0) (w 1.0))
(glim:attrib-f 1 x y z w))
(defun uv (x &optional (y 0.0) (z 0.0) (w 1.0))
(glim:attrib-f 2 x y z w))
(defun color (r g b &optional (a 1.0))
(glim:attrib-f 3 r g b a))
(defparameter *esc* nil)
(defparameter *debug* 0)
(defparameter *flags* (make-array 10 :initial-element nil))
(defparameter *kx* 0)
(defparameter *ky* 0)
(defparameter *mouse* t)
(defvar *replays* nil)
(defclass binpack2-vis2 (scratchpad)
((scale :initform 1 :accessor scale)
(draw-scale :initform 16 :accessor draw-scale)
(spacing :initform 16 :accessor spacing)
(origin :initform :center :accessor origin)
(x1 :initform 0 :accessor x1)
(x2 :initform 0 :accessor x2)
(y1 :initform 0 :accessor y1)
(y2 :initform 0 :accessor y2)
(gmx :initform 0 :accessor gmx)
(gmy :initform 0 :accessor gmy)
(snap :initform t :accessor snap)
(packing :initform nil :accessor packing)
(index :initform 0 :accessor index))
(:default-initargs :shaders '((:tex :vertex vertex/simple
:fragment frag/tex)
(:solid :vertex vertex/simple
:fragment frag/solid))))
(defvar *w* nil)
(defun rnd-rgb (&key (alpha 1))
(let ((a (random (* 2 pi))))
(list (1+ (sin a))
(1+ (sin (+ a (* 1/3 pi))))
(1+ (sin (+ a (* 2/3 pi))))
alpha)))
(defun random-rgb (&key (alpha 1))
(list (+ 0.25 (random 0.75))
(+ 0.25 (random 0.75))
(+ 0.25 (random 0.75))
alpha))
(defun translate-mouse (w x y &key snap)
(let ((mx (+ (x1 w)
(/ x (/ (wx w)
(- (x2 w) (x1 w))))))
(my (+ (y1 w)
(/ (- (wy w) y)
(/ (wy w)
(- (y2 w) (y1 w)))))))
(if (and snap (snap w))
(let ((s (spacing w)))
(values (* s (round mx s))
(* s (round my s))))
(values mx my))))
(defun uniforms ()
(glim:uniform 'modelview (glim:ensure-matrix :modelview))
(glim:uniform 'proj (glim:ensure-matrix :projection))
#++
(glim:uniform 'mvp (sb-cga:matrix*
(glim:ensure-matrix :projection)
(glim:ensure-matrix :modelview)))
(glim:uniform 'normal-matrix (glim:ensure-matrix :modelview)))
(defmethod display ((w binpack2-vis2) now)
(setf *w* w)
(when (and *replays*
(or (not (packing w))
(>= (index w)
(length (packing w)))))
(setf (packing w) (sb-ext:atomic-pop *replays*)
(index w) 0))
(let ((*random-state* (sb-ext:seed-random-state 123)))
(glim:with-state (*format*)
(glim:uniform 'proj sb-cga:+identity-matrix+)
(glim:matrix-mode :projection)
(glim:load-identity)
(glim:matrix-mode :modelview)
(glim:load-identity)
(gl:enable :depth-test :sample-alpha-to-coverage
:polygon-smooth :line-smooth
:blend :multisample :texture-2d)
(gl:blend-func :src-alpha :one-minus-src-alpha)
(gl:disable :cull-face :lighting :light0 :light1
:depth-test :sample-alpha-to-coverage)
(glim:uniform 'debug1 *debug*)
(glim:uniform 'tex0 0)
(glim:uniform 'tex1 1)
(glim:uniform 'flip 0)
(let* ((x1 0)
(y1 0)
(scale (scale w))
(step (spacing w))
(x2 (/ (wx w) scale))
(y2 (/ (wy w) scale)))
(glim:with-pushed-matrix (:modelview)
(ecase (origin w)
(:center
(setf x2 (/ (wx w) scale))
(setf y2 (/ (wy w) scale))
(setf x1 (- x2))
(setf y1 (- y2)))
(:lower-left
(glim:translate -1 -1 0))
;; todo: add others
)
(when (aref *flags* 1)
(incf x1 (/ (wx w) 10/9 scale))
(incf y1 (/ (wy w) 10/9 scale))
(incf x2 (/ (wx w) 10/9 scale))
(incf y2 (/ (wy w) 10/9 scale))
(glim:translate -0.9 -0.9 0))
#++(when (aref *flags* 1)
(incf x1 (/ (wx w) 2 scale))
(incf y1 (/ (wy w) 2 scale))
(incf x2 (/ (wx w) 2 scale))
(incf y2 (/ (wy w) 2 scale))
(glim:translate -0.5 -0.5 0))
#++
(when (aref *flags* 2)
(incf x1 (/ (wx w) 4 scale))
(incf y1 (/ (wy w) 4 scale))
(incf x2 (/ (wx w) 4 scale))
(incf y2 (/ (wy w) 4 scale))
(glim:translate -0.25 -0.25 0))
(glim:scale (/ (wx w)) (/ (wy w)) 1)
(glim:translate 0.5 0.5 0)
(glim:scale scale scale 1)
(uniforms)
(setf (x1 w) x1)
(setf (x2 w) x2)
(setf (y1 w) y1)
(setf (y2 w) y2)
(setf (values (gmx w) (gmy w))
(translate-mouse w (mx w) (my w)))
(glim:with-draw (:lines :shader :solid)
(color 0.2 0.1 0.2 1)
(when (minusp y1)
(loop for x from 0 downto x1 by step
do (vertex x y1)
(vertex x y2)))
(loop for x from 0 upto x2 by step
do (vertex x y1)
(vertex x y2))
(when (minusp y1)
(loop for y from 0 downto y1 by step
do (vertex x1 y)
(vertex x2 y)))
(loop for y from 0 upto y2 by step
do (vertex x1 y)
(vertex x2 y))
(color 0.2 0.2 0.2 1)
(vertex -500 -500)
(vertex 500 500)
(vertex -500 500)
(vertex 500 -500)
(color 0.2 0.2 0.4 1)
(vertex x1 0)
(vertex x2 0)
(vertex 0 y1)
(vertex 0 y2))
(incf (index w) 55)
(glim:with-draw (:quads :shader :solid)
(when (packing w)
(loop for i below (index w)
for (x1 y1 wx wy) across (packing w)
for x2 = (+ x1 wx)
for y2 = (+ y1 wy)
do (apply #'color (random-rgb :alpha 0.75))
(vertex x1 y1)
(vertex x2 y1)
(vertex x2 y2)
(vertex x1 y2))))
(dispatch-draws w))))))
#++
(defmethod mouse ((w binpack2-vis2) button state x y)
(format t "~s ~s~%" button state)
(setf *esc* nil)
(when (eql state :down)
(case button
(:left-button
)
(:right-button
))))
(defmethod mouse-wheel ((window binpack2-vis2) button state x y)
(format t "wheel ~s ~s~%" button state)
(if (eql state :up)
(setf (scale window) (* (scale window) 1.1))
(setf (scale window) (/ (scale window) 1.1))))
#++
(defmethod keyboard-up ((window binpack2-vis2) key x y)
)
(defmethod keyboard :around ((window binpack2-vis2) key x y)
(cond
((and (eql key #\q) *esc*)
(destroy-window window))
((eql key #\esc)
(setf *esc* t))
(t
(setf *esc* nil)
(call-next-method))))
(defmethod keyboard ((window binpack2-vis2) key x y)
(declare (ignore x y))
(print key)
(case key
(:key-right
(setf *mouse* nil))
(:key-left
(setf *mouse* nil))
(:key-down
(setf *mouse* nil))
(:key-up
(setf *mouse* nil))
((#\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
(let ((i (digit-char-p key)))
(when i
(setf (aref *flags* i) (not (aref *flags* i)))
(format t "flags ~s -> ~s~%" i (aref *flags* i)))))
(#\r
(setf (packing window) nil
(index window) 0))
(#\l
(glut:reshape-window 1280 700))
(#\space
(setf (index window) 0))
#++(#\d
(unless *debug* (setf *debug* 0))
(setf *debug* (mod (1+ *debug*) 4))
(format t "debug = ~s~%" *debug*))
(#\Esc
(glut:destroy-current-window))))
(defmethod entry ((w binpack2-vis2) state)
(format t "mouse -> ~s~%" state)
#++(when (eql state :left) (break "enter ~s~%" state))
(setf *mouse* (eql state :entered)))
(defmethod init-gl ((w binpack2-vis2))
(setf *esc* nil)
(gl:pixel-store :unpack-alignment 1)
(gl:disable :dither))
(defun binpack2-vis2 (&rest args)
(glut:display-window (apply #'make-instance 'binpack2-vis2 args)))
#++
(ql:quickload '(binpack parachute
alexandria sb-cga cl-opengl
3b-glim/example/s 3b-glim/2d 3bgl-shader))
#++
(binpack2-vis2 :pos-x 2440 :pos-y 140 :width 1400 :height 850)
#++
(binpack2-vis2 :pos-x 3100 :pos-y 300 :width 700 :height 700)
#++
(binpack2-vis2 :pos-x 00 :pos-y 300 :width 700 :height 700)
#++
(glut:show-window)