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
|
2009-04-04 21:04:35 -04:00
|
|
|
specialized-arrays.float sequences math math.vectors
|
|
|
|
math.matrices generalizations fry arrays ;
|
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-04-04 21:04:35 -04:00
|
|
|
SLOT: display-list
|
|
|
|
|
|
|
|
: draw-texture ( texture -- ) display-list>> [ glCallList ] when* ;
|
2009-03-27 19:31:25 -04:00
|
|
|
|
|
|
|
GENERIC: draw-scaled-texture ( dim texture -- )
|
|
|
|
|
2009-03-10 17:58:35 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-04-04 21:04:35 -04:00
|
|
|
TUPLE: single-texture image dim loc texture-coords texture display-list disposed ;
|
2009-02-19 06:01:21 -05:00
|
|
|
|
2009-04-04 21:04:35 -04:00
|
|
|
: (tex-image) ( image -- )
|
|
|
|
[ GL_TEXTURE_2D 0 GL_RGBA ] dip
|
|
|
|
[ dim>> first2 [ next-power-of-2 ] bi@ 0 ]
|
|
|
|
[ component-order>> component-order>format f ] bi
|
|
|
|
glTexImage2D ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-04-04 21:04:35 -04:00
|
|
|
: (tex-sub-image) ( image -- )
|
|
|
|
[ GL_TEXTURE_2D 0 0 0 ] dip
|
|
|
|
[ dim>> first2 ] [ component-order>> component-order>format ] [ bitmap>> ] tri
|
|
|
|
glTexSubImage2D ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-04-04 21:04:35 -04:00
|
|
|
: make-texture ( image -- id )
|
|
|
|
#! We use glTexSubImage2D to work around the power of 2 texture size
|
|
|
|
#! limitation
|
2009-02-12 04:58:05 -05:00
|
|
|
gen-texture [
|
|
|
|
GL_TEXTURE_BIT [
|
|
|
|
GL_TEXTURE_2D swap glBindTexture
|
2009-04-04 21:04:35 -04:00
|
|
|
[ (tex-image) ] [ (tex-sub-image) ] bi
|
2009-02-12 04:58:05 -05:00
|
|
|
] do-attribs
|
|
|
|
] keep ;
|
|
|
|
|
2009-02-19 05:06:57 -05:00
|
|
|
: init-texture ( -- )
|
2009-04-04 21:04:35 -04:00
|
|
|
GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
|
|
|
|
GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
|
2009-02-19 05:06:57 -05:00
|
|
|
|
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 -- )
|
|
|
|
[
|
2009-04-03 08:01:22 -04:00
|
|
|
[ image>> has-alpha? [ GL_BLEND glDisable ] unless ]
|
|
|
|
[ (draw-textured-rect) GL_TEXTURE_2D 0 glBindTexture ]
|
|
|
|
[ image>> has-alpha? [ GL_BLEND glEnable ] unless ]
|
|
|
|
tri
|
2009-03-27 19:31:25 -04:00
|
|
|
] with-texturing ;
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-04-03 08:01:22 -04:00
|
|
|
: texture-coords ( texture -- coords )
|
|
|
|
[
|
2009-04-04 21:04:35 -04:00
|
|
|
[ dim>> ] [ image>> dim>> [ next-power-of-2 ] map ] bi v/
|
2009-04-03 08:01:22 -04:00
|
|
|
{ { 0 0 } { 1 0 } { 1 1 } { 0 1 } }
|
|
|
|
[ v* ] with map
|
|
|
|
] keep
|
|
|
|
image>> upside-down?>> [ [ first2 1 swap - 2array ] map ] when
|
2009-02-19 05:06:57 -05:00
|
|
|
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-04-04 21:04:35 -04:00
|
|
|
: <single-texture> ( image loc -- texture )
|
|
|
|
single-texture new swap >>loc swap [ >>image ] [ dim>> >>dim ] bi
|
2009-04-03 08:01:22 -04:00
|
|
|
dup image>> dim>> product 0 = [
|
|
|
|
dup texture-coords >>texture-coords
|
|
|
|
dup image>> make-texture >>texture
|
2009-02-20 21:53:51 -05:00
|
|
|
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-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 )
|
2009-04-04 21:04:35 -04:00
|
|
|
[ first [ dim>> first ] map ] [ [ first dim>> second ] map ] bi
|
2009-03-27 19:31:25 -04:00
|
|
|
[ 0 [ + ] accumulate nip ] bi@
|
|
|
|
cross-zip flip ;
|
|
|
|
|
|
|
|
: <texture-grid> ( image-grid loc -- grid )
|
|
|
|
[ dup image-locs ] dip
|
2009-04-04 21:04:35 -04:00
|
|
|
'[ [ _ v+ <single-texture> |dispose ] 2map ] 2map ;
|
2009-03-27 19:31:25 -04:00
|
|
|
|
|
|
|
: draw-textured-grid ( grid -- )
|
|
|
|
[ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ;
|
|
|
|
|
2009-04-03 08:01:22 -04:00
|
|
|
: grid-has-alpha? ( grid -- ? )
|
|
|
|
first first image>> has-alpha? ;
|
|
|
|
|
2009-03-27 19:31:25 -04:00
|
|
|
: make-textured-grid-display-list ( grid -- dlist )
|
|
|
|
GL_COMPILE [
|
|
|
|
[
|
2009-04-03 08:01:22 -04:00
|
|
|
[ grid-has-alpha? [ GL_BLEND glDisable ] unless ]
|
|
|
|
[ [ [ [ dim>> ] keep (draw-textured-rect) ] each ] each ]
|
|
|
|
[ grid-has-alpha? [ GL_BLEND glEnable ] unless ] tri
|
2009-03-27 19:31:25 -04:00
|
|
|
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 dispose* grid>> [ [ dispose ] each ] each ;
|
|
|
|
|
2009-04-03 08:01:22 -04:00
|
|
|
CONSTANT: max-texture-size { 512 512 }
|
2009-03-27 19:31:25 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-04-04 21:04:35 -04:00
|
|
|
: <texture> ( image loc -- texture )
|
|
|
|
over dim>> max-texture-size [ <= ] 2all?
|
2009-03-27 19:31:25 -04:00
|
|
|
[ <single-texture> ]
|
2009-04-04 21:04:35 -04:00
|
|
|
[ [ max-texture-size tesselate ] dip <multi-texture> ] if ;
|