最简单的方法是从图像加载字体映射,例如由位图字体生成器生成的字体映射(我知道它是 windows,但我找不到适用于 linux 的字体映射),例如:
该示例是 256x256 的 gif,尽管您可以将其转换为 png/tga/bmp。它是完整的 ASCII 映射网格,16x16 个字符。加载纹理并使用 glTexCoord2f 将其排列在您的四边形上,您应该一切顺利。
这是使用上述位图的示例:
unsigned texture = 0;
void LoadTexture()
{
// load 24-bit bitmap texture
unsigned offset, width, height, size;
char *buffer;
FILE *file = fopen("text.bmp", "rb");
if (file == NULL)
return;
fseek(file, 10, SEEK_SET);
fread(&offset, 4, 1, file);
fseek(file, 18, SEEK_SET);
fread(&width, 1, 4, file);
fread(&height, 1, 4, file);
size = width * height * 3;
buffer = new char[size];
fseek(file, offset, SEEK_SET);
fread(buffer, 1, size, file);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
fclose(file);
printf("Loaded\n");
}
void DrawCharacter(char c)
{
int column = c % 16, row = c / 16;
float x, y, inc = 1.f / 16.f;
x = column * inc;
y = 1 - (row * inc) - inc;
glBegin(GL_QUADS);
glTexCoord2f( x, y); glVertex3f( 0.f, 0.f, 0.f);
glTexCoord2f( x, y + inc); glVertex3f( 0.f, 1.f, 0.f);
glTexCoord2f( x + inc, y + inc); glVertex3f( 1.f, 1.f, 0.f);
glTexCoord2f( x + inc, y); glVertex3f( 1.f, 0.f, 0.f);
glEnd();
}