1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs cache colors.constants destructors fry 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
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 GENERIC: component-order>format ( component-order -- format type )
27 M: RGB component-order>format drop GL_RGB GL_UNSIGNED_BYTE ;
28 M: BGR component-order>format drop GL_BGR GL_UNSIGNED_BYTE ;
29 M: RGBA component-order>format drop GL_RGBA GL_UNSIGNED_BYTE ;
30 M: ARGB component-order>format drop GL_BGRA_EXT GL_UNSIGNED_INT_8_8_8_8_REV ;
31 M: BGRA component-order>format drop GL_BGRA_EXT GL_UNSIGNED_BYTE ;
32 M: BGRX component-order>format drop GL_BGRA_EXT GL_UNSIGNED_BYTE ;
33 M: LA component-order>format drop GL_LUMINANCE_ALPHA GL_UNSIGNED_BYTE ;
34 M: L component-order>format drop GL_LUMINANCE GL_UNSIGNED_BYTE ;
38 : draw-texture ( texture -- ) display-list>> [ glCallList ] when* ;
40 GENERIC: draw-scaled-texture ( dim texture -- )
44 TUPLE: single-texture image dim loc texture-coords texture display-list disposed ;
46 : adjust-texture-dim ( dim -- dim' )
47 non-power-of-2-textures? get [
48 [ dup 1 = [ next-power-of-2 ] unless ] map
51 : (tex-image) ( image bitmap -- )
53 [ GL_TEXTURE_2D 0 GL_RGBA ] dip
54 [ dim>> adjust-texture-dim first2 0 ]
55 [ component-order>> component-order>format ] bi
59 : (tex-sub-image) ( image -- )
60 [ GL_TEXTURE_2D 0 0 0 ] dip
61 [ dim>> first2 ] [ component-order>> component-order>format ] [ bitmap>> ] tri
64 : make-texture ( image -- id )
65 #! We use glTexSubImage2D to work around the power of 2 texture size
69 GL_TEXTURE_2D swap glBindTexture
70 non-power-of-2-textures? get
71 [ dup bitmap>> (tex-image) ]
72 [ [ f (tex-image) ] [ (tex-sub-image) ] bi ] if
77 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri
78 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri
79 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT glTexParameteri
80 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT glTexParameteri ;
82 : with-texturing ( quot -- )
85 GL_TEXTURE_COORD_ARRAY [
88 ] do-enabled-client-state
92 : (draw-textured-rect) ( dim texture -- )
94 [ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
95 [ init-texture texture-coords>> gl-texture-coord-pointer ] tri
98 : draw-textured-rect ( dim texture -- )
100 [ image>> has-alpha? [ GL_BLEND glDisable ] unless ]
101 [ (draw-textured-rect) GL_TEXTURE_2D 0 glBindTexture ]
102 [ image>> has-alpha? [ GL_BLEND glEnable ] unless ]
106 : texture-coords ( texture -- coords )
107 [ [ dim>> ] [ image>> dim>> adjust-texture-dim ] bi v/ ]
109 image>> upside-down?>>
110 { { 0 1 } { 1 1 } { 1 0 } { 0 0 } }
111 { { 0 0 } { 1 0 } { 1 1 } { 0 1 } } ?
113 [ v* ] with map float-array{ } join ;
115 : make-texture-display-list ( texture -- dlist )
116 GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
118 : <single-texture> ( image loc -- texture )
119 single-texture new swap >>loc swap [ >>image ] [ dim>> >>dim ] bi
120 dup image>> dim>> product 0 = [
121 dup texture-coords >>texture-coords
122 dup image>> make-texture >>texture
123 dup make-texture-display-list >>display-list
126 M: single-texture dispose*
127 [ texture>> [ delete-texture ] when* ]
128 [ display-list>> [ delete-dlist ] when* ] bi ;
130 M: single-texture draw-scaled-texture
131 2dup dim>> = [ nip draw-texture ] [
132 dup texture>> [ draw-textured-rect ] [ 2drop ] if
135 TUPLE: multi-texture grid display-list loc disposed ;
137 : image-locs ( image-grid -- loc-grid )
138 [ first [ dim>> first ] map ] [ [ first dim>> second ] map ] bi
139 [ 0 [ + ] accumulate nip ] bi@
142 : <texture-grid> ( image-grid loc -- grid )
143 [ dup image-locs ] dip
144 '[ [ _ v+ <single-texture> |dispose ] 2map ] 2map ;
146 : draw-textured-grid ( grid -- )
147 [ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ;
149 : grid-has-alpha? ( grid -- ? )
150 first first image>> has-alpha? ;
152 : make-textured-grid-display-list ( grid -- dlist )
155 [ grid-has-alpha? [ GL_BLEND glDisable ] unless ]
156 [ [ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ]
157 [ grid-has-alpha? [ GL_BLEND glEnable ] unless ] tri
158 GL_TEXTURE_2D 0 glBindTexture
162 : <multi-texture> ( image-grid loc -- multi-texture )
166 make-textured-grid-display-list
171 M: multi-texture draw-scaled-texture nip draw-texture ;
173 M: multi-texture dispose* grid>> [ [ dispose ] each ] each ;
175 CONSTANT: max-texture-size { 512 512 }
179 : <texture> ( image loc -- texture )
180 over dim>> max-texture-size [ <= ] 2all?
182 [ [ max-texture-size tesselate ] dip <multi-texture> ] if ;