Sign in to follow this  
AntiGuy

Opengl font

Recommended Posts

Could someone recommend a good and FAST opengl font library. I've been busting my hump for the past 3 days with no luck. I finally got one to work called gltext but after drawing enough objects, performance really drops. When I used SDL_TTF and blit surfaces, there was barely a drop at all. I could handle over 20 text surfaces on the screen at once with no problem. Now that I've made the switch to OpenGL, I can't handle five. I really need help. I'm desperate [bawling]!!

Share this post


Link to post
Share on other sites
You can still use SDL_TTF and blit surfaces with OpenGL! You can use SDL_Surface* with any gl functions that require a GLvoid* pixels parameter. I wish I had some code to give you but it is at home. You can check out the SDL port of NeHe lesson 6 to see how to texture map with SDL_Surfaces - and build off that.

I don't know if this will work if you're not already using SDL to setup OpenGL. Hope this helps!

Share this post


Link to post
Share on other sites
Well it looks like a lot to work with for someone like me but I'll see what I can do. Thanks! But are you sure I can get my text blitting speed back?

EDIT: And as to this link, I got the first part but rendering was slow and I was completely baffled on the FontMGR library.

Share this post


Link to post
Share on other sites
Arggg!! I'm still lost!!! Am I supposed to convert the sdl surface to a texture surface then draw it ( That was slow the last time I've done it). Am I supposed to somehow get the text to go straight to a surface? Am I supposed to load the surface once and somehow draw the characters in parts (clueless as how to do that?). Any info at all would be much obliged.

Share this post


Link to post
Share on other sites
The simplest and fastes way is this. You could use SDL to generate the font bitmap though.

edit: this wasn't what I thought it was... So here some code I use. (be warned of the ugglynes ;)


void glPrint(GLint x, GLint y, int set, const char *fmt, ...) {
char text[1024];
va_list ap;
GLuint err;

if (fmt == NULL)
return;

va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);

if (set > 1)
set = 1;

glPushAttrib(GL_ALL_ATTRIB_BITS);

glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glAlphaFunc ( GL_GREATER, 0.1f ) ;
glEnable ( GL_ALPHA_TEST ) ;

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
/*you have to change this to match your settings*/
glOrtho(0, 1600, 0, 1200, -1, 1);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glTranslated(x,y,0);
glListBase(font_base-32+(128*set));
glScalef(1.0f,-1.0f,1.0f);

glBindTexture(GL_TEXTURE_2D, font_texture[0]);
glCallLists(strlen(text),GL_UNSIGNED_BYTE, text);

glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glMatrixMode(GL_PROJECTION);
}

void build_font() {
int loop;
float cx, cy;

printf("loading font...\n");

font_texture[0] = load_texture_tga(TEXTURE_FONT);
if (font_texture[0]) {
font_base = glGenLists(256);


for (loop = 0; loop < 256; loop++) {
cx = (float)(loop % 16)/16.0f;
cy = (float)(loop / 16)/16.0f;

glNewList(font_base+loop, GL_COMPILE);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glTexCoord2f(cx,cy+0.0625f);
glVertex2d(0,16);

glTexCoord2f(cx+0.0625f,cy+0.0625f);
glVertex2d(16,16);

glTexCoord2f(cx+0.0625f,cy+0.001f);
glVertex2d(16,0);

glTexCoord2f(cx,cy+0.001);
glVertex2d(0,0);
glEnd();
glTranslated(9,0,0);
glEndList();
}
} else {
printf("Error: font\n");
}

}




[Edited by - nefthy on June 2, 2005 10:41:24 AM]

Share this post


Link to post
Share on other sites
If you use a 2D engine like PTK (http://www.funpause.com/getptk.php) instead of raw OpenGL, you get TrueType support for free as well; fonts are rendered to textures which costs next to nothing in terms of framerate.

(PTK also has the benefit of rendering to DirectX _and_ OpenGL, for both
maximum PC and Mac compatibility, while giving you all the 2D effects like
rotations and alpha blending that you would do with raw OGL; you can
still issue OpenGL commands directly even while using PTK though)

Best regards,
Emmanuel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this