Add CL style
[clnl] / src / main / interface.lisp
1 (in-package #:clnl-interface)
2
3 (defvar *patch-size* 13d0)
4 (defvar *world-dims* '(:xmin -5 :xmax 5 :ymin -5 :ymax 5))
5
6 (defvar *turtle-list* nil)
7
8 ; It may be useful to keep windows around
9 (defvar *glut-window-opened* nil)
10
11 (defvar *colors*
12  '((140 140 140) ; gray       (5)
13    (215 48 39) ; red       (15)
14    (241 105 19) ; orange    (25)
15    (156 109 70) ; brown     (35)
16    (237 237 47) ; yellow    (45)
17    (87 176 58) ; green     (55)
18    (42 209 57) ; lime      (65)
19    (27 158 119) ; turquoise (75)
20    (82 196 196) ; cyan      (85)
21    (43 140 190) ; sky       (95)
22    (50 92 168) ; blue     (105)
23    (123 78 163) ; violet   (115)
24    (166 25 105) ; magenta  (125)
25    (224 126 149) ; pink     (135)
26    (0 0 0) ; black
27    (255 255 255))) ; white
28
29 (defun nl-color->rgb (color)
30  (let*
31   ((step (+ (/ (- (mod (floor (* color 10)) 100) 50) 50.48) 0.012)))
32   (mapcar
33    (lambda (x) (/ (+ x (floor (* (if (< step 0d0) x (- 255 x)) step))) 255))
34    (nth (floor color 10) *colors*))))
35
36 (defun render-scene ()
37  (gl:clear :color-buffer-bit :depth-buffer-bit)
38  (gl:matrix-mode :projection)
39  (gl:load-identity)
40  (gl:ortho -71 71 -71 71 1 5000)
41  (gl:matrix-mode :modelview)
42  (gl:load-identity)
43  (mapcar
44   (lambda (turtle)
45    (let
46     ((color (nl-color->rgb (clnl-nvm:turtle-color turtle))))
47     (gl:color (car color) (cadr color) (caddr color)))
48    (gl:with-pushed-matrix
49     (gl:translate (* (clnl-nvm:turtle-xcor turtle) *patch-size*) (* (clnl-nvm:turtle-ycor turtle) *patch-size*) 0)
50     (gl:rotate (clnl-nvm:turtle-heading turtle) 0 0 -1)
51     (gl:call-list *turtle-list*)))
52   (clnl-nvm:turtles))
53  (gl:flush))
54
55 (defun display ()
56  (render-scene)
57  (cl-glut:swap-buffers))
58
59 (defun idle ()
60  (cl-glut:post-redisplay))
61
62 (defun close-func ()
63  (sb-ext:exit))
64
65 (defun reshape (width height)
66  (when (and (/= 0 width) (/= 0 height))
67   (gl:viewport 0 0 width height)))
68
69 (cffi:defcallback display :void () (display))
70 (cffi:defcallback idle :void () (idle))
71 (cffi:defcallback close-func :void () (close-func))
72 (cffi:defcallback reshape :void ((width :int) (height :int)) (reshape width height))
73
74 (defun set-turtle-list ()
75  (setf *turtle-list* (gl:gen-lists 1))
76  (gl:with-new-list (*turtle-list* :compile)
77   (gl:rotate 180 0 0 -1)
78   (gl:scale (* (/ 1d0 300d0) 13) (* (/ 1d0 300d0) 13) 1)
79   (gl:translate -150 -150 -4.0)
80   (gl:begin :polygon)
81   (gl:vertex 150 5 0)
82   (gl:vertex 40 250 0)
83   (gl:vertex 150 205 0)
84   (gl:vertex 260 250 0)
85   (gl:end)))
86
87 (defun run ()
88  ; I do this because I don't know who or what in the many layers
89  ; is causing the floating point errors, but I definitely don't
90  ; want to investigate until simply ignoring them becomes a problem.
91  (sb-int:with-float-traps-masked (:invalid)
92   (cl-glut:init)
93   (gl:clear-color 0 0 0 1)
94   (cl-glut:init-window-size
95    (floor (* *patch-size* (1+ (- (getf *world-dims* :xmax) (getf *world-dims* :xmin)))))
96    (floor (* *patch-size* (1+ (- (getf *world-dims* :ymax) (getf *world-dims* :ymin))))))
97   (setf *glut-window-opened* t)
98   (cl-glut:create-window "CLNL Test Window")
99   (cl-glut:init-display-mode :double :rgba)
100   (cl-glut:display-func (cffi:get-callback 'display))
101   (glut:reshape-func (cffi:callback reshape))
102   (cl-glut:idle-func (cffi:get-callback 'idle))
103   (cl-glut:close-func (cffi:get-callback 'close-func))
104   (set-turtle-list)
105   (cl-glut:main-loop)))
106
107 (defun export-view ()
108  (sb-int:with-float-traps-masked (:invalid)
109   (when (not *glut-window-opened*)
110    (cl-glut:init)
111    (gl:clear-color 0 0 0 1)
112    (cl-glut:init-window-size 1 1)
113    (cl-glut:create-window "CLNL Test Window")
114    (set-turtle-list)
115    (setf *glut-window-opened* t))
116   (let
117    ((fbo (first (gl:gen-framebuffers 1)))
118     (render-buf (first (gl:gen-renderbuffers 1)))
119    ;(width (floor (* *patch-size* (1+ (- (getf *world-dims* :xmax) (getf *world-dims* :xmin))))))
120    ;(height (floor (* *patch-size* (1+ (- (getf *world-dims* :ymax) (getf *world-dims* :ymin))))))
121     (width 143)  ; Hard coded for now, yay v1 (if you see this comment in a year, please cry for me)
122     (height 143))
123    (gl:bind-framebuffer :framebuffer fbo)
124    (gl:bind-renderbuffer :renderbuffer render-buf)
125    (gl:renderbuffer-storage :renderbuffer :rgba8 width height)
126    (gl:framebuffer-renderbuffer :draw-framebuffer :color-attachment0 :renderbuffer render-buf)
127    (gl:viewport 0 0 width height)
128    (render-scene)
129    (gl:read-pixels 0 0 width height :rgba :unsigned-byte))))