mesh-transform-append added by nicklaf on Thu Feb 4 11:25:51 2021

(define (mesh-transform-append pairs #!key (position-name 'position)
                               (normal-name 'normal))
  (let* ((meshes (map first pairs))
         (transforms (map second pairs))
         (normal-transforms (if (> (length (car pairs)) 2)
                                (map third pairs)
                                #f))
         (mesh (mesh-append meshes))
         (attributes (mesh-vertex-attributes mesh))
         (position-offset (vertex-attribute-offset
                           (get-vertex-attribute position-name attributes)))
         (normal-offset (if* (find (lambda (attribute)
                                     (equal? normal-name
                                             (vertex-attribute-name attribute)))
                                   attributes)
                             (vertex-attribute-offset it)
                             #f))
         (stride (mesh-stride mesh))
         (vertex-data (bytevector->pointer (mesh-vertex-data mesh))))
    (let loop ((meshes meshes) (transforms transforms)
               (normal-transforms normal-transforms) (vertex-offset 0))
      (if (null? meshes)
          mesh
          (let ((n-vertices (mesh-n-vertices (car meshes)))
                (offset (* vertex-offset stride)))
            (m*vector-array! (car transforms)
                             (pointer+ vertex-data
                                       (+ position-offset offset))
                             stride: stride
                             length: n-vertices)
            (when normal-offset
              (m*vector-array! (transpose (inverse (car (or normal-transforms
                                                            transforms))))
                               (pointer+ vertex-data
                                         (+ normal-offset offset))
                               stride: stride
                               length: n-vertices))
            (loop (cdr meshes) (cdr transforms)
                  (and normal-transforms (cdr normal-transforms))
                  (+ vertex-offset n-vertices)))))))