Search code examples
openglftgl

FTGL texture fonts display boxes in GL_XOR mode


I would like to use FTGL texture fonts (FTTextureFont) to render fonts in XOR mode. The issue is that all characters are rendered as boxes (who's color is XOR-ed with the background). The calls to render the font are surrounded with:

glPushAttrib(GL_ALL_ATTRIB_BITS);
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);

and

glDisable(GL_COLOR_LOGIC_OP);
glPopAttrib();

I tried disabling the depth bits glDisable(GL_DEPTH_TEST), but it didn't help.


Solution

  • I found a solution in this answer: https://stackoverflow.com/a/29313195/4174026

    The issue was caused by the transparent pixels in the glyphs textures, which were not excluded.

    In OpenGL ES versions 1.0 and 1.1 the alpha test function (GL_ALPHA_TEST) can be used to discard the transparent fragments:

    glAlphaFunc(GL_GREATER, 0.2f); // Reject fragments with alpha < 0.2
    glEnable(GL_ALPHA_TEST);
    

    In newer versions of OpenGL ES custom pixel shader can be used instead:

    void main() {
        gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
    
        if (gl_FragColor.a <= 0.2) {
            discard;
        }
    }