NeHe OpenGL Lesson14 – Outline Fonts

screen_shot8-300x239

This sample program show us how to display 3d characters on the screen. Those characters are called outline fonts. They are different from the bitmap fonts that are 2d only. Outline fonts are created with your computer installed fonts. Those characters even could be lit because of they have vertex normal generated. In addition to that, they also could be created with solid or wireframe mode.

 

Here, most of the source code are the same as the Bitmap fonts one. The main idea was very simple: firstly, create the outline fonts with the system font object and OpenGL display list; Secondly, when draw some characters, just take them as 3d objects and call the created display list. Some part of the source code was copied here, icon_cool I just do not want this article too short. Haha! Actually, they are the core of this program, so …

GLvoid BuildFont(GLvoid)                             // Build Our Bitmap Font
{
    HFONT    font;                                   // Windows Font ID
 
    base = glGenLists(256);                          // Storage For 256 Characters
 
    font = CreateFont(    -12,                       // Height Of Font
                        0,                           // Width Of Font
                        0,                           // Angle Of Escapement
                        0,                           // Orientation Angle
                        FW_BOLD,                     // Font Weight
                        FALSE,                       // Italic
                        FALSE,                       // Underline
                        FALSE,                       // Strikeout
                        ANSI_CHARSET,                // Character Set Identifier
                        OUT_TT_PRECIS,               // Output Precision
                        CLIP_DEFAULT_PRECIS,         // Clipping Precision
                        ANTIALIASED_QUALITY,         // Output Quality
                        FF_DONTCARE|DEFAULT_PITCH,   // Family And Pitch
                        "Comic Sans MS");            // Font Name
 
    SelectObject(hDC, font);                         // Selects The Font We Created
 
    wglUseFontOutlines(    hDC,                      // Select The Current DC
                        0,                           // Starting Character
                        255,                         // Number Of Display Lists To Build
                        base,                        // Starting Display Lists
                        0.0f,                        // Deviation From The True Outlines
                        0.2f,                        // Font Thickness In The Z Direction
                        WGL_FONT_POLYGONS,           // Use Polygons, Not Lines
                        gmf);                        // Address Of Buffer To Recieve Data
}

 

The above code create the fonts and display list.

GLvoid glPrint(const char *fmt, ...)              // Custom GL "Print" Routine
{
    float        length=0;                        // Used To Find The Length Of The Text
    char        text[256];                        // Holds Our String
    va_list        ap;                            // Pointer To List Of Arguments
 
    if (fmt == NULL)                              // If There's No Text
        return;                                   // Do Nothing
 
    va_start(ap, fmt);                            // Parses The String For Variables
        vsprintf(text, fmt, ap);                  // And Converts Symbols To Actual Numbers
    va_end(ap);                                   // Results Are Stored In Text
 
    for (unsigned int loop=0;loop<(strlen(text));loop++) // Loop To Find Text Length
    {
        length+=gmf[text[loop]].gmfCellIncX;   // Increase Length By Each Characters Width
    }
 
    glTranslatef(-length/2,0.0f,0.0f);             // Center Our Text On The Screen
 
    glPushAttrib(GL_LIST_BIT);                     // Pushes The Display List Bits
    glListBase(base);                              // Sets The Base Character to 0
    glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);    // Draws The Display List Text
    glPopAttrib();                                        // Pops The Display List Bits
}

The above code used to render these characters on the screen. The way you call glPrint is almost the same as the way you call the printf, aren't you?

Well, you may want to engage into those staff further, like how the beneath function works? Any other optional solutions? Or some ideas or thoughts? And all those staff are depend on you. You could think them as some APIs. You go along with those API this ways, everything that you expected works. That is enough! There is no endless in the tech area, do not be to crazy to master everything. Keep the life and coding relax and easy.

 

The full source code could be downloaded from here.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/23/2653347.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值