This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Texture Font Demo
  Submitted by

This is a small font demo i made thats been tested and works great. There are one small struct and two methods to be implemented in your code before it runs. Info is written in the cpp file.

It can load a font texture like the q3a font submitted with this code and render any string with arguments. It also supports shading between different colors on the text quad to make it look nicer. Any font texture can be used as long as it looks like the font.tga i sent with this code.

Happy coding

/ Mace

Currently browsing [] (11,109 bytes) - [fontCode.cpp] - (3,285 bytes)


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: 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; }


glDisable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND);

// Reset coordinates glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.