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); // o开发者_如何转开发utline 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
There are probably a lot of ways to achieve that - some with higher quality than others.
Here's what I would do:
- render the text to an in-memory grayscale pixmap.
- perform a gaussian blur on it (probably using a fast library such as QImageBlitz or ImageMagick). The blur radius should be about 2-3px.
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:
- 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.
I am usually doing it this way:
- set color to semitransparent black, eg (0,0,0,0.5)
- draw the text in all the nine directions (move to sides, and then diagonally)
- draw the fg text.
It looks quite good, and you can speed it up with render list and translations.
see here: http://i.stack.imgur.com/Dh68y.png
精彩评论