NeHe OpenGL Lesson13 – Bitmap Fonts

screen_shot7-300x236 This demo shows us how to display some characters on the screen with OpenGL bitmap fonts. This is a very useful feature that we could used to display some text, debug information, fps or even menu content on the screen. Those staffs are not part of 3D scene, but usually head up display(HUD for short). Some very cool effect could be made by Flash, then those flashes could be integrated into the game as the menu pages or elements. Well, Scaleform does that.

Bitmap fonts use any of your computers built in fonts. Bitmaps font’s are 2D scalable fonts, they can not be rotated. They always face forward. Of course you could make your own text with texture mapping. But obviously, bitmap fonts has more flexibility than text texture mapping.

 

Go though the source code, display list and wglUseFontBitmaps works together to make the 96 characters. And one some platform specific code there: GDI device context object and font objects. Set the fonts object to the GDI device content before you create the bitmap fonts.
Here are the funny thing when draw the text on the screen:

GLvoid glPrint(const char *fmt, …)                    
{
    char        text[256];                                
    va_list        ap;                                        

    if (fmt == NULL)                                    
        return;                                            

    va_start(ap, fmt);                                    
        vsprintf(text, fmt, ap);                        
    va_end(ap);                                            

    glPushAttrib(GL_LIST_BIT);                            
    glListBase(base32);                                
    glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);    
    glPopAttrib();                                        
}

One point is that the parameters for this function are not fixed. It does as you see with printf function.

 

The other point is that draw bitmap fonts text is the same as call display list. No special operation should be take care of.  Here are the code the call this function:

glRasterPos2f(-0.45f+0.05f*float(cos(cnt1)), 0.32f*float(sin(cnt2)));
glPrint(“Active OpenGL Text With NeHe – %7.2f”, cnt1);

Here glRasterPos2f used to locate where the text will be display. This function locate the position in the raster space instead of world space.

 

The full source code could be downloaded from here.

posted @ 2012-08-23 22:19  opencoder  阅读(329)  评论(0编辑  收藏  举报