Sign in to follow this  
paulhankin

Bug in the freetype openGL tutorial

Recommended Posts

I've just looked at the NeHe freetype tutorial. [url="http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/"]http://nehe.gamedev....n_opengl/24001/[/url]

It's a great tutorial, and I managed to get freetype fonts in my opengl game in no time at all :)

There's a small, but significant problem with the code though! The textures are created as luminance-alpha textures, which is good, but the mistake is that both the luminance _and_ the alpha are set to the value from the glyph bitmap.

This basically produces an image with premultiplied alpha, yet the alpha gets applied once more during rendering -- causing the edges of glyphs to become much more jaggy.
Simply changing the bitmap creation code from:

[code]
for(int j = 0; j <height ; j++) {
for(int i = 0; i < width; i++) {
expanded_data[2 * (i + j * width)] = expanded_data[2 * (i + j * width) + 1] =
(i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width * j];
}
}[/code]

to: [code]for(int j = 0; j <height ; j++) {
for(int i = 0; i < width; i++) {
expanded_data[2 * (i + j * width)] = 255;
expanded_data[2 * (i + j * width) + 1] =
(i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width * j];
}
}[/code]
produces a softer, less jaggy image.

Share this post


Link to post
Share on other sites
First of all: Thanks for this great tutorial, it really kicks ass...[img]http://public.gamedev.net//public/style_emoticons/default/wub.png[/img] love it..
([url="http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/"]http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/[/url])

There's another bug within this code.
a memory leak is caused by the glyph not getting freed!

bugfix: add the line

[CODE]
FT_Done_Glyph(glyph);
[/CODE]


to the bottom of the "make_dlist" function.

[color=#D1EDFF][font=monospace][size=3][background=rgb(15, 25, 42)]documentation for FT_Get_Glyph says:[/background][/size][/font][/color]

[color=#000000][font=Verdana, Geneva, Arial, Helvetica, serif][size=1]Note that the created [/size][/font][/color][url="http://www.freetype.org/freetype2/docs/reference/ft2-glyph_management.html#FT_Glyph"]FT_Glyph[/url][color=#000000][font=Verdana, Geneva, Arial, Helvetica, serif][size=1] object must be released with [/size][/font][/color][url="http://www.freetype.org/freetype2/docs/reference/ft2-glyph_management.html#FT_Done_Glyph"]FT_Done_Glyph[/url][color=#000000][font=Verdana, Geneva, Arial, Helvetica, serif][size=1].[/size][/font][/color]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this