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
|
|
|
|
opengl opengl.gl combinators images grouping specialized-arrays.float
|
|
|
|
locals sequences math math.vectors generalizations ;
|
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) ;
|
|
|
|
|
2009-02-20 21:53:51 -05:00
|
|
|
TUPLE: texture loc dim texture-coords texture display-list disposed ;
|
2009-02-19 05:06:57 -05:00
|
|
|
|
|
|
|
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-02-19 05:06:57 -05:00
|
|
|
|
2009-03-10 17:58:35 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
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-02-12 04:58:05 -05:00
|
|
|
: draw-textured-rect ( dim texture -- )
|
|
|
|
GL_TEXTURE_2D [
|
|
|
|
GL_TEXTURE_BIT [
|
|
|
|
GL_TEXTURE_COORD_ARRAY [
|
|
|
|
COLOR: white gl-color
|
2009-02-20 21:53:51 -05:00
|
|
|
dup loc>> [
|
|
|
|
[ [ GL_TEXTURE_2D ] dip texture>> glBindTexture ]
|
|
|
|
[ init-texture texture-coords>> gl-texture-coord-pointer ] bi
|
|
|
|
fill-rect-vertices (gl-fill-rect)
|
|
|
|
GL_TEXTURE_2D 0 glBindTexture
|
|
|
|
] with-translation
|
2009-02-12 04:58:05 -05:00
|
|
|
] do-enabled-client-state
|
|
|
|
] do-attribs
|
|
|
|
] do-enabled ;
|
|
|
|
|
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-02-19 05:06:57 -05:00
|
|
|
PRIVATE>
|
2009-02-12 04:58:05 -05:00
|
|
|
|
2009-02-20 21:53:51 -05:00
|
|
|
: <texture> ( image loc -- texture )
|
|
|
|
texture new swap >>loc
|
|
|
|
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
|
|
|
|
|
|
|
M: texture dispose*
|
2009-02-19 05:06:57 -05:00
|
|
|
[ texture>> [ delete-texture ] when* ]
|
|
|
|
[ display-list>> [ delete-dlist ] when* ] bi ;
|
|
|
|
|
|
|
|
: draw-texture ( texture -- )
|
|
|
|
display-list>> [ glCallList ] when* ;
|
|
|
|
|
|
|
|
: draw-scaled-texture ( dim texture -- )
|
|
|
|
dup texture>> [ draw-textured-rect ] [ 2drop ] if ;
|