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!