Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to print text to screen in OpenGL?

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

1 - Menu
2 - Pause
3 - Exit
like image 464
patrick Avatar asked Feb 02 '10 10:02

patrick


People also ask

How does text rendering work?

The process of transforming font outlines into pixels is called rasterization. The operating system's text-rendering engine places the outline (ie the shape) of each character at the desired font size on a pixel grid. Next, it colours all the pixels whose centre is inside the outline (see image below).


2 Answers

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
like image 65
Kornel Kisielewicz Avatar answered Sep 18 '22 12:09

Kornel Kisielewicz


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

like image 26
frachop Avatar answered Sep 21 '22 12:09

frachop