开发者

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

Drawing text with shadow in OpenGL + FTGL


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:

    Drawing text with shadow in OpenGL + FTGL

  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.


I am usually doing it this way:

  1. set color to semitransparent black, eg (0,0,0,0.5)
  2. draw the text in all the nine directions (move to sides, and then diagonally)
  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜