How do I draw a text string onto the screen using GLUT / OpenGL drawing functions?
You draw text in a double-buffered OpenGL window by creating display lists for selected characters in a font, and then executing the appropriate display list for each character you want to draw. The following code sample creates a rendering context, draws a red triangle, and then labels it with text.
The GLUT library has both C, C++ (same as C), FORTRAN, and Ada programming bindings. The GLUT source code distribution is portable to nearly all OpenGL implementations for the X Window System and Windows 95 and NT. GLUT also works well with Brian Paul's Mesa, a freely available implementation of the OpenGL API.
There are two ways to draw strings with GLUT
glutStrokeString will draw text in 3D
(source: uwa.edu.au)
and glutBitmapString will draw text facing the user
(source: sourceforge.net)
void RenderString(float x, float y, void *font, const char* string, RGB const& rgb) { char *c; glColor3f(rgb.r, rgb.g, rgb.b); glRasterPos2f(x, y); glutBitmapString(font, string); }
And you can call it like;
RenderString(0.0f, 0.0f, GLUT_BITMAP_TIMES_ROMAN_24, "Hello", RGB(1.0f, 0.0f, 0.0f));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With