Making Text

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I've pretty much found that the only way to do text on my screen for scoring and such is through textures. I could go through and make about 19 or 20 textures for the numbers and letters I need but I'd rather make one big texture.

1) If I were to do this, where can I get premade font textures (when I draw in Photoshop, the letters don't line up and I'm quite lazy about doing it)?
2) How do I code in OpenGL to take a part of the texture? Currently I'm using glTexCoord2f() with 1 and 0 being the only numbers I've ever used. How do I figure out the float values I'd need in this situation?
3) Is there an easier way out there?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
That method is used by NeHe is many of his tutorials. Check them out for examples of how to implement it.

Another method would be to use FTGL and FreeType. So far, this method has worked well for me.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Thanks I'll check that out.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
why on earth can't people search the forum before they ask questions?

Searching simply for "text opengl" turns up a number of helpful-looking threads!
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #5
My CFont class (wich is based on NeHe's tutorial) loads the characters from two files:
  1. One big texture with all the normal characters
  2. One text file containing the positions and options for each char


Now, how do I generate the texture & the text file? I made a little app wich
  1. takes a 24 or 32 bit (with an alpha channel) TGA image that has all the characters in one long strip
  2. automatically detects each character
  3. figures out the best size for the texture (power of 2, square)
  4. generates a texture file & a txt with all the positions.


This can then be loaded in my CFont class:
Code:
CFont myFont;
myFont.Load("prettyfont"); // This will load prettyfont.tga & prettyfont.txt
and printed:
Code:
myFont.Print(x,y,"Current FPS:%0.1f",fps); // By default, it prints to a virtual screen of 800x600, but has a method to change it

I can upload it to my site if anybody is interested.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
If you're just using system fonts, you can of course render all the characters into a texture at run time. The Untima source shows how to do this.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #7
PowerMacX Wrote:I can upload it to my site if anybody is interested.

I would be very interested in that PMX!
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #8
codemattic Wrote:I would be very interested in that PMX!

I have an exam in about an hour, after that, I'll make a small example project and upload it to my site (tomorrow or the day after).
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #9
great - thanks. there is no rush - exams come first and all that!
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #10
Finished my exam, relatively easy (IA - Expert Systems). I should probably make some simple Cocoa interface for my font-maker app, but at least for now I'll leave it as it is (100 lines of C, expects to find a file called font.tga on it's path, 24 or 32 bits, and generates TextureFont.tga & TextureFont.dat)

In the meantime, in my old site (http://www28.brinkster.com/ignacio3d/), you can download the code for vSpeed, a simple game I made for Vectorized (althrough I didn't finish it on time) wich uses the font loading class, CFont, and has an ugly font file included (ugly because it had to look "vectorized").

Warning: the CFont class in that game is not the latest version I have, I'll upload it along with the FontMaker app later.

Ignacio
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #11
OK, the FontMaker app is ready, I just made a short Cocoa wrapper for it (2 lines of code!) so you can drop a tga file on it and it will create the texture font files. I just need to make a sample OpenGL app to demo the CFont class and then I'll upload it to my site. (Tonight or tomorrow, depending on when I get home)
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #12
Well, I went way to far with this, as you can see in this screenshot:
[Image: cfontdemo01.jpg]

So basically, now you have two apps: FontMakerBasic, for making fonts, and CFontDemo to preview them.

I'm making this available 100% free, even for commercial apps, as long as you credit me in the readme/about box.

VERY IMPORTANT: If you are a contestant in uDG 2004, remember that I am too, so check the rules before using any of this code. Since I'm making this available to everyone, and not just to one contestant, I think that it's allowed by the rules but:

Carlos, if you are reading this, what do you think?


Download
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #13
very cool PMX!
Quote this message in a reply
Post Reply