Changing size of text

Started by
2 comments, last by pi_equals_3 19 years, 9 months ago
Hello everyone, I'm new here! I'm developing a GUI using OpenGL atm and I'd like to change the size of my text on the fly. I've been using the text-routines in the NeHe tutorials, and the only way I've found to change textsize is to rebuild the font each time, which is very slow. What do you guys use to draw text? Cheers everyone
Advertisement
The only way to truly change the size of the text n the metrics data is to rebuild the font.

There is, however, a way to make the text look smaller or larger.

I've never seen any documentation on a recommended way, but I always use glScalef to print text at a different size. For instance...

glScalef(0.5f, 0.5f, 0.5f);
font.printtext("Hello");

would print the text at one half the size of the original font.
If you're in 2D, the third parameter isn't needed.
I don't know about any side effects to this method, but it's never failed me.
you know, i had been wondering the same thing, how i could resize my font..

this works great!! theres only one problem, when i glScale(), say to .5f, then i have to draw the text at twice the coordinates i was planning on. for example

(string, x position, y position)

//draws "this is a test" at the screen coordinates (50,50)
draw_text("this is a test",50,50);



glPushMatrix();
glScalef(0.5f,0.5f,0.0);
draw_text("this is a test, 50,50);
glPopMatrix();

now this second example, will draw "this is a test" with the letters half the size, but they will be drawn at screen coordinates (25,25) !!

so really, i have to multiply my coordinates by ( 1 / the same numbers i put into the call to glScalef ), if i want it to look proper. anyway around this? its not a big deal, though. thanks for the help!

ps - im using 2D Ortho mode, with the ratio's specifying that a unit is equal to one pixel, which is why i say screen coordinates...
FTA, my 2D futuristic action MMORPG
Quote:Original post by graveyard filla
you know, i had been wondering the same thing, how i could resize my font..

this works great!! theres only one problem, when i glScale(), say to .5f, then i have to draw the text at twice the coordinates i was planning on. for example

(string, x position, y position)

//draws "this is a test" at the screen coordinates (50,50)
draw_text("this is a test",50,50);



glPushMatrix();
glScalef(0.5f,0.5f,0.0);
draw_text("this is a test, 50,50);
glPopMatrix();

now this second example, will draw "this is a test" with the letters half the size, but they will be drawn at screen coordinates (25,25) !!

so really, i have to multiply my coordinates by ( 1 / the same numbers i put into the call to glScalef ), if i want it to look proper. anyway around this? its not a big deal, though. thanks for the help!

ps - im using 2D Ortho mode, with the ratio's specifying that a unit is equal to one pixel, which is why i say screen coordinates...

Well, there are plenty of options to do this, so I'll list all the ones that come to mind, just to give you some ideas. I have no idea how you'll be using this exactly, so hopefully you can come up with a solution based on these ideas.

1) You can just multiply the font position by an inverse of the scale factor, as you said. Probably the easiest way to fix the problem, but it's really the kind of thing that should be incorporated into the actual printing function.

2) You can manually move to the postion where you want the text drawn, manually scale the text to the rght size, and call the printing function with 0 as both the x and y positions. I would not recommend this except for a temporary solution, as it involves the most work.

3) You can add an optional parameter to the printing function for the scale of the font, which could default to 1.0 for backwards compatibility. Inside the font printing function, you could translate to the appropriate place like usual, then insert the glScale* function call, using the value passed to the function, and finally, draw the text. Now the function moves to the correct position before the call to glScale*, avoiding any side effects.

This method seems the best to me. Once you've got it set, you can still use the function the same way, and scaling the font only requires one extra parameter. Also, if you ever decide to support center or right allignment, you don't have to worry about more side effects.

I hope I've helped.

This topic is closed to new replies.

Advertisement