summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-04-07 17:06:08 -0400
committerDavid Thompson <dthompson2@worcester.edu>2021-04-07 17:07:35 -0400
commitcd5bd61bfd9e41f9c6112d98ceae9b340b4b60ef (patch)
treedf7f0b15b019da6c2e12528076bc388e2439d9ea
parent37a466f7089b88f5554eafbe9b4bcee7290868c7 (diff)
graphics: Fix texture data being upside down.
-rw-r--r--chickadee/graphics/font.scm2
-rw-r--r--chickadee/graphics/sprite.scm18
-rw-r--r--chickadee/graphics/texture.scm9
-rw-r--r--chickadee/graphics/tiled.scm14
4 files changed, 26 insertions, 17 deletions
diff --git a/chickadee/graphics/font.scm b/chickadee/graphics/font.scm
index 8adba8d..3635676 100644
--- a/chickadee/graphics/font.scm
+++ b/chickadee/graphics/font.scm
@@ -126,7 +126,7 @@ display it at POINT-SIZE. By default, the ASCII character is used."
(row height))
(let ((gray (u8vector-ref glyph-pixels
(+ (* row pitch) column)))
- (offset (+ (* (+ y row) texture-size 4)
+ (offset (+ (* (- height (+ y row) 1) texture-size 4)
(* (+ x column) 4))))
(u8vector-set! pixels offset 255)
(u8vector-set! pixels (+ offset 1) 255)
diff --git a/chickadee/graphics/sprite.scm b/chickadee/graphics/sprite.scm
index 8ab12f9..3a30d49 100644
--- a/chickadee/graphics/sprite.scm
+++ b/chickadee/graphics/sprite.scm
@@ -122,13 +122,11 @@ void main (void) {
(t1 (rect-y texcoords))
(s2 (+ (rect-x texcoords) (rect-width texcoords)))
(t2 (+ (rect-y texcoords) (rect-height texcoords))))
- ;; Texture origin is at the top-left, so we need to flip the Y
- ;; coordinate relative to the vertices.
(sprite-vertex-append! geometry
- (x1 y1 s1 t2)
- (x2 y1 s2 t2)
- (x2 y2 s2 t1)
- (x1 y2 s1 t1))
+ (x1 y1 s1 t1)
+ (x2 y1 s2 t1)
+ (x2 y2 s2 t2)
+ (x1 y2 s1 t2))
(geometry-index-append! geometry 0 3 2 0 2 1)))
(with-graphics-state ((g:blend-mode blend-mode)
(g:texture-0 texture))
@@ -299,10 +297,10 @@ texture may be specified via the TEXTURE-REGION argument."
(b (color-b tint))
(a (color-a tint)))
(batched-sprite-append! geometry
- (x1 y1 s1 t2 r g b a)
- (x2 y2 s2 t2 r g b a)
- (x3 y3 s2 t1 r g b a)
- (x4 y4 s1 t1 r g b a))
+ (x1 y1 s1 t1 r g b a)
+ (x2 y2 s2 t1 r g b a)
+ (x3 y3 s2 t2 r g b a)
+ (x4 y4 s1 t2 r g b a))
(geometry-index-append! geometry
vertex-offset
(+ vertex-offset 3)
diff --git a/chickadee/graphics/texture.scm b/chickadee/graphics/texture.scm
index 3ced853..24aa0f5 100644
--- a/chickadee/graphics/texture.scm
+++ b/chickadee/graphics/texture.scm
@@ -248,9 +248,9 @@ the given MIN-FILTER and MAG-FILTER."
;; Convert to 32 bit RGBA color.
(sdl2:call-with-surface (sdl2:convert-surface-format surface 'abgr8888)
(lambda (surface)
- (let* ((width (sdl2:surface-width surface))
- (height (sdl2:surface-height surface))
- (pixels (sdl2:surface-pixels surface)))
+ (let ((width (sdl2:surface-width surface))
+ (height (sdl2:surface-height surface))
+ (pixels (sdl2:surface-pixels surface)))
;; Zero the alpha channel of pixels that match the transparent
;; color key.
(when transparent-color
@@ -263,7 +263,8 @@ the given MIN-FILTER and MAG-FILTER."
(= g (bytevector-u8-ref pixels (+ i 1)))
(= b (bytevector-u8-ref pixels (+ i 2))))
(bytevector-u8-set! pixels (+ i 3) 0)))))
- (make-texture pixels width height
+ (make-texture (flip-pixels-vertically pixels width height)
+ width height
#:min-filter min-filter
#:mag-filter mag-filter
#:wrap-s wrap-s
diff --git a/chickadee/graphics/tiled.scm b/chickadee/graphics/tiled.scm
index 6500a17..2692418 100644
--- a/chickadee/graphics/tiled.scm
+++ b/chickadee/graphics/tiled.scm
@@ -240,12 +240,21 @@
(duration (attr node 'duration string->number)))
;; TODO: lookup actual tile in tileset
(%make-animation-frame tile-id duration)))
+ (define (atlas-ref atlas id rows columns)
+ ;; Tiled enumerates tiles from the top-left of the tileset image,
+ ;; but here in OpenGL land the origin is in the bottom-left, so we
+ ;; have to do some math invert the rows.
+ (texture-atlas-ref atlas
+ (+ (* (- rows (floor (/ id columns)) 1)
+ columns)
+ (modulo id columns))))
(define (parse-tile node rows columns atlas batch)
(let ((id (attr node 'id string->number))
(animation (map parse-frame ((sxpath '(animation frame)) node)))
(properties (map parse-property
((sxpath '(properties property)) node))))
- (%make-tile id (texture-atlas-ref atlas id) batch animation properties)))
+ (%make-tile id (atlas-ref atlas id rows columns)
+ batch animation properties)))
(define (parse-tiles nodes size columns atlas batch)
(let ((table (make-hash-table))
(tiles (make-vector size))
@@ -257,7 +266,8 @@
(for-range ((i size))
(let ((tile
(or (hash-ref table i)
- (%make-tile i (texture-atlas-ref atlas i) batch #f '()))))
+ (%make-tile i (atlas-ref atlas i rows columns)
+ batch #f '()))))
(vector-set! tiles i tile)))
tiles))
(define (first-gid node)