client array example uses vertex array instead of quad array
authorAndy Wingo <wingo@pobox.com>
Fri, 15 Feb 2013 21:05:57 +0000 (22:05 +0100)
committerAndy Wingo <wingo@pobox.com>
Fri, 15 Feb 2013 21:05:57 +0000 (22:05 +0100)
* figl/contrib/packed-struct.scm (packed-struct-offset): New export.

* examples/particle-system/client-arrays.scm: Rewrite using a vertex
  array instead of a quad array.  It's the same thing, only a bit
  cleaner.

examples/particle-system/client-arrays.scm
figl/contrib/packed-struct.scm

index d05d637..bccf2af 100644 (file)
@@ -8,38 +8,14 @@
              (ice-9 format)
              (figl contrib packed-struct))
 
-(define-packed-struct color-quad
-  (x-- float)
-  (y-- float)
-  (z-- float)
-
-  (r-- float)
-  (g-- float)
-  (b-- float)
-
-  (x+- float)
-  (y+- float)
-  (z+- float)
-
-  (r+- float)
-  (g+- float)
-  (b+- float)
-
-  (x++ float)
-  (y++ float)
-  (z++ float)
-
-  (r++ float)
-  (g++ float)
-  (b++ float)
-
-  (x-+ float)
-  (y-+ float)
-  (z-+ float)
+(define-packed-struct color-vertex
+  (x float)
+  (y float)
+  (z float)
 
-  (r-+ float)
-  (g-+ float)
-  (b-+ float))
+  (r float)
+  (g float)
+  (b float))
 
 (define-packed-struct particle
   (x float)
   (vy float)
   (vz float))
 
-(define *quads* (make-packed-array color-quad 0))
+(define *vertices* (make-packed-array color-vertex 0))
 (define *particles* (make-packed-array particle 0))
 
 (define (draw-particles)
   (gl-enable-client-state (enable-cap vertex-array))
   (gl-enable-client-state (enable-cap color-array))
   (set-gl-vertex-array (vertex-pointer-type float)
-                       *quads*
-                       #:stride (/ (packed-struct-size color-quad) 4))
+                       *vertices*
+                       #:stride (packed-struct-size color-vertex)
+                       #:offset (packed-struct-offset color-vertex x))
   (set-gl-color-array (color-pointer-type float)
-                      *quads*
-                      #:stride (/ (packed-struct-size color-quad) 4)
-                      #:offset (/ (packed-struct-size color-quad) 8))
+                      *vertices*
+                      #:stride (packed-struct-size color-vertex)
+                      #:offset (packed-struct-offset color-vertex r))
   (gl-draw-arrays (begin-mode quads) 0
-                  (* 4 (packed-array-length *quads* color-quad)))
+                  (packed-array-length *vertices* color-vertex))
   (gl-disable-client-state (enable-cap color-array))
   (gl-disable-client-state (enable-cap vertex-array)))
 
            (x- (- x 0.5))
            (y- (- y 0.5))
            (x+ (+ x 0.5))
-           (y+ (+ y 0.5)))
-       (pack *quads* n color-quad
+           (y+ (+ y 0.5))
+           (base (* n 4)))
+       (pack *vertices* base color-vertex
              x- y- z
-             r g b
+             r g b)
+       (pack *vertices* (+ base 1) color-vertex
              x+ y- z
-             r g b
+             r g b)
+       (pack *vertices* (+ base 2) color-vertex
              x+ y+ z
-             r g b
+             r g b)
+       (pack *vertices* (+ base 3) color-vertex
              x- y+ z
              r g b)))))
 
 
 (define (prepare-particles n)
   (set! *particles* (make-packed-array particle n))
-  (set! *quads* (make-packed-array color-quad n))
+  (set! *vertices* (make-packed-array color-vertex (* n 4)))
   (pack-each
    *particles*
    particle
index d962de1..19f43b4 100644 (file)
@@ -27,6 +27,7 @@
   #:export (
             define-packed-struct
             packed-struct-size
+            packed-struct-offset
             pack pack* unpack unpack*
 
             make-packed-array
              #`(define-inlinable (name method field)
                  (case method
                    ((size) byte-size)
+                   ((offset)
+                    (case field
+                      ((field-name) field-offset)
+                      ...
+                      (else (error "unknown field" field))))
                    ((unpacker)
                     (lambda (bv offset k)
                       (k (field-ref bv (+ offset field-offset))
 (define-syntax-rule (packed-struct-size type)
   (type 'size #f))
 
+(define-syntax-rule (packed-struct-offset type field)
+  (type 'offset 'field))
+
 (define-syntax-rule (packed-struct-getter type field)
   (type 'getter 'field))