Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a decent OpenGL text drawing library for the iPhone SDK?

Tags:

I'm trying to figure out a simple to draw some text in OpenGL. My research has shown its a fairly complex task. It involves creating (or generating at runtime) a font atlas texture, and then creating a quad for each letter with just the right placement and texture coordinates.

I've heard some good things about freetype, but have found very little about how to get it running on the iPhone, and it appears to be pretty complex.

So is there any Objective-C wrapped OpenGL text library that works with the iPhone SDK that people have been using? Or am I just over thinking this and there is an easier approach that I am missing?

like image 588
Alex Wayne Avatar asked Feb 04 '09 17:02

Alex Wayne


2 Answers

One way to do this is by setting up a UILabel and then rendering its layer into a texture. An indirect route to this would be to first set up the UILabel with text, font, etc. and then use the following code

UIGraphicsBeginImageContext(yourLabel.frame.size); [yourLabel.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

to capture the UILabel to a UIImage. You could then use the initWithImage: constructor of the Texture2D class in the CrashLanding project to transform this to a texture.

It looks like initWithImage: in that example re-renders the UIImage to a bitmap context and uses that to create the texture. With a little work, you could extract the relevant code from that method and alter the above code to render your layer directly into the texture.

This way, you get the nice Unicode and font support of UILabel when creating the text for your texture.

like image 129
Brad Larson Avatar answered Sep 21 '22 06:09

Brad Larson


Your description of using freetype to create a texture atlas along with all the glyph information is exactly what I'm doing in my current iPhone project.

I do all the parsing of the font in a pre-process step (All in C# on Windows as it happens). The atlas itself gets generated and contains only the characters needed - to cut down on space, I also sometimes end up generating more than a single atlas to keep textures sizes pow2 and of a sensible resolution.

You can easily pull out the glyph information from freetype. Pass that along to your app along with the textures at runtime and then it's fairly trivial to just alter the UV coords of the texture as required.

For what it's worth, this is how I define a glyph character inside my little engine:

struct FontGlyph
{

u32 char_code;

u32 m_x;                // base x position on tpage

u32 m_y;            // base y position on tpage

u32 m_width;            // glyph width

u32 m_height;           // glyph height

i32 m_horiBearingX;     // Distance to X shift

i32 m_horiBearingY;     // Distance to Y shift

u32 m_horiAdvance;      // Total Horizontal advance this character requires

u32 m__tpage_index;     // Which tpage does this glyph use?

};

I then have a 'SpriteString' object, which behaves pretty much like any other string, apart from I can draw it using sprites....

I agree this all might seem like a big job for a fairly trivial problem, but I've found it's given me plenty of flexibility and really didn't take that long to implement when I got down to it.

Feel free to reply or comment if you need any other details. (And good luck :)


Reply to your comment as I ran out of room in the comment section...

Check out the freetype documentation, it basically gives you the glyph data without messing about, it just pulls it right out of the font. As for the textures themselves, you get freetype to render each glyph, and then manage that to build the texture. This I do as a pre-process entirely separate from my main app.

Here's an excerpt of my tool code that does this: http://pastebin.com/md1c6354

Please note though, this was only ever intended for my eyes and not for public consumption, so excuse the fact it's messy as hell :)

like image 33
Ali Parr Avatar answered Sep 18 '22 06:09

Ali Parr