FreeType2 with OpenGL

Started by
2 comments, last by stefu 22 years, 1 month ago
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
Advertisement
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 bitmapchar *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).
Thanks.
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!!!                		memset(bmp,0,sizeof(bmp));		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];			}		}	     glNewList(base+n,GL_COMPILE);		 // bitmap is a binary image		 glBitmap(slot->bitmap.width,   				slot->bitmap.rows,				-slot->bitmap_left,				slot->bitmap.rows-slot->bitmap_top,				(slot->advance.x>>6),				(slot->advance.y>>6),                bmp ); 	     glEndList();       }  



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.)

This topic is closed to new replies.

Advertisement