Text output using OpenGL and SDL?

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?

/Andreas R?nnquist
andreas,ronnquist at gmail.com

Le lundi 16 octobre 2006 15:31, Andreas R?nnquist a ?crit?:

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?

/Andreas R?nnquist
andreas,ronnquist at gmail.com

Enable belnding :
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Load you surface with alpha :
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
image->pixels);

And it should work like in 2d :)++

Yes, that should do it. But if you are using a bunch of these textures, you should use TTF_RenderText_Blended instead. This creates an indexed image resulting in only 1/4 of memory consumption. I couldn’t find a specification of how the palette is created, so you should test this first and eventually convert the image.
Then use GL_ALPHA instead of GL_RGBA in your glTexImage2D call.
With disabled lighting, glColor specifies the text color.

Matthias

Laurent Carlier schrieb:> Le lundi 16 octobre 2006 15:31, Andreas R?nnquist a ?crit :

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?

/Andreas R?nnquist
andreas,ronnquist at gmail.com

Enable belnding :
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Load you surface with alpha :
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
image->pixels);

And it should work like in 2d :slight_smile:

++


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Sorry… TTF_RenderText_Shaded

Yes, that should do it. But if you are using a bunch of these textures,
you should use TTF_RenderText_Blended instead. This creates an indexed
image resulting in only 1/4 of memory consumption. I couldn’t find a
specification of how the palette is created, so you should test this
first and eventually convert the image.
Then use GL_ALPHA instead of GL_RGBA in your glTexImage2D call.
With disabled lighting, glColor specifies the text color.

Matthias

Laurent Carlier schrieb:> Le lundi 16 octobre 2006 15:31, Andreas R?nnquist a ?crit :

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?

/Andreas R?nnquist
andreas,ronnquist at gmail.com

Enable belnding :
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Load you surface with alpha :
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
image->pixels);

And it should work like in 2d :slight_smile:

++


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

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:


Grab the source package and look at bgui.{h|cc}

Screenshot with text:


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_Brodie-Tyrre>
http://www.brodie-tyrrell.org/

Anybody have any hints / pointers on where to start?

If you want to get really fancy, don’t use a texture at all…TrueType
fonts can be broken down into a collection of polygons, so you can draw
at any size and still look sharp (whereas they’ll blur if you build them
as textures and scale them).

An example of doing this:
http://homepages.paradise.net.nz/henryj/code/#FTGL

–ryan.

Ryan C. Gordon wrote:

Anybody have any hints / pointers on where to start?

If you want to get really fancy, don’t use a texture at all…TrueType
fonts can be broken down into a collection of polygons, so you can draw
at any size and still look sharp (whereas they’ll blur if you build them
as textures and scale them).

An example of doing this:
http://homepages.paradise.net.nz/henryj/code/#FTGL

if you have no anti-aliasing enabled / your graphics card cant do AA,
one will get ugly looking fonts of you use the polygon approach. but for
bigger text its really cool. but its not an option if you want to code a
console or something similiar ( a menu with a lot of small text etc). if
you go below 16 points its getting really ugly…

on the other side: if you use triangulated ttf’s, you can extrude them
and have 3d-text that you can easily place in 3d world coordinates.

Ryan C. Gordon skrev:

Anybody have any hints / pointers on where to start?

If you want to get really fancy, don’t use a texture at all…TrueType
fonts can be broken down into a collection of polygons, so you can draw
at any size and still look sharp (whereas they’ll blur if you build them
as textures and scale them).

An example of doing this:
http://homepages.paradise.net.nz/henryj/code/#FTGL

–ryan.

nah, that would be WAY too fancy for me… :wink:
Anyway - With help from the bogl code I found what I was after! I got
desired results using glBlendFunc(GL_SRC_ALPHA, GL_ONE) - thanks William!

Also - thanks to all you other guys contributing to this thread!

/Andreas
andreas.ronnquist at gmail.com