diff options
-rw-r--r-- | chickadee/render/sprite.scm | 96 |
1 files changed, 60 insertions, 36 deletions
diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm index bc2ac1d..8dabd07 100644 --- a/chickadee/render/sprite.scm +++ b/chickadee/render/sprite.scm @@ -70,7 +70,8 @@ void main (void) { (force vertex-buffer) (force texcoord-buffer)))) (tmp-matrix (make-null-matrix4)) - (mvp (make-null-matrix4))) + (mvp (make-null-matrix4)) + (position (vec2 0 0))) (lambda (texture dimensions scale rotation blend-mode shader texture-rect) (with-mapped-vertex-buffer (force vertex-buffer) (let* ((x1 0) @@ -107,7 +108,9 @@ void main (void) { (when scale (matrix4-scale! tmp-matrix scale) (matrix4-mult! mvp mvp tmp-matrix)) - (matrix4-translate! tmp-matrix (rect-x dimensions) (rect-y dimensions)) + (set-vec2-x! position (rect-x dimensions)) + (set-vec2-y! position (rect-y dimensions)) + (matrix4-translate! tmp-matrix position) (matrix4-mult! mvp mvp tmp-matrix) (matrix4-mult! mvp mvp (current-projection)) (with-blend-mode blend-mode @@ -201,7 +204,15 @@ void main (void) { (define sprite-batch-add! (let ((tmp-matrix (make-null-matrix4)) - (matrix (make-null-matrix4))) + (matrix (make-null-matrix4)) + (position (vec2 0 0)) + (world1 (vec2 0 0)) + (world2 (vec2 0 0)) + (offset-bv (make-u32vector 1))) + (define (set-offset offset) + (u32vector-set! offset-bv 0 offset)) + (define (offset) + (u32vector-ref offset-bv 0)) (lambda (batch texture dimensions scale rotation blend-mode shader texture-rect) ;; Expand the buffers when necessary. @@ -217,15 +228,15 @@ void main (void) { (set-sprite-batch-blend-mode! batch blend-mode) (set-sprite-batch-shader! batch shader)) (let ((size (sprite-batch-size batch))) - (let* ((index-offset (* size 6)) - (index-vertex-offset (* size 4)) - (vertex-offset (* size 8)) ;; 4 vertices, 2 floats per vertex - (texture-offset (* size 8)) + (let* (;;(index-offset (* size 6)) + ;;(index-vertex-offset (* size 4)) + ;;(vertex-offset (* size 8)) ;; 4 vertices, 2 floats per vertex + ;;(texture-offset (* size 8)) (indices (vertex-buffer-data (sprite-batch-index-buffer batch))) (vertices (vertex-buffer-data (sprite-batch-position-buffer batch))) (texcoords (vertex-buffer-data (sprite-batch-texture-buffer batch))) - (local-x1 0) - (local-y1 0) + (local-x1 0.0) + (local-y1 0.0) (local-x2 (rect-width dimensions)) (local-y2 (rect-height dimensions)) (s1 (rect-left texture-rect)) @@ -239,45 +250,58 @@ void main (void) { (when scale (matrix4-scale! tmp-matrix scale) (matrix4-mult! matrix matrix tmp-matrix)) - (matrix4-translate! tmp-matrix (rect-x dimensions) (rect-y dimensions)) + (set-vec2-x! position (rect-x dimensions)) + (set-vec2-y! position (rect-y dimensions)) + (matrix4-translate! tmp-matrix position) (matrix4-mult! matrix matrix tmp-matrix) - (let-values (((world-x1 world-y1) - (transform matrix local-x1 local-y1)) - ((world-x2 world-y2) - (transform matrix local-x2 local-y2))) + (set-vec2-x! world1 local-x1) + (set-vec2-y! world1 local-y1) + (set-vec2-x! world2 local-x2) + (set-vec2-y! world2 local-y2) + (transform! matrix world1) + (transform! matrix world2) + (let ((world-x1 (vec2-x world1)) + (world-y1 (vec2-y world1)) + (world-x2 (vec2-x world2)) + (world-y2 (vec2-y world2))) ;; Add indices. - (u32vector-set! indices index-offset index-vertex-offset) - (u32vector-set! indices (+ index-offset 1) (+ index-vertex-offset 3)) - (u32vector-set! indices (+ index-offset 2) (+ index-vertex-offset 2)) - (u32vector-set! indices (+ index-offset 3) index-vertex-offset) - (u32vector-set! indices (+ index-offset 4) (+ index-vertex-offset 2)) - (u32vector-set! indices (+ index-offset 5) (+ index-vertex-offset 1)) + (set-offset (* size 4)) + (let ((index-vertex-offset (offset))) + (set-offset (* size 6)) + (u32vector-set! indices (offset) index-vertex-offset) + (u32vector-set! indices (+ (offset) 1) (+ index-vertex-offset 3)) + (u32vector-set! indices (+ (offset) 2) (+ index-vertex-offset 2)) + (u32vector-set! indices (+ (offset) 3) index-vertex-offset) + (u32vector-set! indices (+ (offset) 4) (+ index-vertex-offset 2)) + (u32vector-set! indices (+ (offset) 5) (+ index-vertex-offset 1))) ;; Add vertices. + (set-offset (* size 8)) ;; 4 vertices, 2 floats per vertex ;; Bottom-left - (f32vector-set! vertices vertex-offset world-x1) - (f32vector-set! vertices (+ vertex-offset 1) world-y1) + (f32vector-set! vertices (offset) world-x1) + (f32vector-set! vertices (+ (offset) 1) world-y1) ;; Bottom-right - (f32vector-set! vertices (+ vertex-offset 2) world-x2) - (f32vector-set! vertices (+ vertex-offset 3) world-y1) + (f32vector-set! vertices (+ (offset) 2) world-x2) + (f32vector-set! vertices (+ (offset) 3) world-y1) ;; Top-right - (f32vector-set! vertices (+ vertex-offset 4) world-x2) - (f32vector-set! vertices (+ vertex-offset 5) world-y2) + (f32vector-set! vertices (+ (offset) 4) world-x2) + (f32vector-set! vertices (+ (offset) 5) world-y2) ;; Top-left - (f32vector-set! vertices (+ vertex-offset 6) world-x1) - (f32vector-set! vertices (+ vertex-offset 7) world-y2) + (f32vector-set! vertices (+ (offset) 6) world-x1) + (f32vector-set! vertices (+ (offset) 7) world-y2) ;; Add texture coordinates. + (set-offset (* size 8)) ;; Bottom-left - (f32vector-set! texcoords texture-offset s1) - (f32vector-set! texcoords (+ texture-offset 1) t1) + (f32vector-set! texcoords (offset) s1) + (f32vector-set! texcoords (+ (offset) 1) t1) ;; Bottom-right - (f32vector-set! texcoords (+ texture-offset 2) s2) - (f32vector-set! texcoords (+ texture-offset 3) t1) + (f32vector-set! texcoords (+ (offset) 2) s2) + (f32vector-set! texcoords (+ (offset) 3) t1) ;; Top-right - (f32vector-set! texcoords (+ texture-offset 4) s2) - (f32vector-set! texcoords (+ texture-offset 5) t2) + (f32vector-set! texcoords (+ (offset) 4) s2) + (f32vector-set! texcoords (+ (offset) 5) t2) ;; Top-left - (f32vector-set! texcoords (+ texture-offset 6) s1) - (f32vector-set! texcoords (+ texture-offset 7) t2) + (f32vector-set! texcoords (+ (offset) 6) s1) + (f32vector-set! texcoords (+ (offset) 7) t2) (set-sprite-batch-size! batch (1+ size)))))))) (define *batch?* #f) |