Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw text with GLUT / OpenGL in C++?

Tags:

How do I draw a text string onto the screen using GLUT / OpenGL drawing functions?

like image 617
KingNestor Avatar asked Feb 11 '09 20:02

KingNestor


People also ask

How do I draw text in OpenGL?

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.

What is glut CPP?

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.


2 Answers

There are two ways to draw strings with GLUT

glutStrokeString will draw text in 3D

alt text
(source: uwa.edu.au)

and glutBitmapString will draw text facing the user

alt text
(source: sourceforge.net)

like image 105
epatel Avatar answered Sep 24 '22 09:09

epatel


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)); 
like image 22
Andrew Grant Avatar answered Sep 26 '22 09:09

Andrew Grant