GLuint C_GLTextureMappedFont::LoadGLTexture( std::string filename )
{
GLuint texture;
AUX_RGBImageRec *TextureImage = NULL; // Create Storage Space For The Textures
if( ( TextureImage = auxDIBImageLoad( filename.c_str() ) ) )
{
::glGenTextures( 1, &texture ); // Create Two Texture
::glBindTexture( GL_TEXTURE_2D, texture );
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
::glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data );
if( TextureImage->data ) // If Texture Image Exists
{
free( TextureImage->data ); // Free The Texture Image Memory
}
free( TextureImage ); // Free The Image Structure
}
return texture;
}
GLuint C_GLTextureMappedFont::LoadGLTextureBitmapFile( char path[], COLORREF transparent_color )
{
HBITMAP hBMP;
BITMAP BMP;
GLuint Texture = ERROR_CNLT;
::glGenTextures( 1, &Texture ); // Generate Textures
hBMP = (HBITMAP)::LoadImage( NULL, path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
if( hBMP )
{
::GetObject( hBMP, sizeof( BMP ), &BMP );
::glBindTexture( GL_TEXTURE_2D, Texture ); // Bind Our Texture
::glPixelStorei( GL_UNPACK_ALIGNMENT, 3 ); // Pixel Storage Mode ( Word Alignment / 4 Bytes )
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Linear Filtering
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Linear Filtering
::glTexImage2D( GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, (unsigned char*)BMP.bmBits );
::DeleteObject( hBMP ); // Delete The Bitmap Object
delete[] data;
return Texture;
}
return ERROR_CNLT;
}
bool C_GLTextureMappedFont::BuildFont( std::string filename, COLORREF transparent_color, float scale_x, float scale_y )
{
Destroy();
m_texture_id = LoadGLTexture( filename );
// m_texture_id = LoadGLTextureBitmapFile( filename, transparent_color );
float cx; // Holds Our X Character Coord
float cy; // Holds Our Y Character Coord
m_base = ::glGenLists( 256 ); // Creating 256 Display Lists
float char_width = 1.0f/16.0f*scale_x;
float char_height = 1.0f/16.0f*scale_y;
m_char_width = char_width;
m_char_height = char_height;
::glLoadIdentity();
::glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
::glShadeModel( GL_SMOOTH ); // Enables Smooth Color Shading
::glBindTexture( GL_TEXTURE_2D, m_texture_id ); // Select Our Font Texture
for( int loop=0; loop<256 ; loop++ ) // Loop Through All 256 Lists
{
cx = float(loop%16)/16.0f; // X Position Of Current Character
cy = float(loop/16)/16.0f; // Y Position Of Current Character
::glNewList( m_base+loop, GL_COMPILE ); // Start Building A List
::glBegin( GL_QUADS ); // Use A Quad For Each Character
::glTexCoord2f( cx, 1-cy-0.0625f ); // Texture Coord (Bottom Left)
::glVertex3f( 0, 0, 0 ) ; // Vertex Coord (Bottom Left)
::glTexCoord2f( cx+0.0625f, 1-cy-0.0625f ); // Texture Coord (Bottom Right)
::glVertex3f( char_width, 0, 0 ); // Vertex Coord (Bottom Right)
::glTexCoord2f( cx+0.0625f, 1-cy ); // Texture Coord (Top Right)
::glVertex3f( char_width, char_height, 0 ); // Vertex Coord (Top Right)
::glTexCoord2f( cx, 1-cy ); // Texture Coord (Top Left)
::glVertex3f( 0, char_height, 0 ); // Vertex Coord (Top Left)
::glEnd(); // Done Building Our Quad (Character)
::glTranslatef( char_width, 0, 0 ); // Move To The Right Of The Character
::glEndList(); // Done Building The Display List
} // Loop Until All 256 Are Built
::glLoadIdentity();
m_bFontBuilt = true;
return true;
}