opengl text/font drawing from CGContext

Hi, I've just registered on this forum as it has provided a lot of help in recent weeks (thanks!) but now I have a problem I've not discovered a solution to...

I'm trying to write a font class that draws text to a CGContext then uses it as an opengl texture. The following code is based on a texture class I wrote which does a similar thing (loads a UIImage, draws it to a CGContext and then uses the context pixel data for an opengl texture, the texture is actually drawn elsewhere however)... anyway... it works... but this doesn't.

When the following method is called a rectangle is displayed onscreen but it is completely black and thus there's no text to be seen. Is this method of rendering text using opengl a daft idea? Any ideas or help on this would be really helpful.

I know there are a few naughty bits in the following code, which i plan on sorting out when I know this is going to work - for example: integers width and height are not determined correctly.

I've scrapped the above since I couldn't get it to work and opted for the more traditional texture-mapped bitmap font method. Just out of interest, is there no one who knows why the above didn't work? The above (if it worked) would give a bit more flexibility with drawing fonts than texture-mapped bitmap fonts, i.e. stroke and fill colours could be altered at runtime.

Yep, your quite right, it does. I've looked at the CrashLanding example in the past but I guess in my haste, overlooked that particular method. Anyway thanks!

However, it seems the example doesn't approach it in the same way. The resulting texture, uses an alpha-only colour space, which will not provide the above mentioned functionality (changing stroke and fill colours).

I'm interested in the performance differences in using this approach vs. the texture-mapped bitmap one that I ended up going for. I'll likely do some tests with this when I have the time (which I don't at the moment) but if anyone else has any info on this, it'd be cool to hear. Also, what about localization? In particular, using Chinese characters?

fretmunky Wrote:I'm interested in the performance differences in using this approach vs. the texture-mapped bitmap one that I ended up going for. I'll likely do some tests with this when I have the time (which I don't at the moment) but if anyone else has any info on this, it'd be cool to hear. Also, what about localization? In particular, using Chinese characters?

Performance will always be better when rendering one textured object vs. multiple textured objects to achieve the same effect (i.e. Apple's example in Texture2D will be faster than the equivalent bitmapped font approach, assuming you don't change the string). I wouldn't worry about it if you're just doing a few characters, but for larger strings, definitely shy away from bitmapped fonts -- unless you are changing the strings frequently. If you are changing the string frequently, like every second or less, it may be much better to do the bitmapped font approach, since there is some overhead in having to re-render the texture which may noticeably affect performance.

Typesetting and kerning is also a pain to take care of in a bitmapped font, if you need it, but is handled automatically in Texture2D.

AnotherJake Wrote:Performance will always be better when rendering one textured object vs. multiple textured objects to achieve the same effect (i.e. Apple's example in Texture2D will be faster than the equivalent bitmapped font approach, assuming you don't change the string). I wouldn't worry about it if you're just doing a few characters, but for larger strings, definitely shy away from bitmapped fonts -- unless you are changing the strings frequently. If you are changing the string frequently, like every second or less, it may be much better to do the bitmapped font approach, since there is some overhead in having to re-render the texture which may noticeably affect performance.

Typesetting and kerning is also a pain to take care of in a bitmapped font, if you need it, but is handled automatically in Texture2D.

Localization naturally works great with the Texture2D sample.

Thanks for the info. Considering what you've said, it seems a combination of the two approaches would be the most performance friendly. I.e. the context approach for strings which generally remain the same and the bitmapped approach for strings which are often changing (timers, counters, etc.).

Yes, unfortunately there is no one-size-fits-all solution to text rendering in OpenGL. Those are the two ends of the spectrum. There are all kinds of different ways to approach it.

On the context end you could replace Apple's Quartz with Freetype for a more cross-platform compatible solution instead. On Mac, OneSadCookie came up with an incredibly simple little way to render text from an NSView which is pretty neat too (don't have a link on-hand).

You could render bitmap fonts yourself in software into your own "context" and upload that as a single texture. I've done that too.

Then you can even split up multiple lines of text into what I call text "panels", consisting of either one line or more at a time. I've done that for some scrollable text.

Generally though, I either choose good-ol' bitmap fonts on quads (four point triangle strips nowadays) often for numbers which change frequently or for stuff I'd like some graphical effects on, done in Photoshop perhaps, or just Texture2D for convenience or nicer looking typeset text. Use the best technique for the situation.

Indeed. Cross platformability (lol) isn't really an issue for me at the moment as I'm working on iPhone stuff but I'll keep in mind using FreeType as opposed to Quartz for my own projects in the future.

Could you elaborate on those "text panels" for scrolling text? That could be VERY handy.

Yeah, I haven't used FreeType in a while myself because I haven't been interested in cross-platform lately. To be honest though, I don't know how to load a custom font using Quartz, but I know that is easy to do with FreeType, so there may or may not be an advantage to FreeType on that point -- I don't know.

On the text panels: It's too tricky to explain in detail, but roughly speaking I just render one or a few lines per panel as the panel "scrolls" into view. It's like one quad per each line of text instead of one quad for each character. That way I don't have the extra overhead of either rendering the entire text view every frame or rendering lots of quads (one for each character), plus typesetting looks pretty good. You have to have a way to either clip each panel to your subview, or you have to calc the proper size of the panel (just a quad) and its corresponding texture coordinates so it *appears* like it's being clipped as it moves onto/off of the view.

I imagine one could also do it by translating texture coordinates on only one texture and quad and only updating part of the texture as it scrolls into view. I'm going to try that next time I need it because I think that might be a better technique, although I haven't fully envisioned the implementation details.

OneSadCookie Wrote:Using custom fonts with Quartz is a matter of an Info.plist key pointing to the font, then it's available through the normal APIs in the normal way.

Awesome, I'm going to have to try that when I get a chance!

Quote:The big disadvantage to avoiding Quartz is if you want to support Arabic, Thai, or any other complicated languages.

That, and a minor disadvantage is that Apple has (or at least had) some patented font rendering stuff which wasn't available to use in FreeType, legally, without a license. I don't know if that situation has changed or not though, as I seem to recall at least one particular patent was expiring around this time.

[edit] Oh yeah, and you have to distribute FreeType with your application too, whereas Quartz is already on the machine.