/*
TEXTURE FONT DEMO By Mace
-------------------------
IMPLEMENTATION:
1) Copy the font struct and the methods to your program
2) Call initFont in your init() method to init the font
3) Load any font texture like the one sent with this demo (Q3A Font)
thats a 24bit TGA file with the transparency color set to (R:255 G:0 B:255).
Now load it as a RGBA texture and then bind your font texture in glPrint()
by replacing glBindTexture(GL_TEXTURE_2D, tLib[0]) to your own font texture.
4) All done. Add this line in your paint method to try it out:
glPrint(12, 0, 0, "Font split: %f", FONT.split);
Note: You can easily change the color of the text by changing the
FONT.C[corner][r,g,b] array. Good luck.
Any comments are welcome at:
E-Mail: mace2@home.se
ICQ: 18822820
*/
// FONT
struct {
GLfloat split; // Width of one letter in a row (1.0f/16)
GLfloat T[16*16][2]; // Font texture coords [char][U,V]
GLfloat C[4][3]; // Font Quad colors [Corner][r,g,b]
} FONT;
// INIT FONT AND GET THE TEXTURE COORDS FOR EACH CHAR
void initFont() {
GLint w, h, i = 0;
FONT.split = 1.0f / 16.0f;
for(h = 0; h < 16; h++) {
for(w = 0; w < 16; w++) {
FONT.T[i][0] = FONT.split*w;
FONT.T[i][1] = FONT.split*(15-h);
i++;
}
}
for(i = 0; i < 4; i++) {
FONT.C[i][0] = 1;
FONT.C[i][1] = 1;
FONT.C[i][2] = 1;
}
}
// CALL THIS TO RENDER THE TEXT
void glPrint(GLint fontSize, GLfloat x, GLfloat y, char *str, ...) {
GLint fontChar = 0;
GLfloat fontSize2 = fontSize/2.0f;
// Combine the args with the string
va_list args;
char buffer[512], *c;
va_start(args, str);
vsprintf(buffer, str, args);
va_end(args);
c = buffer;
x += fontSize2;
y = (winH - fontSize2) - y;
// Change coordinates
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, winW, 0, winH, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, tLib[0]); // tLib[0] is the id of the font texture
glPushMatrix();
glTranslatef(x, y, 0);
// Actual rendering
i = 0;
while(*c) {
fontChar = (int)*c;
glBegin(GL_QUADS);
glColor3f(FONT.C[3][0], FONT.C[3][1], FONT.C[3][2]);
glTexCoord2f(FONT.T[fontChar][0], FONT.T[fontChar][1]);
glVertex3f(-fontSize2, -fontSize2, 0);
glColor3f(FONT.C[2][0], FONT.C[2][1], FONT.C[2][2]);
glTexCoord2f(FONT.T[fontChar][0]+FONT.split, FONT.T[fontChar][1]);
glVertex3f( fontSize2, -fontSize2, 0);
glColor3f(FONT.C[1][0], FONT.C[1][1], FONT.C[1][2]);
glTexCoord2f(FONT.T[fontChar][0]+FONT.split, FONT.T[fontChar][1]+FONT.split);
glVertex3f( fontSize2, fontSize2, 0);
glColor3f(FONT.C[0][0], FONT.C[0][1], FONT.C[0][2]);
glTexCoord2f(FONT.T[fontChar][0], FONT.T[fontChar][1]+FONT.split);
glVertex3f(-fontSize2, fontSize2, 0);
glEnd();
glTranslatef(fontSize, 0, 0);
i++;
*c++;
if(!*c) break;
}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
// Reset coordinates
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
} |