Back to index

extremetuxracer  0.5beta
FTTextureGlyph.cpp
Go to the documentation of this file.
00001 #include    "FTTextureGlyph.h"
00002 
00003 GLint FTTextureGlyph::activeTextureID = 0;
00004  
00005 FTTextureGlyph::FTTextureGlyph( FT_GlyphSlot glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height)
00006 :   FTGlyph( glyph),
00007     destWidth(0),
00008     destHeight(0),
00009     glTextureID(id)
00010 {
00011     err = FT_Render_Glyph( glyph, FT_RENDER_MODE_NORMAL);
00012     if( err || glyph->format != ft_glyph_format_bitmap)
00013     {
00014         return;
00015     }
00016 
00017     FT_Bitmap      bitmap = glyph->bitmap;
00018 
00019     destWidth  = bitmap.width;
00020     destHeight = bitmap.rows;
00021     
00022     if( destWidth && destHeight)
00023     {
00024         glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT);
00025         glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE);
00026         glPixelStorei( GL_UNPACK_ROW_LENGTH, 0);
00027         glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
00028 
00029         glBindTexture( GL_TEXTURE_2D, glTextureID);
00030         glTexSubImage2D( GL_TEXTURE_2D, 0, xOffset, yOffset, destWidth, destHeight, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.buffer);
00031 
00032         glPopClientAttrib();
00033     }
00034 
00035 
00036 //      0    
00037 //      +----+
00038 //      |    |
00039 //      |    |
00040 //      |    |
00041 //      +----+
00042 //           1
00043     
00044     uv[0].X( static_cast<float>(xOffset) / static_cast<float>(width));
00045     uv[0].Y( static_cast<float>(yOffset) / static_cast<float>(height));
00046     uv[1].X( static_cast<float>( xOffset + destWidth) / static_cast<float>(width));
00047     uv[1].Y( static_cast<float>( yOffset + destHeight) / static_cast<float>(height));
00048     
00049     pos.X( glyph->bitmap_left);
00050     pos.Y( glyph->bitmap_top);
00051 }
00052 
00053 
00054 FTTextureGlyph::~FTTextureGlyph()
00055 {}
00056 
00057 
00058 const FTPoint& FTTextureGlyph::Render( const FTPoint& pen)
00059 {
00060     if( activeTextureID != glTextureID)
00061     {
00062         glBindTexture( GL_TEXTURE_2D, (GLuint)glTextureID);
00063         activeTextureID = glTextureID;
00064     }
00065     
00066     glTranslatef( pen.X(),  pen.Y(), 0.0f);
00067 
00068     glBegin( GL_QUADS);
00069         glTexCoord2f( uv[0].X(), uv[0].Y());
00070         glVertex2f( pos.X(), pos.Y());
00071 
00072         glTexCoord2f( uv[0].X(), uv[1].Y());
00073         glVertex2f( pos.X(), pos.Y() - destHeight);
00074 
00075         glTexCoord2f( uv[1].X(), uv[1].Y());
00076         glVertex2f( destWidth + pos.X(), pos.Y() - destHeight);
00077         
00078         glTexCoord2f( uv[1].X(), uv[0].Y());
00079         glVertex2f( destWidth + pos.X(), pos.Y());
00080     glEnd();
00081 
00082     return advance;
00083 }
00084