9

I need to print 3 lines of text to a window as a menu.

1 - Menu
2 - Pause
3 - Exit
patrick
  • 1,022
  • 5
  • 17
  • 29
  • On top of 3D: http://stackoverflow.com/questions/18847109/displaying-fixed-location-2d-text-in-a-3d-opengl-world-using-glut Without any library besides OpenGL: http://stackoverflow.com/questions/8847899/opengl-how-to-draw-text-using-only-opengl-methods – Ciro Santilli OurBigBook.com Apr 12 '16 at 13:23

5 Answers5

15

Considering that you used GLUT in previous questions, the easiest would be using GLUT's built in font rendering functions.

Example:

void output(int x, int y, float r, float g, float b, int font, char *string)
{
  glColor3f( r, g, b );
  glRasterPos2f(x, y);
  int len, i;
  len = (int)strlen(string);
  for (i = 0; i < len; i++) {
    glutBitmapCharacter(font, string[i]);
  }
}

Where font is one of GLUT font constants:

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
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
4

Up this post because i found a really great tool to render high quality 2D text:

freetype-gl library

see sample rendering :

freetype-gl sample image

frachop
  • 191
  • 1
  • 7
2

This webpage outlines the three possible ways, and links to handy libraries that help.

There's no simple built-in way to render text, and there are (as detailed on that page) sevaral possible approaches, it's all a trade-off depending on what properties you require from your text rendering.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

A Windows-only but simple method is described here: Nehe Lesson 13

Basically, it uses these three functions:

  • wglUseFontBitmaps
  • glListBase
  • glCallLists
GhassanPL
  • 2,679
  • 5
  • 32
  • 40
1

This is a simple way to write text on the screen:

glColor3f (1.0, 0.0, 0.0);
        glRasterPos2f(-240, 120); //define position on the screen
        char *string = "Text";
      
      while(*string){
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *string++);
      }