... as long as glyphs don't overlap.
That's the point - they do overlap
Here is simple shader with custom blending.
Vertex Shader
varying vec2 backPos;
void main()
{
// transform the vertex position
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
// transform the texture coordinates
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
// forward the vertex color
gl_FrontColor = gl_Color;
// convert screen coordinates to texture coordinates
backPos = gl_Position.xy / 2.0 + vec2(0.5, 0.5);
}
Fragment shader:
uniform sampler2D texture;
uniform sampler2D background;
varying vec2 backPos;
void main()
{
// lookup the pixel in the texture
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * gl_Color;
// lookup the pixel in the background
vec4 back = texture2D(background, backPos);
if(back.a < 0.001)
{
// special case
gl_FragColor = pixel;
}
else
{
// usual alpha blend
gl_FragColor.rgb = pixel.rgb * pixel.a + back.rgb * (1.0 - pixel.a);
gl_FragColor.a = pixel.a + back.a * (1.0 - pixel.a);
}
}
Code (SFML.Net):
RenderTexture target
= new RenderTexture
(...);Shader customBlendShader
= ...;customBlendShader
.SetParameter("texture", Shader
.CurrentTexture);customBlendShader
.SetParameter("background", target
.Texture);RenderStates customBlendState
= new RenderStates
(customBlendShader
);customBlendState
.BlendMode = BlendMode
.None;...target.Clear(Color
.Transparent);target
.Draw(someSprite, customBlendState
);target
.Display();window
.Clear(Color
.White);window
.Draw(targetSprite
);window
.Display(); But this is not ideal solution, because:
1. If operator in fragment shaders should be avoided
2. AFAIK same texture for reading and writing (target.Texture) may cause undefined behavior.
In my test application there is a small flickering on rendered RenderTexture.