Reputation: 39550
I'm using Linux and GLUT. I have a square as follows:
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
I guess I can't use glutBitmapCharacter
, since this is only ideal for 2D ortho.
Simple enough, I'd like to render "Hello world!" anywhere on said square. Should I create a texture and then apply it to the vertices using glTexCoord2f
?
Upvotes: 1
Views: 3865
Reputation: 608
The simplest way is to load a font map from a image, such as those generated by the bitmap font builder (I know it's windows but I can't find one for linux), eg:
The example is a 256x256 gif, though you may what to convert it to a png/tga/bmp. It's full ASCII mapped gird, 16x16 characters. Load the texture and use glTexCoord2f to line it up on your quad, and you should be good to go.
Here's an example using a bitmap of the above:
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();
}
Upvotes: 4