render: Change texture origin to top-left.
[chickadee.git] / chickadee / render / texture.scm
1 ;;; Chickadee Game Toolkit
2 ;;; Copyright © 2016 David Thompson <davet@gnu.org>
3 ;;;
4 ;;; Chickadee is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published
6 ;;; by the Free Software Foundation, either version 3 of the License,
7 ;;; or (at your option) any later version.
8 ;;;
9 ;;; Chickadee is distributed in the hope that it will be useful, but
10 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;; General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see
16 ;;; <http://www.gnu.org/licenses/>.
17
18 (define-module (chickadee render texture)
19 #:use-module (ice-9 format)
20 #:use-module (ice-9 match)
21 #:use-module (rnrs bytevectors)
22 #:use-module (srfi srfi-9)
23 #:use-module (srfi srfi-9 gnu)
24 #:use-module (system foreign)
25 #:use-module (gl)
26 #:use-module (gl enums)
27 #:use-module (sdl2 surface)
28 #:use-module (oop goops)
29 #:use-module (chickadee math rect)
30 #:use-module (chickadee render gl)
31 #:use-module (chickadee render gpu)
32 #:export (make-texture
33 make-texture-region
34 load-image
35 texture?
36 texture-region?
37 texture-null?
38 texture-id
39 texture-parent
40 texture-min-filter
41 texture-mag-filter
42 texture-wrap-s
43 texture-wrap-t
44 texture-x
45 texture-y
46 texture-width
47 texture-height
48 texture-gl-rect
49 texture-gl-tex-rect
50 null-texture
51 texture-set!
52 texture-ref
53
54 texture-atlas
55 list->texture-atlas
56 split-texture
57 texture-atlas?
58 texture-atlas-texture
59 texture-atlas-ref))
60
61 \f
62 ;;;
63 ;;; Textures
64 ;;;
65
66 ;; The <texture> object is a simple wrapper around an OpenGL texture
67 ;; id.
68 (define-record-type <texture>
69 (%make-texture id parent min-filter mag-filter wrap-s wrap-t
70 x y width height gl-rect gl-tex-rect)
71 texture?
72 (id texture-id)
73 (parent texture-parent)
74 (min-filter texture-min-filter)
75 (mag-filter texture-mag-filter)
76 (wrap-s texture-wrap-s)
77 (wrap-t texture-wrap-t)
78 (x texture-x)
79 (y texture-y)
80 (width texture-width)
81 (height texture-height)
82 (gl-rect texture-gl-rect)
83 (gl-tex-rect texture-gl-tex-rect))
84
85 (set-record-type-printer! <texture>
86 (lambda (texture port)
87 (format port
88 "#<texture region?: ~a x: ~d y: ~d width: ~d height: ~d min-filter: ~a mag-filter: ~a wrap-s: ~a wrap-t: ~a>"
89 (texture-region? texture)
90 (texture-x texture)
91 (texture-y texture)
92 (texture-width texture)
93 (texture-height texture)
94 (texture-min-filter texture)
95 (texture-mag-filter texture)
96 (texture-wrap-s texture)
97 (texture-wrap-t texture))))
98
99 (define null-texture
100 (%make-texture 0 #f 'linear 'linear 'repeat 'repeat 0 0 0 0
101 (make-rect 0.0 0.0 0.0 0.0) (make-rect 0.0 0.0 0.0 0.0)))
102
103 (define <<texture>> (class-of null-texture))
104
105 (define (texture-null? texture)
106 "Return #t if TEXTURE is the null texture."
107 (eq? texture null-texture))
108
109 (define (texture-region? texture)
110 (texture? (texture-parent texture)))
111
112 (define (free-texture texture)
113 (gl-delete-texture (texture-id texture)))
114
115 (define-method (gpu-finalize (texture <<texture>>))
116 (free-texture texture))
117
118 (define (make-apply-texture n)
119 (let ((texture-unit (+ (version-1-3 texture0) n)))
120 (lambda (texture)
121 (set-gl-active-texture texture-unit)
122 (gl-bind-texture (texture-target texture-2d)
123 (texture-id texture)))))
124
125 (define *texture-states*
126 (let ((states (make-vector 32)))
127 (let loop ((i 0))
128 (if (< i 32)
129 (begin
130 (vector-set! states i (make-gpu-state (make-apply-texture i)
131 null-texture))
132 (loop (1+ i)))
133 states))))
134
135 (define (texture-ref! n)
136 (gpu-state-ref (vector-ref *texture-states* n)))
137
138 (define (texture-set! n texture)
139 (gpu-state-set! (vector-ref *texture-states* n) texture))
140
141 (define* (make-texture pixels width height #:key
142 (min-filter 'linear)
143 (mag-filter 'linear)
144 (wrap-s 'repeat)
145 (wrap-t 'repeat)
146 (format 'rgba))
147 "Translate the bytevector PIXELS into an OpenGL texture with
148 dimensions WIDTHxHEIGHT where each pixel is in 32-bit RGBA format.
149 The generated texture uses MIN-FILTER for downscaling and MAG-FILTER
150 for upscaling. WRAP-S and WRAP-T are symbols that control how texture
151 access is handled for texture coordinates outside the [0, 1] range.
152 Allowed symbols are: repeat (the default), clamp, clamp-to-border,
153 clamp-to-edge. FORMAT specifies the pixel format. Currently only
154 32-bit RGBA format is supported."
155 (define (gl-wrap mode)
156 (match mode
157 ('repeat (texture-wrap-mode repeat))
158 ('clamp (texture-wrap-mode clamp))
159 ('clamp-to-border (texture-wrap-mode clamp-to-border-sgis))
160 ('clamp-to-edge (texture-wrap-mode clamp-to-edge-sgis))))
161
162 (let ((texture (gpu-guard
163 (%make-texture (gl-generate-texture) #f
164 min-filter mag-filter wrap-s wrap-t
165 0 0 width height
166 (make-rect 0.0 0.0 width height)
167 (make-rect 0.0 0.0 1.0 1.0)))))
168 (texture-set! 0 texture)
169 (gl-texture-parameter (texture-target texture-2d)
170 (texture-parameter-name texture-min-filter)
171 (match min-filter
172 ('nearest 9728)
173 ('linear 9729)))
174 (gl-texture-parameter (texture-target texture-2d)
175 (texture-parameter-name texture-mag-filter)
176 (match mag-filter
177 ('nearest 9728)
178 ('linear 9729)))
179 (gl-texture-parameter (texture-target texture-2d)
180 (texture-parameter-name texture-wrap-s)
181 (gl-wrap wrap-s))
182 (gl-texture-parameter (texture-target texture-2d)
183 (texture-parameter-name texture-wrap-t)
184 (gl-wrap wrap-t))
185 (gl-texture-image-2d (texture-target texture-2d)
186 0 (pixel-format rgba) width height 0
187 (match format
188 ('rgba (pixel-format rgba)))
189 (color-pointer-type unsigned-byte)
190 (or pixels %null-pointer))
191 texture))
192
193 (define (make-texture-region texture rect)
194 "Create a new texture region covering a section of TEXTURE defined
195 by the bounding box RECT."
196 (let* ((pw (texture-width texture))
197 (ph (texture-height texture))
198 (x (rect-x rect))
199 (y (rect-y rect))
200 (w (rect-width rect))
201 (h (rect-height rect))
202 (vert-rect (make-rect 0.0 0.0 w h))
203 (tex-rect (make-rect (/ x pw) (/ y ph) (/ w pw) (/ h ph))))
204 (%make-texture (texture-id texture)
205 texture
206 (texture-min-filter texture)
207 (texture-mag-filter texture)
208 (texture-wrap-s texture)
209 (texture-wrap-t texture)
210 x y w h
211 vert-rect
212 tex-rect)))
213
214 (define (flip-pixels-vertically pixels width height)
215 "Create a new bytevector that reverses the rows in PIXELS, a WIDTH x
216 HEIGHT, 32 bit color bytevector."
217 (let ((buffer (make-u8vector (bytevector-length pixels)))
218 (row-width (* width 4))) ; assuming 32 bit color
219 (let loop ((y 0))
220 (when (< y height)
221 (let* ((y* (- height y 1))
222 (source-start (* y row-width))
223 (target-start (* y* row-width)))
224 (bytevector-copy! pixels source-start buffer target-start row-width)
225 (loop (1+ y)))))
226 buffer))
227
228 (define (surface->texture surface min-filter mag-filter wrap-s wrap-t)
229 "Convert SURFACE, an SDL2 surface object, into a texture that uses
230 the given MIN-FILTER and MAG-FILTER."
231 ;; Convert to 32 bit RGBA color.
232 (call-with-surface (convert-surface-format surface 'abgr8888)
233 (lambda (surface)
234 (let* ((width (surface-width surface))
235 (height (surface-height surface))
236 (pixels (surface-pixels surface)))
237 (make-texture pixels width height
238 #:min-filter min-filter
239 #:mag-filter mag-filter
240 #:wrap-s wrap-s
241 #:wrap-t wrap-t)))))
242
243 (define* (load-image file #:key
244 (min-filter 'nearest)
245 (mag-filter 'nearest)
246 (wrap-s 'repeat)
247 (wrap-t 'repeat))
248 "Load a texture from an image in FILE. MIN-FILTER and MAG-FILTER
249 describe the method that should be used for minification and
250 magnification. Valid values are 'nearest and 'linear. By default,
251 'nearest is used."
252 (call-with-surface ((@ (sdl2 image) load-image) file)
253 (lambda (surface)
254 (surface->texture surface min-filter mag-filter wrap-s wrap-t))))
255
256 \f
257 ;;;
258 ;;; Texture Atlas
259 ;;;
260
261 (define-record-type <texture-atlas>
262 (%make-texture-atlas texture vector)
263 texture-atlas?
264 (texture texture-atlas-texture)
265 (vector texture-atlas-vector))
266
267 (define (display-texture-atlas atlas port)
268 (format port
269 "#<texture-atlas texture: ~a size: ~d>"
270 (texture-atlas-texture atlas)
271 (vector-length (texture-atlas-vector atlas))))
272
273 (set-record-type-printer! <texture-atlas> display-texture-atlas)
274
275 (define (list->texture-atlas texture rects)
276 "Return a new atlas for TEXTURE containing RECTS, a list of texture
277 coordinate rects denoting the various tiles within."
278 (let ((v (make-vector (length rects))))
279 (let loop ((i 0)
280 (rects rects))
281 (match rects
282 (() (%make-texture-atlas texture v))
283 (((x y width height) . rest)
284 (vector-set! v i (make-texture-region texture (make-rect x y width height)))
285 (loop (1+ i) rest))))))
286
287 (define (texture-atlas texture . rects)
288 "Return a new atlas for TEXTURE containing RECTS, a series of
289 4-tuples in the form (x y width height) describing the various tiles
290 within."
291 (list->texture-atlas texture rects))
292
293 (define (texture-atlas-ref atlas index)
294 "Return the texture region associated with INDEX in
295 ATLAS."
296 (vector-ref (texture-atlas-vector atlas) index))
297
298 (define* (split-texture texture tile-width tile-height #:key
299 (margin 0) (spacing 0))
300 "Return a new texture atlas that splits TEXTURE into a grid of
301 TILE-WIDTH by TILE-HEIGHT rectangles. Optionally, each tile may have
302 SPACING pixels of horizontal and vertical space between surrounding
303 tiles and the entire image may have MARGIN pixels of empty space
304 around its border.
305
306 This type of texture atlas layout is very common for tile map
307 terrain."
308 (let* ((w (texture-width texture))
309 (h (texture-height texture))
310 (sw (/ tile-width w))
311 (th (/ tile-height h))
312 (rows (/ (- h margin) (+ tile-height spacing)))
313 (columns (/ (- w margin) (+ tile-width spacing)))
314 (v (make-vector (* rows columns))))
315 (define (make-tile tx ty)
316 (let* ((x (+ (* tx (+ tile-width spacing)) margin))
317 (y (+ (* ty (+ tile-height spacing)) margin)))
318 (make-texture-region texture (make-rect x y tile-width tile-height))))
319 (let y-loop ((y 0))
320 (when (< y rows)
321 (let x-loop ((x 0))
322 (when (< x columns)
323 (vector-set! v (+ x (* y columns)) (make-tile x y))
324 (x-loop (1+ x))))
325 (y-loop (1+ y))))
326 (%make-texture-atlas texture v)))