5 ;;; Copyright (C) 2013 Andy Wingo <wingo@pobox.com>
7 ;;; Figl is free software: you can redistribute it and/or modify it
8 ;;; under the terms of the GNU Lesser General Public License as
9 ;;; published by the Free Software Foundation, either version 3 of the
10 ;;; License, or (at your option) any later version.
12 ;;; Figl is distributed in the hope that it will be useful, but WITHOUT
13 ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 ;;; Public License for more details.
17 ;;; You should have received a copy of the GNU Lesser General Public
18 ;;; License along with this program. If not, see
19 ;;; <http://www.gnu.org/licenses/>.
21 (use-modules (figl glut)
26 (figl contrib packed-struct))
28 (define-packed-struct color-vertex
37 (define-packed-struct particle
45 (define *vertices* (make-packed-array color-vertex 0))
46 (define *particles* (make-packed-array particle 0))
48 (define (draw-particles)
49 (gl-enable-client-state (enable-cap vertex-array))
50 (gl-enable-client-state (enable-cap color-array))
51 (set-gl-vertex-array (vertex-pointer-type float)
53 #:stride (packed-struct-size color-vertex)
54 #:offset (packed-struct-offset color-vertex x))
55 (set-gl-color-array (color-pointer-type float)
57 #:stride (packed-struct-size color-vertex)
58 #:offset (packed-struct-offset color-vertex r))
59 (gl-draw-arrays (primitive-type quads) 0
60 (packed-array-length *vertices* color-vertex))
61 (gl-disable-client-state (enable-cap color-array))
62 (gl-disable-client-state (enable-cap vertex-array)))
64 (define (update-quads)
68 (lambda (n x y z vx vy vz)
69 (let ((r (/ (abs vx) 5))
77 (pack *vertices* base color-vertex
80 (pack *vertices* (+ base 1) color-vertex
83 (pack *vertices* (+ base 2) color-vertex
86 (pack *vertices* (+ base 3) color-vertex
90 (define (update-particles dt)
91 (let ((half-dt-squared (* 0.5 dt dt)))
95 (lambda (n x y z vx vy vz)
96 (let* ((distance-squared (+ (* x x) (* y y) (* z z)))
97 (distance (sqrt distance-squared))
98 (F (/ -200 distance-squared))
99 (ax (* F (/ x distance)))
100 (ay (* F (/ y distance)))
101 (az (* F (/ z distance))))
102 (values (+ x (* vx dt) (* ax half-dt-squared))
103 (+ y (* vy dt) (* ay half-dt-squared))
104 (+ z (* vz dt) (* az half-dt-squared))
107 (+ vz (* az dt))))))))
109 (define (prepare-particles n)
110 (set! *particles* (make-packed-array particle n))
111 (set! *vertices* (make-packed-array color-vertex (* n 4)))
118 (* (random:normal) 30)
119 (* (random:normal) 30)
120 (* (random:normal) 30)
123 (* (random:normal) 2)
124 (* (random:normal) 2)
125 (* (random:normal) 2))))
128 (define main-window #f)
130 (define (make-fps-accumulator period)
131 (let* ((frame-count 0)
132 (last-fps-time (get-internal-real-time))
133 (last-fps-run-time (get-internal-run-time))
134 (last-frame-time (get-internal-real-time))
135 (max-frame-time (get-internal-real-time))
137 (jiffies-per-sec (exact->inexact internal-time-units-per-second))
138 (jiffies-per-ms (/ jiffies-per-sec 1000)))
140 (let ((now (get-internal-real-time)))
141 (set! frame-count (1+ frame-count))
142 (when (> (- now last-frame-time) max-frame-time)
143 (set! max-frame-time (- now last-frame-time)))
144 (set! last-frame-time now)
145 (when (> (- now last-fps-time) period)
146 (let* ((run (get-internal-run-time))
147 (frames (- frame-count last-frame-count))
148 (secs (/ (- now last-fps-time) jiffies-per-sec))
149 (fps (/ frames secs))
150 (ms-per-frame (/ (* secs 1000) frames))
151 (max-ms-per-frame (/ max-frame-time jiffies-per-ms))
152 (cpu (* 100.0 (/ (- run last-fps-run-time)
153 (- now last-fps-time)))))
156 ";;; ~a frames in ~,2f sec = ~,2f fps; ~,2f ms/frame, ~,2f ms max; ~,2f% CPU\n"
157 frames secs fps ms-per-frame max-ms-per-frame cpu)
158 (set! last-frame-count frame-count)
159 (set! max-frame-time 0)
160 (set! last-fps-time now)
161 (set! last-fps-run-time run)))))))
163 (define accumulate-fps!
164 (make-fps-accumulator (* 2 internal-time-units-per-second)))
166 (define (draw-axis scale)
167 ;; Could disable lighting and depth test.
168 (gl-begin (primitive-type lines)
171 (gl-vertex scale 0 0)
175 (gl-vertex 0 scale 0)
179 (gl-vertex 0 0 scale)))
183 (gl-clear (clear-buffer-mask color-buffer depth-buffer))
189 ;; With double-buffering, swap-buffers will wait for the frame to be shown,
190 ;; which limits this program to the frame rate.
193 (define (on-reshape width height)
194 (pk 'reshape! width height)
195 (gl-viewport 0 0 width height)
197 ;; Projection matrix.
198 (set-gl-matrix-mode (matrix-mode projection))
200 (glu-perspective 60 (/ width height) 0.1 1000))
202 (define full-screen? #f)
205 (define (on-keyboard keycode x y)
206 (let ((c (integer->char keycode)))
209 (set! full-screen? (not full-screen?))
210 (full-screen main-window full-screen?))
212 (format #t "~s pressed; quitting.\n" c)
215 ;; The rotations are a hack to re-orient so that a translation in the Z
216 ;; axis will move us towards the origin.
217 (gl-rotate -10 1 0 0)
218 (gl-translate 0 0 10)
219 (gl-rotate 10 1 0 0))
221 (gl-rotate -10 1 0 0)
222 (gl-translate 0 0 -10)
223 (gl-rotate 10 1 0 0))
225 (set! running? (not running?))
226 (set-idle-callback (and running? (lambda () (on-idle)))))
228 (pk 'keyboard c x y)))))
230 (define (on-special keycode x y)
231 (pk 'special keycode x y))
233 (define (on-mouse button state x y)
234 (pk 'mouse button state x y))
236 (define (on-motion x y)
239 (define (on-visibility visible?)
240 (pk 'visible visible?))
243 (set-gl-matrix-mode (matrix-mode modelview))
244 ;; Rotate the camera 0.05 degree per frame about the Z axis. At 60 fps that's
245 ;; 120 seconds per full rotation. Rotating about the Z axis keeps all the
246 ;; quads facing our way.
247 (gl-rotate 0.05 0 0 1)
248 (update-particles 0.016)
250 (post-redisplay main-window))
252 (define (register-glut-callbacks)
253 ;; The trampolines allow the handlers to be overridden at runtime by
254 ;; an attached Guile REPL client.
255 (set-display-callback (lambda () (on-display)))
256 (set-reshape-callback (lambda (w h) (on-reshape w h)))
257 (set-keyboard-callback (lambda (k x y) (on-keyboard k x y)))
258 (set-special-callback (lambda (k x y) (on-special k x y)))
259 (set-mouse-callback (lambda (b s x y) (on-mouse b s x y)))
260 (set-motion-callback (lambda (x y) (on-motion x y)))
261 (set-visibility-callback (lambda (visible?) (on-visibility visible?)))
262 (set-idle-callback (lambda () (on-idle))))
266 (initialize-glut args
267 #:window-size '(640 . 480)
268 #:display-mode (display-mode rgba alpha double
270 (set! main-window (make-window "particle-system"))
271 (register-glut-callbacks)
272 (set-gl-clear-color 0 0 0 1)
273 (set-gl-clear-depth 1)
275 (set-gl-shade-model (shading-model smooth))
277 ;; Resetting the modelview matrix mode leaves the camera at the origin,
278 ;; oriented to look down the Z axis. Rotate up a bit so that we can see the
280 (set-gl-matrix-mode (matrix-mode modelview))
282 (gl-translate 0 0 -100)
285 (gl-enable (enable-cap depth-test))
287 (set! *random-state* (random-state-from-platform))
288 (prepare-particles (match args
290 ((_ n) (string->number n))))
295 (exit (main (program-arguments))))