2009-02-12 04:58:05 -05:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-19 05:06:57 -05:00
|
|
|
USING: accessors assocs cache colors.constants destructors fry kernel
|
2009-03-27 19:31:25 -04:00
|
|
|
opengl opengl.gl combinators images images.tesselation grouping
|
|
|
|
specialized-arrays.float locals sequences math math.vectors
|
|
|
|
math.matrices generalizations fry columns ;
|
2009-02-12 04:58:05 -05:00
|
|
|
IN: opengl.textures
|
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: gen-texture ( -- id ) [ glGenTextures ] (gen-gl-object) ;
|
|
|
|
|
|
|
|
: delete-texture ( id -- ) [ glDeleteTextures ] (delete-gl-object) ;
|
|
|
|
|
|
|
|
GENERIC: component-order>format ( component-order -- format type )
|
|
|
|
|
2009-03-10 17:58:35 -04:00
|
|
|
M: RGB component-order>format drop GL_RGB GL_UNSIGNED_BYTE ;
|
|
|
|
M: BGR component-order>format drop GL_BGR GL_UNSIGNED_BYTE ;
|
2009-02-19 05:06:57 -05:00
|
|
|
M: RGBA component-order>format drop GL_RGBA GL_UNSIGNED_BYTE ;
|
|
|
|
M: ARGB component-order>format drop GL_BGRA_EXT GL_UNSIGNED_INT_8_8_8_8_REV ;
|
2009-02-28 02:31:51 -05:00
|
|
|
M: BGRA component-order>format drop GL_BGRA_EXT GL_UNSIGNED_BYTE ;
|
2009-04-02 14:05:26 -04:00
|
|
|
M: BGRX component-order>format drop GL_BGRA_EXT GL_UNSIGNED_BYTE ;
|
2009-02-19 05:06:57 -05:00
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
GENERIC: draw-texture ( texture -- )
|
|
|
|
|
|
|
|
GENERIC: draw-scaled-texture ( dim texture -- )
|
|
|
|
|
2009-03-10 17:58:35 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
TUPLE: single-texture loc dim texture-coords texture display-list disposed ;
|
|
|
|
|
2009-02-19 06:01:21 -05:00
|
|
|
: repeat-last ( seq n -- seq' )
|
|
|
|
over peek pad-tail concat ;
|
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: power-of-2-bitmap ( rows dim size -- bitmap dim )
|
|
|
|
'[
|
2009-02-19 06:01:21 -05:00
|
|
|
first2
|
|
|
|
[ [ _ ] dip '[ _ group _ repeat-last ] map ]
|
|
|
|
[ repeat-last ]
|
|
|
|
bi*
|
2009-02-19 05:06:57 -05:00
|
|
|
] keep ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: image-rows ( image -- rows )
|
|
|
|
[ bitmap>> ]
|
|
|
|
[ dim>> first ]
|
2009-02-19 06:01:21 -05:00
|
|
|
[ component-order>> bytes-per-pixel ]
|
2009-02-19 05:06:57 -05:00
|
|
|
tri * group ; inline
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: power-of-2-image ( image -- image )
|
|
|
|
dup dim>> [ 0 = ] all? [
|
|
|
|
clone dup
|
|
|
|
[ image-rows ]
|
|
|
|
[ dim>> [ next-power-of-2 ] map ]
|
2009-02-19 06:01:21 -05:00
|
|
|
[ component-order>> bytes-per-pixel ] tri
|
2009-02-19 05:06:57 -05:00
|
|
|
power-of-2-bitmap
|
|
|
|
[ >>bitmap ] [ >>dim ] bi*
|
|
|
|
] unless ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
:: make-texture ( image -- id )
|
2009-02-12 04:58:05 -05:00
|
|
|
gen-texture [
|
|
|
|
GL_TEXTURE_BIT [
|
|
|
|
GL_TEXTURE_2D swap glBindTexture
|
|
|
|
GL_TEXTURE_2D
|
|
|
|
0
|
|
|
|
GL_RGBA
|
2009-02-19 05:06:57 -05:00
|
|
|
image dim>> first2
|
2009-02-12 04:58:05 -05:00
|
|
|
0
|
2009-02-19 05:06:57 -05:00
|
|
|
image component-order>> component-order>format
|
|
|
|
image bitmap>>
|
2009-02-12 04:58:05 -05:00
|
|
|
glTexImage2D
|
|
|
|
] do-attribs
|
|
|
|
] keep ;
|
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: init-texture ( -- )
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT glTexParameteri
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT glTexParameteri ;
|
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
: with-texturing ( quot -- )
|
2009-02-12 04:58:05 -05:00
|
|
|
GL_TEXTURE_2D [
|
|
|
|
GL_TEXTURE_BIT [
|
|
|
|
GL_TEXTURE_COORD_ARRAY [
|
|
|
|
COLOR: white gl-color
|
2009-03-27 19:31:25 -04:00
|
|
|
call
|
2009-02-12 04:58:05 -05:00
|
|
|
] do-enabled-client-state
|
|
|
|
] do-attribs
|
2009-03-27 19:31:25 -04:00
|
|
|
] do-enabled ; inline
|
|
|
|
|
|
|
|
: (draw-textured-rect) ( dim texture -- )
|
|
|
|
[ loc>> ]
|
|
|
|
[ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
|
|
|
|
[ init-texture texture-coords>> gl-texture-coord-pointer ] tri
|
|
|
|
swap gl-fill-rect ;
|
|
|
|
|
|
|
|
: draw-textured-rect ( dim texture -- )
|
|
|
|
[
|
|
|
|
(draw-textured-rect)
|
|
|
|
GL_TEXTURE_2D 0 glBindTexture
|
|
|
|
] with-texturing ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: texture-coords ( dim -- coords )
|
|
|
|
[ dup next-power-of-2 /f ] map
|
|
|
|
{ { 0 0 } { 1 0 } { 1 1 } { 0 1 } } [ v* ] with map
|
|
|
|
float-array{ } join ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-02-20 21:53:51 -05:00
|
|
|
: make-texture-display-list ( texture -- dlist )
|
|
|
|
GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
: <single-texture> ( image loc -- texture )
|
|
|
|
single-texture new swap >>loc
|
2009-02-20 21:53:51 -05:00
|
|
|
swap
|
|
|
|
[ dim>> >>dim ] keep
|
2009-02-21 00:30:41 -05:00
|
|
|
[ dim>> product 0 = ] keep '[
|
2009-02-20 21:53:51 -05:00
|
|
|
_
|
|
|
|
[ dim>> texture-coords >>texture-coords ]
|
|
|
|
[ power-of-2-image make-texture >>texture ] bi
|
|
|
|
dup make-texture-display-list >>display-list
|
|
|
|
] unless ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
M: single-texture dispose*
|
2009-02-19 05:06:57 -05:00
|
|
|
[ texture>> [ delete-texture ] when* ]
|
|
|
|
[ display-list>> [ delete-dlist ] when* ] bi ;
|
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
M: single-texture draw-texture display-list>> [ glCallList ] when* ;
|
|
|
|
|
|
|
|
M: single-texture draw-scaled-texture
|
|
|
|
dup texture>> [ draw-textured-rect ] [ 2drop ] if ;
|
2009-02-19 05:06:57 -05:00
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
TUPLE: multi-texture grid display-list loc disposed ;
|
|
|
|
|
|
|
|
: image-locs ( image-grid -- loc-grid )
|
|
|
|
[ first [ dim>> first ] map ] [ 0 <column> [ dim>> second ] map ] bi
|
|
|
|
[ 0 [ + ] accumulate nip ] bi@
|
|
|
|
cross-zip flip ;
|
|
|
|
|
|
|
|
: <texture-grid> ( image-grid loc -- grid )
|
|
|
|
[ dup image-locs ] dip
|
|
|
|
'[ [ _ v+ <single-texture> |dispose ] 2map ] 2map ;
|
|
|
|
|
|
|
|
: draw-textured-grid ( grid -- )
|
|
|
|
[ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ;
|
|
|
|
|
|
|
|
: make-textured-grid-display-list ( grid -- dlist )
|
|
|
|
GL_COMPILE [
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ dim>> ] keep (draw-textured-rect)
|
|
|
|
] each
|
|
|
|
] each
|
|
|
|
GL_TEXTURE_2D 0 glBindTexture
|
|
|
|
] with-texturing
|
|
|
|
] make-dlist ;
|
|
|
|
|
|
|
|
: <multi-texture> ( image-grid loc -- multi-texture )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
<texture-grid> dup
|
|
|
|
make-textured-grid-display-list
|
|
|
|
] keep
|
|
|
|
f multi-texture boa
|
|
|
|
] with-destructors ;
|
|
|
|
|
|
|
|
M: multi-texture draw-texture display-list>> [ glCallList ] when* ;
|
|
|
|
|
|
|
|
M: multi-texture dispose* grid>> [ [ dispose ] each ] each ;
|
|
|
|
|
|
|
|
CONSTANT: max-texture-size { 256 256 }
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: <texture> ( image loc -- texture )
|
|
|
|
over dim>> max-texture-size [ <= ] 2all?
|
|
|
|
[ <single-texture> ]
|
|
|
|
[ [ max-texture-size tesselate ] dip <multi-texture> ] if ;
|