summaryrefslogtreecommitdiff
path: root/2d/texture.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-02-15 21:17:47 -0500
committerDavid Thompson <dthompson2@worcester.edu>2014-02-16 10:46:58 -0500
commitd20605bb985002fc5f6285259bfd9c41d55e6fbe (patch)
treee687c6959ac0b4862dae9be01711365552f28107 /2d/texture.scm
parent11dd0ef869d2f45c88583464beef8a5243e3998a (diff)
Change sprite-vertex to texture-vertex.
* 2d/sprite.scm (sprite-vertex, sprite-vertex-size, x-offset, s-offset, pack-sprite-vertices, draw-sprite-vertices): Delete. (update-sprite-vertices!): Use 'pack-texture-vertices'. (make-sprite): Use 'texture-vertex'. (draw-sprite): Use 'draw-texture-vertices'. * 2d/texture.scm (texture-vertex, texture-vertex-size, x-offset, s-offset, pack-texture-vertices, draw-texture-vertices): New variables.
Diffstat (limited to '2d/texture.scm')
-rw-r--r--2d/texture.scm47
1 files changed, 46 insertions, 1 deletions
diff --git a/2d/texture.scm b/2d/texture.scm
index cfcd2bf..684ad42 100644
--- a/2d/texture.scm
+++ b/2d/texture.scm
@@ -25,6 +25,7 @@
(define-module (2d texture)
#:use-module (srfi srfi-9)
#:use-module (figl gl)
+ #:use-module (figl contrib packed-struct)
#:use-module (2d color)
#:use-module (2d helpers)
#:use-module (2d wrappers gl)
@@ -42,7 +43,9 @@
texture-s2
texture-t2
surface->texture
- draw-texture))
+ texture-vertex
+ pack-texture-vertices
+ draw-texture-vertices))
;;;
;;; Textures
@@ -150,3 +153,45 @@ that will be rendered, in pixels."
(texture (bitmap->texture bitmap)))
(freeimage-unload bitmap)
texture))
+
+;;;
+;;; Texture Vertices
+;;;
+
+(define-packed-struct texture-vertex
+ ;; Position
+ (x float)
+ (y float)
+ ;; Texture Coordinates
+ (s float)
+ (t float))
+
+(define texture-vertex-size (packed-struct-size texture-vertex))
+(define x-offset (packed-struct-offset texture-vertex x))
+(define s-offset (packed-struct-offset texture-vertex s))
+
+(define (pack-texture-vertices vertices offset width height s1 t1 s2 t2)
+ ;; Vertices go counter clockwise, starting from the top-left
+ ;; corner.
+ (pack vertices offset texture-vertex 0 0 s1 t1)
+ (pack vertices (+ offset 1) texture-vertex 0 height s1 t2)
+ (pack vertices (+ offset 2) texture-vertex width height s2 t2)
+ (pack vertices (+ offset 3) texture-vertex width 0 s2 t1))
+
+(define (draw-texture-vertices texture vertices size)
+ (let ((pointer-type (tex-coord-pointer-type float)))
+ (gl-enable-client-state (enable-cap vertex-array))
+ (gl-enable-client-state (enable-cap texture-coord-array))
+ (with-gl-bind-texture (texture-target texture-2d) (texture-id texture)
+ (set-gl-vertex-array pointer-type
+ vertices
+ 2
+ #:stride texture-vertex-size
+ #:offset x-offset)
+ (set-gl-texture-coordinates-array pointer-type
+ vertices
+ #:stride texture-vertex-size
+ #:offset s-offset)
+ (gl-draw-arrays (begin-mode quads) 0 (* size 4)))
+ (gl-disable-client-state (enable-cap texture-coord-array))
+ (gl-disable-client-state (enable-cap vertex-array))))