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 @   opencoder  阅读(330)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示