SAMPLE: Drawing Three-Dimensional Text in OpenGL Applications

Last reviewed: August 5, 1996
Article ID: Q131024
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT version 3.51
    

SUMMARY

GDI operations, such as TextOut, can be performed on an OpenGL window only if the window is single-buffered. The Windows NT implementation of OpenGL does not support GDI graphics in a double-buffered window. Therefore, you cannot use GDI functions to draw text in a double-buffered window, for example. To draw text in a double-buffered window, an application can use the wglUseFontBitmaps and wglUseFontOutlines functions to create display lists for characters in a font, and then draw the characters in the font with the glCallLists function.

The wglUseFontOutlines function is new to Windows NT 3.51 and can be used to draw 3-D characters of TrueType fonts. These characters can be rotated, scaled, transformed, and viewed like any other OpenGL 3-D image. This function is designed to work with TrueType fonts.

The GLFONT sample shows how to use the wglUseFontOutlines function to create display lists for characters in a TrueType font and how to draw, scale, and rotate the glyphs in the font by using glCallLists to draw the characters and other OpenGL functions to rotate and scale them. You need the Win32 SDK for Windows NT 3.51 to compile this sample, and you need to incorporate wglUseFontOutlines in your own application. You alos need Windows NT 3.51 to execute the application.

Download GLFONT.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download GLFONT.EXE (size: 29858 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
    

          Get GLFONT.EXE (size: 29858 bytes) 
    

MORE INFORMATION

To specify the TrueType font for which you want wglUseFontOutlines to create display lists, you must first create the desired logical font by using CreateFont or CreateFontIndirect. Then, you must select the HFONT created into a screen device context (HDC) with SelectObject, and send the HDC to the wglUseFontOutlines function. Each character is mapped in the x and y directions in the display lists. You need to specify the depth in the negative z direction in the extrusion parameter of wglUseFontOutlines.

You can also specify whether you want the 3-D glyphs to be created with line segments or polygons. To instruct wglUseFontOutlines to create the 3-D glyphs with lines segments, specify WGL_FONT_LINES in the format parameter. To create them with polygons, specify WGL_FONT_POLYGONS.

Here is an example showing how to create a set of display lists to draw the characters of the Arial TrueType font as a set of polygons:

     LOGFONT     lf;
     HFONT       hFont, hOldFont;
     GLYPHMETRICSFLOAT agmf[256];

     // An hDC and an hRC have already been created.
     wglMakeCurrent( hDC, hRC );

     // Let's create a TrueType font to display.
     memset(&lf,0,sizeof(LOGFONT));
     lf.lfHeight               =   -20 ;
     lf.lfWeight               =   FW_NORMAL ;
     lf.lfCharSet              =   ANSI_CHARSET ;
     lf.lfOutPrecision         =   OUT_DEFAULT_PRECIS ;
     lf.lfClipPrecision        =   CLIP_DEFAULT_PRECIS ;
     lf.lfQuality              =   DEFAULT_QUALITY ;
     lf.lfPitchAndFamily       =   FF_DONTCARE|DEFAULT_PITCH;
     lstrcpy (lf.lfFaceName, "Arial") ;

     hFont = CreateFontIndirect(&lf);
     hOldFont = SelectObject(hDC,hFont);

     // Create a set of display lists based on the TT font we selected
     if (!(wglUseFontOutlines(hDC, 0, 255, GLF_START_LIST, 0.0f, 0.15f,
        WGL_FONT_POLYGONS, agmf)))
          MessageBox(hWnd,"wglUseFontOutlines failed!","GLFont",MB_OK);

     DeleteObject(SelectObject(hDC,hOldFont));
     .  .  .  .
     .  .  .  .
     .  .  .  .
     .  .  .  .

To display these 3-D characters in a string, use the following code:

   // Display string with display lists created by wglUseFontOutlines()
   glListBase(GLF_START_LIST); // indicate start of display lists

   // Draw the characters
   glCallLists(6, GL_UNSIGNED_BYTE, "OpenGL");


Additional reference words: graphics
KBCategory: kbgraphic kbcode kbfile kbwebcontent
KBSubcategory: GdiOpenGL


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 5, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.