- Forward


Text in OpenGL
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Getting Started
Back SMYC Forward
  • OpenGL does not provide support for fonts, and hence does not provide support for "text"
  • OpenGL does provide support for bitmaps and this can be used for "text"
  • GLUT (and other windowing systems) do provide support for text
GLUT Bitmap Fonts
Back SMYC Forward
  • Fonts:
    • GLUT_BITMAP_8_BY_13
    • GLUT_BITMAP_9_BY_15
    • GLUT_BITMAP_TIMES_ROMAN_10
    • GLUT_BITMAP_TIMES_ROMAN_24
    • GLUT_BITMAP_HELVETICA_10
    • GLUT_BITMAP_HELVETICA_12
    • GLUT_BITMAP_HELVETICA_18
    • These are all void*
  • The Sizes:
    • The number in the name of proportionally spaced fonts indicates the height of the font
    • The numbers in the name of monospaced fonts indicate the width and height
GLUT Bitmap Fonts (cont.)
Back SMYC Forward

An Example

openglexamples/text/bitmapfontGLUT.c (Fragment: drawString)
 
Working in Screen Coordinates
Back SMYC Forward
  • The Issue:
    • Text is often used to put "labels" on the window
    • Hence, we often want to work in screen coordinates
  • One Approach:
    • Render text using a 2D orthographic projection based on the screen size
Working in Screen Coordinates (cont.)
Back SMYC Forward

Handling Reshape Events

openglexamples/text/labelsGLUT.c (Fragment: reshape)
 
Working in Screen Coordinates (cont.)
Back SMYC Forward

The Overall Process

openglexamples/text/labelsGLUT.c (Fragment: paint)
 
Working in Screen Coordinates (cont.)
Back SMYC Forward

Before Rendering Text

openglexamples/text/labelsGLUT.c (Fragment: beginText)
 
Working in Screen Coordinates (cont.)
Back SMYC Forward

After Rendering Text

openglexamples/text/labelsGLUT.c (Fragment: endText)
 
GLUT Vector/Stroke Fonts
Back SMYC Forward
  • Fonts:
    • GLUT_STROKE_ROMAN
    • GLUT_STROKE_MONO_ROMAN
    • These are both void*
  • Functions:
    • void glutStrokeCharacter(void* font, char* c)
    • int glutStrokeWidth(void* font, char* c)
GLUT Vector/Stroke Fonts (cont.)
Back SMYC Forward

An Example

openglexamples/text/vectorfontGLUT.c (Fragment: drawString)
 
Working in OpenGL
Back SMYC Forward
  • When All Else Fails:
    • It is possible to use bitmaps to render text in OpenGL
  • To Improve Performance:
    • Use display lists
Working in OpenGL (cont.)
Back SMYC Forward

An Example from the "Red Book"

openglexamples/text/font.c
 
There's Always More to Learn
Back -