OpenGL 3.x GLSL font rendering question

Snoob 105 Sep 11, 2013 at 17:59 glsl opengl fonts

Actually I’ve migrated my FreeType font renderer from OpenGL 2.x to OpenGL 3.x support. Now rendering is supported on both platforms without any code change. So far so good. However I got a question to the GLSL fragment shader routine. Before I’ve migrated my source I’ve used this call for font sprite rasterization:

uniform sampler2D DiffuseMap; 
varying vec4 vColor; 	
varying vec2 vTexCoord0; 

gl_FragColor = vec4(vColor.rgb, texture2D(DiffuseMap, vTexCoord0).a);

With this snipped I got pin sharp results on font rendering also on blending: Pin sharp rendering sample But this snipped is not working on OpenGL 3.x. If I use this snipped I only got the sprite quads rasterized, filled with the vertex attribute color. The single font characters are not visible. By this reason I changed the snipped to:

vec4 vTexColor = texture2D(DiffuseMap,;
gl_FragColor = (vTexColor.r, vTexColor.r, vTexColor.r, vTexColor.r)*vColor;

With this snipped the font rendering is working with both renderer. But on both renderer the font looks now unsharpen, especially on blending: Unsharpen rendering sample. For blending I use: GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_FUNC_ADD.

Could someone explain me what is the difference between both routines and why the first one doesn’t work with OpenGL 3.x? Is it possible that alpha value mapping is not supported? Maybe knows someone how I could sharpen the rendering results if I have to use the second snipped?

10 Replies

Please log in or register to post a reply.

v71 105 Sep 11, 2013 at 22:12

I have the same problem , and i am still waiting an answer from freetype2 support , actually seems like the option is unsupported, i tried every blend mode, nothing worked for me as well.

Vilem_Otte 117 Sep 11, 2013 at 23:02

This is just one of the things that forced me to create my own fonts (distance field ones), achieving good enough quality text without the need for 3rd party libraries (like FreeType).

Alpha value mapping is definitely supported, although a bug might be in your shader. Try using some GLSL version and sticking heavily to it’s specifications. Also are you sure that blending is correctly set up and enabled (try it with just 0.5 value in alpha - whether it does render semi transparent stuff)?

Also it is not common to use .rgb indexing instead of .xyz (I know that some Mobility Radeon drivers screams warnings about these, but runs), try to avoid them.

Stainless 151 Sep 12, 2013 at 08:33

Try this

gl_FragColor = vec4(, texture2D(DiffuseMap,;
Snoob 105 Sep 12, 2013 at 11:45

Thanks for your feedback guys. Meanwhile I could solve the issue based on Stainless snipped with a short modification:

gl_FragColor = vec4(, texture(DiffuseMap, vTexCoord0).y);

Now I’ts working with both renderer with pin sharp results, also on blending. What I doesn’t know until yet is that the GLSL .rgba indexing is mapped to the texture color channel values, while .xyzw indexing only to vector positions. By this reason .a indexing has no effect on OpenGL 3.x while the internal texture format is RG8 and there is no alpha value channel given.

v71 105 Sep 13, 2013 at 08:44

I have used Snoob’s snippet but i still get blurred fonts

Stainless 151 Sep 13, 2013 at 12:19

Check the opengl state, do you have blending enabled? What is the screen pixel format?

v71 105 Sep 13, 2013 at 16:30

Yes i have blending enable, if i disable it i get only white quads , and hte screen format is RGBA 32 bits color and 32bits for depth

TheNut 179 Sep 14, 2013 at 03:41

What do you mean by blurred fonts? Is the library properly rasterizing the font? Are you mip-mapping the font (you shouldn’t be)? Make sure you add 1 pixel padding to your glyphs so that OpenGL’s bilinear filter doesn’t render the edges from adjacent glyphs. Perhaps obvious, but don’t scale the fonts. Always rasterize the highest resolution and scale down.

I stopped using FreeType long ago as it’s a fairly bloated library and rarely do you distribute true type fonts (for legal reasons mostly). I prefer to create bitmap fonts from a bitmap font converter or render them through Blender. It’s something worth considering, unless you really need the flexibility of having vector fonts on hand.

Snoob 105 Sep 14, 2013 at 11:09

Fell free to check my FreeType font plug-in and renderer sources:

The actual sources are in an early alpha version. There are a lot of things I’ve to do, but it works and may be a help.

rombust 102 Sep 26, 2013 at 12:28

The ClanLib SDK uses OpenGL subpixel rendering without problems. This is with freetype (on Linux) and without (on Windows).

It uses the blend function: src = blend_constant_color dest = blend_one_minus_src_color src_alpha = blend_zero dest_alpha = blend_one

Using FT_LOAD_TARGET_LCD and FT_RENDER_MODE_LCD to obtain the subpixel glyphs