11

I'm attempting to draw text to the screen using GLUT in 2d.

I want to use glutBitmapString(), can someone show me a simple example of what you have to do to setup and properly use this method in C++ so I can draw an arbitrary string at an (X,Y) position?

glutBitmapString(void *font, const unsigned char *string); 

I'm using linux, and I know I need to create a Font object, although I'm not sure exactly how and I can supply it with the string as the second arguement. However, how do I also specify the x/y position?

A quick example of this would help me greatly. If you can show me from creating the font, to calling the method that would be best.

KingNestor
  • 65,976
  • 51
  • 121
  • 152

2 Answers2

14

You have to use glRasterPos to set the raster position before calling glutBitmapString(). Note that each call to glutBitmapString() advances the raster position, so several consecutive calls will print out the strings one after another. You can also set the text color by using glColor(). The set of available fonts are listed here.

// Draw blue text at screen coordinates (100, 120), where (0, 0) is the top-left of the
// screen in an 18-point Helvetica font
glRasterPos2i(100, 120);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 2
    Thanks for the help adam. Also, for a long time it kept telling me glutBitmapString was not defined, and I eventually found it named as "_glutBitmapString" in GL/glui.h. Any idea why? – KingNestor Feb 13 '09 at 00:19
  • `glutBitmapString` is an extension implemented on `freeglut`, no present in the old `glut`, `GL/freeglut.h` must be included instead of `GL/glut.h` – Alex Sep 17 '14 at 14:22
0

Adding to Adam's answer,

glColor4f(0.0f, 0.0f, 1.0f, 1.0f);  //RGBA values of text color
glRasterPos2i(100, 120);            //Top left corner of text
const unsigned char* t = reinterpret_cast<const unsigned char *>("text to render");
// Since 2nd argument of glutBitmapString must be const unsigned char*
glutBitmapString(GLUT_BITMAP_HELVETICA_18,t);

Check out https://www.opengl.org/resources/libraries/glut/spec3/node76.html for more font options