Sprite Text - is it really this huge?

Problems with Canvas, OpenGL, etc...

Sprite Text - is it really this huge?

Postby afail » Wed Nov 03, 2010 10:01 am

Hi, I am OpenGLES newbie as you can see, so I was in need to learn how to put text on a texture that is already applied on a rectangle. The Sprite Text example with the LabelMaker from the API's samples seems too complicated for a simpler application:

http://android-developers.blogspot.com/ ... etext.html

Is there any other, simpler way to render text onto a texture in OpenGLES under Android? What is your experience on this?

Thanks!
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Top

Re: Sprite Text - is it really this huge?

Postby SilverLogic » Thu Nov 04, 2010 12:28 am

Yea that sprite text sample couldn't get any worse as an api sample, I don't recommend using it. If you are new to opengl I suggest bitmap fonts as they are probabaly the easiest to use.

Here is a great tool for creating bitmap fonts:
http://www.lmnopc.com/bitmapfontbuilder/

All you need to do is load the bitmap font as a regular texture, then determine the character coordinates. This is especially simple since the bitmap font is laid out in ascii order.

For example, here is the code i used in my FontManager class ( a class i can pass to any text object, which provides coordinate information on the font loaded into that fontmanager class ):
Code: Select all
ArrayList<float[]> charCoords = new ArrayList<float[]>
...
      // First we need to set the character dimensions in coordinate space
      float charWidth = 0.0625f;   // More accurate than typing (1/16)
      float charHeight = 0.0625f;
      
      // Then we need to determine the actual character size in pixels
      this.charWidth = textureWidth / 16;
      this.charHeight = textureHeight / 16;
      
      // Next we'll loop through all 256 Characters (16x16 Map), and add the characters coords to the charCoords arraylist at that index
      // So a character with an ascii code of 33 would be found at index 33
      for(int y = 0; y < 16; y++) {
         for(int x = 0; x < 16; x++) {
            // Now we'll assign the texture coordinates
            float[] tempCoords = new float[12];
            
            // x
            tempCoords[0] = (x * charWidth);
            tempCoords[4] = (x * charWidth);
            tempCoords[10] = (x * charWidth);
            
            // x + w
            tempCoords[2] = (x * charWidth) + charWidth;
            tempCoords[6] = (x * charWidth) + charWidth;
            tempCoords[8] = (x * charWidth) + charWidth;
            
            // y
            tempCoords[5] = (y * charHeight) + yOffset;
            tempCoords[9] = (y * charHeight) + yOffset;
            tempCoords[11] = (y * charHeight) + yOffset;
            
            // y + h
            tempCoords[1] = (y * charHeight) + charHeight;
            tempCoords[3] = (y * charHeight) + charHeight;
            tempCoords[7] = (y * charHeight) + charHeight;
            
            charCoords.add(tempCoords);
         }
      }
...
   // Returns an openGL friendly floatbuffer for mapping the character
   public FloatBuffer GetCharCoords(char c) {
      return FloatBuffer.wrap(charCoords.get((int)c));
   }


Then, you can simply pass the coords in the arraylist to opengl, for example:
Code: Select all
char c;
      for(int i = 0; i < text.length(); i++) {
         c = text.charAt(i);
         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, fm.GetCharQuadCoords());
         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, fm.GetCharCoords(c));
         gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
         gl.glTranslatef(fm.GetVSpacing(), 0, 0);
      }


Sorry i know this is kind of chopped together, but the mapping of the bitmap image alone should be very helpful, the rest is just to guide you in the right direction. Good luck, hope this helps!
User avatar
SilverLogic
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Oct 21, 2010 10:31 pm

Re: Sprite Text - is it really this huge?

Postby afail » Thu Nov 04, 2010 6:52 am

thanks!
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest