Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


OpenGL FreeType2 with OpenGL

This topic is 6032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!!11111 I'm trying to use glBitmap fonts with freetype. This is actually part of the code (from freetype tutorial)

       FT_GlyphSlot  slot = face->glyph;  // a small shortcut

       FT_UInt       glyph_index;
       int           pen_x, pen_y, n;

       .. initialise library ..
       .. create face object ..
       .. set character size ..
       pen_x = 300;
       pen_y = 200;
       for ( n = 0; n < num_chars; n++ )
         // load glyph image into the slot (erase previous one)

         error = FT_Load_Char( face, text[n], FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
         if (error) continue;  // ignore errors

         // now, draw to our target surface

         my_draw_bitmap( &slot->bitmap,
                         pen_x + slot->bitmap_left,
                         pen_y - slot->bitmap_top );
         // increment pen position 

         pen_x += slot->advance.x >> 6;

I'v modified it to go throught each character from 32 to 127 and create opengl displaylist in place if my_draw_bitmap. The problem is that I get only some extraordinary pixel-figures where can not be seen any characters. The problem is that I can't get correct conversion for the data from FT_Bitmap to glBitmap. I'w googled and searched all tutorials but can't get it working. There's not any example for the my_draw_bitmap procedure. Can anyone help? Edited by - stefu on March 21, 2002 3:21:03 PM

Share this post

Link to post
Share on other sites
Hmm, I did something similar a while ago. But I used standard alpha textures instead of GL bitmaps, so that I could get antialiased fonts. The conversion from the FT_Bitmap was straight forward:

FT_Bitmap *bitmap = ... // source bitmap

char *texture_data = ... // destination alpha texture (512*128)

for( y=0; y<bitmap->rows; y++ ) for( x=0; x<bitmap->width; x++ ) texture_data[y*512+x] = bitmap->buffer[y*bitmap->pitch+x];

The destination texture has a fixed width of 512, so be careful if you want to modify it, don''t forget to adapt the loop. And this thing is not fast, so use it only in a preprocess, not in realtime (or optimize it).

Share this post

Link to post
Share on other sites
The problem was that I didn''t know the bitmap format.
It seems each row must be k*32 bits (dword alignment).
I used k*8 bits, that was wrong.

Also all characters were upside-down.

Here''s the conversion that works now for me
if someone want''s to use Freetype in OpenGL

base = 0;
pixel_height = 18;

int error = FT_Init_FreeType( &library );
if ( error ) return;

error = FT_New_Face( library, "/usr/share/fonts/truetype/commercial/arial.ttf",0, &face );

if ( error ) return;

error = FT_Set_Pixel_Sizes(
face, /* handle to face object */
0, /* pixel_width */
16 ); /* pixel_height */

FT_GlyphSlot slot = face->glyph; // a small shortcut

base = glGenLists(96); // Storage For 96 Characters

for ( int n = 0; n < 96; n++ )

int ch = n+32;

// load glyph image into the slot (erase previous one)

error = FT_Load_Char( face, ch, FT_LOAD_RENDER|FT_LOAD_MONOCHROME);
if (error) continue; // ignore errors

char bmp[64*64]; // be carrefull!!!


for(int y=0; y<slot->bitmap.rows; y++)
int dst_pitch = 4;
while(slot->bitmap.pitch>dst_pitch) dst_pitch+=4;

GLubyte *src_row = slot->bitmap.buffer + y*slot->bitmap.pitch;
GLubyte *dst_row = bmp + (slot->bitmap.rows-y-1)*dst_pitch;

int src_pitch = slot->bitmap.pitch;

for(int x=0; x<src_pitch; x++ )
dst_row[x] = src_row[x];


// bitmap is a binary image

bmp );



ps. Can I share fon''t with my app? Is there free font''s to do that? (Just to ake sure font is always available in the same location.)

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!