Check-in [4784aecda7]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:A bit refactoring, added a very simple example
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4784aecda7000eb6cfe397de138ea85d708cc5f8
User & Date: bernd 2014-04-29 13:02:02
Context
2014-05-14
21:02
Better keyboard handling check-in: c71e1435c8 user: bernd tags: trunk
2014-04-29
13:02
A bit refactoring, added a very simple example check-in: 4784aecda7 user: bernd tags: trunk
2014-04-01
20:23
Destdir depends on where it is called check-in: 7aa55ededf user: bernd tags: trunk
Changes

Changes to gl-helper.fs.

   205    205       2dup ';' scan 2dup 1 /string 2>r drop >r over r> swap - nip
   206    206       BEGIN  2dup >wordend  dup WHILE  2nip >wordend >word  REPEAT  2drop
   207    207       2r> 2swap ;
   208    208   
   209    209   Variable $attrib
   210    210   
   211    211   : >bindattrib ( xt program -- )  0 { prog idx } shader>string
   212         -    BEGIN  s" attribute " >attrib extract-name dup  WHILE  $attrib $!
   213         -	    prog idx $attrib $@ over + 0 swap c! glBindAttribLocation
          212  +    BEGIN  s" attribute " >attrib extract-name dup  WHILE
          213  +	    $attrib $!  0 $attrib c$+!
          214  +	    prog idx $attrib $@ drop glBindAttribLocation
   214    215   	    idx 1+ to idx
   215    216       REPEAT  2drop 2drop ;
   216    217   
   217    218   : >univattrib ( xt program -- )  { prog }
   218    219       shader>string
   219    220       BEGIN  s" uniform " >attrib extract-name dup  WHILE  $attrib $!
   220    221   	    prog $attrib $@ over + 0 swap c! glGetUniformLocation ,
................................................................................
   404    405       0e sf!+ near f2* top bottom f- f/ sf!+ 0e sf!+ 0e sf!+
   405    406       right left f+ right left f- f/ sf!+
   406    407       top bottom f+ top bottom f- f/ sf!+
   407    408       near far f+ near far f- f/ sf!+ -1e sf!+
   408    409       sfloat+ sfloat+ near far f* f2* near far f- f/ sf!+ 0e sf!+
   409    410       drop ;
   410    411   
          412  +: ap-set ( -- )
          413  +    ap-matrix MVPMatrix set-matrix
          414  +    ap-matrix MVMatrix set-matrix ;
          415  +
   411    416   : >ap ( near far scale -- ) f2* 1/f { f: scale }
   412    417       scale dpy-w @ fm* fdup fnegate fswap
   413    418       scale dpy-h @ fm* fdup fnegate fswap >ortho
   414    419       \ cr ap-matrix .matrix
   415         -    ap-matrix MVPMatrix set-matrix
   416         -    ap-matrix MVMatrix set-matrix ;
          420  +    ap-set ;
   417    421   : >apxy ( xoff yoff -- )  y-apos sf!  x-apos sf! ;
   418    422   
   419    423   \ textures
   420    424   
   421    425   Create white-texture \ aabbggrr
   422    426     $ffffffff l,  $ffffffff l,
   423    427     $ffffffff l,  $ffffffff l,
................................................................................
   508    512       none-tex no-texture
   509    513       Texture 0 glUniform1i
   510    514       Ambient 1 ambient% glUniform1fv
   511    515       LightPos 0.0e 0.0e -0.3e glUniform3f
   512    516       z-bias set-color+
   513    517       .01e 100e dpy-w @ dpy-h @ min s>f f2/ 100 fm* >ap ;
   514    518   
   515         -: 2d-vertex ( index addr -- ) >r
   516         -    dup 2 GL_FLOAT  0 0 r> glVertexAttribPointer
   517         -    glEnableVertexAttribArray ;
   518         -
   519         -: 3d-vertex ( index addr -- ) >r
   520         -    dup 3 GL_FLOAT  0 0 r> glVertexAttribPointer
   521         -    glEnableVertexAttribArray ;
   522         -
   523         -: 4d-vertex ( index addr -- ) >r
   524         -    dup 4 GL_FLOAT  0 0 r> glVertexAttribPointer
   525         -    glEnableVertexAttribArray ;
   526         -
   527         -: rgba-vertex ( index addr -- ) >r
   528         -    dup 4 GL_UNSIGNED_BYTE GL_TRUE 0 r> glVertexAttribPointer
   529         -    glEnableVertexAttribArray ;
   530         -
   531    519   \ glDrawElements helper
   532    520   
   533    521   $8 cells buffer: gl-buffers
   534    522   : buf@ ( n -- buf ) cells gl-buffers + @ ;
   535    523   : bind-buf ( type n -- ) buf@ glBindBuffer ;
   536    524   
   537    525   0 Value array-buf

Changes to gl-sample.fs.

     1      1   \ opengl example
     2      2   
     3      3   require gl-helper.fs
     4      4   
     5         -also opengl
            5  +also opengl also [IFDEF] android android [THEN]
     6      6   
     7      7   tex: ascii-tex
     8      8   
     9      9   : load-textures ( -- )
    10     10       ascii-tex s" ascii.png" load-texture wrap mipmap ;
    11     11   
    12     12   \ triangle example
................................................................................
    35     35       1e0 2e0 >st v+
    36     36       0e0 0e0 >st v+ o> ;
    37     37   
    38     38   : normals >v
    39     39       3 0 DO  0e 0e -1e n>xyz v+  LOOP  o> ;
    40     40   
    41     41   Variable new-config
    42         -
    43         -[IFDEF] android
    44         -also android
    45         -
    46         -:noname  defers window-init
    47         -    ['] VertexShader ['] FragmentShader create-program to program
    48         -    program init load-textures ; IS window-init
    49         -
    50         -:noname new-config on ; is config-changed
    51         -[THEN]
    52         -
    53     42   FVariable touch -100e touch f!
    54     43   FVariable motion 0.01e motion f!
    55     44   
    56     45   : draw-tri-angle ( f -- )
    57     46       0.01e 0.02e 0.15e 1.0e glClearColor
    58     47       clear
    59     48       Ambient 1 ambient% glUniform1fv
................................................................................
    64     53       GL_TRIANGLES draw-elements
    65     54       v0 colors'
    66     55       ascii-tex
    67     56       GL_TRIANGLES draw-elements
    68     57       sync ;
    69     58   
    70     59   : draw-tri { f: angle -- angle' }
    71         -    new-config @ IF
    72         -	dpy-w @ dpy-h @ getwh dpy-w @ dpy-h @ d<> IF  new-config off  THEN
    73         -    THEN
    74     60       angle draw-tri-angle
    75     61       >looper
    76     62       *input >r r@ IF
    77     63   	r@ action @ abs 1 <> IF
    78     64   	    \ ." Touch at " r@ x0 ? r@ y0 ? cr
    79     65   	    \ r@ x0 @ 20 < r@ y0 @ 20 < and IF -1 (bye) THEN
    80     66   	    r@ x0 @ dpy-w @ 2/ - s>f dpy-h @ 2/ fm/

Added gl-simple.fs.

            1  +\ simple opengl test
            2  +
            3  +[defined] init-opengl 0= constant standalone
            4  +require gl-helper.fs
            5  +also opengl
            6  +also [IFDEF] android android [THEN]
            7  +standalone [if] init-opengl buffer-init [then]
            8  +
            9  +: simple-init ( -- )
           10  +    ['] VertexShader ['] FragmentShader create-program to program
           11  +    program init  0e >y-pos
           12  +    1e 0e 0e 1e glClearColor ;
           13  +
           14  +: draw ( -- )  none-tex
           15  +    v0 >v
           16  +    -1e -1e >xy  $FFFFFFFF rgba>c  n> v+
           17  +     1e -1e >xy  $FFFFFFFF rgba>c  n> v+
           18  +     0e  1e >xy  $FFFFFFFF rgba>c  n> v+ v>
           19  +    i0 0 i, 1 i, 2 i,
           20  +    GL_TRIANGLES draw-elements ;
           21  +
           22  +: simple ( -- )  simple-init
           23  +  1 level# +!  begin clear draw sync >looper level# @ 0= until ;
           24  +
           25  +simple

Changes to gl-terminal.fs.

    94     94   $ff40bf40 l, \ dimm Green
    95     95   $ff40bfbf l, \ dimm Yellow
    96     96   $ffbf4040 l, \ dimm Blue
    97     97   $ffbf40bf l, \ dimm Magenta
    98     98   $ffbfbf40 l, \ dimm Cyan
    99     99   $ffbfbfbf l, \ dimm White
   100    100   
   101         -: term-load-textures ( -- )
   102         -    chars-tex s" ascii.png" load-texture 2drop linear
          101  +: term-load-textures ( addr u -- )
          102  +    chars-tex load-texture 2drop linear
   103    103       GL_TEXTURE2 glActiveTexture
   104    104       color-tex color-matrix $10 1 rgba-map nearest
   105    105       GL_TEXTURE0 glActiveTexture ;
   106    106   
   107    107   Variable color-index
   108    108   bl dup $70 and 5 lshift or $F0F and 4 lshift color-index !
   109    109   Variable std-bg
................................................................................
   310    310   ' gl-page IS page
   311    311   ' gl-attr! IS attr!
   312    312   default-out op-vector !
   313    313   
   314    314   \ initialize
   315    315   
   316    316   : term-init ( -- )
   317         -    clazz >o hideProgress o>
          317  +    [IFDEF] clazz clazz >o hideProgress o> [THEN]
   318    318       >screen-orientation
   319    319       create-terminal-program to terminal-program
   320         -    terminal-program terminal-init term-load-textures form-chooser
          320  +    terminal-program terminal-init
          321  +    s" ascii.png" term-load-textures form-chooser
   321    322       unit-matrix MVPMatrix set-matrix ;
   322    323   
   323    324   :noname  defers window-init term-init config-changer ; IS window-init
   324    325   
   325    326   window-init
   326    327   
   327    328   previous previous \ remove opengl from search order

Changes to gles2/gl-helper.fs.

   205    205       2dup ';' scan 2dup 1 /string 2>r drop >r over r> swap - nip
   206    206       BEGIN  2dup >wordend  dup WHILE  2nip >wordend >word  REPEAT  2drop
   207    207       2r> 2swap ;
   208    208   
   209    209   Variable $attrib
   210    210   
   211    211   : >bindattrib ( xt program -- )  0 { prog idx } shader>string
   212         -    BEGIN  s" attribute " >attrib extract-name dup  WHILE  $attrib $!
   213         -	    prog idx $attrib $@ over + 0 swap c! glBindAttribLocation
          212  +    BEGIN  s" attribute " >attrib extract-name dup  WHILE
          213  +	    $attrib $!  0 $attrib c$+!
          214  +	    prog idx $attrib $@ drop glBindAttribLocation
   214    215   	    idx 1+ to idx
   215    216       REPEAT  2drop 2drop ;
   216    217   
   217    218   : >univattrib ( xt program -- )  { prog }
   218    219       shader>string
   219    220       BEGIN  s" uniform " >attrib extract-name dup  WHILE  $attrib $!
   220    221   	    prog $attrib $@ over + 0 swap c! glGetUniformLocation ,
................................................................................
   404    405       0e sf!+ near f2* top bottom f- f/ sf!+ 0e sf!+ 0e sf!+
   405    406       right left f+ right left f- f/ sf!+
   406    407       top bottom f+ top bottom f- f/ sf!+
   407    408       near far f+ near far f- f/ sf!+ -1e sf!+
   408    409       sfloat+ sfloat+ near far f* f2* near far f- f/ sf!+ 0e sf!+
   409    410       drop ;
   410    411   
          412  +: ap-set ( -- )
          413  +    ap-matrix MVPMatrix set-matrix
          414  +    ap-matrix MVMatrix set-matrix ;
          415  +
   411    416   : >ap ( near far scale -- ) f2* 1/f { f: scale }
   412    417       scale dpy-w @ fm* fdup fnegate fswap
   413    418       scale dpy-h @ fm* fdup fnegate fswap >ortho
   414    419       \ cr ap-matrix .matrix
   415         -    ap-matrix MVPMatrix set-matrix
   416         -    ap-matrix MVMatrix set-matrix ;
          420  +    ap-set ;
   417    421   : >apxy ( xoff yoff -- )  y-apos sf!  x-apos sf! ;
   418    422   
   419    423   \ textures
   420    424   
   421    425   Create white-texture \ aabbggrr
   422    426     $ffffffff l,  $ffffffff l,
   423    427     $ffffffff l,  $ffffffff l,
................................................................................
   508    512       none-tex no-texture
   509    513       Texture 0 glUniform1i
   510    514       Ambient 1 ambient% glUniform1fv
   511    515       LightPos 0.0e 0.0e -0.3e glUniform3f
   512    516       z-bias set-color+
   513    517       .01e 100e dpy-w @ dpy-h @ min s>f f2/ 100 fm* >ap ;
   514    518   
   515         -: 2d-vertex ( index addr -- ) >r
   516         -    dup 2 GL_FLOAT  0 0 r> glVertexAttribPointer
   517         -    glEnableVertexAttribArray ;
   518         -
   519         -: 3d-vertex ( index addr -- ) >r
   520         -    dup 3 GL_FLOAT  0 0 r> glVertexAttribPointer
   521         -    glEnableVertexAttribArray ;
   522         -
   523         -: 4d-vertex ( index addr -- ) >r
   524         -    dup 4 GL_FLOAT  0 0 r> glVertexAttribPointer
   525         -    glEnableVertexAttribArray ;
   526         -
   527         -: rgba-vertex ( index addr -- ) >r
   528         -    dup 4 GL_UNSIGNED_BYTE GL_TRUE 0 r> glVertexAttribPointer
   529         -    glEnableVertexAttribArray ;
   530         -
   531    519   \ glDrawElements helper
   532    520   
   533    521   $8 cells buffer: gl-buffers
   534    522   : buf@ ( n -- buf ) cells gl-buffers + @ ;
   535    523   : bind-buf ( type n -- ) buf@ glBindBuffer ;
   536    524   
   537    525   0 Value array-buf

Changes to gles2/gl-sample.fs.

     1      1   \ opengl example
     2      2   
     3      3   require gl-helper.fs
     4      4   
     5         -also opengl
            5  +also opengl also [IFDEF] android android [THEN]
     6      6   
     7      7   tex: ascii-tex
     8      8   
     9      9   : load-textures ( -- )
    10     10       ascii-tex s" ascii.png" load-texture wrap mipmap ;
    11     11   
    12     12   \ triangle example
................................................................................
    35     35       1e0 2e0 >st v+
    36     36       0e0 0e0 >st v+ o> ;
    37     37   
    38     38   : normals >v
    39     39       3 0 DO  0e 0e -1e n>xyz v+  LOOP  o> ;
    40     40   
    41     41   Variable new-config
    42         -
    43         -[IFDEF] android
    44         -also android
    45         -
    46         -:noname  defers window-init
    47         -    ['] VertexShader ['] FragmentShader create-program to program
    48         -    program init load-textures ; IS window-init
    49         -
    50         -:noname new-config on ; is config-changed
    51         -[THEN]
    52         -
    53     42   FVariable touch -100e touch f!
    54     43   FVariable motion 0.01e motion f!
    55     44   
    56     45   : draw-tri-angle ( f -- )
    57     46       0.01e 0.02e 0.15e 1.0e glClearColor
    58     47       clear
    59     48       Ambient 1 ambient% glUniform1fv
................................................................................
    64     53       GL_TRIANGLES draw-elements
    65     54       v0 colors'
    66     55       ascii-tex
    67     56       GL_TRIANGLES draw-elements
    68     57       sync ;
    69     58   
    70     59   : draw-tri { f: angle -- angle' }
    71         -    new-config @ IF
    72         -	dpy-w @ dpy-h @ getwh dpy-w @ dpy-h @ d<> IF  new-config off  THEN
    73         -    THEN
    74     60       angle draw-tri-angle
    75     61       >looper
    76     62       *input >r r@ IF
    77     63   	r@ action @ abs 1 <> IF
    78     64   	    \ ." Touch at " r@ x0 ? r@ y0 ? cr
    79     65   	    \ r@ x0 @ 20 < r@ y0 @ 20 < and IF -1 (bye) THEN
    80     66   	    r@ x0 @ dpy-w @ 2/ - s>f dpy-h @ 2/ fm/

Added gles2/gl-simple.fs.

            1  +\ simple opengl test
            2  +
            3  +[defined] init-opengl 0= constant standalone
            4  +require gl-helper.fs
            5  +also opengl
            6  +also [IFDEF] android android [THEN]
            7  +standalone [if] init-opengl buffer-init [then]
            8  +
            9  +: simple-init ( -- )
           10  +    ['] VertexShader ['] FragmentShader create-program to program
           11  +    program init  0e >y-pos
           12  +    1e 0e 0e 1e glClearColor ;
           13  +
           14  +: draw ( -- )  none-tex
           15  +    v0 >v
           16  +    -1e -1e >xy  $FFFFFFFF rgba>c  n> v+
           17  +     1e -1e >xy  $FFFFFFFF rgba>c  n> v+
           18  +     0e  1e >xy  $FFFFFFFF rgba>c  n> v+ v>
           19  +    i0 0 i, 1 i, 2 i,
           20  +    GL_TRIANGLES draw-elements ;
           21  +
           22  +: simple ( -- )  simple-init
           23  +  1 level# +!  begin clear draw sync >looper level# @ 0= until ;
           24  +
           25  +simple

Changes to gles2/gl-terminal.fs.

    94     94   $ff40bf40 l, \ dimm Green
    95     95   $ff40bfbf l, \ dimm Yellow
    96     96   $ffbf4040 l, \ dimm Blue
    97     97   $ffbf40bf l, \ dimm Magenta
    98     98   $ffbfbf40 l, \ dimm Cyan
    99     99   $ffbfbfbf l, \ dimm White
   100    100   
   101         -: term-load-textures ( -- )
   102         -    chars-tex s" ascii.png" load-texture 2drop linear
          101  +: term-load-textures ( addr u -- )
          102  +    chars-tex load-texture 2drop linear
   103    103       GL_TEXTURE2 glActiveTexture
   104    104       color-tex color-matrix $10 1 rgba-map nearest
   105    105       GL_TEXTURE0 glActiveTexture ;
   106    106   
   107    107   Variable color-index
   108    108   bl dup $70 and 5 lshift or $F0F and 4 lshift color-index !
   109    109   Variable std-bg
................................................................................
   310    310   ' gl-page IS page
   311    311   ' gl-attr! IS attr!
   312    312   default-out op-vector !
   313    313   
   314    314   \ initialize
   315    315   
   316    316   : term-init ( -- )
   317         -    clazz >o hideProgress o>
          317  +    [IFDEF] clazz clazz >o hideProgress o> [THEN]
   318    318       >screen-orientation
   319    319       create-terminal-program to terminal-program
   320         -    terminal-program terminal-init term-load-textures form-chooser
          320  +    terminal-program terminal-init
          321  +    s" ascii.png" term-load-textures form-chooser
   321    322       unit-matrix MVPMatrix set-matrix ;
   322    323   
   323    324   :noname  defers window-init term-init config-changer ; IS window-init
   324    325   
   325    326   window-init
   326    327   
   327    328   previous previous \ remove opengl from search order