[SDL] text output using OpenGL and SDL?
William Brodie-Tyrrell
william at brodie-tyrrell.org
Mon Oct 16 09:31:41 PDT 2006
I was using blend mode ONE,ONE which adds the letters to the
background... works fine for solid colours (e.g. hand drawn bitmapped
fonts) but anti-aliased edges tend to vary in brightness with the
background (of course).
I can post code (using freetype2; nothing SDL about it) if you like;
it'd not be complex to change to SRC_ALPHA, ONE_MINUS_SRC_ALPHA and
generating the src alpha channel from the rendered text. What Matthias
says about using a GL_ALPHA formatted texture is a great suggestion,
just be aware that it limits you to monochrome text.
Actually (shameless plug!), some code is available here:
http://sourceforge.net/projects/bogl
Grab the source package and look at bgui.{h|cc}
Screenshot with text:
http://sourceforge.net/project/screenshots.php?group_id=103764&ssid=35204
You can see the brightening there as the text goes over non-black
things.
In fact, I've just changed my font renderer to implement Matthias'
suggestion and it works very nicely. Blend mode is
SRC_ALPHA/ONE_MINUS_SRC_ALPHA and you render your text to an ALPHA8
texture instead of LUMINANCE8. You won't see that on my sourceforge
version though it has everything else; change these lines in bgui.cc
81:blend=new BlendMode(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
227:img=new Texture(xres,yres,GL_ALPHA8,GL_UNSIGNED_BYTE,data,xres*yres);
My code builds the texture from freetype and then given a string,
generates a vertex array filled with quads that draw the desired
glyphs. That vertex array is reused across frames of course and
changed only when the string changes.
Stoned koala bears drooled eucalyptus spit in awe as Andreas Rönnquist
said:
> Does anybody have any good examples uploading fonts/text to
> OpenGL-textures, making it use alpha-channel for transparency?
>
> In my code I output some text using TTF_RenderText_Blended to a surface
> - I make a new surface that is modified to work fine with OpenGL (power
> of 2 sized) - and then make an OpenGL-texture with the data simply using
> glTexImage2D. - But I would like it to be truly blended towards the
> _background_. As it is now there is a black square around all the
> letters (no transparency at all), the letters are nicely blended
> towards black, but I dont really know where to start to get the results
> I am after.
>
> The goal is to make text output functionality using OpenGL and OpenGL
> Display Lists.
>
> Anybody have any hints / pointers on where to start?
--
William Brodie-Tyrrell
Carpe Diem - fish of the day.
<william at brodie-tyrrell.org>
http://www.brodie-tyrrell.org/
More information about the SDL
mailing list