How write Text in OpenGL

I use Visual C++ 6.0 and I want to know how to write texts in OpenGL. I want to use ttf fonts (Times New Roman, Verdana, …)

Use either wglUseFontBitMaps or wglUseFontOutlines both of which can be found at http://msdn.microsoft.com .

There is a really good tutorial over at NeHe’s that you should check out. It lets you use the fonts that you are talking about. There are tutorials on outline fonts too (the 3d kind!!!).

  • Halcyon

if your window is not double-buffered you can use TextOut

i will post the source code of an function to draw text in opengl.place this in an header file.

#define FIRST 32
#define LAST 126

static GLubyte bitmapFont[][1+13] = {
{
32,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
},
{
33,
0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18
},
{
34,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x36, 0x36, 0x36, 0x36
},
{
35,
0x00, 0x00, 0x00, 0x66, 0x66, 0xff, 0x66,
0x66, 0xff, 0x66, 0x66, 0x00, 0x00
},
{
36,
0x00, 0x00, 0x18, 0x7e, 0xff, 0x1b, 0x1f,
0x7e, 0xf8, 0xd8, 0xff, 0x7e, 0x18
},
{
37,
0x00, 0x00, 0x0e, 0x1b, 0xdb, 0x6e, 0x30,
0x18, 0x0c, 0x76, 0xdb, 0xd8, 0x70
},
{
38,
0x00, 0x00, 0x7f, 0xc6, 0xcf, 0xd8, 0x70,
0x70, 0xd8, 0xcc, 0xcc, 0x6c, 0x38
},
{
39,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x1c, 0x0c, 0x0e
},
{
40,
0x00, 0x00, 0x0c, 0x18, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x18, 0x0c
},
{
41,
0x00, 0x00, 0x30, 0x18, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x0c, 0x0c, 0x18, 0x30
},
{
42,
0x00, 0x00, 0x00, 0x00, 0x99, 0x5a, 0x3c,
0xff, 0x3c, 0x5a, 0x99, 0x00, 0x00
},
{
43,
0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0xff,
0xff, 0x18, 0x18, 0x18, 0x00, 0x00
},
{
44,
0x00, 0x00, 0x30, 0x18, 0x1c, 0x1c, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
},
{
45,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00
},
{
46,
0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
},
{
47,
0x00, 0x60, 0x60, 0x30, 0x30, 0x18, 0x18,
0x0c, 0x0c, 0x06, 0x06, 0x03, 0x03
},
{
48,
0x00, 0x00, 0x3c, 0x66, 0xc3, 0xe3, 0xf3,
0xdb, 0xcf, 0xc7, 0xc3, 0x66, 0x3c
},
{
49,
0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x78, 0x38, 0x18
},
{
50,
0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30,
0x18, 0x0c, 0x06, 0x03, 0xe7, 0x7e
},
{
51,
0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07,
0x7e, 0x07, 0x03, 0x03, 0xe7, 0x7e
},
{
52,
0x00, 0x00, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0xff, 0xcc, 0x6c, 0x3c, 0x1c, 0x0c
},
{
53,
0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07,
0xfe, 0xc0, 0xc0, 0xc0, 0xc0, 0xff
},
{
54,
0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc7,
0xfe, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e
},
{
55,
0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x18,
0x0c, 0x06, 0x03, 0x03, 0x03, 0xff
},
{
56,
0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xe7,
0x7e, 0xe7, 0xc3, 0xc3, 0xe7, 0x7e
},
{
57,
0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x03,
0x7f, 0xe7, 0xc3, 0xc3, 0xe7, 0x7e
},
{
58,
0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00,
0x38, 0x38, 0x00, 0x00, 0x00, 0x00
},
{
59,
0x00, 0x00, 0x30, 0x18, 0x1c, 0x1c, 0x00,
0x00, 0x1c, 0x1c, 0x00, 0x00, 0x00
},
{
60,
0x00, 0x00, 0x06, 0x0c, 0x18, 0x30, 0x60,
0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06
},
{
61,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00,
0xff, 0xff, 0x00, 0x00, 0x00, 0x00
},
{
62,
0x00, 0x00, 0x60, 0x30, 0x18, 0x0c, 0x06,
0x03, 0x06, 0x0c, 0x18, 0x30, 0x60
},
{
63,
0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x18,
0x0c, 0x06, 0x03, 0xc3, 0xc3, 0x7e
},
{
64,
0x00, 0x00, 0x3f, 0x60, 0xcf, 0xdb, 0xd3,
0xdd, 0xc3, 0x7e, 0x00, 0x00, 0x00
},
{
65,
0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff,
0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18
},
{
66,
0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7,
0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe
},
{
67,
0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0,
0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e
},
{
68,
0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3,
0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc
},
{
69,
0x00,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0x3c
},
{
82,
0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0,
0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe
},
{
83,
0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07,
0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e
},
{
84,
0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0xff
},
{
85,
0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3,
0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3
},
{
86,
0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66,
0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3
},
{
87,
0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb,
0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3
},
{
88,
0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c,
0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3
},
{
89,
0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3
},
{
90,
0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30,
0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff
},
{
91,
0x00, 0x00, 0x3c, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x3c
},
{
92,
0x00, 0x03, 0x03, 0x06, 0x06, 0x0c, 0x0c,
0x18, 0x18, 0x30, 0x30, 0x60, 0x60
},
{
93,
0x00, 0x00, 0x3c, 0x0c, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x3c
},
{
94,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xc3, 0x66, 0x3c, 0x18
},
{
95,
0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
},
{
96,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x38, 0x30, 0x70
},
{
97,
0x00, 0x00, 0x7f, 0xc3, 0xc3, 0x7f, 0x03,
0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00
},
{
98,
0x00, 0x00, 0xfe, 0xc3, 0xc3, 0xc3, 0xc3,
0xfe, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0
},
{
99,
0x00, 0x00, 0x7e, 0xc3, 0xc0, 0xc0, 0xc0,
0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00
},
{
100,
0x00, 0x00, 0x7f, 0xc3, 0xc3, 0xc3, 0xc3,
0x7f, 0x03, 0x03, 0x03, 0x03, 0x03
},
{
101,
0x00, 0x00, 0x7f, 0xc0, 0xc0, 0xfe, 0xc3,
0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00
},
{
102,
0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30,
0xfc, 0x30, 0x30, 0x30, 0x33, 0x1e
},
{
103,
0x7e, 0xc3, 0x03, 0x03, 0x7f, 0xc3, 0xc3,
0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00
},
{
104,
0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3,
0xc3, 0xfe, 0xc0, 0xc0, 0xc0, 0xc0
},
{
105,
0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x00, 0x00, 0x18, 0x00
},
{
106,
0x38, 0x6c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x00
},
{
107,
0x00, 0x00, 0xc6, 0xcc, 0xf8, 0xf0, 0xd8,
0xcc, 0xc6, 0xc0, 0xc0, 0xc0, 0xc0
},
{
108,
0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x78
},
{
109,
0x00, 0x00, 0xdb, 0xdb, 0xdb, 0xdb, 0xdb,
0xdb, 0xfe, 0x00, 0x00, 0x00, 0x00
},
{
110,
0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
0xc6, 0xfc, 0x00, 0x00, 0x00, 0x00
},
{
111,
0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6,
0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00
},
{
112,
0xc0, 0xc0, 0xc0, 0xfe, 0xc3, 0xc3, 0xc3,
0xc3, 0xfe, 0x00, 0x00, 0x00, 0x00
},
{
113,
0x03, 0x03, 0x03, 0x7f, 0xc3, 0xc3, 0xc3,
0xc3, 0x7f, 0x00, 0x00, 0x00, 0x00
},
{
114,
0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
0xe0, 0xfe, 0x00, 0x00, 0x00, 0x00
},
{
115,
0x00, 0x00, 0xfe, 0x03, 0x03, 0x7e, 0xc0,
0xc0, 0x7f, 0x00, 0x00, 0x00, 0x00
},
{
116,
0x00, 0x00, 0x1c, 0x36, 0x30, 0x30, 0x30,
0x30, 0xfc, 0x30, 0x30, 0x30, 0x00
},
{
117,
0x00, 0x00, 0x7e, 0xc6, 0xc6, 0xc6, 0xc6,
0xc6, 0xc6, 0x00, 0x00, 0x00, 0x00
},
{
118,
0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66,
0xc3, 0xc3, 0x00, 0x00, 0x00, 0x00
},
{
119,
0x00, 0x00, 0xc3, 0xe7, 0xff, 0xdb, 0xc3,
0xc3, 0xc3, 0x00, 0x00, 0x00, 0x00
},
{
120,
0x00, 0x00, 0xc3, 0x66, 0x3c, 0x18, 0x3c,
0x66, 0xc3, 0x00, 0x00, 0x00, 0x00
},
{
121,
0xc0, 0x60, 0x60, 0x30, 0x18, 0x3c, 0x66,
0x66, 0xc3, 0x00, 0x00, 0x00, 0x00
},
{
122,
0x00, 0x00, 0xff, 0x60, 0x30, 0x18, 0x0c,
0x06, 0xff, 0x00, 0x00, 0x00, 0x00
},
{
123,
0x00, 0x00, 0x0f, 0x18, 0x18, 0x18, 0x38,
0xf0, 0x38, 0x18, 0x18, 0x18, 0x0f
},
{
124,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18
},
{
125,
0x00, 0x00, 0xf0, 0x18, 0x18, 0x18, 0x1c,
0x0f, 0x1c, 0x18, 0x18, 0x18, 0xf0
},
{
126,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
0x8f, 0xf1, 0x60, 0x00, 0x00, 0x00
}
};

int fontBitmapString(char *str)
{
int len,i;
GLint swapbytes, lsbfirst, rowlength;
GLint skiprows, skippixels, alignment;

len=(int)strlen(str); 
glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes); 
glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst); 
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength); 
glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows); 
glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels); 
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); 
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE); 
glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); 
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); 
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); 
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

for(i=0;i < len;i++) 
{ 
    char    ch = str[i]; 

    if(ch==0)    /* end of string */ 
        break; 
    if(ch < FIRST) 
        continue; 
    if(ch > LAST) 
        continue; 
    ch -= FIRST; 
    glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, &bitmapFont[ch][1]); 
} 

glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes); 
glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst); 
glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength); 
glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows); 
glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels); 
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); 
return(1); 

}

void glPrint(int x, int y, char *string, GLfloat red, GLfloat green, GLfloat blue)
{
char buffer[256];
va_list args; ;
GLboolean texturing;

va_start(args, string); 
vsprintf(buffer, string, args); 
va_end(args); 

glPushMatrix(); 
glLoadIdentity(); 
glDisable(GL_TEXTURE_2D); 

glPushAttrib(GL_ALL_ATTRIB_BITS); 
glGetFloatv(GL_CURRENT_COLOR, color); 
glGetBooleanv(GL_TEXTURE_2D, &texturing); 
glGetFloatv(GL_CURRENT_RASTER_POSITION, position); 

glColor3f(red,green,blue); 
glRasterPos2i(x, y); 

fontBitmapString(buffer); 

glPopAttrib(); 
glColor4fv(color); 

glEnable(GL_TEXTURE_2D); 
glPopMatrix(); 

}

now u can use the glPrint function to draw text on the screen.

example glPrint(300,300,“This text is drawn at position 300/300 in red color”,1.0f,0.0f,0.0f);

the first 2 parameters are the position in x and y on the screen, then the text and the color.

hi,
u can use GlRasterPos(), to get a position wher e u want the text to be displayed and glbyte() something to print text.

I think you can just let wgl… to that for you. I certainly would not want to define the stipple pattern for each and every font i want to use.

  • Halcyon