Text in OpenGL
An Introduction
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Getting Started
- 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
- 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.)
An Example
openglexamples/text/bitmapfontGLUT.c
(Fragment: drawString)
Working in Screen Coordinates
- 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.)
Handling Reshape Events
openglexamples/text/labelsGLUT.c
(Fragment: reshape)
Working in Screen Coordinates (cont.)
The Overall Process
openglexamples/text/labelsGLUT.c
(Fragment: paint)
Working in Screen Coordinates (cont.)
Before Rendering Text
openglexamples/text/labelsGLUT.c
(Fragment: beginText)
Working in Screen Coordinates (cont.)
After Rendering Text
openglexamples/text/labelsGLUT.c
(Fragment: endText)
GLUT Vector/Stroke Fonts
- 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.)
An Example
openglexamples/text/vectorfontGLUT.c
(Fragment: drawString)
Working in OpenGL
- When All Else Fails:
- It is possible to use bitmaps to render text in OpenGL
- To Improve Performance:
Working in OpenGL (cont.)
An Example from the "Red Book"
openglexamples/text/font.c
There's Always More to Learn