How to Draw Text Using Only Opengl Methods

Draw text in OpenGL ES

The Android SDK doesn't come with any easy way to draw text on OpenGL views. Leaving you with the following options.

  1. Place a TextView over your SurfaceView. This is slow and bad, but the most direct approach.
  2. Render common strings to textures, and simply draw those textures. This is by far the simplest and fastest, but the least flexible.
  3. Roll-your-own text rendering code based on a sprite. Probably second best choice if 2 isn't an option. A good way to get your feet wet but note that while it seems simple (and basic features are), it get's harder and more challenging as you add more features (texture-alignment, dealing with line-breaks, variable-width fonts etc.) - if you take this route, make it as simple as you can get away with!
  4. Use an off-the-shelf/open-source library. There are a few around if you hunt on Google, the tricky bit is getting them integrated and running. But at least, once you do that, you'll have all the flexibility and maturity they provide.

How do I draw text with GLUT / OpenGL in C++?

There are two ways to draw strings with GLUT

glutStrokeString will draw text in 3D

alt text
(source: uwa.edu.au)

and glutBitmapString will draw text facing the user

alt text
(source: sourceforge.net)

OpenGL, Is this the best way to draw text?

that change every millisecond

First of all, that would require a display update frequency of 1000Hz to be even representable. No display in the world is that fast. The fastest displays around manage some 200Hz. So don't even think about updating your timer display at that rate.

An important rule about displays is: Don't display more digits than you can actually accurately measure. The accuracy of display updates is in the order of 1/10s of a second, so you should display only that much.

If you want to store a screenshot of a game, then the game update tick interval determines the accuracy. Usually no more than 100Hz, i.e. 1/100 of a second.

Now since you don't want to upload a new string texture every frame, the common way to go about this are glyph texture atlases, i.e. a texture with every glyph you want to render in it, from which you then draw quads addresses the respective characters by their texture coordinate. To save on texture memory you can use some method like Distance Fields (implemented for example in libGDX).

Also I recommend reading this SO answer on OpenGL text rendering methods

How to draw a text (write on the screen) using standard OpenGL functions?

You can't do it with standard OpenGL functions, unless you basically have textures with text in them, or a list of characters on a texture that you draw from. But don't reinvent the wheel - I would recommend FTGL for rendering text in an OpenGL view. It has several different rendering methods and takes care of things like kerning for you, supports unicode, and has good text metrics features too.



Related Topics



Leave a reply



Submit