Search code examples
opengltextdrawingftgl

Drawing text with shadow in OpenGL + FTGL


I'm drawing text in OpenGL using FTGL library and everything works just fine, however I would like to add shadow to the text. What I tried is drawing same text with black color and then draw text above that with normal color like this (pseudocode):

glColor3f(0, 0, 0); // outline color
DrawText(x-1, y-1, str);
DrawText(x+1, y-1, str);
DrawText(x+1, y+1, str);
DrawText(x-1, y+1, str);
glColor3f(1, 1, 1); // primary color
DrawText(x,y,str);

But I have to draw the text 5 times and it still does not look very good.

I would like to get something like on the screenshot

Text with shadow


Solution

  • There are probably a lot of ways to achieve that - some with higher quality than others.

    Here's what I would do:

    1. render the text to an in-memory grayscale pixmap.
    2. perform a gaussian blur on it (probably using a fast library such as QImageBlitz or ImageMagick). The blur radius should be about 2-3px.
    3. apply a steep tone-curve to the blurred image, so the luminance range [0.0, 0.9] is mapped to nearly 0.0. This makes it stop being blurry, and the result is a "fattened" version of the text. The tone curve should look something like this:

      alt text

    4. render that as the shadow, in black (using an appropriate blending mode to emulate alpha-blending). Then render the regular yellow text on top of it (with a small offset of your choice).

    Also, you can use different tone-curves depending on how soft a shadow you want. A linear tone-curve would give a very soft shadow.