render: buffer: Fixed default typed buffer length calculation.
authorDavid Thompson <dthompson2@worcester.edu>
Thu, 6 Sep 2018 13:27:46 +0000 (09:27 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Thu, 6 Sep 2018 13:27:46 +0000 (09:27 -0400)
* chickadee/render/buffer.scm (num-elements): New procedure.
  (make-typed-buffer): Default 'length' to the correct value in
  elements, not bytes, when a custom length is not specified.

chickadee/render/buffer.scm

index 4363d22..9726698 100644 (file)
@@ -242,14 +242,25 @@ vertex buffer data back to the GPU."
       (* (type-size (typed-buffer-type typed-buffer))
          (component-type-size (typed-buffer-component-type typed-buffer)))))
 
+(define (num-elements byte-length byte-offset type component-type)
+  (inexact->exact
+   (floor
+    (/ (- byte-length byte-offset)
+       (* (component-type-size component-type)
+          (type-size type))))))
+
+
 (define* (make-typed-buffer #:key
                             (name "anonymous")
                             buffer
-                            (offset 0)
+                            type
                             component-type
                             normalized?
-                            (length (buffer-length buffer))
-                            type
+                            (offset 0)
+                            (length (num-elements (buffer-length buffer)
+                                                  offset
+                                                  type
+                                                  component-type))
                             max
                             min
                             sparse)