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 combinators images images.tesselation grouping
5 specialized-arrays.float locals sequences math math.vectors
6 math.matrices generalizations fry columns ;
9 : gen-texture ( -- id ) [ glGenTextures ] (gen-gl-object) ;
11 : delete-texture ( id -- ) [ glDeleteTextures ] (delete-gl-object) ;
13 GENERIC: component-order>format ( component-order -- format type )
15 M: RGB component-order>format drop GL_RGB GL_UNSIGNED_BYTE ;
16 M: BGR component-order>format drop GL_BGR GL_UNSIGNED_BYTE ;
17 M: RGBA component-order>format drop GL_RGBA GL_UNSIGNED_BYTE ;
18 M: ARGB component-order>format drop GL_BGRA_EXT GL_UNSIGNED_INT_8_8_8_8_REV ;
19 M: BGRA component-order>format drop GL_BGRA_EXT GL_UNSIGNED_BYTE ;
21 GENERIC: draw-texture ( texture -- )
23 GENERIC: draw-scaled-texture ( dim texture -- )
27 TUPLE: single-texture loc dim texture-coords texture display-list disposed ;
29 : repeat-last ( seq n -- seq' )
30 over peek pad-tail concat ;
32 : power-of-2-bitmap ( rows dim size -- bitmap dim )
35 [ [ _ ] dip '[ _ group _ repeat-last ] map ]
40 : image-rows ( image -- rows )
43 [ component-order>> bytes-per-pixel ]
46 : power-of-2-image ( image -- image )
47 dup dim>> [ 0 = ] all? [
50 [ dim>> [ next-power-of-2 ] map ]
51 [ component-order>> bytes-per-pixel ] tri
53 [ >>bitmap ] [ >>dim ] bi*
56 :: make-texture ( image -- id )
59 GL_TEXTURE_2D swap glBindTexture
65 image component-order>> component-order>format
72 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
73 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
74 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT glTexParameteri
75 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT glTexParameteri ;
77 : with-texturing ( quot -- )
80 GL_TEXTURE_COORD_ARRAY [
83 ] do-enabled-client-state
87 : (draw-textured-rect) ( dim texture -- )
89 [ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
90 [ init-texture texture-coords>> gl-texture-coord-pointer ] tri
93 : draw-textured-rect ( dim texture -- )
96 GL_TEXTURE_2D 0 glBindTexture
99 : texture-coords ( dim -- coords )
100 [ dup next-power-of-2 /f ] map
101 { { 0 0 } { 1 0 } { 1 1 } { 0 1 } } [ v* ] with map
102 float-array{ } join ;
104 : make-texture-display-list ( texture -- dlist )
105 GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
107 : <single-texture> ( image loc -- texture )
108 single-texture new swap >>loc
111 [ dim>> product 0 = ] keep '[
113 [ dim>> texture-coords >>texture-coords ]
114 [ power-of-2-image make-texture >>texture ] bi
115 dup make-texture-display-list >>display-list
118 M: single-texture dispose*
119 [ texture>> [ delete-texture ] when* ]
120 [ display-list>> [ delete-dlist ] when* ] bi ;
122 M: single-texture draw-texture display-list>> [ glCallList ] when* ;
124 M: single-texture draw-scaled-texture
125 dup texture>> [ draw-textured-rect ] [ 2drop ] if ;
127 TUPLE: multi-texture grid display-list loc disposed ;
129 : image-locs ( image-grid -- loc-grid )
130 [ first [ dim>> first ] map ] [ 0 <column> [ dim>> second ] map ] bi
131 [ 0 [ + ] accumulate nip ] bi@
134 : <texture-grid> ( image-grid loc -- grid )
135 [ dup image-locs ] dip
136 '[ [ _ v+ <single-texture> |dispose ] 2map ] 2map ;
138 : draw-textured-grid ( grid -- )
139 [ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ;
141 : make-textured-grid-display-list ( grid -- dlist )
146 [ dim>> ] keep (draw-textured-rect)
149 GL_TEXTURE_2D 0 glBindTexture
153 : <multi-texture> ( image-grid loc -- multi-texture )
157 make-textured-grid-display-list
162 M: multi-texture draw-texture display-list>> [ glCallList ] when* ;
164 M: multi-texture dispose* grid>> [ [ dispose ] each ] each ;
166 CONSTANT: max-texture-size { 256 256 }
170 : <texture> ( image loc -- texture )
171 over dim>> max-texture-size [ <= ] 2all?
173 [ [ max-texture-size tesselate ] dip <multi-texture> ] if ;