GL Text Function!

Processing Tip: Rendering Large Amounts of Text… Fast!

I’ve been working on a project at the New York Times R&D Lab which involves rendering a fair amount of text to screen, which needs to be animated smoothly in real time. I’ve never had much luck working with large amounts of text in Processing, using the traditional approach. But I’ve found a nice way using an external library and a old trick, which allows for the rendering of large amounts of text at high framerates (the image above shows 1000+ text blocks running at ~50fps on my two-year-old MacBook).

How does it work?

Well, let’s first talk about how we’d typically approach this problem. Because they need to be animated independently, each of the text blocks is a member of a custom Class. Once per frame, we loop through all of the text objects, and ask them to render. In the old-school way, each of these text blocks would render using Processing’s text() method, and all of the shapes would be drawn to screen. While this works well with limited amounts of text, if we add more text objects to the sketch, things start to slow down at an alarming rate:

Here, I’ve barely added 200 objects, and the frame rate has crawled down to a measly 10fps. Clearly not good.

Blit it.

The first part of the solution is to use an old-school technique called ‘blitting’. Now, before all of you demo-scene kids get all worked up, I know this isn’t really blitting, but I like the sound of the word, and it’s my tutorial, so deal with it. Instead of drawing every letter of every text block every frame, what we’re going to do is this:

  1. Render the text once to an off-screen graphics object
  2. Transfer the pixels from this off-screen object to a PImage
  3. Render that PImage every frame

The core reason this works is that Processing is much better at rendering hundreds of images than it is at rendering hundreds of text objects. With this implemented, we can get many, many more objects onto our screen:

Here, we’ve got about 550 objects on the screen, at full framerate. But, at least on my Mac, this system seems to collapse after about 600 objects. So, how can we get up to the 1000 objects that I showed in the first image in this post?

Let’s push it a little.

The last step of this process owes its existence to Andres Colubri, who has built an excellent library calledGLGraphics which lets us do all kinds of things in Processing that get accelerated by the super-fancy graphics cards (GPUs) which most of us are lucky enough to have in our computers (Andres is also responsible for all of the tasty Processing for Android 3D stuff which I’ll be posting about later this week). In this case, the final step involves rendering all of the text blocks as GL Textures, instead of as PImages (note that this will give you a performance boost in any case where you are using images – yay!)

Around now you’re probably asking about the code. Well, here it is. I’ve tried to make it as simple as possible – just remember that you need the GLGraphics library installed.

GL Text Example (4kb)

This solution isn’t perfect – it doesn’t allow for perfect scaling, or dynamic effects within the text objects. But is does solve the basic problem, and also gives us a set of GLTextures which are ready to be used in whatever wacky 3D system we want to put them in. Let me know how it works for you.

 

 

Advertisements

Inspiration

  • Inspiration

• Make a game about a story you’ve written.
• Mix existing ideas from other games.
• Steal ideas (but not characters) from other media: Books, Movies, Comics, …
• Market Research: surveys, focus groups,
• Take a current idea and do it better:
• Better technology – graphics, sound, AI, …
• Better story
• Different environment
• Brainstorm, throw out lots of ideas

  • Iphone app games
I found an interesting game called ‘Plasma Globe”. It is available and can be played on iphone.”Plasma globe is fascinating to watch and even more fun to play with. The visual interaction masterpiece takes full advantages of the Retina display in order to bring beautiful and crisp graphics along with addictive lighting effects and sounds”I played this game several time and looked into the use of visual effects; such as the electrical lighting bolt effect, colour scheme and also the use of sound effects.
OpenProcessing
 I found that  “OpenProcessing ” website provides lots of good examples.Two good examples1) Gravity Swarmhttp://www.openprocessing.org/visuals/?visualID=2363

2) Gravity Well :

http://www.openprocessing.org/visuals/?visualID=2363

Typography
 I love typography. Inspired lots of typography examples in many advertisements and works that are available on the internet, I thought that using a variety of texts in a game will be interesting and new to people.