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