Check-in [2e267bcf9f]
Not logged in

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

Overview
Comment:Uniform program for text and normal textures
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2e267bcf9f6cd1add1b46fe297b5ce6e39da9f2f
User & Date: bernd 2013-11-27 00:21:07
Context
2013-11-27
22:22
Test case for text widget check-in: 2afe3e3af7 user: bernd tags: trunk
00:21
Uniform program for text and normal textures check-in: 2e267bcf9f user: bernd tags: trunk
2013-11-26
23:02
Some changes to freetype-gl stuff check-in: fd1cd6736c user: bernd tags: trunk
Changes

Changes to freetype-gl/Makefile.am.

14
15
16
17
18
19
20
21
## flags are passed to the compiler for both C and C++, in addition to the
## language-specific options.
AM_CPPFLAGS = $(EXAMPLE_CFLAGS) -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include/ -I/home/bernd/proj/freetype2-android/include

lib_LTLIBRARIES = libfreetype-gl.la
libfreetype_gl_la_SOURCES = texture-atlas.c texture-font.c vector.c
include_HEADERS = freetype-gl.h
libfreetype_gl_la_LIBADD = -lm -lGLESv2 -L. -lfreetype -lz -lglib-2.0 @LIBOBJS@







|
14
15
16
17
18
19
20
21
## flags are passed to the compiler for both C and C++, in addition to the
## language-specific options.
AM_CPPFLAGS = $(EXAMPLE_CFLAGS) -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include/ -I/home/bernd/proj/freetype2-android/include

lib_LTLIBRARIES = libfreetype-gl.la
libfreetype_gl_la_SOURCES = texture-atlas.c texture-font.c vector.c
include_HEADERS = freetype-gl.h
libfreetype_gl_la_LIBADD = -lm -l@LIBGL@ -L. -lfreetype -lz -lglib-2.0 @LIBOBJS@

Changes to freetype-gl/configure.ac.

9
10
11
12
13
14
15




16
17
18

19
20
21
AC_PROG_CC
AM_PROG_AR
LT_INIT

case $CC in
     *android*)
	LIBOBJS="-llog -lglib-android-1.0"




	;;
esac
AC_SUBST(LIBOBJS)


AC_CONFIG_FILES([Makefile])
AC_OUTPUT







>
>
>
>



>



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
AC_PROG_CC
AM_PROG_AR
LT_INIT

case $CC in
     *android*)
	LIBOBJS="-llog -lglib-android-1.0"
	LIBGL=GLESv2
	;;
     *)
	LIBGL=GL
	;;
esac
AC_SUBST(LIBOBJS)
AC_SUBST(LIBGL)

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Changes to freetype-gl/texture-atlas.c.

33
34
35
36
37
38
39

40



41
42
43
44
45
46
47
 */
//#if defined(__APPLE__)
//    #include <OpenGL/gl.h>
//#else
//    #include <GL/gl.h>
//#endif


#include <GLES2/gl2.h>




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "texture-atlas.h"







>

>
>
>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 */
//#if defined(__APPLE__)
//    #include <OpenGL/gl.h>
//#else
//    #include <GL/gl.h>
//#endif

#ifdef __ANDROID__
#include <GLES2/gl2.h>
#else
#include <GL/gl.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "texture-atlas.h"

Changes to ftgl-helper.fs.

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
..
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

121
122
123
124

125
126
." Textur in Atlas: " atlas texture_atlas_t-id l@ . cr
: atlas-tex  atlas texture_atlas_t-id l@ dup to current-tex
    GL_TEXTURE_2D swap glBindTexture ;

\ render font into vertex buffers

2 sfloats buffer: penxy
Variable color $FFC0A000 color !

: xy, { glyph -- }
    penxy sf@ penxy sfloat+ sf@ { f: xp f: yp }
    glyph texture_glyph_t-offset_x l@ s>f
    glyph texture_glyph_t-offset_y l@ s>f { f: xo f: yo }
    glyph texture_glyph_t-width  l@ s>f
    glyph texture_glyph_t-height l@ s>f { f: w f: h }    
................................................................................
	I xc@+ swap >r xchar@xy r>
    I - +LOOP  drop ;

: load-glyphs ( -- )
    font
    "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\0" >wide$ drop texture_font_load_glyphs drop ;

GL_FRAGMENT_SHADER shader: textFragmentShader
#precision
uniform vec3 u_LightPos;        // The position of the light in eye space.
uniform sampler2D u_Texture;    // The input texture.
uniform float u_Ambient;        // ambient lighting level
 
varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the
                                // triangle per fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
 
// The entry point for our fragment shader.
void main()
{
    gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
//    gl_FragColor.rgb = v_Color.rgb;
}

' VertexShader ' textFragmentShader create-program Value textprogram
textprogram init

: <render ( -- )
    textprogram glUseProgram

    .01e 100e 100e >ap
    atlas-tex v0 i0 ;

: render> ( -- )  GL_TRIANGLES draw-elements ;


previous previous







|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|


|
>



|
>


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
..
90
91
92
93
94
95
96




















97
98
99
100
101
102
103
104
105
106
107
108
." Textur in Atlas: " atlas texture_atlas_t-id l@ . cr
: atlas-tex  atlas texture_atlas_t-id l@ dup to current-tex
    GL_TEXTURE_2D swap glBindTexture ;

\ render font into vertex buffers

2 sfloats buffer: penxy
Variable color $FFC0A0FF color !

: xy, { glyph -- }
    penxy sf@ penxy sfloat+ sf@ { f: xp f: yp }
    glyph texture_glyph_t-offset_x l@ s>f
    glyph texture_glyph_t-offset_y l@ s>f { f: xo f: yo }
    glyph texture_glyph_t-width  l@ s>f
    glyph texture_glyph_t-height l@ s>f { f: w f: h }    
................................................................................
	I xc@+ swap >r xchar@xy r>
    I - +LOOP  drop ;

: load-glyphs ( -- )
    font
    "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\0" >wide$ drop texture_font_load_glyphs drop ;





















program init

: <render ( -- )
    program glUseProgram
    1-bias set-color+
    .01e 100e 100e >ap
    atlas-tex v0 i0 ;

: render> ( -- )  GL_TRIANGLES draw-elements
    ( Coloradd 0e fdup fdup fdup glUniform4f ) ;

previous previous

Changes to ftgl-sample.fs.

1
2
3
4
5
6
7
8
9
10
11
\ freetype GL example

require gl-helper.fs
[IFUNDEF] android window-init [THEN]

require ftgl-helper.fs

\ Demo Toplevel

also freetype-gl
also opengl



|







1
2
3
4
5
6
7
8
9
10
11
\ freetype GL example

require gl-helper.fs
[IFUNDEF] android helper-init [THEN]

require ftgl-helper.fs

\ Demo Toplevel

also freetype-gl
also opengl

Changes to gl-helper.fs.

263
264
265
266
267
268
269

270
271
272
273
274
275
276
...
289
290
291
292
293
294
295
296

297
298
299
300
301
302
303
304
305
306
307
308
309
310

311
312
313
314
315
316
317
...
321
322
323
324
325
326
327

328
329
330
331
332
333
334
...
354
355
356
357
358
359
360





361
362
363
364
365
366
367
...
381
382
383
384
385
386
387


388
389
390
391
392
393
394
...
486
487
488
489
490
491
492

493
494
495
496
497
498

499
500
501
502
503
504
505
}

GL_FRAGMENT_SHADER shader: FragmentShader
#precision
uniform vec3 u_LightPos;        // The position of the light in eye space.
uniform sampler2D u_Texture;    // The input texture.
uniform float u_Ambient;        // ambient lighting level

 
varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the
                                // triangle per fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
 
................................................................................
 
    // Add attenuation.
    diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));
 
    // Add ambient lighting
    diffuse = (diffuse * ( 1.0 - u_Ambient )) + u_Ambient;
 
    // Multiply the color by the diffuse illumination level and texture value to get final output color.

    gl_FragColor = vec4(diffuse, diffuse, diffuse, 1.0) * v_Color * texture2D(u_Texture, v_TexCoordinate);
    // vec4 pixel = (v_Color * texture2D(u_Texture, v_TexCoordinate));
    // gl_FragColor = vec4(pixel.rgb, pixel.a);
    // gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
    // gl_FragColor = (v_Color * diffuse);
    // gl_FragColor = v_Color;
}


0 Value MVPMatrix
0 Value MVMatrix
0 Value LightPos
0 Value Texture
0 Value ambient

0 Value program

: create-program { vs-xt fs-xt -- program }
    glCreateProgram dup >r IF
	r@ vs-xt execute glAttachShader
	r@ fs-xt execute glAttachShader
	vs-xt r@ >bindattrib
................................................................................
[IFDEF] GL_TEXTURE_EXTERNAL_OES
GL_FRAGMENT_SHADER shader: oesShader
#extension GL_OES_EGL_image_external : require
#precision
uniform vec3 u_LightPos;        // The position of the light in eye space.
uniform samplerExternalOES u_Texture;
uniform float u_Ambient;        // ambient lighting level

 
varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the
                                // triangle per fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
void main() {
................................................................................

: clear ( -- )
    GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT or glClear ;

: sf,  ( float -- )  here 1 sfloats allot sf! ;
: sf!+ ( float addr -- addr' )  dup sf! sfloat+ ;






Create unit-matrix
1.0e sf, 0.0e sf, 0.0e sf, 0.0e sf,
0.0e sf, 1.0e sf, 0.0e sf, 0.0e sf,
0.0e sf, 0.0e sf, 1.0e sf, 0.0e sf,
0.0e sf, 0.0e sf, 0.0e sf, 1.0e sf,

unit-matrix 12 sfloats + Constant x-pos
................................................................................
	I 4 sfloats bounds DO
	    I sf@ f.
	1 sfloats +LOOP cr
    4 sfloats +LOOP ;

: set-matrix ( addr handle -- ) swap >r
    1 false r> glUniformMatrix4fv ;



: >ortho { f: near f: far f: left f: right f: top f: bottom -- }
    ap-matrix
    near f2* right left f- f/ sf!+ 0e sf!+ 0e sf!+ 0e sf!+
    0e sf!+ near f2* top bottom f- f/ sf!+ 0e sf!+ 0e sf!+
    right left f+ right left f- f/ sf!+
    top bottom f+ top bottom f- f/ sf!+
................................................................................
    GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
    program glUseProgram
    program "u_MVPMatrix\0" drop glGetUniformLocation to MVPMatrix
    program "u_MVMatrix\0" drop glGetUniformLocation to MVMatrix
    program "u_LightPos\0" drop glGetUniformLocation to LightPos
    program "u_Texture\0" drop glGetUniformLocation to Texture
    program "u_Ambient\0" drop glGetUniformLocation to Ambient

    GL_UNPACK_ALIGNMENT 1 glPixelStorei
    GL_TEXTURE0 glActiveTexture
    none-tex no-texture
    Texture 0 glUniform1i
    Ambient 1 ambient% glUniform1fv
    LightPos 0.0e 0.0e -0.3e glUniform3f

    .01e 100e dpy-w @ dpy-h @ min s>f f2/ 100 fm* >ap ;

: 2d-vertex ( index addr -- ) >r
    dup 2 GL_FLOAT  0 0 r> glVertexAttribPointer
    glEnableVertexAttribArray ;

: 3d-vertex ( index addr -- ) >r







>







 







|
>
|













>







 







>







 







>
>
>
>
>







 







>
>







 







>






>







263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
...
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
}

GL_FRAGMENT_SHADER shader: FragmentShader
#precision
uniform vec3 u_LightPos;        // The position of the light in eye space.
uniform sampler2D u_Texture;    // The input texture.
uniform float u_Ambient;        // ambient lighting level
uniform vec4 u_Coloradd;        // color bias for texture
 
varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the
                                // triangle per fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
 
................................................................................
 
    // Add attenuation.
    diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));
 
    // Add ambient lighting
    diffuse = (diffuse * ( 1.0 - u_Ambient )) + u_Ambient;
 
// Multiply the color by the diffuse illumination level and texture value to get final output color.
    vec4 texture = texture2D(u_Texture, v_TexCoordinate) + u_Coloradd;
    gl_FragColor = vec4(diffuse, diffuse, diffuse, 1.0) * v_Color * texture;
    // vec4 pixel = (v_Color * texture2D(u_Texture, v_TexCoordinate));
    // gl_FragColor = vec4(pixel.rgb, pixel.a);
    // gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
    // gl_FragColor = (v_Color * diffuse);
    // gl_FragColor = v_Color;
}


0 Value MVPMatrix
0 Value MVMatrix
0 Value LightPos
0 Value Texture
0 Value ambient
0 Value Coloradd
0 Value program

: create-program { vs-xt fs-xt -- program }
    glCreateProgram dup >r IF
	r@ vs-xt execute glAttachShader
	r@ fs-xt execute glAttachShader
	vs-xt r@ >bindattrib
................................................................................
[IFDEF] GL_TEXTURE_EXTERNAL_OES
GL_FRAGMENT_SHADER shader: oesShader
#extension GL_OES_EGL_image_external : require
#precision
uniform vec3 u_LightPos;        // The position of the light in eye space.
uniform samplerExternalOES u_Texture;
uniform float u_Ambient;        // ambient lighting level
uniform vec4 u_Colorad;         // color bias for texture
 
varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the
                                // triangle per fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
void main() {
................................................................................

: clear ( -- )
    GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT or glClear ;

: sf,  ( float -- )  here 1 sfloats allot sf! ;
: sf!+ ( float addr -- addr' )  dup sf! sfloat+ ;

Create z-bias
0e sf, 0e sf, 0e sf, 0e sf,
Create 1-bias
1e sf, 1e sf, 1e sf, 0e sf,

Create unit-matrix
1.0e sf, 0.0e sf, 0.0e sf, 0.0e sf,
0.0e sf, 1.0e sf, 0.0e sf, 0.0e sf,
0.0e sf, 0.0e sf, 1.0e sf, 0.0e sf,
0.0e sf, 0.0e sf, 0.0e sf, 1.0e sf,

unit-matrix 12 sfloats + Constant x-pos
................................................................................
	I 4 sfloats bounds DO
	    I sf@ f.
	1 sfloats +LOOP cr
    4 sfloats +LOOP ;

: set-matrix ( addr handle -- ) swap >r
    1 false r> glUniformMatrix4fv ;

: set-color+ ( addr -- )  Coloradd 1 rot glUniform4fv ;

: >ortho { f: near f: far f: left f: right f: top f: bottom -- }
    ap-matrix
    near f2* right left f- f/ sf!+ 0e sf!+ 0e sf!+ 0e sf!+
    0e sf!+ near f2* top bottom f- f/ sf!+ 0e sf!+ 0e sf!+
    right left f+ right left f- f/ sf!+
    top bottom f+ top bottom f- f/ sf!+
................................................................................
    GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
    program glUseProgram
    program "u_MVPMatrix\0" drop glGetUniformLocation to MVPMatrix
    program "u_MVMatrix\0" drop glGetUniformLocation to MVMatrix
    program "u_LightPos\0" drop glGetUniformLocation to LightPos
    program "u_Texture\0" drop glGetUniformLocation to Texture
    program "u_Ambient\0" drop glGetUniformLocation to Ambient
    program "u_Coloradd\0" drop glGetUniformLocation to Coloradd
    GL_UNPACK_ALIGNMENT 1 glPixelStorei
    GL_TEXTURE0 glActiveTexture
    none-tex no-texture
    Texture 0 glUniform1i
    Ambient 1 ambient% glUniform1fv
    LightPos 0.0e 0.0e -0.3e glUniform3f
    z-bias set-color+
    .01e 100e dpy-w @ dpy-h @ min s>f f2/ 100 fm* >ap ;

: 2d-vertex ( index addr -- ) >r
    dup 2 GL_FLOAT  0 0 r> glVertexAttribPointer
    glEnableVertexAttribArray ;

: 3d-vertex ( index addr -- ) >r