views:

255

answers:

2

Dear All

Having little experience in Opengl es (or opengl) I was hoping somebody could help me out with this.

I'm creating an opengl es application and I would like to be able to dynamically create the text of my buttons etc.

The 2 obvious reasons for that are:

-to be modular (changing the text quickly without using photoshop)

-to be able to add languages later down the line

can anybody point me to a tutorial or at least give me the basic steps/functions to:

  • create textures form text

  • load custom fonts to display text textures with the chosen custom font

  • indicate if I can use this for loading Kanji ?

Best regards

Jason Rogers

+1  A: 

Hi buddy,

I don't know if this is the best way, and I haven't done it before... but here's how I'd do it:

  • Create a Paint object that will describe the appearance of the text.
  • Set the required parameters for font (Paint.setTypeFace()), size (Paint.setTextSize()), color, etc.
  • Measure the size of the required bitmap using Paint.getTextBounds() and the string you want to draw.
  • Create a new bitmap of that size using Bitmap.createBitmap(), make sure it has an alpha component.
  • Create a canvas using the bitmap you created to draw into using new Canvas(bitmap).
  • Draw the text into the bitmap: Canvas.drawText(), using the paint object you created and the string you want.
  • Scale the bitmap using Bitmap.createScaledBitmap() to be the next larger size as a power of 2 in x and y. I have a handy function (round up to power of 2) for that if you need it.
  • Bind, set texture parameters and load the bitmap to gl using GLUtils.texImage2D()
  • Draw a quad/sprite using the texture; scale appropriately using the measured text dimensions.

Of course, if you want to do the button background/button text compositing beforehand, you can use the canvas to draw into the background instead.

I presume that whatever fonts android supports, Canvas.drawText() will support using the appropriate Paint settings, the same should go for string characters (Kanji or other) that are input to it. It will be easy to find out by experimenting once you get the method above up and running.

Hope this was somewhat helpful. Cheers, Aert.

Aert
A: 

In our app we've decided to draw HUD (buttons, icons, text) using android XML layout instead of OpenGL ES.

There were severals reasons for that:

  • all features of xml layout and controls are available out of the box
  • easier to handle clicks and screen rotation
  • don't need to create/resize texture
  • out-of-the-box support of resource folder (multi-language)

To do it you just need to add GlSurfaceView as a child view to your main view.

cement