1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs cache colors.constants destructors kernel
4 opengl opengl.gl opengl.capabilities combinators images
5 images.tesselation grouping specialized-arrays.float sequences math
6 math.vectors math.matrices generalizations fry arrays namespaces
7 system locals literals ;
10 SYMBOL: non-power-of-2-textures?
12 : check-extensions ( -- )
13 #! ATI frglx driver doesn't implement GL_ARB_texture_non_power_of_two properly.
14 #! See thread 'Linux font display problem' April 2009 on Factor-talk
15 gl-vendor "ATI Technologies Inc." = not os macosx? or [
16 "2.0" { "GL_ARB_texture_non_power_of_two" }
17 has-gl-version-or-extensions?
18 non-power-of-2-textures? set
21 : gen-texture ( -- id ) [ glGenTextures ] (gen-gl-object) ;
23 : delete-texture ( id -- ) [ glDeleteTextures ] (delete-gl-object) ;
25 ERROR: unsupported-component-order component-order component-type ;
27 CONSTANT: image-internal-formats H{
28 { { A ubyte-components } $ GL_ALPHA8 }
29 { { A ushort-components } $ GL_ALPHA16 }
30 { { A half-components } $ GL_ALPHA16F_ARB }
31 { { A float-components } $ GL_ALPHA32F_ARB }
32 { { A byte-integer-components } $ GL_ALPHA8I_EXT }
33 { { A ubyte-integer-components } $ GL_ALPHA8UI_EXT }
34 { { A short-integer-components } $ GL_ALPHA16I_EXT }
35 { { A ushort-integer-components } $ GL_ALPHA16UI_EXT }
36 { { A int-integer-components } $ GL_ALPHA32I_EXT }
37 { { A uint-integer-components } $ GL_ALPHA32UI_EXT }
39 { { L ubyte-components } $ GL_LUMINANCE8 }
40 { { L ushort-components } $ GL_LUMINANCE16 }
41 { { L half-components } $ GL_LUMINANCE16F_ARB }
42 { { L float-components } $ GL_LUMINANCE32F_ARB }
43 { { L byte-integer-components } $ GL_LUMINANCE8I_EXT }
44 { { L ubyte-integer-components } $ GL_LUMINANCE8UI_EXT }
45 { { L short-integer-components } $ GL_LUMINANCE16I_EXT }
46 { { L ushort-integer-components } $ GL_LUMINANCE16UI_EXT }
47 { { L int-integer-components } $ GL_LUMINANCE32I_EXT }
48 { { L uint-integer-components } $ GL_LUMINANCE32UI_EXT }
50 { { R ubyte-components } $ GL_R8 }
51 { { R ushort-components } $ GL_R16 }
52 { { R half-components } $ GL_R16F }
53 { { R float-components } $ GL_R32F }
54 { { R byte-integer-components } $ GL_R8I }
55 { { R ubyte-integer-components } $ GL_R8UI }
56 { { R short-integer-components } $ GL_R16I }
57 { { R ushort-integer-components } $ GL_R16UI }
58 { { R int-integer-components } $ GL_R32I }
59 { { R uint-integer-components } $ GL_R32UI }
61 { { INTENSITY ubyte-components } $ GL_INTENSITY8 }
62 { { INTENSITY ushort-components } $ GL_INTENSITY16 }
63 { { INTENSITY half-components } $ GL_INTENSITY16F_ARB }
64 { { INTENSITY float-components } $ GL_INTENSITY32F_ARB }
65 { { INTENSITY byte-integer-components } $ GL_INTENSITY8I_EXT }
66 { { INTENSITY ubyte-integer-components } $ GL_INTENSITY8UI_EXT }
67 { { INTENSITY short-integer-components } $ GL_INTENSITY16I_EXT }
68 { { INTENSITY ushort-integer-components } $ GL_INTENSITY16UI_EXT }
69 { { INTENSITY int-integer-components } $ GL_INTENSITY32I_EXT }
70 { { INTENSITY uint-integer-components } $ GL_INTENSITY32UI_EXT }
72 { { DEPTH ushort-components } $ GL_DEPTH_COMPONENT16 }
73 { { DEPTH u-24-components } $ GL_DEPTH_COMPONENT24 }
74 { { DEPTH uint-components } $ GL_DEPTH_COMPONENT32 }
76 { { LA ubyte-components } $ GL_LUMINANCE8_ALPHA8 }
77 { { LA ushort-components } $ GL_LUMINANCE16_ALPHA16 }
78 { { LA half-components } $ GL_LUMINANCE_ALPHA16F_ARB }
79 { { LA float-components } $ GL_LUMINANCE_ALPHA32F_ARB }
80 { { LA byte-integer-components } $ GL_LUMINANCE_ALPHA8I_EXT }
81 { { LA ubyte-integer-components } $ GL_LUMINANCE_ALPHA8UI_EXT }
82 { { LA short-integer-components } $ GL_LUMINANCE_ALPHA16I_EXT }
83 { { LA ushort-integer-components } $ GL_LUMINANCE_ALPHA16UI_EXT }
84 { { LA int-integer-components } $ GL_LUMINANCE_ALPHA32I_EXT }
85 { { LA uint-integer-components } $ GL_LUMINANCE_ALPHA32UI_EXT }
87 { { RG ubyte-components } $ GL_RG8 }
88 { { RG ushort-components } $ GL_RG16 }
89 { { RG half-components } $ GL_RG16F }
90 { { RG float-components } $ GL_RG32F }
91 { { RG byte-integer-components } $ GL_RG8I }
92 { { RG ubyte-integer-components } $ GL_RG8UI }
93 { { RG short-integer-components } $ GL_RG16I }
94 { { RG ushort-integer-components } $ GL_RG16UI }
95 { { RG int-integer-components } $ GL_RG32I }
96 { { RG uint-integer-components } $ GL_RG32UI }
98 { { DEPTH-STENCIL u-24-8-components } $ GL_DEPTH24_STENCIL8_EXT }
100 { { RGB ubyte-components } $ GL_RGB8 }
101 { { RGB ushort-components } $ GL_RGB16 }
102 { { RGB half-components } $ GL_RGB16F_ARB }
103 { { RGB float-components } $ GL_RGB32F_ARB }
104 { { RGB byte-integer-components } $ GL_RGB8I_EXT }
105 { { RGB ubyte-integer-components } $ GL_RGB8UI_EXT }
106 { { RGB byte-integer-components } $ GL_RGB8I_EXT }
107 { { RGB ubyte-integer-components } $ GL_RGB8UI_EXT }
108 { { RGB short-integer-components } $ GL_RGB16I_EXT }
109 { { RGB ushort-integer-components } $ GL_RGB16UI_EXT }
110 { { RGB int-integer-components } $ GL_RGB32I_EXT }
111 { { RGB uint-integer-components } $ GL_RGB32UI_EXT }
112 { { RGB u-5-6-5-components } $ GL_RGB5 }
113 { { RGB u-9-9-9-e5-components } $ GL_RGB9_E5_EXT }
114 { { RGB float-11-11-10-components } $ GL_R11F_G11F_B10F_EXT }
116 { { RGBA ubyte-components } $ GL_RGBA8 }
117 { { RGBA ushort-components } $ GL_RGBA16 }
118 { { RGBA half-components } $ GL_RGBA16F_ARB }
119 { { RGBA float-components } $ GL_RGBA32F_ARB }
120 { { RGBA byte-integer-components } $ GL_RGBA8I_EXT }
121 { { RGBA ubyte-integer-components } $ GL_RGBA8UI_EXT }
122 { { RGBA byte-integer-components } $ GL_RGBA8I_EXT }
123 { { RGBA ubyte-integer-components } $ GL_RGBA8UI_EXT }
124 { { RGBA short-integer-components } $ GL_RGBA16I_EXT }
125 { { RGBA ushort-integer-components } $ GL_RGBA16UI_EXT }
126 { { RGBA int-integer-components } $ GL_RGBA32I_EXT }
127 { { RGBA uint-integer-components } $ GL_RGBA32UI_EXT }
128 { { RGBA u-5-5-5-1-components } $ GL_RGB5_A1 }
129 { { RGBA u-10-10-10-2-components } $ GL_RGB10_A2 }
132 GENERIC: fix-internal-component-order ( order -- order' )
134 M: object fix-internal-component-order ;
135 M: BGR fix-internal-component-order drop RGB ;
136 M: BGRA fix-internal-component-order drop RGBA ;
137 M: ARGB fix-internal-component-order drop RGBA ;
138 M: ABGR fix-internal-component-order drop RGBA ;
139 M: RGBX fix-internal-component-order drop RGBA ;
140 M: BGRX fix-internal-component-order drop RGBA ;
141 M: XRGB fix-internal-component-order drop RGBA ;
142 M: XBGR fix-internal-component-order drop RGBA ;
144 : image-internal-format ( component-order component-type -- internal-format )
146 [ fix-internal-component-order ] dip 2array image-internal-formats at
147 [ 2nip ] [ unsupported-component-order ] if* ;
149 : reversed-type? ( component-type -- ? )
150 { u-9-9-9-e5-components float-11-11-10-components } member? ;
152 : (component-order>format) ( component-order component-type -- gl-format )
153 dup unnormalized-integer-components? [
155 { A [ drop GL_ALPHA_INTEGER_EXT ] }
156 { L [ drop GL_LUMINANCE_INTEGER_EXT ] }
157 { R [ drop GL_RED_INTEGER_EXT ] }
158 { LA [ drop GL_LUMINANCE_ALPHA_INTEGER_EXT ] }
159 { RG [ drop GL_RG_INTEGER ] }
160 { BGR [ drop GL_BGR_INTEGER_EXT ] }
161 { RGB [ drop GL_RGB_INTEGER_EXT ] }
162 { BGRA [ drop GL_BGRA_INTEGER_EXT ] }
163 { RGBA [ drop GL_RGBA_INTEGER_EXT ] }
164 { BGRX [ drop GL_BGRA_INTEGER_EXT ] }
165 { RGBX [ drop GL_RGBA_INTEGER_EXT ] }
166 [ swap unsupported-component-order ]
170 { A [ drop GL_ALPHA ] }
171 { L [ drop GL_LUMINANCE ] }
172 { R [ drop GL_RED ] }
173 { LA [ drop GL_LUMINANCE_ALPHA ] }
174 { RG [ drop GL_RG ] }
175 { BGR [ reversed-type? GL_RGB GL_BGR ? ] }
176 { RGB [ reversed-type? GL_BGR GL_RGB ? ] }
177 { BGRA [ drop GL_BGRA ] }
178 { RGBA [ drop GL_RGBA ] }
179 { ARGB [ drop GL_BGRA ] }
180 { ABGR [ drop GL_RGBA ] }
181 { BGRX [ drop GL_BGRA ] }
182 { RGBX [ drop GL_RGBA ] }
183 { XRGB [ drop GL_BGRA ] }
184 { XBGR [ drop GL_RGBA ] }
185 { INTENSITY [ drop GL_INTENSITY ] }
186 { DEPTH [ drop GL_DEPTH_COMPONENT ] }
187 { DEPTH-STENCIL [ drop GL_DEPTH_STENCIL_EXT ] }
188 [ swap unsupported-component-order ]
192 GENERIC: (component-type>type) ( component-order component-type -- gl-type )
194 M: object (component-type>type) unsupported-component-order ;
196 : four-channel-alpha-first? ( component-order component-type -- ? )
197 over component-count 4 =
198 [ drop alpha-channel-precedes-colors? ]
199 [ unsupported-component-order ] if ;
201 : not-alpha-first ( component-order component-type -- )
202 over alpha-channel-precedes-colors?
203 [ unsupported-component-order ]
206 M: ubyte-components (component-type>type)
207 drop alpha-channel-precedes-colors?
208 [ GL_UNSIGNED_INT_8_8_8_8_REV ]
209 [ GL_UNSIGNED_BYTE ] if ;
211 M: ushort-components (component-type>type) not-alpha-first GL_UNSIGNED_SHORT ;
212 M: uint-components (component-type>type) not-alpha-first GL_UNSIGNED_INT ;
213 M: half-components (component-type>type) not-alpha-first GL_HALF_FLOAT_ARB ;
214 M: float-components (component-type>type) not-alpha-first GL_FLOAT ;
215 M: byte-integer-components (component-type>type) not-alpha-first GL_BYTE ;
216 M: ubyte-integer-components (component-type>type) not-alpha-first GL_UNSIGNED_BYTE ;
217 M: short-integer-components (component-type>type) not-alpha-first GL_SHORT ;
218 M: ushort-integer-components (component-type>type) not-alpha-first GL_UNSIGNED_SHORT ;
219 M: int-integer-components (component-type>type) not-alpha-first GL_INT ;
220 M: uint-integer-components (component-type>type) not-alpha-first GL_UNSIGNED_INT ;
222 M: u-5-5-5-1-components (component-type>type)
223 four-channel-alpha-first?
224 [ GL_UNSIGNED_SHORT_1_5_5_5_REV ]
225 [ GL_UNSIGNED_SHORT_5_5_5_1 ] if ;
227 M: u-5-6-5-components (component-type>type) 2drop GL_UNSIGNED_SHORT_5_6_5 ;
229 M: u-10-10-10-2-components (component-type>type)
230 four-channel-alpha-first?
231 [ GL_UNSIGNED_INT_2_10_10_10_REV ]
232 [ GL_UNSIGNED_INT_10_10_10_2 ] if ;
234 M: u-24-components (component-type>type)
236 [ 2drop GL_UNSIGNED_INT ] [ unsupported-component-order ] if ;
238 M: u-24-8-components (component-type>type)
240 [ 2drop GL_UNSIGNED_INT_24_8_EXT ] [ unsupported-component-order ] if ;
242 M: u-9-9-9-e5-components (component-type>type)
244 [ 2drop GL_UNSIGNED_INT_5_9_9_9_REV_EXT ] [ unsupported-component-order ] if ;
246 M: float-11-11-10-components (component-type>type)
248 [ 2drop GL_UNSIGNED_INT_10F_11F_11F_REV_EXT ] [ unsupported-component-order ] if ;
250 : image-data-format ( component-order component-type -- gl-format gl-type )
251 [ (component-order>format) ] [ (component-type>type) ] 2bi ;
255 : draw-texture ( texture -- ) display-list>> [ glCallList ] when* ;
257 GENERIC: draw-scaled-texture ( dim texture -- )
261 : (image-format) ( component-order component-type -- internal-format format type )
262 [ image-internal-format ] [ image-data-format ] 2bi ;
264 : image-format ( image -- internal-format format type )
265 [ component-order>> ] [ component-type>> ] bi (image-format) ;
269 TUPLE: single-texture image dim loc texture-coords texture display-list disposed ;
271 : adjust-texture-dim ( dim -- dim' )
272 non-power-of-2-textures? get [
273 [ dup 1 = [ next-power-of-2 ] unless ] map
276 :: tex-image ( image bitmap -- )
277 image image-format :> type :> format :> internal-format
278 GL_TEXTURE_2D 0 internal-format
279 image dim>> adjust-texture-dim first2 0
280 format type bitmap glTexImage2D ;
282 : tex-sub-image ( image -- )
283 [ GL_TEXTURE_2D 0 0 0 ] dip
285 [ image-format [ drop ] 2dip ]
289 : init-texture ( -- )
290 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri
291 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri
292 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT glTexParameteri
293 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT glTexParameteri ;
295 : with-texturing ( quot -- )
298 GL_TEXTURE_COORD_ARRAY [
299 COLOR: white gl-color
301 ] do-enabled-client-state
303 ] do-enabled ; inline
305 : (draw-textured-rect) ( dim texture -- )
307 [ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
308 [ init-texture texture-coords>> gl-texture-coord-pointer ] tri
311 : draw-textured-rect ( dim texture -- )
313 [ image>> has-alpha? [ GL_BLEND glDisable ] unless ]
314 [ (draw-textured-rect) GL_TEXTURE_2D 0 glBindTexture ]
315 [ image>> has-alpha? [ GL_BLEND glEnable ] unless ]
319 : texture-coords ( texture -- coords )
320 [ [ dim>> ] [ image>> dim>> adjust-texture-dim ] bi v/ ]
322 image>> upside-down?>>
323 { { 0 1 } { 1 1 } { 1 0 } { 0 0 } }
324 { { 0 0 } { 1 0 } { 1 1 } { 0 1 } } ?
326 [ v* ] with map float-array{ } join ;
328 : make-texture-display-list ( texture -- dlist )
329 GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
331 : <single-texture> ( image loc -- texture )
332 single-texture new swap >>loc swap [ >>image ] [ dim>> >>dim ] bi
333 dup image>> dim>> product 0 = [
334 dup texture-coords >>texture-coords
335 dup image>> make-texture >>texture
336 dup make-texture-display-list >>display-list
339 M: single-texture dispose*
340 [ texture>> [ delete-texture ] when* ]
341 [ display-list>> [ delete-dlist ] when* ] bi ;
343 M: single-texture draw-scaled-texture
344 2dup dim>> = [ nip draw-texture ] [
345 dup texture>> [ draw-textured-rect ] [ 2drop ] if
348 TUPLE: multi-texture grid display-list loc disposed ;
350 : image-locs ( image-grid -- loc-grid )
351 [ first [ dim>> first ] map ] [ [ first dim>> second ] map ] bi
352 [ 0 [ + ] accumulate nip ] bi@
355 : <texture-grid> ( image-grid loc -- grid )
356 [ dup image-locs ] dip
357 '[ [ _ v+ <single-texture> |dispose ] 2map ] 2map ;
359 : grid-has-alpha? ( grid -- ? )
360 first first image>> has-alpha? ;
362 : make-textured-grid-display-list ( grid -- dlist )
365 [ grid-has-alpha? [ GL_BLEND glDisable ] unless ]
366 [ [ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ]
367 [ grid-has-alpha? [ GL_BLEND glEnable ] unless ] tri
368 GL_TEXTURE_2D 0 glBindTexture
372 : <multi-texture> ( image-grid loc -- multi-texture )
376 make-textured-grid-display-list
381 M: multi-texture draw-scaled-texture nip draw-texture ;
383 M: multi-texture dispose* grid>> [ [ dispose ] each ] each ;
385 CONSTANT: max-texture-size { 512 512 }
389 : make-texture ( image -- id )
390 #! We use glTexSubImage2D to work around the power of 2 texture size
394 GL_TEXTURE_2D swap glBindTexture
395 non-power-of-2-textures? get
396 [ dup bitmap>> tex-image ]
397 [ [ f tex-image ] [ tex-sub-image ] bi ] if
401 : <texture> ( image loc -- texture )
402 over dim>> max-texture-size [ <= ] 2all?
404 [ [ max-texture-size tesselate ] dip <multi-texture> ] if ;