render: texture: Add texture region data type.
[chickadee.git] / chickadee / render / texture.scm
CommitLineData
98dc87a0
DT
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)
51d3c475 24 #:use-module (system foreign)
98dc87a0
DT
25 #:use-module (gl)
26 #:use-module ((gl enums)
27 #:select (texture-min-filter texture-mag-filter)
28 #:prefix gl:)
29 #:use-module ((sdl2 image) #:prefix sdl-image:)
30 #:use-module (sdl2 surface)
31 #:use-module (oop goops)
2f4eab3c 32 #:use-module (chickadee math rect)
98dc87a0
DT
33 #:use-module (chickadee render gl)
34 #:use-module (chickadee render gpu)
35 #:export (make-texture
36 load-image
37 texture?
38 texture-null?
39 texture-id
40 texture-parent
41 texture-width
42 texture-height
43 texture-min-filter
44 texture-mag-filter
45 texture-wrap-s
46 texture-wrap-t
47 null-texture
8152ddc9
DT
48 texture-set!
49 texture-ref
98dc87a0 50
c8457c8c
DT
51 make-texture-region
52 texture-region?
53 texture-region-texture
54 texture-region-x
55 texture-region-y
56 texture-region-width
57 texture-region-height
58
2f4eab3c
DT
59 texture-atlas
60 list->texture-atlas
61 split-texture
62 texture-atlas?
69d61338 63 texture-atlas-texture
2f4eab3c
DT
64 texture-atlas-ref))
65
66\f
98dc87a0
DT
67;;;
68;;; Textures
69;;;
70
71;; The <texture> object is a simple wrapper around an OpenGL texture
72;; id.
73(define-record-type <texture>
74 (%make-texture id width height min-filter mag-filter wrap-s wrap-t)
75 texture?
76 (id texture-id)
77 (width texture-width)
78 (height texture-height)
79 (min-filter texture-min-filter)
80 (mag-filter texture-mag-filter)
81 (wrap-s texture-wrap-s)
82 (wrap-t texture-wrap-t))
83
84(set-record-type-printer! <texture>
85 (lambda (texture port)
86 (format port
87 "#<texture width: ~d height: ~d min-filter: ~a mag-filter: ~a wrap-s: ~a wrap-t: ~a>"
88 (texture-width texture)
89 (texture-height texture)
90 (texture-min-filter texture)
91 (texture-mag-filter texture)
92 (texture-wrap-s texture)
93 (texture-wrap-t texture))))
94
95(define null-texture (%make-texture 0 0 0 'linear 'linear 'repeat 'repeat))
96
97(define <<texture>> (class-of null-texture))
98
99(define (texture-null? texture)
100 "Return #t if TEXTURE is the null texture."
101 (eq? texture null-texture))
102
103(define (free-texture texture)
104 (gl-delete-texture (texture-id texture)))
105
106(define-method (gpu-finalize (texture <<texture>>))
107 (free-texture texture))
108
8152ddc9
DT
109(define (make-apply-texture n)
110 (let ((texture-unit (+ (version-1-3 texture0) n)))
111 (lambda (texture)
112 (set-gl-active-texture texture-unit)
113 (gl-bind-texture (texture-target texture-2d)
114 (texture-id texture)))))
98dc87a0 115
8152ddc9
DT
116(define *texture-states*
117 (let ((states (make-vector 32)))
118 (let loop ((i 0))
119 (if (< i 32)
120 (begin
121 (vector-set! states i (make-gpu-state (make-apply-texture i)
122 null-texture))
123 (loop (1+ i)))
124 states))))
125
126(define (texture-ref! n)
127 (gpu-state-ref (vector-ref *texture-states* n)))
128
129(define (texture-set! n texture)
130 (gpu-state-set! (vector-ref *texture-states* n) texture))
98dc87a0
DT
131
132(define* (make-texture pixels width height #:key
133 (min-filter 'linear)
134 (mag-filter 'linear)
135 (wrap-s 'repeat)
136 (wrap-t 'repeat)
137 (format 'rgba))
138 "Translate the bytevector PIXELS into an OpenGL texture with
139dimensions WIDTHxHEIGHT where each pixel is in 32-bit RGBA format.
140The generated texture uses MIN-FILTER for downscaling and MAG-FILTER
141for upscaling. WRAP-S and WRAP-T are symbols that control how texture
142access is handled for texture coordinates outside the [0, 1] range.
143Allowed symbols are: repeat (the default), clamp, clamp-to-border,
144clamp-to-edge. FORMAT specifies the pixel format. Currently only
14532-bit RGBA format is supported."
146 (define (gl-wrap mode)
147 (match mode
148 ('repeat (texture-wrap-mode repeat))
149 ('clamp (texture-wrap-mode clamp))
150 ('clamp-to-border (texture-wrap-mode clamp-to-border-sgis))
151 ('clamp-to-edge (texture-wrap-mode clamp-to-edge-sgis))))
152
153 (let ((texture (gpu-guard
154 (%make-texture (gl-generate-texture) width height
155 min-filter mag-filter wrap-s wrap-t))))
8152ddc9 156 (texture-set! 0 texture)
98dc87a0
DT
157 (gl-texture-parameter (texture-target texture-2d)
158 (texture-parameter-name texture-min-filter)
159 (match min-filter
160 ('nearest (gl:texture-min-filter nearest))
161 ('linear (gl:texture-min-filter linear))))
162 (gl-texture-parameter (texture-target texture-2d)
163 (texture-parameter-name texture-mag-filter)
164 (match mag-filter
165 ('nearest (gl:texture-mag-filter nearest))
166 ('linear (gl:texture-mag-filter linear))))
167 (gl-texture-parameter (texture-target texture-2d)
168 (texture-parameter-name texture-wrap-s)
169 (gl-wrap wrap-s))
170 (gl-texture-parameter (texture-target texture-2d)
171 (texture-parameter-name texture-wrap-t)
172 (gl-wrap wrap-t))
173 (gl-texture-image-2d (texture-target texture-2d)
174 0 (pixel-format rgba) width height 0
175 (match format
176 ('rgba (pixel-format rgba)))
177 (color-pointer-type unsigned-byte)
51d3c475 178 (or pixels %null-pointer))
98dc87a0
DT
179 texture))
180
181(define (flip-pixels-vertically pixels width height)
182 "Create a new bytevector that reverses the rows in PIXELS, a WIDTH x
183HEIGHT, 32 bit color bytevector."
184 (let ((buffer (make-u8vector (bytevector-length pixels)))
185 (row-width (* width 4))) ; assuming 32 bit color
186 (let loop ((y 0))
187 (when (< y height)
188 (let* ((y* (- height y 1))
189 (source-start (* y row-width))
190 (target-start (* y* row-width)))
191 (bytevector-copy! pixels source-start buffer target-start row-width)
192 (loop (1+ y)))))
193 buffer))
194
195(define (surface->texture surface min-filter mag-filter wrap-s wrap-t)
196 "Convert SURFACE, an SDL2 surface object, into a texture that uses
197the given MIN-FILTER and MAG-FILTER."
198 ;; Convert to 32 bit RGBA color.
199 (call-with-surface (convert-surface-format surface 'abgr8888)
200 (lambda (surface)
201 (let* ((width (surface-width surface))
202 (height (surface-height surface))
203 ;; OpenGL textures use the bottom-left corner as the
204 ;; origin, whereas SDL uses the top-left, so the rows
205 ;; of pixels must be reversed before creating a
206 ;; texture from them.
207 (pixels (flip-pixels-vertically (surface-pixels surface)
208 width height)))
209 (make-texture pixels width height
210 #:min-filter min-filter
211 #:mag-filter mag-filter
212 #:wrap-s wrap-s
213 #:wrap-t wrap-t)))))
214
7015cd49 215(define* (load-image file #:key
98dc87a0
DT
216 (min-filter 'nearest)
217 (mag-filter 'nearest)
218 (wrap-s 'repeat)
219 (wrap-t 'repeat))
220 "Load a texture from an image in FILE. MIN-FILTER and MAG-FILTER
221describe the method that should be used for minification and
222magnification. Valid values are 'nearest and 'linear. By default,
223'nearest is used."
224 (call-with-surface (sdl-image:load-image file)
225 (lambda (surface)
226 (surface->texture surface min-filter mag-filter wrap-s wrap-t))))
2f4eab3c
DT
227
228\f
229;;;
c8457c8c
DT
230;;; Texture Regions
231;;;
232
233(define-record-type <texture-region>
234 (%make-texture-region texture x y width height gl-rect gl-size)
235 texture-region?
236 (texture texture-region-texture)
237 (x texture-region-x)
238 (y texture-region-y)
239 (width texture-region-width)
240 (height texture-region-height)
241 (gl-rect texture-region-gl-rect)
242 (gl-size texture-region-gl-size))
243
244(define (make-texture-region texture x y width height)
245 "Create a new texture region covering a section of TEXTURE defined
246by the bounding box X, Y, WIDTH, and HEIGHT. All coordinates are
247measured in pixels and must be integers."
248 (let* ((tw (texture-width texture))
249 (th (texture-height texture))
250 (gl-rect (make-rect (/ x tw) (/ y th)
251 (/ (+ x width) tw) (/ (+ y height) th))))
252 (%make-texture-region texture x y width height gl-rect
253 (f32vector width height))))
254
255\f
256;;;
2f4eab3c
DT
257;;; Texture Atlas
258;;;
259
260(define-record-type <texture-atlas>
261 (%make-texture-atlas texture vector)
262 texture-atlas?
263 (texture texture-atlas-texture)
264 (vector texture-atlas-vector))
265
266(define (list->texture-atlas texture rects)
267 "Return a new atlas for TEXTURE containing RECTS, a list of texture
268coordinate rects denoting the various tiles within."
269 (let ((v (make-vector (length rects))))
270 (let loop ((i 0)
271 (rects rects))
272 (match rects
273 (() (%make-texture-atlas texture v))
274 ((r . rest)
275 (vector-set! v i r)
276 (loop (1+ i) rest))))))
277
278(define (texture-atlas texture . rects)
279 "Return a new atlas for TEXTURE containing RECTS, a series of
280texture coordinate rect arguments denoting the various tiles within."
281 (list->texture-atlas texture rects))
282
283(define (texture-atlas-ref atlas index)
284 "Return the texture coordinate rect associated with INDEX in
285ATLAS."
286 (vector-ref (texture-atlas-vector atlas) index))
287
288(define* (split-texture texture tile-width tile-height #:key
289 (margin 0) (spacing 0))
290 "Return a new texture atlas that splits TEXTURE into a grid of
291TILE-WIDTH by TILE-HEIGHT rectangles. Optionally, each tile may have
292SPACING pixels of horizontal and vertical space between surrounding
293tiles and the entire image may have MARGIN pixels of empty space
294around its border.
295
296This type of texture atlas layout is very common for tile map
297terrain."
298 (let* ((w (texture-width texture))
299 (h (texture-height texture))
300 (sw (/ tile-width w))
301 (th (/ tile-height h))
302 (rows (/ (- h margin) (+ tile-height spacing)))
303 (columns (/ (- w margin) (+ tile-width spacing)))
304 (v (make-vector (* rows columns))))
305 (define (make-tile tx ty)
306 (let* ((x (+ (* tx (+ tile-width spacing)) margin))
307 (y (+ (* ty (+ tile-height spacing)) margin)))
308 (make-rect (/ x w) (/ y h) sw th)))
309 (let y-loop ((y 0))
310 (when (< y rows)
311 (let x-loop ((x 0))
312 (when (< x columns)
313 (vector-set! v (+ x (* y columns)) (make-tile x y))
314 (x-loop (1+ x))))
315 (y-loop (1+ y))))
316 (%make-texture-atlas texture v)))